1 | function writeFile(filename, contents, encoding, cb) { |
---|
2 | if (arguments.length==3 && typeof encoding!="string") { |
---|
3 | cb= encoding; |
---|
4 | encoding= 0; |
---|
5 | } |
---|
6 | var |
---|
7 | outFile = new java.io.File(filename), |
---|
8 | outWriter; |
---|
9 | if(encoding){ |
---|
10 | outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile), encoding); |
---|
11 | }else{ |
---|
12 | outWriter = new java.io.OutputStreamWriter(new java.io.FileOutputStream(outFile)); |
---|
13 | } |
---|
14 | |
---|
15 | var os = new java.io.BufferedWriter(outWriter); |
---|
16 | try{ |
---|
17 | os.write(contents); |
---|
18 | }finally{ |
---|
19 | os.close(); |
---|
20 | } |
---|
21 | if (cb) { |
---|
22 | cb(0); |
---|
23 | }; |
---|
24 | } |
---|
25 | |
---|
26 | var built = "//>>built\n"; |
---|
27 | |
---|
28 | function sscompile(src, dest, optimizeSwitch, copyright){ |
---|
29 | // decode the optimize switch |
---|
30 | var |
---|
31 | options= optimizeSwitch.split("."), |
---|
32 | comments= 0, |
---|
33 | keepLines= 0, |
---|
34 | strip= null; |
---|
35 | while(options.length){ |
---|
36 | switch(options.pop()){ |
---|
37 | case "normal": |
---|
38 | strip= "normal"; |
---|
39 | break; |
---|
40 | case "warn": |
---|
41 | strip= "warn"; |
---|
42 | break; |
---|
43 | case "all": |
---|
44 | strip= "all"; |
---|
45 | break; |
---|
46 | case "keeplines": |
---|
47 | keepLines= 1; |
---|
48 | break; |
---|
49 | case "comments": |
---|
50 | comments= 1; |
---|
51 | break; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | //Use rhino to help do minifying/compressing. |
---|
56 | var context = Packages.org.mozilla.javascript.Context.enter(); |
---|
57 | try{ |
---|
58 | // Use the interpreter for interactive input (copied this from Main rhino class). |
---|
59 | context.setOptimizationLevel(-1); |
---|
60 | |
---|
61 | var text= readFile(src, "utf-8"); |
---|
62 | if(comments){ |
---|
63 | //Strip comments |
---|
64 | var script = context.compileString(text, dest, 1, null); |
---|
65 | text = new String(context.decompileScript(script, 0)); |
---|
66 | |
---|
67 | //Replace the spaces with tabs. |
---|
68 | //Ideally do this in the pretty printer rhino code. |
---|
69 | text = text.replace(/ /g, "\t"); |
---|
70 | }else{ |
---|
71 | //Apply compression using custom compression call in Dojo-modified rhino. |
---|
72 | text = new String(Packages.org.dojotoolkit.shrinksafe.Compressor.compressScript(text, 0, 1, strip)); |
---|
73 | if(!keepLines){ |
---|
74 | text = text.replace(/[\r\n]/g, ""); |
---|
75 | } |
---|
76 | } |
---|
77 | }finally{ |
---|
78 | Packages.org.mozilla.javascript.Context.exit(); |
---|
79 | } |
---|
80 | writeFile(dest, copyright + built + text, "utf-8"); |
---|
81 | } |
---|
82 | |
---|
83 | var JSSourceFilefromCode, closurefromCode, jscomp= 0; |
---|
84 | function ccompile(src, dest, optimizeSwitch, copyright){ |
---|
85 | if(!jscomp){ |
---|
86 | // don't do this unless demanded...it may not be available |
---|
87 | JSSourceFilefromCode=java.lang.Class.forName('com.google.javascript.jscomp.JSSourceFile').getMethod('fromCode',[java.lang.String,java.lang.String]); |
---|
88 | closurefromCode = function(filename,content){ |
---|
89 | return JSSourceFilefromCode.invoke(null,[filename,content]); |
---|
90 | }; |
---|
91 | jscomp = com.google.javascript.jscomp; |
---|
92 | } |
---|
93 | //Fake extern |
---|
94 | var externSourceFile = closurefromCode("fakeextern.js", " "); |
---|
95 | |
---|
96 | //Set up source input |
---|
97 | var jsSourceFile = closurefromCode(String(dest), String(readFile(src, "utf-8"))); |
---|
98 | |
---|
99 | //Set up options |
---|
100 | var options = new jscomp.CompilerOptions(); |
---|
101 | options.prettyPrint = optimizeSwitch.indexOf(".keepLines") !== -1; |
---|
102 | |
---|
103 | var FLAG_compilation_level = jscomp.CompilationLevel.SIMPLE_OPTIMIZATIONS; |
---|
104 | FLAG_compilation_level.setOptionsForCompilationLevel(options); |
---|
105 | var FLAG_warning_level = jscomp.WarningLevel.DEFAULT; |
---|
106 | FLAG_warning_level.setOptionsForWarningLevel(options); |
---|
107 | |
---|
108 | //Run the compiler |
---|
109 | var compiler = new Packages.com.google.javascript.jscomp.Compiler(Packages.java.lang.System.err); |
---|
110 | var result = compiler.compile(externSourceFile, jsSourceFile, options); |
---|
111 | writeFile(dest, copyright + built + compiler.toSource(), "utf-8"); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | var |
---|
116 | console= new java.io.BufferedReader(new java.io.InputStreamReader(java.lang.System["in"])), |
---|
117 | readLine= function(){ |
---|
118 | // the + "" convert to a Javascript string |
---|
119 | return console.readLine() + ""; |
---|
120 | }, |
---|
121 | src, dest, optimizeSwitch, copyright; |
---|
122 | |
---|
123 | while(1){ |
---|
124 | // the + "" convert to a Javascript string |
---|
125 | src= readLine(); |
---|
126 | if(src=="."){ |
---|
127 | break; |
---|
128 | } |
---|
129 | dest= readLine(); |
---|
130 | optimizeSwitch= readLine(); |
---|
131 | copyright= eval(readLine()); |
---|
132 | print(dest + ":"); |
---|
133 | var start= (new Date()).getTime(); |
---|
134 | if(/closure/.test(optimizeSwitch)){ |
---|
135 | ccompile(src, dest, optimizeSwitch, copyright); |
---|
136 | }else{ |
---|
137 | sscompile(src, dest, optimizeSwitch, copyright); |
---|
138 | } |
---|
139 | print("Done (compile time:" + ((new Date()).getTime()-start)/1000 + "s)"); |
---|
140 | } |
---|
141 | |
---|