1 // <script type="text/javascript">
2 XObject = imports.XObject.XObject;
3 File = imports.File.File;
5 TokenReader = imports['JSDOC/TokenReader.js'].TokenReader;
6 ScopeParser = imports['JSDOC/ScopeParser.js'].ScopeParser;
7 TokenStream = imports['JSDOC/TokenStream.js'].TokenStream;
8 CompressWhite = imports['JSDOC/CompressWhite.js'].CompressWhite;
10 GLib = imports.gi.GLib;
22 Packer = imports['JSDOC/Packer.js'].Packer;
25 files : [ "/location/of/file1.js", "/location/of/file2.js", ... ],
26 target : "/tmp/output.js",
27 debugTarget : "/tmp/output.debug.js", // merged file without compression.
28 translateJSON: "/tmp/translate.json",
33 "/location/of/temp_batch_dir",
34 "/location/of/output-compacted-file.js",
35 "/location/of/output-debug-merged-file.js"
40 * Notes for improving compacting:
41 * if you add a jsdoc comment
48 * directly before an eval statement, it will compress all the code around the eval,
49 * and not rename the variables 'avarname'
51 * Dont try running this on a merged uncompressed large file - it's used to be horrifically slow. not sure about now..
52 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
56 * Notes for translation
57 * - translation relies on you using double quotes for strings if they need translating
58 * - single quoted strings are ignored.
60 * Generation of indexFiles
61 * - translateIndex = the indexfile
68 Packer = function(cfg)
71 XObject.extend(this, cfg);
76 if ((this.tmpDir != Packer.prototype.tmpDir) && (!cfg.cleanup)) {
77 this.cleanup = false; // do not clean up files.. = as tmpdir is set.
80 this.timer = new Date() * 1;
87 * @prop files {Array} list of files to compress (must be full path)
91 * @prop target {String} target to write files to - must be full path.
95 * @prop debugTarget {String} target to write files debug version to (uncompacted)- must be full path.
97 debugTarget : '', // merged file without compression.
99 * @prop tmpDir {String} (optional) where to put the temporary files.
100 * if you set this, then files will not be cleaned up
104 translateJSON : '', // json based list of strings in all files.
107 * @prop cleanup {Boolean} (optional) clean up temp files after done -
108 * Defaults to false if you set tmpDir, otherwise true.
113 out : '', // if no target is specified - then this will contain the result
115 packAll : function() // do the packing (run from constructor)
118 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
119 //File.write(this.transfile, "");
121 File.write(this.target, "");
124 if (this.debugTarget) {
125 File.write(this.debugTarget, "");
128 for(var i=0; i < this.files.length; i++) {
129 var file = this.files[i];
131 print("reading " +file );
132 if (!File.exists(file)) {
133 print("SKIP (does not exist) " + file);
137 if (this.debugTarget) {
138 File.append(this.debugTarget, File.read(files[i]));
140 // it's a good idea to check with 0 compression to see if the code can parse!!
143 //File.append(dout, str +"\n");
147 var minfile = this.tmpDir + '/' +files.replace(/\//g, '.');
150 // let's see if we have a min file already?
151 // this might happen if tmpDir is set ..
152 if (true && File.exists(minfile)) {
153 var mt = File.mtime(minfile);
154 var ot = File.mtime(files);
155 print("compare : " + mt + "=>" + ot);
159 // then the min'files time is > than original..
160 var str = File.read(minfile);
161 print("using MIN FILE "+ minfile);
163 File.append(outpath, str + "\n");
172 print("COMPRESSING ");
173 //var codeComp = pack(str, 10, 0, 0);
174 if (File.exists(minfile)) {
175 File.remove(minfile);
177 var str = File.read(files);
178 var str = this.pack(str, files, minfile);
180 File.write(minfile, str);
181 this.tmpFiles.push(minfile);
187 if (this.translateJSON) {
190 print("MERGING LANGUAGE");
191 var out = "if (typeof(_T) == 'undefined') { _T={};}\n"
193 File.write(this.target, out);
200 File.write(this.translateJSON, "");
201 for(var i=0; i < this.files.length; i++) {
202 var file = this.files[i];
203 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
204 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
205 if (File.exists(transmd5)) {
206 var str = File.read(transmd5);
209 File.append(this.target, str + "\n");
211 this.out += str + "\n";
216 File.remove(transmd5);
219 if (File.exists(transfile)) {
220 var str = File.read(transfile);
222 File.append(this.translateJSON, str);
225 File.remove(transfile);
233 print("MERGING SOURCE");
235 for(var i=0; i < files.length; i++) {
237 var minfile = bpath + '/' + this.files.replace(/\//g, '.');
240 if (!File.exists(minfile)) {
243 var str = File.read(minfile);
244 print("using MIN FILE "+ minfile);
247 File.append(this.target, str + "\n");
249 this.out += str + "\n";
254 File.remove(minfile);
264 * Core packing routine for a file
266 * @param str - str source text..
267 * @param fn - filename (for reference?)
268 * @param minfile - min file location...
272 pack : function (str,fn,minfile)
275 var tr = new TokenReader( { keepDocs :true, keepWhite : true, keepComments : true, sepIdents : true });
276 this.timerPrint("START" + fn);
278 // we can load translation map here...
280 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
282 // at this point we can write a language file...
283 if (this.translateJSON) {
284 this.writeTranslateFile(fn, minfile, tr.translateMap);
287 this.activeFile = fn;
289 // and replace if we are generating a different language..
291 this.timerPrint("Tokenized");
293 var sp = new ScopeParser(new TokenStream(toks));
294 this.timerPrint("Converted to Parser");
296 sp.buildSymbolTree();
297 this.timerPrint("Built Sym tree");
298 sp.mungeSymboltree();
299 this.timerPrint("Munged Sym tree");
300 print(sp.warnings.join("\n"));
301 var out = JSDOC.CompressWhite(sp.ts, this);
302 this.timerPrint("Compressed");
309 timerPrint: function (str) {
310 var ntime = new Date() * 1;
311 var tdif = ntime -this.timer;
313 print('['+tdif+']'+str);
318 * Translation concept...
319 * -> replace text strings with _T....
320 * -> this file will need inserting at the start of the application....
321 * -> we need to generate 2 files,
322 * -> a reference used to do the translation, and the _T file..
326 writeTranslateFile : function(fn, minfile, map)
328 var transfile = minfile + '.lang.trans';
329 var transmd5 = minfile + '.lang';
332 if (File.exists(transfile)) {
333 File.remove(transfile);
335 if (File.exists(transmd5)) {
336 File.remove(transmd5);
338 for(v in map) { i++; break };
340 return; // no strings in file...
342 var ff = fn.split('/');
343 var ffn = ff[ff.length-1];
346 File.write(transfile, "\n" + ffn.toSource() + " : {");
350 File.write(transmd5, '');
352 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
354 // strings are raw... - as the where encoded to start with!!!
355 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
357 File.append(transfile, "\n},"); // always one trailing..
361 md5 : function (string)
364 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
367 stringHandler : function(tok)
369 //print("STRING HANDLER");
370 // callback when outputing compressed file,
371 if (!this.translateJSON) {
372 // print("TURNED OFF");
375 if (tok.name == SINGLE_QUOTE) {
378 var sval = tok.data.substring(1,tok.data.length-1);
379 // we do not clean up... quoting here!??!!?!?!?!?
382 // blank with tabs or spaces..
383 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
384 // return tok.outData;
390 var ff = this.activeFile.split('/');
391 var ffn = ff[ff.length-1];
392 return '_T["' + this.md5(ffn + '-' + sval) + '"]';