1 // <script type="text/javascript">
2 XObject = imports.XObject.XObject;
3 File = imports.File.File;
5 TextStream = imports['JSDOC/TextStream.js'].TextStream;
6 TokenReader = imports['JSDOC/TokenReader.js'].TokenReader;
7 ScopeParser = imports['JSDOC/ScopeParser.js'].ScopeParser;
8 TokenStream = imports['JSDOC/TokenStream.js'].TokenStream;
9 CompressWhite = imports['JSDOC/CompressWhite.js'].CompressWhite;
11 GLib = imports.gi.GLib;
23 Packer = imports['JSDOC/Packer.js'].Packer;
26 files : [ "/location/of/file1.js", "/location/of/file2.js", ... ],
27 target : "/tmp/output.js",
28 debugTarget : "/tmp/output.debug.js", // merged file without compression.
29 translateJSON: "/tmp/translate.json",
34 "/location/of/temp_batch_dir",
35 "/location/of/output-compacted-file.js",
36 "/location/of/output-debug-merged-file.js"
41 * Notes for improving compacting:
42 * if you add a jsdoc comment
49 * directly before an eval statement, it will compress all the code around the eval,
50 * and not rename the variables 'avarname'
52 * Dont try running this on a merged uncompressed large file - it's used to be horrifically slow. not sure about now..
53 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
57 * Notes for translation
58 * - translation relies on you using double quotes for strings if they need translating
59 * - single quoted strings are ignored.
61 * Generation of indexFiles
62 * - translateIndex = the indexfile
69 Packer = function(cfg)
72 XObject.extend(this, cfg);
78 this.timer = new Date() * 1;
85 * @prop files {Array} list of files to compress (must be full path)
89 * @prop target {String} target to write files to - must be full path.
93 * @prop debugTarget {String} target to write files debug version to (uncompacted)- must be full path.
95 debugTarget : '', // merged file without compression.
97 * @prop tmpDir {String} (optional) where to put the temporary files.
98 * if you set this, then files will not be cleaned up
102 translateJSON : '', // json based list of strings in all files.
105 * @prop cleanup {Boolean} (optional) clean up temp files after done -
106 * Defaults to false if you set tmpDir, otherwise true.
111 * @prop prefix {String} (optional) prefix of directory to be stripped of when
112 * Calculating md5 of filename
115 out : '', // if no target is specified - then this will contain the result
117 packAll : function() // do the packing (run from constructor)
120 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
121 //File.write(this.transfile, "");
123 File.write(this.target, "");
126 if (this.debugTarget) {
127 File.write(this.debugTarget, "");
130 for(var i=0; i < this.files.length; i++) {
131 var file = this.files[i];
133 print("reading " +file );
134 if (!File.isFile(file)) {
135 print("SKIP (is not a file) " + file);
139 if (this.debugTarget) {
140 File.append(this.debugTarget, File.read(file));
142 // it's a good idea to check with 0 compression to see if the code can parse!!
145 //File.append(dout, str +"\n");
149 var minfile = this.tmpDir + '/' +file.replace(/\//g, '.');
152 // let's see if we have a min file already?
153 // this might happen if tmpDir is set ..
154 if (true && File.exists(minfile)) {
155 var mt = File.mtime(minfile);
156 var ot = File.mtime(file);
157 print("compare : " + mt + "=>" + ot);
161 // then the min'files time is > than original..
162 var str = File.read(minfile);
163 print("using MIN FILE "+ minfile);
165 File.append(outpath, str + "\n");
174 print("COMPRESSING ");
175 //var codeComp = pack(str, 10, 0, 0);
176 if (File.exists(minfile)) {
177 File.remove(minfile);
179 var str = File.read(file);
180 var str = this.pack(str, file, minfile);
182 File.write(minfile, str);
188 if (this.translateJSON) {
191 print("MERGING LANGUAGE");
192 var out = "if (typeof(_T) == 'undefined') { _T={};}\n"
194 File.write(this.target, out);
201 File.write(this.translateJSON, "");
202 for(var i=0; i < this.files.length; i++) {
203 var file = this.files[i];
204 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
205 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
206 if (File.exists(transmd5)) {
207 var str = File.read(transmd5);
210 File.append(this.target, str + "\n");
212 this.out += str + "\n";
217 File.remove(transmd5);
220 if (File.exists(transfile)) {
221 var str = File.read(transfile);
223 File.append(this.translateJSON, str);
226 File.remove(transfile);
234 print("MERGING SOURCE");
236 for(var i=0; i < this.files.length; i++) {
237 var file = this.files[i];
238 var minfile = this.tmpDir + '/' + file.replace(/\//g, '.');
241 if (!File.exists(minfile)) {
244 var str = File.read(minfile);
245 print("using MIN FILE "+ minfile);
248 File.append(this.target, str + "\n");
250 this.out += str + "\n";
255 File.remove(minfile);
265 * Core packing routine for a file
267 * @param str - str source text..
268 * @param fn - filename (for reference?)
269 * @param minfile - min file location...
273 pack : function (str,fn,minfile)
276 var tr = new TokenReader( { keepDocs :true, keepWhite : true, keepComments : true, sepIdents : true });
277 this.timerPrint("START" + fn);
279 // we can load translation map here...
281 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
283 // at this point we can write a language file...
284 if (this.translateJSON) {
286 this.writeTranslateFile(fn, minfile, toks);
289 this.activeFile = fn;
291 // and replace if we are generating a different language..
293 this.timerPrint("Tokenized");
295 var sp = new ScopeParser(new TokenStream(toks));
296 this.timerPrint("Converted to Parser");
298 sp.buildSymbolTree();
299 this.timerPrint("Built Sym tree");
300 sp.mungeSymboltree();
301 this.timerPrint("Munged Sym tree");
302 print(sp.warnings.join("\n"));
303 var out = CompressWhite(sp.ts, this);
304 this.timerPrint("Compressed");
311 timerPrint: function (str) {
312 var ntime = new Date() * 1;
313 var tdif = ntime -this.timer;
315 print('['+tdif+']'+str);
320 * Translation concept...
321 * -> replace text strings with _T....
322 * -> this file will need inserting at the start of the application....
323 * -> we need to generate 2 files,
324 * -> a reference used to do the translation, and the _T file..
328 writeTranslateFile : function(fn, minfile, toks)
333 toks.forEach(function (t) {
334 if (t.type == 'STRN' && t.name == 'DOUBLE_QUOTE') {
335 var sval = t.data.substring(1,t.data.length-1);
336 var ffn = fn.substring(_this.prefix.length);
337 map[sval] = _this.md5(ffn + '-' + sval);
341 var transfile = minfile + '.lang.trans';
342 var transmd5 = minfile + '.lang';
343 print("writeTranslateFile " + transfile);
346 if (File.exists(transfile)) {
347 File.remove(transfile);
349 if (File.exists(transmd5)) {
350 File.remove(transmd5);
352 for(v in map) { i++; break };
354 return; // no strings in file...
356 var ffn = fn.substring(this.prefix.length);
359 File.write(transfile, "\n'" + ffn + "' : {");
363 File.write(transmd5, '');
365 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
367 // strings are raw... - as the where encoded to start with!!!
368 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
370 File.append(transfile, "\n},"); // always one trailing..
374 md5 : function (string)
377 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
380 stringHandler : function(tok)
382 //print("STRING HANDLER");
383 // callback when outputing compressed file,
385 if (!this.translateJSON) {
386 // print("TURNED OFF");
389 if (tok.name == 'SINGLE_QUOTE') {
393 var sval = data.substring(1,data.length-1);
394 // we do not clean up... quoting here!??!!?!?!?!?
397 // blank with tabs or spaces..
398 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
399 // return tok.outData;
402 var sval = tok.data.substring(1,data.length-1);
403 var fn = this.activeFile.substring(this.prefix.length);
406 return '_T["' + this.md5(fn + '-' + sval) + '"]';