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.
108 * @prop cleanup {Boolean} (optional) clean up temp files after done -
109 * Defaults to false if you set tmpDir, otherwise true.
113 packAll : function() // do the packing (run from constructor)
116 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
117 //File.write(this.transfile, "");
118 File.write(this.target, "");
119 if (this.debugTarget) {
120 File.write(this.debugTarget, "");
123 for(var i=0; i < this.files.length; i++) {
124 var file = this.files[i];
126 print("reading " +file );
127 if (!File.exists(file)) {
128 print("SKIP (does not exist) " + file);
132 if (this.debugTarget) {
133 File.append(this.debugTarget, File.read(files[i]));
135 // it's a good idea to check with 0 compression to see if the code can parse!!
138 //File.append(dout, str +"\n");
142 var minfile = this.tmpDir + '/' +files.replace(/\//g, '.');
145 // let's see if we have a min file already?
146 // this might happen if tmpDir is set ..
147 if (true && File.exists(minfile)) {
148 var mt = File.mtime(minfile);
149 var ot = File.mtime(files);
150 print("compare : " + mt + "=>" + ot);
154 // then the min'files time is > than original..
155 var str = File.read(minfile);
156 print("using MIN FILE "+ minfile);
158 File.append(outpath, str + "\n");
167 print("COMPRESSING ");
168 //var codeComp = pack(str, 10, 0, 0);
169 if (File.exists(minfile)) {
170 File.remove(minfile);
172 var str = File.read(files);
173 var str = this.pack(str, files, minfile);
175 File.write(minfile, str);
176 this.tmpFiles.push(minfile);
182 if (this.translateJSON) {
185 print("MERGING LANGUAGE");
186 File.write(this.target, "if (typeof(_T) == 'undefined') { _T={};}\n");
189 File.write(this.translateJSON, "");
190 for(var i=0; i < this.files.length; i++) {
191 var file = this.files[i];
192 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
193 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
194 if (File.exists(transmd5)) {
195 var str = File.read(transmd5);
197 File.append(this.target, str + "\n");
200 File.remove(transmd5);
203 if (File.exists(transfile)) {
204 var str = File.read(transfile);
206 File.append(this.translateJSON, str);
209 File.remove(transfile);
217 print("MERGING SOURCE");
219 for(var i=0; i < files.length; i++) {
221 var minfile = bpath + '/' + this.files.replace(/\//g, '.');
224 if (!File.exists(minfile)) {
227 var str = File.read(minfile);
228 print("using MIN FILE "+ minfile);
230 File.append(this.target, str + "\n");
233 File.remove(minfile);
243 * Core packing routine for a file
245 * @param str - str source text..
246 * @param fn - filename (for reference?)
247 * @param minfile - min file location...
251 pack : function (str,fn,minfile)
254 var tr = new TokenReader( { keepDocs :true, keepWhite : true, keepComments : true, sepIdents : true });
255 this.timerPrint("START" + fn);
257 // we can load translation map here...
259 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
261 // at this point we can write a language file...
262 if (this.translateJSON) {
263 this.writeTranslateFile(fn, minfile, tr.translateMap);
266 this.activeFile = fn;
268 // and replace if we are generating a different language..
270 this.timerPrint("Tokenized");
272 var sp = new ScopeParser(new TokenStream(toks));
273 this.timerPrint("Converted to Parser");
275 sp.buildSymbolTree();
276 this.timerPrint("Built Sym tree");
277 sp.mungeSymboltree();
278 this.timerPrint("Munged Sym tree");
279 print(sp.warnings.join("\n"));
280 var out = JSDOC.CompressWhite(sp.ts, this);
281 this.timerPrint("Compressed");
288 timerPrint: function (str) {
289 var ntime = new Date() * 1;
290 var tdif = ntime -this.timer;
292 print('['+tdif+']'+str);
297 * Translation concept...
298 * -> replace text strings with _T....
299 * -> this file will need inserting at the start of the application....
300 * -> we need to generate 2 files,
301 * -> a reference used to do the translation, and the _T file..
305 writeTranslateFile : function(fn, minfile, map)
307 var transfile = minfile + '.lang.trans';
308 var transmd5 = minfile + '.lang';
311 if (File.exists(transfile)) {
312 File.remove(transfile);
314 if (File.exists(transmd5)) {
315 File.remove(transmd5);
317 for(v in map) { i++; break };
319 return; // no strings in file...
321 var ff = fn.split('/');
322 var ffn = ff[ff.length-1];
325 File.write(transfile, "\n" + ffn.toSource() + " : {");
329 File.write(transmd5, '');
331 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
333 // strings are raw... - as the where encoded to start with!!!
334 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
336 File.append(transfile, "\n},"); // always one trailing..
340 md5 : function (string)
343 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
346 stringHandler : function(tok)
348 //print("STRING HANDLER");
349 // callback when outputing compressed file,
350 if (!this.translateJSON) {
351 // print("TURNED OFF");
354 if (tok.name == SINGLE_QUOTE) {
357 var sval = tok.data.substring(1,tok.data.length-1);
358 // blank with tabs or spaces..
359 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
360 // return tok.outData;
366 var ff = this.activeFile.split('/');
367 var ffn = ff[ff.length-1];
368 return '_T["' + this.md5(ffn + '-' + sval) + '"]';