-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompile
executable file
·98 lines (83 loc) · 2.93 KB
/
compile
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
#!/usr/bin/env node
var sys = require('sys'),
http = require('http'),
fs = require('fs'),
querystring = require('querystring'),
path = require('path'),
spawn = require('child_process').spawn;
//sys.puts(process.argv.join("\n")+"\n");
var FILE_TO_COMPILE = "src/js/Main.js";
var request = http.createClient(80, 'closure-compiler.appspot.com').request('POST', '/compile', {
'Host': 'closure-compiler.appspot.com',
'Content-Type': 'application/x-www-form-urlencoded'
});
var code = injectScripts(fs.readFileSync(FILE_TO_COMPILE, 'utf8'));
//sys.puts(code+"\n\n");
sys.print("Sending \""+FILE_TO_COMPILE+"\" to Google Closure Compiler");
var printDots = new PrintDots(30);
request.write(querystring.stringify({
'compilation_level': 'ADVANCED_OPTIMIZATIONS',
'output_format': 'json',
'output_info': ['compiled_code', 'warnings', 'errors', 'statistics'],
//'formatting': 'pretty_print',
'js_externs': 'swfobject.embedSWF=function(){};',
'js_code': code
}, '&', '=', false));
request.addListener('response', function (response) {
var res = "";
response.setEncoding('utf8');
response.addListener('data', function (chunk) {
sys.print('.');
res += chunk;
});
response.addListener('end', function () {
printDots.stop();
sys.puts(' done!');
//sys.puts(res);
var json = JSON.parse(res);
var compiledCode = json.compiledCode;
delete json.compiledCode;
sys.puts(sys.inspect(json));
var outputFilename = path.join(__dirname, "dist/Sound.min.js");
sys.puts('Saving compiled code to "'+outputFilename+'"');
fs.writeFileSync(outputFilename, compiledCode);
// Now that the Closure Compiler is complete, invoke 'haxe'
haxeCompile();
});
});
request.end();
/**
* Calls the 'haxe' command line build program to build the HaXe source
* in 'src' into a Flash player compliant SWF file. This gets called
* AFTER the Closure Compiler request completes.
*/
function haxeCompile() {
sys.print("\nInvoking HaXe Compiler.");
var dots = new PrintDots(30);
var haxe = spawn("haxe", "-cp src/haxe -main Main -swf-version 10 -swf9 dist/Sound.swf".split(" "));
haxe.stdout.addListener("data", function(data) {
sys.puts('\nstdout: ' + data);
});
haxe.stderr.addListener("data", function(data) {
sys.puts('\nstderr: ' + data);
});
haxe.addListener('exit', function(code) {
dots.stop();
sys.puts(' done! (exit code: ' + code + ')');
});
}
function PrintDots(interval) {
var num = setInterval(function() {
sys.print('.');
}, interval);
this.stop = function() {
clearInterval(num);
}
}
function injectScripts(main) {
return main.replace(/\/\/{[^{}]+}/g, function(key){
var val = key.replace(/[{}]+/g, "").substring(2);
sys.puts("Injecting \""+val+"\"");
return fs.readFileSync(val, 'utf8');
});
}