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;
105 public string activeFile = "";
108 public string out = ""; // if no target is specified - then this will contain the result
110 public Packer(string target, string targetDebug = "")
112 this.target = target;
113 this.targetDebug = targetDebug;
117 public void loadSourceIndexes(Gee.ArrayList<string> indexes)
119 foreach(var f in indexes) {
120 this.loadSourceIndex(f);
124 public void loadFiles(Gee.ArrayList<string> fs)
126 foreach(var f in fs) {
127 this.files.add(f); //?? easier way?
134 if (this.files.size < 1) {
135 throw new PackerError.ArgumentError("No Files loaded before pack() called");
137 if (this.target.length > 0 ) {
138 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
140 if (this.targetDebug.length > 0 ) {
141 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
154 * load a dependancy list -f option
155 * @param {String} srcfile sourcefile to parse
159 public void loadSourceIndex(string srcfile)
162 FileUtils.get_contents(srcfile,out str);
164 var lines = str.split("\n");
165 for(var i =0; i < lines.length;i++) {
167 var f = lines[i].strip();
169 Regex.match_simple ("^/", f) ||
170 !Regex.match_simple ("[a-zA-Z]+", f)
172 continue; // blank comment or not starting with a-z
175 if (Regex.match_simple ("\\.js$", f)) {
181 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
182 // should we prefix? =- or should this be done elsewhere?
184 var add = f.replace(".", "/") + ".js";
185 if (this.files.contains(add)) {
188 this.files.add( add );
194 private void packAll() // do the packing (run from constructor)
197 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
198 //File.write(this.transfile, "");
199 if (this.target.length > 0) {
200 this.targetStream.write("".data);
203 if (this.targetDebugStream != null) {
204 this.targetDebugStream.write("".data);
208 foreach(var file in this.files) {
210 print("reading %s\n",file );
212 if (FileUtils.test (file, FileTest.EXISTS) && ! FileUtils.test (file, FileTest.IS_DIR)) {
213 print("SKIP (is not a file) %s\n ", file);
217 var loaded_string = false;
218 string file_contents;
221 if (this.targetDebugStream !=null) {
223 FileUtils.get_contents(file,out file_contents);
224 this.targetDebugStream.write(file_contents.data);
225 loaded_string = false;
227 // it's a good idea to check with 0 compression to see if the code can parse!!
230 //File.append(dout, str +"\n");
234 var minfile = this.tmpDir + "/" + file.replace("/", ".");
237 // let's see if we have a min file already?
238 // this might happen if tmpDir is set ..
241 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
243 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
244 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
247 ot.set_time_val(otv);
249 mt.set_time_val(mtv);
250 //print("compare : " + mt + "=>" + ot);
251 if (mt.compare(ot) >= 0) {
252 continue; // file is newer or the same time..
258 print("COMPRESSING ");
259 //var codeComp = pack(str, 10, 0, 0);
260 if (FileUtils.test (minfile, FileTest.EXISTS)) {
261 FileUtils.remove(minfile);
263 if (!loaded_string) {
264 FileUtils.get_contents(file,out file_contents);
267 var str = this.packFile(file_contents, file, minfile);
274 // if we are translating, write the translations strings at the top
277 if (this.translateJSON) {
280 print("MERGING LANGUAGE");
281 var out = "if (typeof(_T) == 'undefined') { _T={};}\n";
283 File.write(this.target, out);
288 File.write(this.translateJSON, "");
289 for(var i=0; i < this.files.length; i++) {
290 var file = this.files[i];
291 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
292 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
293 if (File.exists(transmd5)) {
294 var str = File.read(transmd5);
297 File.append(this.target, str + "\n");
299 this.out += str + "\n";
304 File.remove(transmd5);
307 if (File.exists(transfile)) {
308 var str = File.read(transfile);
310 File.append(this.translateJSON, str);
313 File.remove(transfile);
321 print("MERGING SOURCE");
323 for(var i=0; i < this.files.size; i++) {
324 var file = this.files[i];
325 var minfile = this.tmpDir + "/" + file.replace("/", ".");
328 if ( FileUtils.test(minfile, FileTest.EXISTS)) {
332 FileUtils.get_contents(minfile, out str);
333 print("using MIN FILE %s\n", minfile);
334 if (str.length > 0) {
335 if (this.targetStream != null) {
336 this.targetStream.write(("//" + file + "\n").data);
337 this.targetStream.write((str + "\n").data);
340 this.out += "//" + file + "\n";
341 this.out += str + "\n";
346 FileUtils.remove(minfile);
350 print("Output file: " + this.target);
351 if (this.targetDebug.length > 0) {
352 print("Output debug file: " + this.targetDebug);
360 * Core packing routine for a file
362 * @param str - str source text..
363 * @param fn - filename (for reference?)
364 * @param minfile - min file location...
368 private string packFile (string str,string fn, string minfile)
371 var tr = new TokenReader();
374 tr.keepComments = true;
376 tr.collapseWhite = false;
379 // we can load translation map here...
381 var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
385 this.activeFile = fn;
387 // and replace if we are generating a different language..
390 //var ts = new TokenStream(toks);
391 //print(JSON.stringify(toks, null,4 )); Seed.quit();
392 var ts = new Collapse(toks);
393 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
395 var sp = new ScopeParser(ts);
398 sp.buildSymbolTree();
400 sp.mungeSymboltree();
402 print(sp.warnings.join("\n"));
405 var outf = CompressWhite(new TokenStream(toks), this, this.keepWhite); // do not kill whitespace..
410 if (outf.length > 0) {
411 FileUtils.set_contents(minfile, outf);
422 public string md5(string str)
425 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
429 //stringHandler : function(tok) -- not used...