14 var x = new JSON.Packer(target, debugTarget);
16 x.files = an array of files
17 x.srcfiles = array of files (that list other files...) << not supported?
18 x.target = "output.pathname.js"
19 x.debugTarget = "output.pathname.debug.js"
22 x.debugTranslateTarget : "/tmp/output.translate.js" << this used to be the single vs double quotes.. we may not use it in future..
23 x.translateJSON: "/tmp/translate.json",
25 x.packAll(); // writes files etc..
29 * Notes for improving compacting:
30 * if you add a jsdoc comment
37 * directly before an eval statement, it will compress all the code around the eval,
38 * and not rename the variables 'avarname'
40 * Dont try running this on a merged uncompressed large file - it's used to be horrifically slow. not sure about now..
41 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
45 * Notes for translation
46 * - translation relies on you using double quotes for strings if they need translating
47 * - single quoted strings are ignored.
49 * Generation of indexFiles
50 * - translateIndex = the indexfile
59 public errordomain PackerError {
63 public class Packer : Object
66 * @cfg {String} target to write files to - must be full path.
69 GLib.FileOutputStream targetStream = null;
71 * @cfg {String} debugTarget target to write files debug version to (uncompacted)- must be full path.
76 GLib.FileOutputStream targetDebugStream = null;
78 * @cfg {String} tmpDir (optional) where to put the temporary files.
79 * if you set this, then files will not be cleaned up
81 public string tmpDir = "/tmp"; // FIXME??? in ctor?
86 * @cfg {Boolean} cleanup (optional) clean up temp files after done -
87 * Defaults to false if you set tmpDir, otherwise true.
89 public bool cleanup = true;
93 * @cfg {Boolean} keepWhite (optional) do not remove white space in output.
94 * usefull for debugging compressed files.
97 public bool keepWhite = true;
100 // list of files to compile...
101 Gee.ArrayList<string> files;
103 public string out = ""; // if no target is specified - then this will contain the result
105 public Packer(string target, string targetDebug = "")
107 this.target = target;
108 this.targetDebug = targetDebug;
112 public void loadSourceIndexes(Gee.ArrayList<string> indexes)
114 foreach(var f in indexes) {
115 this.loadSourceIndex(f);
119 public void loadFiles(Gee.ArrayList<string> fs)
121 foreach(var f in fs) {
122 this.files.add(f); //?? easier way?
129 if (this.files.size < 1) {
130 throw new PackerError.ArgumentError("No Files loaded before pack() called");
132 if (this.target.length > 0 ) {
133 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
135 if (this.targetDebug.length > 0 ) {
136 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
149 * load a dependancy list -f option
150 * @param {String} srcfile sourcefile to parse
154 public void loadSourceIndex(string srcfile)
157 FileUtils.get_contents(srcfile,out str);
159 var lines = str.split("\n");
160 for(var i =0; i < lines.length;i++) {
162 var f = lines[i].strip();
164 Regex.match_simple ("^/", f) ||
165 !Regex.match_simple ("[a-zA-Z]+", f)
167 continue; // blank comment or not starting with a-z
170 if (Regex.match_simple ("\\.js$", f)) {
176 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
177 // should we prefix? =- or should this be done elsewhere?
179 var add = f.replace(".", "/") + ".js";
180 if (_this.files.contains(add)) {
183 _this.files.add( add );
189 private void packAll() // do the packing (run from constructor)
192 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
193 //File.write(this.transfile, "");
194 if (this.target.length > 0) {
195 this.targetStream.write("");
198 if (this.debugTarget > 0) {
199 this.targetDebugStream.write("");
201 foreach(var file in this.files) {
203 print("reading %s\n",file );
205 if (FileUtils.test (file, FileTest.EXISTS) && ! FileUtils.test (file, FileTest.IS_DIR)) {
206 print("SKIP (is not a file) %s\n ", file);
210 var loaded_string = false;
211 string file_contents;
214 if (this.targetDebugStream !=null) {
216 FileUtils.get_contents(file,out file_contents);
217 this.targetDebugStream.write(file_contents);
218 loaded_string = false;
220 // it's a good idea to check with 0 compression to see if the code can parse!!
223 //File.append(dout, str +"\n");
227 var minfile = this.tmpDir + '/' + file.replace("/", '.');
230 // let's see if we have a min file already?
231 // this might happen if tmpDir is set ..
234 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
236 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
237 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
240 ot.set_time_val(otv);
242 mt.set_time_val(mtv);
243 //print("compare : " + mt + "=>" + ot);
244 if (mt.compare(ot) >= 0) {
245 continue; // file is newer or the same time..
251 print("COMPRESSING ");
252 //var codeComp = pack(str, 10, 0, 0);
253 if (FileUtils.test (minfile, FileTest.EXISTS)) {
254 FileUtils.remove(minfile);
256 if (!loaded_string) {
257 FileUtils.get_contents(file,out file_contents);
260 var str = this.packFile(file_contents, file, minfile);
267 // if we are translating, write the translations strings at the top
270 if (this.translateJSON) {
273 print("MERGING LANGUAGE");
274 var out = "if (typeof(_T) == 'undefined') { _T={};}\n";
276 File.write(this.target, out);
281 File.write(this.translateJSON, "");
282 for(var i=0; i < this.files.length; i++) {
283 var file = this.files[i];
284 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
285 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
286 if (File.exists(transmd5)) {
287 var str = File.read(transmd5);
290 File.append(this.target, str + "\n");
292 this.out += str + "\n";
297 File.remove(transmd5);
300 if (File.exists(transfile)) {
301 var str = File.read(transfile);
303 File.append(this.translateJSON, str);
306 File.remove(transfile);
314 print("MERGING SOURCE");
316 for(var i=0; i < this.files.length; i++) {
317 var file = this.files[i];
318 var minfile = this.tmpDir + '/' + file.replace('/', '.');
321 if (!File.exists(minfile)) {
324 var str = File.read(minfile);
325 print("using MIN FILE "+ minfile);
327 if (this.targetStream != null) {
328 this.targetStream.write("//" + file + "\n");
329 this.targetStream.write(str + "\n");
332 this.out += "//" + file + "\n";
333 this.out += str + "\n";
338 FileUtils.remove(minfile);
342 print("Output file: " + this.target);
343 if (this.debugTarget) print("Output debug file: " + this.debugTarget);
350 * Core packing routine for a file
352 * @param str - str source text..
353 * @param fn - filename (for reference?)
354 * @param minfile - min file location...
358 private string packFile (string str,string fn, string minfile)
361 var tr = new TokenReader();
364 tr.keepComments = true;
366 tr.collapseWhite = false;
369 this.timerPrint("START" + fn);
371 // we can load translation map here...
373 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
377 this.activeFile = fn;
379 // and replace if we are generating a different language..
382 //var ts = new TokenStream(toks);
383 //print(JSON.stringify(toks, null,4 )); Seed.quit();
384 var ts = new Collapse(toks);
385 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
387 var sp = new ScopeParser(ts);
390 sp.buildSymbolTree();
392 sp.mungeSymboltree();
394 print(sp.warnings.join("\n"));
397 var outf = CompressWhite(new TokenStream(toks), this, this.keepWhite); // do not kill whitespace..
402 if (out.length > 0) {
403 FileUtils.put_contents(minfile, outf);
414 public string md5(string str)
417 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, str);
421 //stringHandler : function(tok) -- not used...