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;
15 var x = new JSDOC.Packer(
16 [ "/location/of/file1.js", "/location/of/file2.js", ... ],
20 "/location/of/temp_batch_dir",
21 "/location/of/output-compacted-file.js",
22 "/location/of/output-debug-merged-file.js"
27 * Notes for improving compacting:
28 * if you add a jsdoc comment
35 * directly before an eval statement, it will compress all the code around the eval,
36 * and not rename the variables 'avarname'
38 * Dont try running this on a merged uncompressed large file - it's horrifically slow.
39 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
43 * @param {Array} files List of Files - MUST BE WITH ABSOLUTE PATH eg. [ '/usr/src/xyz/abc.js', .... ]
44 * @param {String} source_path top level directory of source (used to work out the relative names for the minimized temp files)
46 Packer = function(files, spath)
49 this.spath = spath; // source path
50 this.aliasList = { }; // list of maps Roo.asdfasdfasf => Roo.A1
51 this.timer = new Date() * 1;
52 this.translate = true;
58 // set to false to stop translation happening..
63 * @param {String} batch_path location of batched temporary min files.
64 * @param {String} compressed_file eg. roo-all.js
65 * @param {String} debug_file eg. roo-debug.js
69 packFiles : function(bpath, allfile, debugfile) {
71 var spath = this.spath;
72 var files = this.files;
74 // old var names - fixme..
76 //File.write(dout, "");
78 var outpath = allfile;
80 var transfile = bpath + '/_translation_.js';
81 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
82 //File.write(this.transfile, "");
84 File.write(allfile, "");
85 for(var i=0; i < files.length; i++) {
87 print("reading " +files[i] );
88 if (!File.exists(files[i])) {
89 print("SKIP (does not exist) " + files[i]);
94 File.append(dout, File.read(files[i]));
95 // it's a good idea to check with 0 compression to see if the code can parse!!
98 //File.append(dout, str +"\n");
102 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
103 var transfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
104 // let's see if we have a min file already?
105 if (true && File.exists(minfile)) {
106 var mt = File.mtime(minfile);
107 var ot = File.mtime(files[i]);
108 print("compare : " + mt + "=>" + ot);
112 // then the min'files time is > than original..
113 var str = File.read(minfile);
114 print("using MIN FILE "+ minfile);
116 File.append(outpath, str + "\n");
125 print("COMPRESSING ");
126 //var codeComp = pack(str, 10, 0, 0);
127 var str = File.read(files[i]);
128 var str = this.pack(str, files[i], minfile);
130 File.write(minfile, str);
135 //var str = File.read(minfile);
136 //print("using MIN FILE "+ minfile);
137 //File.append(outpath, str + "\n");
138 //this.timerPrint("Wrote Files");
140 if (codeComp.length) {
143 File.append(outpath, codeComp+"\n");
144 File.write(minfile, codeComp);
148 // if (i > 10) return;
150 if (this.translate) {
153 print("MERGING LANGUAGE");
154 File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
156 var transfileAll = bpath + '/_translation_.js';
157 File.write(transfileAll, "");
158 for(var i=0; i < files.length; i++) {
159 var transfile= bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang.trans';
160 var transmd5 = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
161 if (File.exists(transmd5)) {
162 var str = File.read(transmd5);
164 File.append(outpath, str + "\n");
167 if (File.exists(transfile)) {
168 var str = File.read(transfile);
170 File.append(transfileAll, str);
177 print("MERGING SOURCE");
179 for(var i=0; i < files.length; i++) {
181 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
182 if (!File.exists(minfile)) {
185 var str = File.read(minfile);
186 print("using MIN FILE "+ minfile);
188 File.append(outpath, str + "\n");
193 //File.append(dout, "\n");// end the function
199 * Core packing routine for a file
201 * @param str - str source text..
202 * @param fn - filename (for reference?)
203 * @param minfile - min file location...
207 pack : function (str,fn,minfile)
210 var tr = new TokenReader();
211 this.timerPrint("START" + fn);
213 // we can load translation map here...
215 var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
217 // at this point we can write a language file...
218 if (this.translate) {
219 this.writeTranslateFile(fn, minfile, tr.translateMap);
222 this.activeFile = fn;
224 // and replace if we are generating a different language..
229 this.timerPrint("Tokenized");
231 var sp = new ScopeParser(new TokenStream(toks, str.length));
232 this.timerPrint("Converted to Parser");
234 sp.buildSymbolTree();
235 this.timerPrint("Built Sym tree");
236 sp.mungeSymboltree();
237 this.timerPrint("Munged Sym tree");
238 print(sp.warnings.join("\n"));
239 var out = JSDOC.CompressWhite(sp.ts, this);
240 this.timerPrint("Compressed");
245 timerPrint: function (str) {
246 var ntime = new Date() * 1;
247 var tdif = ntime -this.timer;
249 print('['+tdif+']'+str);
254 * Translation concept...
255 * -> replace text strings with _T....
256 * -> this file will need inserting at the start of the application....
257 * -> we need to generate 2 files,
258 * -> a reference used to do the translation, and the _T file..
262 writeTranslateFile : function(fn, minfile, map)
264 var transfile = minfile + '.lang.trans';
265 var transmd5 = minfile + '.lang';
268 if (File.exists(transfile)) {
269 File.remove(transfile);
271 if (File.exists(transmd5)) {
272 File.remove(transmd5);
274 for(v in map) { i++; break };
276 return; // no strings in file...
278 var ff = fn.split('/');
279 var ffn = ff[ff.length-1];
282 File.write(transfile, "\n" + ffn.toSource() + " : {");
286 File.write(transmd5, '');
288 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
290 // strings are raw... - as the where encoded to start with!!!
291 File.append(transmd5, '_T[' + this.md5(ffn + '-' + v) + ']="'+v+"\";\n");
293 File.append(transfile, "\n},"); // always one trailing..
297 md5 : function (string)
300 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
303 stringHandler : function(tok)
305 //print("STRING HANDLER");
306 // callback when outputing compressed file,
307 if (!this.translate) {
308 // print("TURNED OFF");
314 var sval = tok.data.substring(1,tok.data.length-1);
315 // blank with tabs or spaces..
316 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
317 // return tok.outData;
323 var ff = this.activeFile.split('/');
324 var ffn = ff[ff.length-1];
325 return '_T[' + this.md5(ffn + '-' + sval) + ']';