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);
75 this.loadSourceFile();
83 this.timer = new Date() * 1;
90 * @prop srcfiles {String} file containing a list of files/or classes to use.
95 * @prop files {Array} list of files to compress (must be full path)
99 * @prop target {String} target to write files to - must be full path.
103 * @prop debugTarget {String} target to write files debug version to (uncompacted)- must be full path.
105 debugTarget : '', // merged file without compression.
107 * @prop tmpDir {String} (optional) where to put the temporary files.
108 * if you set this, then files will not be cleaned up
112 translateJSON : '', // json based list of strings in all files.
115 * @prop cleanup {Boolean} (optional) clean up temp files after done -
116 * Defaults to false if you set tmpDir, otherwise true.
121 * @prop prefix {String} (optional) prefix of directory to be stripped of when
122 * Calculating md5 of filename
125 out : '', // if no target is specified - then this will contain the result
128 loadSourceFile : function()
130 var lines = File.read(this.srcfile).split("\n");;
134 packAll : function() // do the packing (run from constructor)
137 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
138 //File.write(this.transfile, "");
140 File.write(this.target, "");
143 if (this.debugTarget) {
144 File.write(this.debugTarget, "");
147 for(var i=0; i < this.files.length; i++) {
148 var file = this.files[i];
150 print("reading " +file );
151 if (!File.isFile(file)) {
152 print("SKIP (is not a file) " + file);
156 if (this.debugTarget) {
157 File.append(this.debugTarget, File.read(file));
159 // it's a good idea to check with 0 compression to see if the code can parse!!
162 //File.append(dout, str +"\n");
166 var minfile = this.tmpDir + '/' +file.replace(/\//g, '.');
169 // let's see if we have a min file already?
170 // this might happen if tmpDir is set ..
171 if (true && File.exists(minfile)) {
172 var mt = File.mtime(minfile);
173 var ot = File.mtime(file);
174 print("compare : " + mt + "=>" + ot);
178 // then the min'files time is > than original..
179 var str = File.read(minfile);
180 print("using MIN FILE "+ minfile);
182 File.append(outpath, str + "\n");
191 print("COMPRESSING ");
192 //var codeComp = pack(str, 10, 0, 0);
193 if (File.exists(minfile)) {
194 File.remove(minfile);
196 var str = File.read(file);
197 var str = this.pack(str, file, minfile);
199 File.write(minfile, str);
205 if (this.translateJSON) {
208 print("MERGING LANGUAGE");
209 var out = "if (typeof(_T) == 'undefined') { _T={};}\n"
211 File.write(this.target, out);
218 File.write(this.translateJSON, "");
219 for(var i=0; i < this.files.length; i++) {
220 var file = this.files[i];
221 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
222 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
223 if (File.exists(transmd5)) {
224 var str = File.read(transmd5);
227 File.append(this.target, str + "\n");
229 this.out += str + "\n";
234 File.remove(transmd5);
237 if (File.exists(transfile)) {
238 var str = File.read(transfile);
240 File.append(this.translateJSON, str);
243 File.remove(transfile);
251 print("MERGING SOURCE");
253 for(var i=0; i < this.files.length; i++) {
254 var file = this.files[i];
255 var minfile = this.tmpDir + '/' + file.replace(/\//g, '.');
258 if (!File.exists(minfile)) {
261 var str = File.read(minfile);
262 print("using MIN FILE "+ minfile);
265 File.append(this.target, str + "\n");
267 this.out += str + "\n";
272 File.remove(minfile);
282 * Core packing routine for a file
284 * @param str - str source text..
285 * @param fn - filename (for reference?)
286 * @param minfile - min file location...
290 pack : function (str,fn,minfile)
293 var tr = new TokenReader( { keepDocs :true, keepWhite : true, keepComments : true, sepIdents : true });
294 this.timerPrint("START" + fn);
296 // we can load translation map here...
298 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
300 // at this point we can write a language file...
301 if (this.translateJSON) {
303 this.writeTranslateFile(fn, minfile, toks);
306 this.activeFile = fn;
308 // and replace if we are generating a different language..
310 this.timerPrint("Tokenized");
312 var sp = new ScopeParser(new TokenStream(toks));
313 this.timerPrint("Converted to Parser");
315 sp.buildSymbolTree();
316 this.timerPrint("Built Sym tree");
317 sp.mungeSymboltree();
318 this.timerPrint("Munged Sym tree");
319 print(sp.warnings.join("\n"));
320 var out = CompressWhite(sp.ts, this);
321 this.timerPrint("Compressed");
328 timerPrint: function (str) {
329 var ntime = new Date() * 1;
330 var tdif = ntime -this.timer;
332 print('['+tdif+']'+str);
337 * Translation concept...
338 * -> replace text strings with _T....
339 * -> this file will need inserting at the start of the application....
340 * -> we need to generate 2 files,
341 * -> a reference used to do the translation, and the _T file..
345 writeTranslateFile : function(fn, minfile, toks)
350 toks.forEach(function (t) {
351 if (t.type == 'STRN' && t.name == 'DOUBLE_QUOTE') {
352 var sval = t.data.substring(1,t.data.length-1);
353 var ffn = fn.substring(_this.prefix.length);
354 map[sval] = _this.md5(ffn + '-' + sval);
358 var transfile = minfile + '.lang.trans';
359 var transmd5 = minfile + '.lang';
360 print("writeTranslateFile " + transfile);
363 if (File.exists(transfile)) {
364 File.remove(transfile);
366 if (File.exists(transmd5)) {
367 File.remove(transmd5);
369 for(v in map) { i++; break };
371 return; // no strings in file...
373 var ffn = fn.substring(this.prefix.length);
376 File.write(transfile, "\n'" + ffn + "' : {");
380 File.write(transmd5, '');
382 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
384 // strings are raw... - as the where encoded to start with!!!
385 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
387 File.append(transfile, "\n},"); // always one trailing..
391 md5 : function (string)
394 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
397 stringHandler : function(tok)
399 //print("STRING HANDLER");
400 // callback when outputing compressed file,
402 if (!this.translateJSON) {
403 // print("TURNED OFF");
406 if (tok.name == 'SINGLE_QUOTE') {
410 var sval = data.substring(1,data.length-1);
411 // we do not clean up... quoting here!??!!?!?!?!?
414 // blank with tabs or spaces..
415 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
416 // return tok.outData;
419 var sval = tok.data.substring(1,data.length-1);
420 var fn = this.activeFile.substring(this.prefix.length);
423 return '_T["' + this.md5(fn + '-' + sval) + '"]';