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);
77 this.timer = new Date() * 1;
84 * @prop files {Array} list of files to compress (must be full path)
88 * @prop target {String} target to write files to - must be full path.
92 * @prop debugTarget {String} target to write files debug version to (uncompacted)- must be full path.
94 debugTarget : '', // merged file without compression.
96 * @prop tmpDir {String} (optional) where to put the temporary files.
97 * if you set this, then files will not be cleaned up
101 translateJSON : '', // json based list of strings in all files.
104 * @prop cleanup {Boolean} (optional) clean up temp files after done -
105 * Defaults to false if you set tmpDir, otherwise true.
110 out : '', // if no target is specified - then this will contain the result
112 packAll : function() // do the packing (run from constructor)
115 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
116 //File.write(this.transfile, "");
118 File.write(this.target, "");
121 if (this.debugTarget) {
122 File.write(this.debugTarget, "");
125 for(var i=0; i < this.files.length; i++) {
126 var file = this.files[i];
128 print("reading " +file );
129 if (!File.exists(file)) {
130 print("SKIP (does not exist) " + file);
134 if (this.debugTarget) {
135 File.append(this.debugTarget, File.read(file));
137 // it's a good idea to check with 0 compression to see if the code can parse!!
140 //File.append(dout, str +"\n");
144 var minfile = this.tmpDir + '/' +file.replace(/\//g, '.');
147 // let's see if we have a min file already?
148 // this might happen if tmpDir is set ..
149 if (true && File.exists(minfile)) {
150 var mt = File.mtime(minfile);
151 var ot = File.mtime(file);
152 print("compare : " + mt + "=>" + ot);
156 // then the min'files time is > than original..
157 var str = File.read(minfile);
158 print("using MIN FILE "+ minfile);
160 File.append(outpath, str + "\n");
169 print("COMPRESSING ");
170 //var codeComp = pack(str, 10, 0, 0);
171 if (File.exists(minfile)) {
172 File.remove(minfile);
174 var str = File.read(file);
175 var str = this.pack(str, file, minfile);
177 File.write(minfile, str);
183 if (this.translateJSON) {
186 print("MERGING LANGUAGE");
187 var out = "if (typeof(_T) == 'undefined') { _T={};}\n"
189 File.write(this.target, out);
196 File.write(this.translateJSON, "");
197 for(var i=0; i < this.files.length; i++) {
198 var file = this.files[i];
199 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
200 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
201 if (File.exists(transmd5)) {
202 var str = File.read(transmd5);
205 File.append(this.target, str + "\n");
207 this.out += str + "\n";
212 File.remove(transmd5);
215 if (File.exists(transfile)) {
216 var str = File.read(transfile);
218 File.append(this.translateJSON, str);
221 File.remove(transfile);
229 print("MERGING SOURCE");
231 for(var i=0; i < this.files.length; i++) {
232 var file = this.files[i];
233 var minfile = this.tmpDir + '/' + file.replace(/\//g, '.');
236 if (!File.exists(minfile)) {
239 var str = File.read(minfile);
240 print("using MIN FILE "+ minfile);
243 File.append(this.target, str + "\n");
245 this.out += str + "\n";
250 File.remove(minfile);
260 * Core packing routine for a file
262 * @param str - str source text..
263 * @param fn - filename (for reference?)
264 * @param minfile - min file location...
268 pack : function (str,fn,minfile)
271 var tr = new TokenReader( { keepDocs :true, keepWhite : true, keepComments : true, sepIdents : true });
272 this.timerPrint("START" + fn);
274 // we can load translation map here...
276 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
278 // at this point we can write a language file...
279 if (this.translateJSON) {
280 this.writeTranslateFile(fn, minfile, tr.translateMap);
283 this.activeFile = fn;
285 // and replace if we are generating a different language..
287 this.timerPrint("Tokenized");
289 var sp = new ScopeParser(new TokenStream(toks));
290 this.timerPrint("Converted to Parser");
292 sp.buildSymbolTree();
293 this.timerPrint("Built Sym tree");
294 sp.mungeSymboltree();
295 this.timerPrint("Munged Sym tree");
296 print(sp.warnings.join("\n"));
297 var out = JSDOC.CompressWhite(sp.ts, this);
298 this.timerPrint("Compressed");
305 timerPrint: function (str) {
306 var ntime = new Date() * 1;
307 var tdif = ntime -this.timer;
309 print('['+tdif+']'+str);
314 * Translation concept...
315 * -> replace text strings with _T....
316 * -> this file will need inserting at the start of the application....
317 * -> we need to generate 2 files,
318 * -> a reference used to do the translation, and the _T file..
322 writeTranslateFile : function(fn, minfile, map)
324 var transfile = minfile + '.lang.trans';
325 var transmd5 = minfile + '.lang';
328 if (File.exists(transfile)) {
329 File.remove(transfile);
331 if (File.exists(transmd5)) {
332 File.remove(transmd5);
334 for(v in map) { i++; break };
336 return; // no strings in file...
338 var ff = fn.split('/');
339 var ffn = ff[ff.length-1];
342 File.write(transfile, "\n" + ffn.toSource() + " : {");
346 File.write(transmd5, '');
348 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
350 // strings are raw... - as the where encoded to start with!!!
351 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
353 File.append(transfile, "\n},"); // always one trailing..
357 md5 : function (string)
360 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
363 stringHandler : function(tok)
365 //print("STRING HANDLER");
366 // callback when outputing compressed file,
367 if (!this.translateJSON) {
368 // print("TURNED OFF");
371 if (tok.name == SINGLE_QUOTE) {
374 var sval = tok.data.substring(1,tok.data.length-1);
375 // we do not clean up... quoting here!??!!?!?!?!?
378 // blank with tabs or spaces..
379 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
380 // return tok.outData;
386 var ff = this.activeFile.split('/');
387 var ffn = ff[ff.length-1];
388 return '_T["' + this.md5(ffn + '-' + sval) + '"]';