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);
78 if ((typeof(cfg.tmpDir) != 'undefined') && (!cfg.cleanup)) {
79 this.cleanup = false; // do not clean up files.. = as tmpdir is set.
82 this.timer = new Date() * 1;
88 * @prop files {Array} list of files to compress (must be full path)
92 * @prop target {String} target to write files to - must be full path.
96 * @prop debugTarget {String} target to write files debug version to (uncompacted)- must be full path.
98 debugTarget : '', // merged file without compression.
100 * @prop tmpDir {String} (optional) where to put the temporary files.
101 * if you set this, then files will not be cleaned up
105 translateJson : '', // json based list of strings in all files.
107 tmpFiles : false, // list of temporary files - cleaned up at end..
109 * @prop cleanup {Boolean} (optional) clean up temp files after done -
110 * Defaults to false if you set tmpDir, otherwise true.
114 packAll : function() // do the packing (run from constructor)
117 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
118 //File.write(this.transfile, "");
119 File.write(this.target, "");
120 if (this.debugTarget) {
121 File.write(this.debugTarget, "");
124 for(var i=0; i < this.files.length; i++) {
125 var file = this.files[i];
127 print("reading " +file );
128 if (!File.exists(file)) {
129 print("SKIP (does not exist) " + file);
133 if (this.debugTarget) {
134 File.append(this.debugTarget, File.read(files[i]));
136 // it's a good idea to check with 0 compression to see if the code can parse!!
139 //File.append(dout, str +"\n");
143 var minfile = this.tmpDir + '/' +files.replace(/\//g, '.');
146 // let's see if we have a min file already?
147 // this might happen if tmpDir is set ..
148 if (true && File.exists(minfile)) {
149 var mt = File.mtime(minfile);
150 var ot = File.mtime(files);
151 print("compare : " + mt + "=>" + ot);
155 // then the min'files time is > than original..
156 var str = File.read(minfile);
157 print("using MIN FILE "+ minfile);
159 File.append(outpath, str + "\n");
168 print("COMPRESSING ");
169 //var codeComp = pack(str, 10, 0, 0);
170 var str = File.read(files);
171 var str = this.pack(str, files, minfile);
173 File.write(minfile, str);
174 this.tmpFiles.push(minfile);
180 if (this.translateJson) {
183 print("MERGING LANGUAGE");
184 File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
186 var transfileAll = bpath + '/_translation_.js';
187 File.write(transfileAll, "");
188 for(var i=0; i < files.length; i++) {
189 var transfile= bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang.trans';
190 var transmd5 = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
191 if (File.exists(transmd5)) {
192 var str = File.read(transmd5);
194 File.append(outpath, str + "\n");
197 if (File.exists(transfile)) {
198 var str = File.read(transfile);
200 File.append(transfileAll, str);
207 print("MERGING SOURCE");
209 for(var i=0; i < files.length; i++) {
211 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
212 if (!File.exists(minfile)) {
215 var str = File.read(minfile);
216 print("using MIN FILE "+ minfile);
218 File.append(outpath, str + "\n");
223 //File.append(dout, "\n");// end the function
229 * Core packing routine for a file
231 * @param str - str source text..
232 * @param fn - filename (for reference?)
233 * @param minfile - min file location...
237 pack : function (str,fn,minfile)
240 var tr = new TokenReader();
241 this.timerPrint("START" + fn);
243 // we can load translation map here...
245 var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
247 // at this point we can write a language file...
248 if (this.translate) {
249 this.writeTranslateFile(fn, minfile, tr.translateMap);
252 this.activeFile = fn;
254 // and replace if we are generating a different language..
259 this.timerPrint("Tokenized");
261 var sp = new ScopeParser(new TokenStream(toks, str.length));
262 this.timerPrint("Converted to Parser");
264 sp.buildSymbolTree();
265 this.timerPrint("Built Sym tree");
266 sp.mungeSymboltree();
267 this.timerPrint("Munged Sym tree");
268 print(sp.warnings.join("\n"));
269 var out = JSDOC.CompressWhite(sp.ts, this);
270 this.timerPrint("Compressed");
275 timerPrint: function (str) {
276 var ntime = new Date() * 1;
277 var tdif = ntime -this.timer;
279 print('['+tdif+']'+str);
284 * Translation concept...
285 * -> replace text strings with _T....
286 * -> this file will need inserting at the start of the application....
287 * -> we need to generate 2 files,
288 * -> a reference used to do the translation, and the _T file..
292 writeTranslateFile : function(fn, minfile, map)
294 var transfile = minfile + '.lang.trans';
295 var transmd5 = minfile + '.lang';
298 if (File.exists(transfile)) {
299 File.remove(transfile);
301 if (File.exists(transmd5)) {
302 File.remove(transmd5);
304 for(v in map) { i++; break };
306 return; // no strings in file...
308 var ff = fn.split('/');
309 var ffn = ff[ff.length-1];
312 File.write(transfile, "\n" + ffn.toSource() + " : {");
316 File.write(transmd5, '');
318 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
320 // strings are raw... - as the where encoded to start with!!!
321 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
323 File.append(transfile, "\n},"); // always one trailing..
327 md5 : function (string)
330 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
333 stringHandler : function(tok)
335 //print("STRING HANDLER");
336 // callback when outputing compressed file,
337 if (!this.translate) {
338 // print("TURNED OFF");
344 var sval = tok.data.substring(1,tok.data.length-1);
345 // blank with tabs or spaces..
346 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
347 // return tok.outData;
353 var ff = this.activeFile.split('/');
354 var ffn = ff[ff.length-1];
355 return '_T["' + this.md5(ffn + '-' + sval) + '"]';