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;
19 Packer = imports['JSDOC/Packer.js'].Packer;
21 [ "/location/of/file1.js", "/location/of/file2.js", ... ],
25 "/location/of/temp_batch_dir",
26 "/location/of/output-compacted-file.js",
27 "/location/of/output-debug-merged-file.js"
32 * Notes for improving compacting:
33 * if you add a jsdoc comment
40 * directly before an eval statement, it will compress all the code around the eval,
41 * and not rename the variables 'avarname'
43 * Dont try running this on a merged uncompressed large file - it's horrifically slow.
44 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
48 * @param {Array} files List of Files - MUST BE WITH ABSOLUTE PATH eg. [ '/usr/src/xyz/abc.js', .... ]
49 * @param {String} source_path top level directory of source (used to work out the relative names for the minimized temp files)
51 Packer = function(files, spath)
54 this.spath = spath; // source path
55 this.aliasList = { }; // list of maps Roo.asdfasdfasf => Roo.A1
56 this.timer = new Date() * 1;
57 this.translate = true;
63 // set to false to stop translation happening..
68 * @param {String} batch_path location of batched temporary min files.
69 * @param {String} compressed_file eg. roo-all.js
70 * @param {String} debug_file eg. roo-debug.js
74 packFiles : function(bpath, allfile, debugfile) {
76 var spath = this.spath;
77 var files = this.files;
79 // old var names - fixme..
81 //File.write(dout, "");
83 var outpath = allfile;
85 var transfile = bpath + '/_translation_.js';
86 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
87 //File.write(this.transfile, "");
89 File.write(allfile, "");
90 for(var i=0; i < files.length; i++) {
92 print("reading " +files[i] );
93 if (!File.exists(files[i])) {
94 print("SKIP (does not exist) " + files[i]);
99 File.append(dout, File.read(files[i]));
100 // it's a good idea to check with 0 compression to see if the code can parse!!
103 //File.append(dout, str +"\n");
107 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
108 var transfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
109 // let's see if we have a min file already?
110 if (true && File.exists(minfile)) {
111 var mt = File.mtime(minfile);
112 var ot = File.mtime(files[i]);
113 print("compare : " + mt + "=>" + ot);
117 // then the min'files time is > than original..
118 var str = File.read(minfile);
119 print("using MIN FILE "+ minfile);
121 File.append(outpath, str + "\n");
130 print("COMPRESSING ");
131 //var codeComp = pack(str, 10, 0, 0);
132 var str = File.read(files[i]);
133 var str = this.pack(str, files[i], minfile);
135 File.write(minfile, str);
140 //var str = File.read(minfile);
141 //print("using MIN FILE "+ minfile);
142 //File.append(outpath, str + "\n");
143 //this.timerPrint("Wrote Files");
145 if (codeComp.length) {
148 File.append(outpath, codeComp+"\n");
149 File.write(minfile, codeComp);
153 // if (i > 10) return;
155 if (this.translate) {
158 print("MERGING LANGUAGE");
159 File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
161 var transfileAll = bpath + '/_translation_.js';
162 File.write(transfileAll, "");
163 for(var i=0; i < files.length; i++) {
164 var transfile= bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang.trans';
165 var transmd5 = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
166 if (File.exists(transmd5)) {
167 var str = File.read(transmd5);
169 File.append(outpath, str + "\n");
172 if (File.exists(transfile)) {
173 var str = File.read(transfile);
175 File.append(transfileAll, str);
182 print("MERGING SOURCE");
184 for(var i=0; i < files.length; i++) {
186 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
187 if (!File.exists(minfile)) {
190 var str = File.read(minfile);
191 print("using MIN FILE "+ minfile);
193 File.append(outpath, str + "\n");
198 //File.append(dout, "\n");// end the function
204 * Core packing routine for a file
206 * @param str - str source text..
207 * @param fn - filename (for reference?)
208 * @param minfile - min file location...
212 pack : function (str,fn,minfile)
215 var tr = new TokenReader();
216 this.timerPrint("START" + fn);
218 // we can load translation map here...
220 var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
222 // at this point we can write a language file...
223 if (this.translate) {
224 this.writeTranslateFile(fn, minfile, tr.translateMap);
227 this.activeFile = fn;
229 // and replace if we are generating a different language..
234 this.timerPrint("Tokenized");
236 var sp = new ScopeParser(new TokenStream(toks, str.length));
237 this.timerPrint("Converted to Parser");
239 sp.buildSymbolTree();
240 this.timerPrint("Built Sym tree");
241 sp.mungeSymboltree();
242 this.timerPrint("Munged Sym tree");
243 print(sp.warnings.join("\n"));
244 var out = JSDOC.CompressWhite(sp.ts, this);
245 this.timerPrint("Compressed");
250 timerPrint: function (str) {
251 var ntime = new Date() * 1;
252 var tdif = ntime -this.timer;
254 print('['+tdif+']'+str);
259 * Translation concept...
260 * -> replace text strings with _T....
261 * -> this file will need inserting at the start of the application....
262 * -> we need to generate 2 files,
263 * -> a reference used to do the translation, and the _T file..
267 writeTranslateFile : function(fn, minfile, map)
269 var transfile = minfile + '.lang.trans';
270 var transmd5 = minfile + '.lang';
273 if (File.exists(transfile)) {
274 File.remove(transfile);
276 if (File.exists(transmd5)) {
277 File.remove(transmd5);
279 for(v in map) { i++; break };
281 return; // no strings in file...
283 var ff = fn.split('/');
284 var ffn = ff[ff.length-1];
287 File.write(transfile, "\n" + ffn.toSource() + " : {");
291 File.write(transmd5, '');
293 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
295 // strings are raw... - as the where encoded to start with!!!
296 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
298 File.append(transfile, "\n},"); // always one trailing..
302 md5 : function (string)
305 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
308 stringHandler : function(tok)
310 //print("STRING HANDLER");
311 // callback when outputing compressed file,
312 if (!this.translate) {
313 // print("TURNED OFF");
319 var sval = tok.data.substring(1,tok.data.length-1);
320 // blank with tabs or spaces..
321 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
322 // return tok.outData;
328 var ff = this.activeFile.split('/');
329 var ffn = ff[ff.length-1];
330 return '_T["' + this.md5(ffn + '-' + sval) + '"]';