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;
17 Packer = imports['JSDOC/Packer.js'].Packer;
19 [ "/location/of/file1.js", "/location/of/file2.js", ... ],
23 "/location/of/temp_batch_dir",
24 "/location/of/output-compacted-file.js",
25 "/location/of/output-debug-merged-file.js"
30 * Notes for improving compacting:
31 * if you add a jsdoc comment
38 * directly before an eval statement, it will compress all the code around the eval,
39 * and not rename the variables 'avarname'
41 * Dont try running this on a merged uncompressed large file - it's horrifically slow.
42 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
46 * @param {Array} files List of Files - MUST BE WITH ABSOLUTE PATH eg. [ '/usr/src/xyz/abc.js', .... ]
47 * @param {String} source_path top level directory of source (used to work out the relative names for the minimized temp files)
49 Packer = function(files, spath)
52 this.spath = spath; // source path
53 this.aliasList = { }; // list of maps Roo.asdfasdfasf => Roo.A1
54 this.timer = new Date() * 1;
55 this.translate = true;
61 // set to false to stop translation happening..
66 * @param {String} batch_path location of batched temporary min files.
67 * @param {String} compressed_file eg. roo-all.js
68 * @param {String} debug_file eg. roo-debug.js
72 packFiles : function(bpath, allfile, debugfile) {
74 var spath = this.spath;
75 var files = this.files;
77 // old var names - fixme..
79 //File.write(dout, "");
81 var outpath = allfile;
83 var transfile = bpath + '/_translation_.js';
84 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
85 //File.write(this.transfile, "");
87 File.write(allfile, "");
88 for(var i=0; i < files.length; i++) {
90 print("reading " +files[i] );
91 if (!File.exists(files[i])) {
92 print("SKIP (does not exist) " + files[i]);
97 File.append(dout, File.read(files[i]));
98 // it's a good idea to check with 0 compression to see if the code can parse!!
101 //File.append(dout, str +"\n");
105 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
106 var transfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
107 // let's see if we have a min file already?
108 if (true && File.exists(minfile)) {
109 var mt = File.mtime(minfile);
110 var ot = File.mtime(files[i]);
111 print("compare : " + mt + "=>" + ot);
115 // then the min'files time is > than original..
116 var str = File.read(minfile);
117 print("using MIN FILE "+ minfile);
119 File.append(outpath, str + "\n");
128 print("COMPRESSING ");
129 //var codeComp = pack(str, 10, 0, 0);
130 var str = File.read(files[i]);
131 var str = this.pack(str, files[i], minfile);
133 File.write(minfile, str);
138 //var str = File.read(minfile);
139 //print("using MIN FILE "+ minfile);
140 //File.append(outpath, str + "\n");
141 //this.timerPrint("Wrote Files");
143 if (codeComp.length) {
146 File.append(outpath, codeComp+"\n");
147 File.write(minfile, codeComp);
151 // if (i > 10) return;
153 if (this.translate) {
156 print("MERGING LANGUAGE");
157 File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
159 var transfileAll = bpath + '/_translation_.js';
160 File.write(transfileAll, "");
161 for(var i=0; i < files.length; i++) {
162 var transfile= bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang.trans';
163 var transmd5 = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
164 if (File.exists(transmd5)) {
165 var str = File.read(transmd5);
167 File.append(outpath, str + "\n");
170 if (File.exists(transfile)) {
171 var str = File.read(transfile);
173 File.append(transfileAll, str);
180 print("MERGING SOURCE");
182 for(var i=0; i < files.length; i++) {
184 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
185 if (!File.exists(minfile)) {
188 var str = File.read(minfile);
189 print("using MIN FILE "+ minfile);
191 File.append(outpath, str + "\n");
196 //File.append(dout, "\n");// end the function
202 * Core packing routine for a file
204 * @param str - str source text..
205 * @param fn - filename (for reference?)
206 * @param minfile - min file location...
210 pack : function (str,fn,minfile)
213 var tr = new TokenReader();
214 this.timerPrint("START" + fn);
216 // we can load translation map here...
218 var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
220 // at this point we can write a language file...
221 if (this.translate) {
222 this.writeTranslateFile(fn, minfile, tr.translateMap);
225 this.activeFile = fn;
227 // and replace if we are generating a different language..
232 this.timerPrint("Tokenized");
234 var sp = new ScopeParser(new TokenStream(toks, str.length));
235 this.timerPrint("Converted to Parser");
237 sp.buildSymbolTree();
238 this.timerPrint("Built Sym tree");
239 sp.mungeSymboltree();
240 this.timerPrint("Munged Sym tree");
241 print(sp.warnings.join("\n"));
242 var out = JSDOC.CompressWhite(sp.ts, this);
243 this.timerPrint("Compressed");
248 timerPrint: function (str) {
249 var ntime = new Date() * 1;
250 var tdif = ntime -this.timer;
252 print('['+tdif+']'+str);
257 * Translation concept...
258 * -> replace text strings with _T....
259 * -> this file will need inserting at the start of the application....
260 * -> we need to generate 2 files,
261 * -> a reference used to do the translation, and the _T file..
265 writeTranslateFile : function(fn, minfile, map)
267 var transfile = minfile + '.lang.trans';
268 var transmd5 = minfile + '.lang';
271 if (File.exists(transfile)) {
272 File.remove(transfile);
274 if (File.exists(transmd5)) {
275 File.remove(transmd5);
277 for(v in map) { i++; break };
279 return; // no strings in file...
281 var ff = fn.split('/');
282 var ffn = ff[ff.length-1];
285 File.write(transfile, "\n" + ffn.toSource() + " : {");
289 File.write(transmd5, '');
291 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
293 // strings are raw... - as the where encoded to start with!!!
294 File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
296 File.append(transfile, "\n},"); // always one trailing..
300 md5 : function (string)
303 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
306 stringHandler : function(tok)
308 //print("STRING HANDLER");
309 // callback when outputing compressed file,
310 if (!this.translate) {
311 // print("TURNED OFF");
317 var sval = tok.data.substring(1,tok.data.length-1);
318 // blank with tabs or spaces..
319 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
320 // return tok.outData;
326 var ff = this.activeFile.split('/');
327 var ffn = ff[ff.length-1];
328 return '_T["' + this.md5(ffn + '-' + sval) + '"]';