-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull-compile.js
167 lines (146 loc) · 5.21 KB
/
pull-compile.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
const { exec } = require('child_process');
var fs = require('fs');
let file = process.argv[2];
if (file == undefined) {
console.log("Please specify a file to build!");
process.exit(1);
} else if (!fs.existsSync(file)) {
console.log("Unknown file '" + file + "'.");
process.exit(1);
}
let skip = 0;
let verbose = false;
let showErrors = false;
for (let i = 0; i < process.argv.length; i++) {
if (process.argv[i] == "-s" || process.argv[i] == "--skip") {
try {
skip = parseInt(process.argv[i + 1]);
} catch (e) {
console.log("Invalid value for skip. Must be a number!")
process.exit(1);
}
} else if (process.argv[i] == "-v" || process.argv[i] == "--verbose") {
verbose = true;
} else if (process.argv[i] == "-e" || process.argv[i] == "--errors") {
showErrors = true;
}
}
const loadedFile = loadLoafFile(file);
function loadLoafFile(filePath) {
let data = fs.readFileSync(filePath, 'utf8');
let lines = data.split("\n");
let doneExcl = false;
const args = [];
const instructions = [];
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (line.trim().startsWith("#") || line.trim().length == 0) continue;
line = line.split("#")[0].trim();
if (line.startsWith("!")) {
if (!doneExcl) {
args.push(line.substring(1));
} else {
console.error(`Line ${i+1}: Rule after rule definitions ended!`);
process.exit(1);
}
continue;
} else {
doneExcl = true;
}
if (line.startsWith("gradle ") || line.startsWith("maven ")) {
const mode = line.split(" ")[0];
const actionsRaw = line.split(" ").slice(1).join(" ");
const actionsSplit = actionsRaw.split(",");
const actions = [];
for (const action of actionsSplit) {
const actionTrim = action.trim();
if (actionTrim.length == 0) {
console.error(`Line ${i+1}: Missing path!`);
process.exit(1);
}
if (!fs.existsSync(actionTrim)) {
console.error(`Line ${i+1}: Path ${actionTrim} does not exist!`);
process.exit(1);
}
actions.push(actionTrim.trim());
}
instructions.push({
"mode": mode,
"actions": actions
});
}
}
return {
"args": args,
"instructions": instructions
};
}
function mavenInstall(dir, pull) {
return new Promise((resolve, reject) => {
exec(`cd ${dir} ${pull ? "&& git pull " : ""}&& mvn clean package install -U`, (err, stdout, stderr) => {
if (err) {
reject(err)
} else {
resolve(stdout)
}
})
})
}
function gradleBuild(dir, pull) {
return new Promise((resolve, reject) => {
exec(`cd ${dir} ${pull ? "&& git pull " : ""}&& .\\gradlew build && .\\gradlew publishToMavenLocal`, (err, stdout, stderr) => {
if (err) {
reject(err)
} else {
resolve(stdout)
}
})
})
}
async function main() {
const start = Date.now();
let index = 0;
const pull = loadedFile.args.includes("pull");
for (const instruction of loadedFile.instructions) {
let running = [];
const single = instruction.actions.length == 1;
if (!single) {
const numbers = [];
for (let i = 0; i < instruction.actions.length; i++) {
numbers.push(index + 1 + i);
}
console.log(color(`${numbers.join(", ")}. Building Multiple: ${instruction.actions.join(", ")}...`, 33));
}
for (const action of instruction.actions) {
if (index++ < skip) {
if (single) console.log(color(`${index}. Skipped ${action}.`, 33));
else console.log(color(`-> ${action} skipped!`, 33));
} else {
if (single) console.log(color(`${index}. Building ${action}...`, 33));
running.push(handle(action, instruction.mode == "gradle" ? gradleBuild(action, pull) : mavenInstall(action, pull), single));
}
}
await Promise.all(running);
if (!single) console.log(" ");
}
console.log(color(`Finished all after ${(Date.now() - start) / 1000} seconds!`, 32))
}
function color(str, color) {
return `\x1b[${color}m${str}\x1b[0m`
}
async function handle(name, promise, single) {
const start = Date.now()
await promise.then(it => {
if (verbose) console.log(it)
if (single) console.log(color(`-> Built! (${Date.now() - start} ms)`, 32))
else console.log(color(`-> ${name} built! (${Date.now() - start} ms)`, 32))
}
).catch(it => {
if (verbose || showErrors) console.log(it)
if (single) console.log(color(`-> Failed! (${Date.now() - start} ms)`, 31))
else console.log(color(`-> ${name} failed! (${Date.now() - start} ms)`, 31))
}
)
if (single) console.log(" ")
}
main();