This repository has been archived by the owner on Jun 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
configure.js
342 lines (314 loc) · 12.1 KB
/
configure.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
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* ClosureCompiler.js: Configure Script (c) 2013 Daniel Wirtz <dcode@dcode.io>
* Released under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/ClosureCompiler.js for details
*/
var ClosureCompiler = require(__dirname+"/../ClosureCompiler.js"),
fs = require("fs"),
path = require("path"),
child_process = require("child_process"),
pkg = require(__dirname+"/../package.json"),
zlib = require("zlib"),
tar = require("tar");
console.log("Configuring ClosureCompiler.js "+pkg.version+" ...\n");
// Closure Compiler download url
var ccUrl = "https://dl.google.com/closure-compiler/compiler-latest.tar.gz";
// Temporary file for the download
var ccTempFile = path.normalize(path.join(__dirname, "..", "compiler", "compiler.tar.gz"));
// Gets the platform postfix for downloads
function platformPostfix() {
if (/^win/.test(process.platform)) {
return process.arch == 'x64' ? 'win64' : 'win32';
} else if (/^darwin/.test(process.platform)) {
return 'osx64';
}
// This might not be ideal, but we don't have anything else and there is always a chance that it will work
return process.arch == 'x64' ? 'linux64' : 'linux32';
}
// Bundled JRE download url
var jrePrefix = "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/bundled-openjdk-jre/OpenJDK-JRE-7u6_24-";
var jrePostfix = ".tar.gz";
var jreUrl = jrePrefix+platformPostfix()+jrePostfix;
// Temporary file for the download
var jreTempFile = path.normalize(path.join(__dirname, "..", "jre", "jre.tar.gz"));
if (!fs.existsSync) fs.existsSync = path.existsSync; // node < 0.8
console.log(" Downloading "+ccUrl+" ...");
var lastBytes = 0, currentBytes = 0, mb = 1024*1024;
download(ccUrl, ccTempFile, function(error, bytes) {
if (error) {
console.log(" ✖ Download failed: "+error+"\n");
fail();
}
console.log(" ✔ Download complete: "+ccTempFile+" ("+parseInt(bytes/mb, 10)+" mb)\n");
setTimeout(function() {
console.log(" Unpacking "+ccTempFile+" ...");
unpack(ccTempFile, function(error) {
if (error) {
console.log(" ✖ Unpack failed: "+error+"\n");
fail();
}
setTimeout(function() { // Let the entry callbacks finish
console.log(" ✔ Unpack complete.\n");
configure_jre();
}, 1000);
}, function(entry) {
console.log(" | "+entry["path"]);
});
}, 1000);
}, function(bytes, total) {
currentBytes += bytes;
if (currentBytes == bytes || currentBytes - lastBytes >= mb) {
console.log(" | "+parseInt(currentBytes/mb, 10)+" / "+(total > 0 ? parseInt(total/mb, 10) : "???")+" mb");
lastBytes = currentBytes;
}
});
/**
* Configures the JRE.
*/
function configure_jre() {
console.log(" Configuring JRE ...");
var mb = 1024*1024;
// Test if there is already a global Java so we don't need to download anything
ClosureCompiler.testJava(ClosureCompiler.getGlobalJava(), function(ok) {
if (ok) {
console.log(" ✔ Global Java is available, perfect!\n");
// Travis CI for example has one, so we save their bandwidth. And Google's. And yours. And...
finish();
} else {
if (fs.existsSync(path.join(__dirname, "..", "jre", "bin"))) {
console.log(" ✖ Global Java not found or outdated, testing bundled JRE ...\n");
runTest(true);
} else {
console.log(" ✖ Global Java not found or outdated, downloading bundled JRE ...");
console.log(" Downloading "+jreUrl+" ...");
lastBytes = 0; currentBytes = 0;
download(jreUrl, jreTempFile, function(error, bytes) {
if (error) {
console.log(" ✖ Download failed: "+error+"\n");
fail();
}
console.log(" ✔ Download complete: "+jreTempFile+" ("+parseInt(bytes/mb, 10)+" mb)\n");
setTimeout(function() {
console.log(" Unpacking "+jreTempFile+" ...");
unpack(jreTempFile, function(error) {
if (error) {
console.log(" ✖ Unpack failed: "+error+"\n");
fail();
}
setTimeout(function() { // Let the entry callbacks finish
console.log(" ✔ Unpack complete.\n");
configure();
runTest(true);
}, 1000);
}, function(entry) {
console.log(" | "+entry["path"]);
});
}, 1000);
}, function(bytes, total) {
currentBytes += bytes;
if (currentBytes == bytes || currentBytes - lastBytes >= mb) {
console.log(" | "+parseInt(currentBytes/mb, 10)+" / "+(total > 0 ? parseInt(total/mb, 10) : "???")+" mb");
lastBytes = currentBytes;
}
});
}
}
});
}
/**
* Downloads a file.
* @param {string} downloadUrl
* @param {string} filename
* @param {function(?Error,number)} callback
* @param {function(number)=} ondata
*/
function download(downloadUrl, filename, callback, ondata) {
var url = require("url").parse(downloadUrl);
var out = require("fs").createWriteStream(filename, { flags: 'w', encoding: null, mode: 0666 });
var bytes = 0, total = -1;
var options = {
"hostname" : url["host"],
"method" : "GET",
"path" : url["path"],
"agent" : false
}
// Reconfigure request options if an HTTP proxy setting exists
if (process.env.http_proxy) {
var http_proxy = require("url").parse(process.env.http_proxy);
options["hostname"] = http_proxy["hostname"];
options["port"] = http_proxy["port"];
options["path"] = url["href"];
options["headers"] = {
"Host" : url["host"]
};
}
var req = require(path.join(__dirname, "..", "lib", "follow-redirects.js")).http.request(options,
function(res) {
if (res.headers["content-length"]) {
total = parseInt(res.headers["content-length"], 10);
}
if (res.statusCode != 200) {
res.setEncoding(null);
callback(new Error("Download failed: HTTP status code "+res.statusCode), -1);
return;
}
res.on("data", function(chunk) {
bytes += chunk.length;
if (ondata) ondata(chunk.length, total);
out.write(chunk);
});
res.on("end", function() {
callback(null, bytes);
});
});
req.on("error", function(e) {
callback(e, -1);
});
req.end();
}
/**
* Unpacks a file in place.
* @param {string} filename File name
* @param {function(?Error)} callback
* @param {function(Object)=} entryCallback
*/
function unpack(filename, callback, entryCallback) {
var input = fs.createReadStream(filename, { flags: 'r', encoding: null }),
files = {},
dir = path.dirname(filename),
returned = false,
to = null;
// Finishs the unpack if all files are done
function maybeFinish() {
if (to !== null) clearTimeout(to);
to = setTimeout(function() {
var alldone = true;
var names = Object.keys(files);
for (var i=0; i<names.length; i++) {
if (!files[names[i]]["done"]) {
alldone = false;
break;
}
}
if (alldone && !returned) {
returned = true;
callback(null);
}
}, 1000);
}
input.pipe(zlib.createGunzip()).pipe(tar.Parse()).on("entry", function(entry) {
if (entryCallback) entryCallback(entry);
if (entry["type"] == 'File') {
files[entry["path"]] = fs.createWriteStream(path.join(dir, entry["path"]), { flags: 'w', encoding: null });
entry.pipe(files[entry["path"]]);
entry.on("end", function() {
files[entry["path"]].end();
files[entry["path"]]["done"] = true;
maybeFinish();
});
} else if (entry["type"] == "Directory") {
try {
fs.mkdirSync(path.join(dir, entry["path"]));
} catch (e) {
if (!fs.existsSync(path.join(dir, entry["path"]))) {
if (!returned) {
returned = true;
callback(e);
}
}
}
}
}).on("error", function(e) {
if (!returned) {
returned = true;
callback(e);
}
});
}
/**
* Configures our bundled Java.
*/
function configure() {
var java = path.normalize(path.join(__dirname, "..", "jre", "bin", "java"+ClosureCompiler.JAVA_EXT));
console.log(" Configuring bundled JRE for platform '"+platformPostfix()+"' ...");
if (!/^win/.test(process.platform)) {
var jre = path.normalize(path.join(__dirname, "..", "jre"));
console.log(" | 0755 "+jre);
fs.chmodSync(jre, 0755);
console.log(" | 0755 "+path.join(jre, "bin"));
fs.chmodSync(path.join(jre, "bin"), 0755);
console.log(" | 0755 "+java);
fs.chmodSync(java, 0755);
console.log(" Complete.\n");
} else {
console.log(" Complete (not necessary).\n");
}
}
/**
* Runs the final test.
* @param {boolean=} ensureBundled
*/
function runTest(ensureBundled) {
console.log(" Testing bundled Java ...");
console.log(" | exec "+ClosureCompiler.getBundledJava());
ClosureCompiler.testJava(ClosureCompiler.getBundledJava(), function(ok, err) {
if (ok) {
console.log(" ✔ Successfully called bundled Java!\n");
finish();
} else {
if (ensureBundled) {
console.log(" ✖ Failed to call bundled java:");
console.log("-----\n"+err+"\n-----");
console.log(" ✖ Trying global java instead ...");
} else {
console.log(" ✖ Failed to call bundled java, trying global java instead ...");
}
console.log(" | exec "+ClosureCompiler.getGlobalJava()+"\n");
ClosureCompiler.testJava(ClosureCompiler.getGlobalJava(), function(ok, err) {
if (ok) {
console.log(" ✔ Successfully called global Java!\n");
finish();
} else {
console.log(" ✖ Failed to call global Java.\n");
fail();
}
});
}
});
}
/**
* Cleans up.
*/
function cleanUp() {
try { fs.unlinkSync(ccTempFile); } catch (e) {}
try { fs.unlinkSync(jreTempFile); } catch (e) {}
// ...your harddrive's space.
}
/**
* Fails.
*/
function fail() {
cleanUp();
console.log(" ✖ Unfortunately, ClosureCompiler.js could not be configured.");
console.log(" See: https://github.com/dcodeIO/ClosureCompiler.js (create an issue maybe)\n");
process.exit(1);
}
/**
* Finishes.
*/
function finish() {
cleanUp();
console.log(" ✔ ClosureCompiler.js has successfully been configured!\n");
}