-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.babel.js
124 lines (110 loc) · 3.45 KB
/
gulpfile.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// gulp-compile-c-cpp-java
// A script that automatically compile a .c or .cpp or .java file from a folder
//
// Created by Remi Lelaidier on 12/10/2016
// Contributor : Ghostfly
// Email : [email protected]
//
// For install dependancies, run 'npm install'
// Dependancies
import gulp from 'gulp';
import {exec} from 'child_process';
import fs from "fs";
import colors from "colors/safe";
import confirm from "gulp-confirm";
import {argv} from 'yargs';
const dirname = __dirname;
const srcFolder = `${dirname}/src/`; // your src directory
const binFolder = `${dirname}/bin/`; // Your bin folder
const gcc = "gcc "; // the c compilator
const gpp = "g++ "; // the c++ compilator
/*
* Functions
*/
// TODO
// function asking for launching the file
function confirmRun(fileName) {
return gulp.src(binFolder).pipe(confirm({
question : `Launch ${fileName} (y) `,
input: '_key:y'
})).pipe(gulp.dest(''));
}
// Current date
function dateNow(){
const date = new Date();
const time = date.toLocaleTimeString();
console.log(`[${colors.blue(time)}]`);
}
// Printing function
function print(stdout, stderr, fileName, buildCommand){
dateNow();
console.log(`Compilation de : ${colors.green(fileName + ".c")}`);
if(argv.v){
console.log(`Commande : ${colors.blue(buildCommand)}`);
}
const errFormat = stderr.replace(`${dirname}/src/`, "");
console.log(stdout);
if(errFormat.length !== 0){
console.log(colors.red(errFormat));
}
/*else{
Prochaine version -> Lancement de l'executable
confirmRun(fileName);
}*/
}
// Compil function
function exeCompil(fileName, lang){
let compilator;
let extension;
switch (lang){
case "c":
compilator = gcc;
extension = ".c";
break;
case "cpp":
compilator = gpp;
extension = ".cpp";
break;
}
// If there is options for the compiler
if(argv.flags){
const flags = `--${argv.flags} `;
var buildCommand = `${compilator + flags + srcFolder + fileName + extension} -o ${binFolder}${fileName}`;
}
else{
var buildCommand = `${compilator + srcFolder + fileName + extension} -o ${binFolder}${fileName}`;
}
exec(buildCommand, (error, stdout, stderr) => {
print(stdout, stderr, fileName, buildCommand);
});
}
/*
TASKS
*/
// Compile on launch (C only)
gulp.task('compile',() => {
const arrayFiles = fs.readdirSync(`${dirname}/src/`);
for(let i = 0; i < arrayFiles.length; i++){
const fileName = arrayFiles[i].replace(".c", "");
exeCompil(fileName, "c");
}
});
// Watch change on file
gulp.task('watch', () => {
/*---- C Language ----*/
gulp.watch(`${srcFolder}*.c`).on('change',file => {
let fileName = file.path;
fileName = fileName.replace(".c", "");
fileName = fileName.replace(srcFolder, "");
exeCompil(fileName, "c");
});
/*---- C++ Language ----*/
gulp.watch(`${srcFolder}*.cpp`).on('change', file => {
let fileName = file.path;
fileName = fileName.replace(".cpp", "");
fileName = fileName.replace(srcFolder, '');
exeCompil(fileName, "cpp");
});
});
gulp.task('default',['compile', 'watch'],() => {});