-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsmake.js
616 lines (544 loc) · 13.5 KB
/
jsmake.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
const {
argv
} = require('process');
const fs = require('fs');
const path = require('path');
module.exports = (function () {
const {
exec, spawn
} = require('child_process');
let glob = {};
// Shell command support
let privates = {};
privates["tasks"] = {};
privates["source_set"] = [];
/**
* Convert command string to `argv` array.
* @param {string} cmd_str
* @returns {string[]} The arguments.
*/
function _commandArgConverter(cmd_str) {
let parseState = 0;
let buffer = "";
/**
* @type {string[]}
*/
let result = [];
for (let i of cmd_str) {
if (parseState === 0) {
if (i === " " && buffer !== "") {
result.push(buffer);
buffer = "";
continue;
}
if (i === "\"") {
parseState = 1;
continue;
}
buffer += i
continue;
}
if (parseState === 1) {
if (i === "\"") {
parseState = 0;
continue;
}
buffer += i;
}
}
if (buffer !== "") {
result.push(buffer);
}
return result;
}
/**
* Execute a shell command using an `argv` array.
* @param {string[]} argv
*/
glob["shellRaw"] = async function (argv) {
if (argv.length < 1) {
throw new Error("Not enough arguments");
}
let awaiter = new Promise((res, rej) => {
let process = spawn(argv[0], argv.slice(1));
process.stdout.on('data', data => {
console.log(`${data}`);
});
process.stderr.on('data', data => {
console.error(`${data}`);
});
process.on('error', error => {
console.log(error);
});
process.on('close', code => {
res();
});
});
return awaiter;
};
/**
* Execute a shell command.
* @param {string} cmd The command to execute.
* @param {boolean} [echo=true] Determine if the command itself should be printed to the screen.
* @returns
*/
glob["shell"] = async function (cmd, echo = true) {
if (echo) {
console.log(cmd);
}
await this.shellRaw(_commandArgConverter(cmd));
};
glob["variables"] = {
/**
* The C compiler to use.
*/
"CC": "gcc",
/**
* The C++ compiler to use.
*/
"CXX": "g++",
/**
* The arguments to pass to the C compiler.
*/
"CC_FLAGS": "",
/**
* The arguments to pass to the C++ compiler.
*/
"CXX_FLAGS": "",
/**
* The Go compiler to use.
*/
"GO": "go",
/**
* The arguments to pass to the Go compiler.
*/
"GO_FLAGS": "",
/**
* The node package manager to use.
*/
"NPM": "npm", // May use yarn or something
/**
* Shall we ignore errors and continue building or
* stop immediately.
*/
"IGNORE_ERRORS": false,
/**
* @type {string[]}
*/
"SOURCE_INCLUDED": [],
/**
* The nodejs interpreter to use.
*/
"NODE_INTERPRETER": "node",
/**
* The git to use.
*/
"GIT": "git",
/**
* The linker to use.
*/
"LD": "ld",
/**
* The arguments to pass to the linker.
*/
"LD_FLAGS": "",
/**
* The rust package manager to use.
*/
"CARGO": "cargo",
/**
* The rust compiler to use.
*/
"RUSTC": "rustc",
/**
* The rust toolchain installer to use.
*/
"RUSTUP": "rustup",
/**
* The java compiler to use.
*/
"JAVAC": "javac",
/**
* The java vm to use.
*/
"JAVA": "java",
/**
* The java archiever to use.
*/
"JAR": "jar",
};
/**
* Build C source files.
* @param {...string} c_sources The path to sources.
*/
glob["buildC"] = async function (...c_sources) {
await this.shell(`${this.variables.CC} ${this.variables.CC_FLAGS} ${c_sources.join(' ')}`)
};
/**
* Build C++ source files.
* @param {...string} cxx_sources The path to sources.
*/
glob["buildCxx"] = async function (...cxx_sources) {
await this.shell(`${this.variables.CXX} ${this.variables.CXX_FLAGS} ${cxx_sources.join(' ')}`)
};
/**
* Build C++ source files to a named executable.
* @param {string} name The name of the executable.
* @param {...string} cxx_sources The path to sources.
*/
glob["buildExecutable"] = async function (name, ...cxx_sources) {
await this.shell(`${this.variables.CXX} ${this.variables.CXX_FLAGS} -o ${name} ${cxx_sources.join(' ')}`)
}
/**
* Build go packages.
*/
glob["buildGoPackages"] = async function () {
await this.shell(`${this.variables.GO} build ${this.variables.GO_FLAGS}`)
};
/**
* Define a build task.
* @param {string} taskName The task's name.
* @param {()=>void} task The task itself.
*/
glob["task"] = function (taskName, task) {
if (typeof (taskName) !== "string" || typeof (task) !== "function") {
throw new Error("TypeError");
}
privates.tasks[taskName] = task;
};
/**
* Start the build script.
*/
glob["build"] = async function () {
// Determine by the command argument
if (argv.slice(2).length == 0) {
throw new Error("error: no enough argument.");
}
if (privates.tasks["pre before"]) {
await this.buildTask("pre before");
}
try {
await this.buildTask(argv[2]);
} catch(e) {
console.error(e);
}
if (privates.tasks["pre after"]) {
await this.buildTask("pre after");
}
};
/**
* Build a task with the name.
* @param {string} name The name of the task.
*/
glob["buildTask"] = async function (name) {
if (typeof (name) !== "string") {
throw new Error("TypeError");
}
if (typeof (privates.tasks[name]) !== "function") {
throw new Error(`Unknown task name: ${name}`);
}
console.log(`Start building task ${name}...`)
try {
await privates.tasks[name]();
} catch (e) {
console.log(`jsmake: ***Make Failed due to following errors***\n${e}`);
throw e;
}
};
/**
* Execute the `npm` command.
* @param {...string} args Arguments to pass to `npm`.
*/
glob["npm"] = async function (...args) {
await this.shellRaw([glob.variables.NPM, ...args]);
};
/**
* A bunch of methods to operate with npm.
*/
glob["nodePack"] = {
/**
* Set the version number of the npm.
* @param {string} v The new version.
*/
async version(v) {
await glob.shell(`${glob.variables.NPM} version ${v}`);
},
/**
* Publish a node package.
*/
async publish() {
await glob.shell(`${glob.variables.NPM} publish`);
},
/**
* Install a node package.
* @param {string} pack_name Package's name.
*/
async install(pack_name) {
if (glob.variables.NPM == "yarn") {
await glob.shell(`${glob.variables.NPM} add ${pack_name}`);
} else {
await glob.shell(`${glob.variables.NPM} install ${pack_name}`);
}
},
/**
* Patch the version number of the node package.
*/
async patch() {
await glob.npm('version', 'patch');
},
};
/**
* Call `callback` for every file under the directory.
* @param {string} dir Path to the directory.
* @param {(pthName: string)=>void} callback The callback.
*/
glob["dir"] = async function(dir, callback) {
if (typeof dir !== "string") {
throw new TypeError("dir must be a string");
}
if (typeof callback !== "function") {
throw new TypeError("callback should be a callback function");
}
let ds = fs.readdirSync(dir);
for (i of ds) {
callback(i);
}
};
/**
* Select a source.
* @param {string} src The name of the source.
*/
glob["selectSource"] = function(src) {
privates.source_set.push(string(src));
};
/**
* Include a source.
* @param {string} src The name of the source.
*/
glob["includeSource"] = function(src) {
this.variables.SOURCE_INCLUDED.push(src);
};
/**
* Include all the sources in the working directory.
*/
glob["includeAllInDir"] = function() {
this.dir('.', (path) => {
this.includeSource(path);
});
};
/**
* Check if the source is included.
* @param {string} src The source to include.
* @returns {boolean} `true` if the source is included, `false` otherwise.
*/
glob["isIncluded"] = function(src) {
for (let i of this.variables.SOURCE_INCLUDED) {
if (i === src || path.resolve(src) === path.resolve(i)) {
return true;
}
}
return false;
};
/**
* Include all the sources that are selected.
*/
glob["includeAllSelected"] = function() {
for (let i of privates.source_set) {
this.includeSource(i);
}
};
/**
* Select all the sources that are included.
*/
glob["selectAllIncluded"] = function() {
for (let i of this.variables.SOURCE_INCLUDED) {
this.selectSource(i);
}
};
/**
* Compile all the selected sources as C and deselect them.
*/
glob["compileAllSelectedAsC"] = async function() {
await this.buildC(...privates.source_set);
privates.source_set = [];
};
/**
* Compile all the selected sources as C++ and deselect them.
*/
glob["compileAllSelectedAsCxx"] = async function() {
await this.buildCxx(...privates.source_set);
privates.source_set = [];
};
/**
* Check if a file exists.
* @param {string} path Path to the file.
* @returns {boolean} `true` if the file exists and `false` otherwise.
*/
glob["exists"] = function(path) {
// Return if the path exists
return fs.existsSync(path);
};
/**
* Delete a file.
* @param {string} path Path to the file.
*/
glob["rm"] = function(path) {
fs.rmSync(path);
};
// Git support.
glob["git"] = {
/**
* Check if the directory is a git repository.
* @returns {boolean} `true` if the directory is a git repository and `false` otherwise.
*/
isGitted() {
return glob.exists(".git");
},
/**
* Initialize the directory as a git repository.
*/
async init() {
await glob.shell(`${glob.variables.GIT} init`);
},
/**
* Add files to the repository.
* @param {...string} paths The path to files to add.
*/
async add(...paths) {
await glob.shell(`${glob.variables.GIT} add ${paths.join(' ')}`);
},
/**
* Push the repository.
*/
async push() {
await glob.shell(`${glob.variables.GIT} push`)
},
/**
* Push the repository with given options.
* @param {...string} options Options to pass.
*/
async pushWithOptions(...options) {
await glob.shell(`${glob.variables.GIT} push ${options.join(' ')}`);
},
/**
* Execute `git branch` with given options.
* @param {...string} options Options to pass.
*/
async branch(...options) {
await glob.shell(`${glob.variables.GIT} branch ${options.join(' ')}`);
},
/**
* Execute `git remote add origin` with given options.
* @param {string} originName The name of the origin.
* @param {string} url The url of the origin.
*/
async remoteAddOrigin(originName, url) {
await glob.shell(`${glob.variables.GIT} remote add ${originName} ${url}`);
},
/**
* Execute `git remote add origin` with given options on the given branch.
* @param {string} branch The branch to set.
* @param {string} originName The name of the origin.
* @param {string} url The url of the origin.
*/
async remoteAddOriginForBranch(branch, originName, url) {
await glob.shell(`${glob.variables.GIT} remote add -t ${branch} ${originName} ${url}`);
},
/**
*
* @param {...string} options Arguments.
*/
async invoke(...options) {
await glob.shellRaw([glob.variables.GIT, ...options]);
},
/**
* Commit the repository with the given message.
* @param {string} msg The message to display.
*/
async commit(msg) {
await this.invoke('commit', '-m', msg);
},
};
/**
*
* @param {string} patha The path to file A
* @param {string} pathb The path to file B
* @returns Whether file A is newer than file B.
*/
glob["newer"] = function(patha, pathb) {
let pas = fs.statSync(patha);
let pbs = fs.statSync(pathb);
return pas.mtime.getTime() > pbs.mtime.getTime();
};
/**
* Build the task from another build script.
* @param {string} path_to_script Path to the script.
* @param {string} taskName The task name.
*/
glob["buildTaskFrom"] = async function(path_to_script, taskName) {
await this.shell(`${this.variables.NODE_INTERPRETER} ${path_to_script} ${taskName}`);
};
/**
* Iterate the directoy and call the callback if and only if the `rule` callback returns true for that filename.
* @param {string} path The path.
* @param {(s:string)=>boolean} rule The rule.
* @param {(s:string)=>void} callback The callback.
*/
glob["dirIf"] = async function(path, rule, callback) {
if (typeof path !== "string" || typeof rule !== "function" || typeof path !== "function") {
console.error("Invalid Type");
throw new TypeError("Invalid Argument Type");
}
this.dir(path, (fpath) => {
if (rule(path)) {
callback(path);
}
});
};
/**
* Link the object files.
* @param {...string} object_files Path to object files.
*/
glob["link"] = async function(...object_files) {
await this.shell(`${this.variables.LD} ${this.variables.LD_FLAGS} ${object_files.join(' ')}`)
};
/**
* Run the `cargo` command.
* @param {...string} args Arguments.
*/
glob["cargo"] = async function (...args) {
await this.shell(`${glob.variables.CARGO} ${args.join(' ')}`);
};
/**
* Run the `rustc` command.
* @param {...string} args Arguments.
*/
glob["rustc"] = async function (...args) {
await this.shell(`${glob.variables.RUSTC} ${args.join(' ')}`);
};
/**
* Run the `rustup` command.
* @param {...string} args Arguments.
*/
glob["rustup"] = async function (...args) {
await this.shell(`${glob.variables.RUSTUP} ${args.join(' ')}`);
};
/**
* Run the `java` command.
* @param {...string} args Arguments.
*/
glob["java"] = async function (...args) {
await this.shell(`${glob.variables.JAVA} ${args.join(' ')}`);
};
/**
* Run the `javac` command.
* @param {...string} args Arguments.
*/
glob["javac"] = async function (...args) {
await this.shell(`${glob.variables.JAVAC} ${args.join(' ')}`);
};
// Rust support.
// glob["rust"] = {
// };
return glob;
})();