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);
79 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.
98 translateJson : '', // json based list of strings in all files.
100 // set to false to stop translation happening..
105 * @param {String} batch_path location of batched temporary min files.
106 * @param {String} compressed_file eg. roo-all.js
107 * @param {String} debug_file eg. roo-debug.js
111 packFiles : function() {
113 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
114 //File.write(this.transfile, "");
115 File.write(this.target, "");
116 if (this.debugTarget) {
117 File.write(this.debugTarget, "");
120 for(var i=0; i < this.files.length; i++) {
121 var file = this.files[i];
123 print("reading " +file );
124 if (!File.exists(file)) {
125 print("SKIP (does not exist) " + file);
129 if (this.debugTarget) {
130 File.append(this.debugTarget, File.read(files[i]));
132 // it's a good idea to check with 0 compression to see if the code can parse!!
135 //File.append(dout, str +"\n");
139 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
140 var transfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
141 // let's see if we have a min file already?
142 if (true && File.exists(minfile)) {
143 var mt = File.mtime(minfile);
144 var ot = File.mtime(files[i]);
145 print("compare : " + mt + "=>" + ot);
149 // then the min'files time is > than original..
150 var str = File.read(minfile);
151 print("using MIN FILE "+ minfile);
153 File.append(outpath, str + "\n");
162 print("COMPRESSING ");
163 //var codeComp = pack(str, 10, 0, 0);
164 var str = File.read(files[i]);
165 var str = this.pack(str, files[i], minfile);
167 File.write(minfile, str);
172 //var str = File.read(minfile);
173 //print("using MIN FILE "+ minfile);
174 //File.append(outpath, str + "\n");
175 //this.timerPrint("Wrote Files");
177 if (codeComp.length) {
180 File.append(outpath, codeComp+"\n");
181 File.write(minfile, codeComp);
185 // if (i > 10) return;
187 if (this.translate) {
190 print("MERGING LANGUAGE");
191 File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
193 var transfileAll = bpath + '/_translation_.js';
194 File.write(transfileAll, "");
195 for(var i=0; i < files.length; i++) {
196 var transfile= bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang.trans';
197 var transmd5 = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
198 if (File.exists(transmd5)) {
199 var str = File.read(transmd5);
201 File.append(outpath, str + "\n");
204 if (File.exists(transfile)) {
205 var str = File.read(transfile);
207 File.append(transfileAll, str);
214 print("MERGING SOURCE");
216 for(var i=0; i < files.length; i++) {
218 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
219 if (!File.exists(minfile)) {
222 var str = File.read(minfile);
223 print("using MIN FILE "+ minfile);
225 File.append(outpath, str + "\n");
230 //File.append(dout, "\n");// end the function
236 * Core packing routine for a file
238 * @param str - str source text..
239 * @param fn - filename (for reference?)
240 * @param minfile - min file location...
244 pack : function (str,fn,minfile)
247 var tr = new TokenReader();
248 this.timerPrint("START" + fn);
250 // we can load translation map here...
252 var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
254 // at this point we can write a language file...
255 if (this.translate) {
256 this.writeTranslateFile(fn, minfile, tr.translateMap);
259 this.activeFile = fn;
261 // and replace if we are generating a different language..
266 this.timerPrint("Tokenized");
268 var sp = new ScopeParser(new TokenStream(toks, str.length));
269 this.timerPrint("Converted to Parser");
271 sp.buildSymbolTree();
272 this.timerPrint("Built Sym tree");
273 sp.mungeSymboltree();
274 this.timerPrint("Munged Sym tree");
275 print(sp.warnings.join("\n"));
276 var out = JSDOC.CompressWhite(sp.ts, this);
277 this.timerPrint("Compressed");
282 timerPrint: function (str) {
283 var ntime = new Date() * 1;
284 var tdif = ntime -this.timer;
286 print('['+tdif+']'+str);
291 * Translation concept...
292 * -> replace text strings with _T....
293 * -> this file will need inserting at the start of the application....
294 * -> we need to generate 2 files,
295 * -> a reference used to do the translation, and the _T file..
299 writeTranslateFile : function(fn, minfile, map)
301 var transfile = minfile + '.lang.trans';
302 var transmd5 = minfile + '.lang';
305 if (File.exists(transfile)) {
306 File.remove(transfile);
308 if (File.exists(transmd5)) {
309 File.remove(transmd5);
311 for(v in map) { i++; break };
313 return; // no strings in file...
315 var ff = fn.split('/');
316 var ffn = ff[ff.length-1];
319 File.write(transfile, "\n" + ffn.toSource() + " : {");
323 File.write(transmd5, '');
325 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
327 // strings are raw... - as the where encoded to start with!!!
328 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
330 File.append(transfile, "\n},"); // always one trailing..
334 md5 : function (string)
337 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
340 stringHandler : function(tok)
342 //print("STRING HANDLER");
343 // callback when outputing compressed file,
344 if (!this.translate) {
345 // print("TURNED OFF");
351 var sval = tok.data.substring(1,tok.data.length-1);
352 // blank with tabs or spaces..
353 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
354 // return tok.outData;
360 var ff = this.activeFile.split('/');
361 var ffn = ff[ff.length-1];
362 return '_T["' + this.md5(ffn + '-' + sval) + '"]';