JSDOC/Packer.js
[gnome.introspection-doc-generator] / JSDOC / Packer.js
1 // <script type="text/javascript">
2 XObject = imports.XObject.XObject;
3 File = imports.File.File;
4
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;
9 /**
10  * @namespace JSDOC
11  * @class  Packer
12  * Create a new packer
13  * 
14  * Usage:
15  * <code>
16  *
17 Packer = imports['JSDOC/Packer.js'].Packer;
18 var x = new  Packer(
19     [ "/location/of/file1.js", "/location/of/file2.js", ... ],
20     "/location/of"
21 );
22 x.packFiles(
23     "/location/of/temp_batch_dir", 
24     "/location/of/output-compacted-file.js",
25     "/location/of/output-debug-merged-file.js"
26 );
27     
28  *</code> 
29  *
30  * Notes for improving compacting:
31  *  if you add a jsdoc comment 
32  * <code>
33  * /**
34  *   eval:var:avarname
35  *   eval:var:bvarname
36  *   ....
37  * </code>
38  * directly before an eval statement, it will compress all the code around the eval, 
39  * and not rename the variables 'avarname'
40  * 
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
43  * 
44  * 
45  * 
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)
48  */
49 Packer = function(files, spath)
50 {
51     this.files = files;
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;
56 }
57 Packer.prototype = {
58     
59     bpath : '',
60     
61     // set to false to stop translation happening..
62     
63     /**
64      * Pack the files.
65      * 
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
69      * 
70      */
71     
72     packFiles : function(bpath, allfile, debugfile) {
73         var str;
74         var spath = this.spath;
75         var files = this.files;
76         this.bpath = bpath;
77         // old var names - fixme..
78         var dout = debugfile;
79         //File.write(dout, "");
80         
81         var outpath = allfile;
82       
83         var transfile = bpath + '/_translation_.js';
84         //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
85         //File.write(this.transfile, "");
86         File.write(dout, "");
87         File.write(allfile, "");
88         for(var i=0; i < files.length; i++)  {
89             
90             print("reading " +files[i] );
91             if (!File.exists(files[i])) {
92                 print("SKIP (does not exist) " + files[i]);
93                 continue;
94             }
95            
96             
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!!
99             
100             // debug file..
101             //File.append(dout, str +"\n"); 
102             
103        
104             
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);
112                 if (mt >= ot) {
113                     continue;
114                     /*
115                     // then the min'files time is > than original..
116                     var str = File.read(minfile);
117                     print("using MIN FILE  "+ minfile);
118                     if (str.length) {
119                         File.append(outpath, str + "\n");
120                     }
121                     
122                     continue;
123                     */
124                 }
125                 
126             }
127             
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);
132             if (str.length) {
133                 File.write(minfile, str);   
134             }
135             
136              
137             
138             //var str = File.read(minfile);
139             //print("using MIN FILE  "+ minfile);
140             //File.append(outpath, str + "\n");
141             //this.timerPrint("Wrote Files");
142             /*
143             if (codeComp.length) {
144                 //print(codeComp);
145                 
146                 File.append(outpath, codeComp+"\n");
147                 File.write(minfile, codeComp);
148             }
149             */
150             //print(codeComp);
151            // if (i > 10) return;
152         }  
153         if (this.translate) {
154             
155                
156             print("MERGING LANGUAGE");
157             File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
158             
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);
166                     if (str.length) {
167                         File.append(outpath, str + "\n");
168                     }
169                 }
170                 if (File.exists(transfile)) {
171                     var str = File.read(transfile);
172                     if (str.length) {
173                         File.append(transfileAll, str);
174                     }
175                 }
176                 
177                
178             }
179         }
180         print("MERGING SOURCE");
181         
182         for(var i=0; i < files.length; i++)  {
183          
184             var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
185             if (!File.exists(minfile)) {
186                 continue;
187             }
188             var str = File.read(minfile);
189             print("using MIN FILE  "+ minfile);
190             if (str.length) {
191                 File.append(outpath, str + "\n");
192             }
193         }
194         
195         
196         //File.append(dout, "\n");// end the function 
197         
198     
199     
200     },
201     /**
202      * Core packing routine  for a file
203      * 
204      * @param str - str source text..
205      * @param fn - filename (for reference?)
206      * @param minfile - min file location...
207      * 
208      */
209     
210     pack : function (str,fn,minfile)
211     {
212     
213         var tr = new  TokenReader();
214         this.timerPrint("START" + fn);
215         
216         // we can load translation map here...
217         
218         var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
219         
220         // at this point we can write a language file...
221         if (this.translate) {
222             this.writeTranslateFile(fn, minfile, tr.translateMap);
223         }
224         
225         this.activeFile = fn;
226         
227         // and replace if we are generating a different language..
228         
229         
230         
231         
232         this.timerPrint("Tokenized");
233         //return;//
234         var sp = new ScopeParser(new TokenStream(toks, str.length));
235         this.timerPrint("Converted to Parser");
236         sp.packer = this;
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");
244         return out;
245         
246     },
247     
248     timerPrint: function (str) {
249         var ntime = new Date() * 1;
250         var tdif =  ntime -this.timer;
251         this.timer = ntime;
252         print('['+tdif+']'+str);
253     },
254     
255     /**
256      * 
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..
262      * 
263      */
264     
265     writeTranslateFile : function(fn, minfile, map) 
266     {
267         var transfile = minfile + '.lang.trans';
268         var transmd5 = minfile + '.lang';
269         var i = 0;
270         var v = '';
271         if (File.exists(transfile)) {
272             File.remove(transfile);
273         }
274         if (File.exists(transmd5)) {
275             File.remove(transmd5);
276         }
277         for(v in map) { i++; break };
278         if (!i ) {
279             return; // no strings in file...
280         }
281         var ff = fn.split('/');
282         var ffn = ff[ff.length-1];
283          
284          
285         File.write(transfile, "\n" + ffn.toSource() + " : {");
286         var l = '';
287         var _tout = {}
288          
289         File.write(transmd5, '');
290         for(v in map) {
291             File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
292             l = ',';
293             // strings are raw... - as the where encoded to start with!!!
294             File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
295         }
296         File.append(transfile, "\n},"); // always one trailing..
297         
298          
299     },
300     md5 : function (string)
301     {
302         
303         return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
304         
305     }
306     stringHandler : function(tok)
307     {
308         //print("STRING HANDLER");
309        // callback when outputing compressed file, 
310         if (!this.translate) {
311          //   print("TURNED OFF");
312             return tok.outData;
313         }
314         if (tok.qc != '"') {
315             return tok.outData;
316         }
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;
321        // }
322         
323         
324         
325         
326         var ff = this.activeFile.split('/');
327         var ffn = ff[ff.length-1];
328         return '_T["' + this.md5(ffn + '-' + sval) + '"]';
329         
330         
331     }
332     
333     
334 });