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("".data);
198 if (this.targetDebugStream != null) {
199 this.targetDebugStream.write("".data);
203 foreach(var file in this.files) {
205 print("reading %s\n",file );
207 if (FileUtils.test (file, FileTest.EXISTS) && ! FileUtils.test (file, FileTest.IS_DIR)) {
208 print("SKIP (is not a file) %s\n ", file);
212 var loaded_string = false;
213 string file_contents;
216 if (this.targetDebugStream !=null) {
218 FileUtils.get_contents(file,out file_contents);
219 this.targetDebugStream.write(file_contents.data);
220 loaded_string = false;
222 // it's a good idea to check with 0 compression to see if the code can parse!!
225 //File.append(dout, str +"\n");
229 var minfile = this.tmpDir + "/" + file.replace("/", ".");
232 // let's see if we have a min file already?
233 // this might happen if tmpDir is set ..
236 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
238 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
239 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
242 ot.set_time_val(otv);
244 mt.set_time_val(mtv);
245 //print("compare : " + mt + "=>" + ot);
246 if (mt.compare(ot) >= 0) {
247 continue; // file is newer or the same time..
253 print("COMPRESSING ");
254 //var codeComp = pack(str, 10, 0, 0);
255 if (FileUtils.test (minfile, FileTest.EXISTS)) {
256 FileUtils.remove(minfile);
258 if (!loaded_string) {
259 FileUtils.get_contents(file,out file_contents);
262 var str = this.packFile(file_contents, file, minfile);
269 // if we are translating, write the translations strings at the top
272 if (this.translateJSON) {
275 print("MERGING LANGUAGE");
276 var out = "if (typeof(_T) == 'undefined') { _T={};}\n";
278 File.write(this.target, out);
283 File.write(this.translateJSON, "");
284 for(var i=0; i < this.files.length; i++) {
285 var file = this.files[i];
286 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
287 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
288 if (File.exists(transmd5)) {
289 var str = File.read(transmd5);
292 File.append(this.target, str + "\n");
294 this.out += str + "\n";
299 File.remove(transmd5);
302 if (File.exists(transfile)) {
303 var str = File.read(transfile);
305 File.append(this.translateJSON, str);
308 File.remove(transfile);
316 print("MERGING SOURCE");
318 for(var i=0; i < this.files.size; i++) {
319 var file = this.files[i];
320 var minfile = this.tmpDir + '/' + file.replace('/', '.');
323 if (!File.exists(minfile)) {
326 var str = File.read(minfile);
327 print("using MIN FILE "+ minfile);
329 if (this.targetStream != null) {
330 this.targetStream.write("//" + file + "\n");
331 this.targetStream.write(str + "\n");
334 this.out += "//" + file + "\n";
335 this.out += str + "\n";
340 FileUtils.remove(minfile);
344 print("Output file: " + this.target);
345 if (this.debugTarget) print("Output debug file: " + this.debugTarget);
352 * Core packing routine for a file
354 * @param str - str source text..
355 * @param fn - filename (for reference?)
356 * @param minfile - min file location...
360 private string packFile (string str,string fn, string minfile)
363 var tr = new TokenReader();
366 tr.keepComments = true;
368 tr.collapseWhite = false;
371 this.timerPrint("START" + fn);
373 // we can load translation map here...
375 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
379 this.activeFile = fn;
381 // and replace if we are generating a different language..
384 //var ts = new TokenStream(toks);
385 //print(JSON.stringify(toks, null,4 )); Seed.quit();
386 var ts = new Collapse(toks);
387 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
389 var sp = new ScopeParser(ts);
392 sp.buildSymbolTree();
394 sp.mungeSymboltree();
396 print(sp.warnings.join("\n"));
399 var outf = CompressWhite(new TokenStream(toks), this, this.keepWhite); // do not kill whitespace..
404 if (out.length > 0) {
405 FileUtils.put_contents(minfile, outf);
416 public string md5(string str)
419 return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, str);
423 //stringHandler : function(tok) -- not used...