Debian Codegraph @

util/build/transforms/optimizer/sendJob.js

raw symbols SCIP
1 /*jshint node:true */
2 define([
3 "../../buildControl",
4 "../../fileUtils",
5 "dojo/has"
6 ], function(bc, fileUtils, has){
7 // start up a few processes to compensate for the miserably slow closure compiler
8
9 var processes = [],
10 killAllRunners = function(){
11 processes.forEach(function(proc){
12 try{
13 proc.runner && proc.runner.kill();
14 proc.runner = 0;
15 }catch(e){
16 //squelch
17 }
18 });
19 };
20
21 // don't leave orphan child procs
22 global.process.on("exit", killAllRunners);
23 global.process.on("uncaughtException", function(err){
24 killAllRunners();
25 // TODO: log these via bc.log
26 console.log(err);
27 console.log(err.stack);
28 global.process.exit(-1);
29 });
30
31 if(bc.maxOptimizationProcesses < 0){
32 bc.maxOptimizationProcesses = require.nodeRequire('os').cpus().length;
33 }
34 var
35 processesStarted = 0,
36 totalOptimizerOutput = "",
37 nextProcId = 0,
38 i, //used in for loop
39 sendJob = function(src, dest, optimizeSwitch, copyright){
40 processes[nextProcId++].write(src, dest, optimizeSwitch, copyright);
41 nextProcId= nextProcId % bc.maxOptimizationProcesses;
42 },
43 doneRe = new RegExp("^Done\\s\\(compile\\stime.+$", "m"),
44 optimizerRunner = require.toUrl("build/optimizeRunner.js"),
45 buildRoot = optimizerRunner.match(/(.+)\/build\/optimizeRunner\.js$/)[1],
46 runJava, //function, defined later
47 oldSendJob = sendJob, //preserves reference if sendJob is replaced
48 child_process = require.nodeRequire("child_process"),
49 isCygwin = global.process.platform === 'cygwin',
50 separator = has("is-windows") ? ";" : ":",
51 javaClasses = fileUtils.catPath(buildRoot, "closureCompiler/compiler.jar") + separator + fileUtils.catPath(buildRoot, "shrinksafe/js.jar") + separator + fileUtils.catPath(buildRoot, "shrinksafe/shrinksafe.jar");
52 if(isCygwin){
53 //assume we're working with Windows Java, and need to translate paths
54 runJava = function(cb){
55 child_process.exec("cygpath -wp '" + javaClasses + "'", function(err, stdout){
56 javaClasses = stdout.trim();
57 child_process.exec("cygpath -w '" + optimizerRunner + "'", function(err, stdout){
58 optimizerRunner = stdout.trim();
59 cb();
60 });
61 });
62 };
63 //wrap sendJob calls to convert to windows paths first
64 sendJob = function(src, dest, optimizeSwitch, copyright){
65 child_process.exec("cygpath -wp '" + src + "'", function(err, srcstdout){
66 child_process.exec("cygpath -wp '" + dest + "'", function(err, deststdout){
67 oldSendJob(srcstdout.trim(), deststdout.trim(),
68 optimizeSwitch, copyright);
69 });
70 });
71 };
72 }else if(has("is-windows")){
73 runJava = function(cb){
74 javaClasses = fileUtils.normalize(javaClasses);
75 optimizerRunner = fileUtils.normalize(optimizerRunner);
76 cb();
77 };
78 sendJob = function(src, dest, optimizeSwitch, copyright){
79 var wsrc = fileUtils.normalize(src);
80 var wdest = fileUtils.normalize(dest);
81 oldSendJob(wsrc, wdest, optimizeSwitch, copyright);
82 };
83 }else{
84 //no waiting necessary, pass through
85 runJava = function(cb) { cb(); };
86 }
87 runJava(function() {
88 for(i = 0; i < bc.maxOptimizationProcesses; i++) {(function(){
89 var
90 runner = child_process.spawn("java", ["-cp", javaClasses, "org.mozilla.javascript.tools.shell.Main", optimizerRunner]),
91 proc = {
92 runner:runner,
93 results:"",
94 tempResults:"",
95 sent:[],
96 write:function(src, dest, optimizeSwitch, copyright){
97 proc.sent.push(dest);
98 runner.stdin.write(src + "\n" + dest + "\n" + optimizeSwitch + "\n" + JSON.stringify({ copyright: copyright, options: bc.optimizeOptions, useSourceMaps: bc.useSourceMaps }) + "\n");
99 },
100 sink:function(output){
101 proc.tempResults += output;
102 var match, message, chunkLength;
103 while((match = proc.tempResults.match(doneRe))){
104 message = match[0];
105 if(/OPTIMIZER\sFAILED/.test(message)){
106 bc.log("optimizeFailed", ["module identifier", proc.sent.shift(), "exception", message.substring(5)]);
107 }else{
108 bc.log("optimizeDone", [proc.sent.shift() + " " + message.substring(5)]);
109 }
110 chunkLength = match.index + message.length;
111 proc.results += proc.tempResults.substring(0, chunkLength);
112 proc.tempResults = proc.tempResults.substring(chunkLength);
113 }
114 }
115 };
116 processesStarted++; // matches *3*
117 runner.stdout.on("data", function(data){
118 // the +"" converts to Javascript string
119 proc.sink(data + "");
120 }),
121 runner.stderr.on("data", function(data){
122 // the +"" converts to Javascript string
123 proc.sink(data + "");
124 }),
125 runner.on("exit", function(){
126 proc.results += proc.tempResults;
127 totalOptimizerOutput += proc.results;
128 bc.logOptimizerOutput(totalOptimizerOutput);
129 processesStarted--; // matches *3*
130 if(!processesStarted){
131 // all the processes have completed and shut down at this point
132 if(bc.showOptimizerOutput){
133 bc.log("optimizeMessages", [totalOptimizerOutput]);
134 }
135 bc.passGate(); // matched with *1*
136 }
137 });
138 processes.push(proc);
139 })();}
140 }); //end runJava(function...)
141
142 bc.gateListeners.push(function(gate){
143 if(gate==="cleanup"){
144 // going through the cleanup gate signals that all optimizations have been started;
145 // we now signal the runner there are no more files and wait for the runner to stop
146 bc.log("pacify", "waiting for the optimizer runner to finish...");
147 bc.waiting++; // matched with *1*
148 processes.forEach(function(proc){
149 proc.write(".\n");
150 });
151 processes = [];
152 }
153 });
154
155 return sendJob;
156 });