1 // <script type="text/javascript">
2 XObject = imports.XObject.XObject;
3 File = imports.File.File;
5 TokenReader = imports['JSDOC/TokenReader.js']/TokenReader;
13 var x = new JSDOC.Packer(
14 [ "/location/of/file1.js", "/location/of/file2.js", ... ],
18 "/location/of/temp_batch_dir",
19 "/location/of/output-compacted-file.js",
20 "/location/of/output-debug-merged-file.js"
25 * Notes for improving compacting:
26 * if you add a jsdoc comment
33 * directly before an eval statement, it will compress all the code around the eval,
34 * and not rename the variables 'avarname'
36 * Dont try running this on a merged uncompressed large file - it's horrifically slow.
37 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
41 * @param {Array} files List of Files - MUST BE WITH ABSOLUTE PATH eg. [ '/usr/src/xyz/abc.js', .... ]
42 * @param {String} source_path top level directory of source (used to work out the relative names for the minimized temp files)
44 Packer = function(files, spath)
47 this.spath = spath; // source path
48 this.aliasList = { }; // list of maps Roo.asdfasdfasf => Roo.A1
49 this.timer = new Date() * 1;
50 this.translate = true;
56 // set to false to stop translation happening..
61 * @param {String} batch_path location of batched temporary min files.
62 * @param {String} compressed_file eg. roo-all.js
63 * @param {String} debug_file eg. roo-debug.js
67 packFiles : function(bpath, allfile, debugfile) {
69 var spath = this.spath;
70 var files = this.files;
72 // old var names - fixme..
74 //File.write(dout, "");
76 var outpath = allfile;
78 var transfile = bpath + '/_translation_.js';
79 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
80 //File.write(this.transfile, "");
82 File.write(allfile, "");
83 for(var i=0; i < files.length; i++) {
85 print("reading " +files[i] );
86 if (!File.exists(files[i])) {
87 print("SKIP (does not exist) " + files[i]);
92 File.append(dout, File.read(files[i]));
93 // it's a good idea to check with 0 compression to see if the code can parse!!
96 //File.append(dout, str +"\n");
100 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
101 var transfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
102 // let's see if we have a min file already?
103 if (true && File.exists(minfile)) {
104 var mt = File.mtime(minfile);
105 var ot = File.mtime(files[i]);
106 print("compare : " + mt + "=>" + ot);
110 // then the min'files time is > than original..
111 var str = File.read(minfile);
112 print("using MIN FILE "+ minfile);
114 File.append(outpath, str + "\n");
123 print("COMPRESSING ");
124 //var codeComp = pack(str, 10, 0, 0);
125 var str = File.read(files[i]);
126 var str = this.pack(str, files[i], minfile);
128 File.write(minfile, str);
133 //var str = File.read(minfile);
134 //print("using MIN FILE "+ minfile);
135 //File.append(outpath, str + "\n");
136 //this.timerPrint("Wrote Files");
138 if (codeComp.length) {
141 File.append(outpath, codeComp+"\n");
142 File.write(minfile, codeComp);
146 // if (i > 10) return;
148 if (this.translate) {
151 print("MERGING LANGUAGE");
152 File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
154 var transfileAll = bpath + '/_translation_.js';
155 File.write(transfileAll, "");
156 for(var i=0; i < files.length; i++) {
157 var transfile= bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang.trans';
158 var transmd5 = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
159 if (File.exists(transmd5)) {
160 var str = File.read(transmd5);
162 File.append(outpath, str + "\n");
165 if (File.exists(transfile)) {
166 var str = File.read(transfile);
168 File.append(transfileAll, str);
175 print("MERGING SOURCE");
177 for(var i=0; i < files.length; i++) {
179 var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
180 if (!File.exists(minfile)) {
183 var str = File.read(minfile);
184 print("using MIN FILE "+ minfile);
186 File.append(outpath, str + "\n");
191 //File.append(dout, "\n");// end the function
197 * Core packing routine for a file
199 * @param str - str source text..
200 * @param fn - filename (for reference?)
201 * @param minfile - min file location...
205 pack : function (str,fn,minfile)
208 var tr = new TokenReader();
209 this.timerPrint("START" + fn);
211 // we can load translation map here...
213 var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
215 // at this point we can write a language file...
216 if (this.translate) {
217 this.writeTranslateFile(fn, minfile, tr.translateMap);
220 this.activeFile = fn;
222 // and replace if we are generating a different language..
227 this.timerPrint("Tokenized");
229 var sp = new JSDOC.ScopeParser(new JSDOC.TokenStream(toks, str.length));
230 this.timerPrint("Converted to Parser");
232 sp.buildSymbolTree();
233 this.timerPrint("Built Sym tree");
234 sp.mungeSymboltree();
235 this.timerPrint("Munged Sym tree");
236 print(sp.warnings.join("\n"));
237 var out = JSDOC.CompressWhite(sp.ts, this);
238 this.timerPrint("Compressed");
243 timerPrint: function (str) {
244 var ntime = new Date() * 1;
245 var tdif = ntime -this.timer;
247 print('['+tdif+']'+str);
252 * Translation concept...
253 * -> replace text strings with _T....
254 * -> this file will need inserting at the start of the application....
255 * -> we need to generate 2 files,
256 * -> a reference used to do the translation, and the _T file..
260 writeTranslateFile : function(fn, minfile, map)
262 var transfile = minfile + '.lang.trans';
263 var transmd5 = minfile + '.lang';
266 if (File.exists(transfile)) {
267 File.remove(transfile);
269 if (File.exists(transmd5)) {
270 File.remove(transmd5);
272 for(v in map) { i++; break };
274 return; // no strings in file...
276 var ff = fn.split('/');
277 var ffn = ff[ff.length-1];
280 File.write(transfile, "\n" + ffn.toSource() + " : {");
284 File.write(transmd5, '');
286 File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
288 // strings are raw... - as the where encoded to start with!!!
289 File.append(transmd5, '_T[' + (ffn + '-' + v).md5().toSource() + ']="'+v+"\";\n");
291 File.append(transfile, "\n},"); // always one trailing..
295 stringHandler : function(tok)
297 //print("STRING HANDLER");
298 // callback when outputing compressed file,
299 if (!this.translate) {
300 // print("TURNED OFF");
306 var sval = tok.data.substring(1,tok.data.length-1);
307 // blank with tabs or spaces..
308 //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
309 // return tok.outData;
315 var ff = this.activeFile.split('/');
316 var ffn = ff[ff.length-1];
317 return '_T[' + (ffn + '-' + sval).md5().toSource() + ']';