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"
23 x.pack(); // writes files etc..
27 * Notes for improving compacting:
28 * if you add a jsdoc comment
35 * directly before an eval statement, it will compress all the code around the eval,
36 * and not rename the variables 'avarname'
38 * Dont try running this on a merged uncompressed large file - it's used to be horrifically slow. not sure about now..
39 * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
43 * Notes for translation
44 * - translation relies on you using double quotes for strings if they need translating
45 * - single quoted strings are ignored.
47 * Generation of indexFiles
48 * - translateIndex = the indexfile
57 public errordomain PackerError {
61 public class Packer : Object
64 * @cfg {String} target to write files to - must be full path.
67 GLib.FileOutputStream targetStream = null;
69 * @cfg {String} debugTarget target to write files debug version to (uncompacted)- must be full path.
74 GLib.FileOutputStream targetDebugStream = null;
76 * @cfg {String} tmpDir (optional) where to put the temporary files.
77 * if you set this, then files will not be cleaned up
79 public string tmpDir = "/tmp"; // FIXME??? in ctor?
84 * @cfg {Boolean} cleanup (optional) clean up temp files after done -
85 * Defaults to false if you set tmpDir, otherwise true.
87 public bool cleanup = true;
91 * @cfg {Boolean} keepWhite (optional) do not remove white space in output.
92 * usefull for debugging compressed files.
95 public bool keepWhite = true;
98 // list of files to compile...
99 Gee.ArrayList<string> files;
102 * @cfg debug -- pretty obvious.
105 public string activeFile = "";
108 public string outstr = ""; // 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(string[] fs)
126 foreach(var f in fs) {
127 GLib.debug("add File: %s", f);
128 this.files.add(f); //?? easier way?
131 public void loadFile(string f)
133 GLib.debug("add File: %s", f);
135 GLib.debug("FILE LEN: %d", this.files.size);
140 if (this.files.size < 1) {
141 throw new PackerError.ArgumentError("No Files loaded before pack() called");
143 if (this.target.length > 0 ) {
144 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
146 if (this.targetDebug.length > 0 ) {
147 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
160 * load a dependancy list -f option
161 * @param {String} srcfile sourcefile to parse
165 public void loadSourceIndex(string srcfile)
168 FileUtils.get_contents(srcfile,out str);
170 var lines = str.split("\n");
171 for(var i =0; i < lines.length;i++) {
173 var f = lines[i].strip();
175 Regex.match_simple ("^/", f) ||
176 !Regex.match_simple ("[a-zA-Z]+", f)
178 continue; // blank comment or not starting with a-z
181 if (Regex.match_simple ("\\.js$", f)) {
187 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
188 // should we prefix? =- or should this be done elsewhere?
190 var add = f.replace(".", "/") + ".js";
191 if (this.files.contains(add)) {
194 this.files.add( add );
200 private void packAll() // do the packing (run from constructor)
203 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
204 //File.write(this.transfile, "");
205 if (this.target.length > 0) {
206 this.targetStream.write("".data);
209 if (this.targetDebugStream != null) {
210 this.targetDebugStream.write("".data);
214 foreach(var file in this.files) {
216 print("reading %s\n",file );
218 if (FileUtils.test (file, FileTest.EXISTS) && ! FileUtils.test (file, FileTest.IS_DIR)) {
219 print("SKIP (is not a file) %s\n ", file);
223 var loaded_string = false;
224 string file_contents = "";
227 if (this.targetDebugStream !=null) {
229 FileUtils.get_contents(file,out file_contents);
230 this.targetDebugStream.write(file_contents.data);
231 loaded_string = false;
233 // it's a good idea to check with 0 compression to see if the code can parse!!
236 //File.append(dout, str +"\n");
240 var minfile = this.tmpDir + "/" + file.replace("/", ".");
243 // let's see if we have a min file already?
244 // this might happen if tmpDir is set ..
247 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
249 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
250 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
253 ot.set_time_val(otv);
255 mt.set_time_val(mtv);
256 //print("compare : " + mt + "=>" + ot);
257 if (mt.compare(ot) >= 0) {
258 continue; // file is newer or the same time..
264 print("COMPRESSING ");
265 //var codeComp = pack(str, 10, 0, 0);
266 if (FileUtils.test (minfile, FileTest.EXISTS)) {
267 FileUtils.remove(minfile);
269 if (!loaded_string) {
270 FileUtils.get_contents(file,out file_contents);
273 this.packFile(file_contents, file, minfile);
280 // if we are translating, write the translations strings at the top
283 if (this.translateJSON) {
286 print("MERGING LANGUAGE");
287 var out = "if (typeof(_T) == 'undefined') { _T={};}\n";
289 File.write(this.target, out);
294 File.write(this.translateJSON, "");
295 for(var i=0; i < this.files.length; i++) {
296 var file = this.files[i];
297 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
298 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
299 if (File.exists(transmd5)) {
300 var str = File.read(transmd5);
303 File.append(this.target, str + "\n");
305 this.out += str + "\n";
310 File.remove(transmd5);
313 if (File.exists(transfile)) {
314 var str = File.read(transfile);
316 File.append(this.translateJSON, str);
319 File.remove(transfile);
327 print("MERGING SOURCE");
329 for(var i=0; i < this.files.size; i++) {
330 var file = this.files[i];
331 var minfile = this.tmpDir + "/" + file.replace("/", ".");
334 if ( FileUtils.test(minfile, FileTest.EXISTS)) {
338 FileUtils.get_contents(minfile, out str);
339 print("using MIN FILE %s\n", minfile);
340 if (str.length > 0) {
341 if (this.targetStream != null) {
342 this.targetStream.write(("//" + file + "\n").data);
343 this.targetStream.write((str + "\n").data);
346 this.outstr += "//" + file + "\n";
347 this.outstr += str + "\n";
352 FileUtils.remove(minfile);
356 print("Output file: " + this.target);
357 if (this.targetDebug.length > 0) {
358 print("Output debug file: " + this.targetDebug);
366 * Core packing routine for a file
368 * @param str - str source text..
369 * @param fn - filename (for reference?)
370 * @param minfile - min file location...
374 private string packFile (string str,string fn, string minfile)
377 var tr = new TokenReader();
380 tr.keepComments = true;
382 tr.collapseWhite = false;
385 // we can load translation map here...
387 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
391 this.activeFile = fn;
393 // and replace if we are generating a different language..
396 //var ts = new TokenStream(toks);
397 //print(JSON.stringify(toks, null,4 )); Seed.quit();
398 var ts = new Collapse(toks.tokens);
399 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
401 var sp = new ScopeParser(ts);
404 sp.buildSymbolTree();
406 sp.mungeSymboltree();
408 //print(sp.warnings.join("\n"));
411 var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
416 if (outf.length > 0) {
417 FileUtils.set_contents(minfile, outf);
428 public string md5(string str)
431 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
435 //stringHandler : function(tok) -- not used...