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;
114 this.files = new Gee.ArrayList<string>();
118 public void loadSourceIndexes(Gee.ArrayList<string> indexes)
120 foreach(var f in indexes) {
121 this.loadSourceIndex(f);
125 public void loadFiles(string[] fs)
127 foreach(var f in fs) {
128 GLib.debug("add File: %s", f);
129 this.files.add(f); //?? easier way?
132 public void loadFile(string f)
134 GLib.debug("add File: %s", f);
136 GLib.debug("FILE LEN: %d", this.files.size);
141 if (this.files.size < 1) {
142 throw new PackerError.ArgumentError("No Files loaded before pack() called");
144 if (this.target.length > 0 ) {
145 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
147 if (this.targetDebug.length > 0 ) {
148 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
161 * load a dependancy list -f option
162 * @param {String} srcfile sourcefile to parse
166 public void loadSourceIndex(string srcfile)
169 FileUtils.get_contents(srcfile,out str);
171 var lines = str.split("\n");
172 for(var i =0; i < lines.length;i++) {
174 var f = lines[i].strip();
176 Regex.match_simple ("^/", f) ||
177 !Regex.match_simple ("[a-zA-Z]+", f)
179 continue; // blank comment or not starting with a-z
182 if (Regex.match_simple ("\\.js$", f)) {
188 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
189 // should we prefix? =- or should this be done elsewhere?
191 var add = f.replace(".", "/") + ".js";
192 if (this.files.contains(add)) {
195 this.files.add( add );
201 private void packAll() // do the packing (run from constructor)
204 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
205 //File.write(this.transfile, "");
206 if (this.target.length > 0) {
207 this.targetStream.write("".data);
210 if (this.targetDebugStream != null) {
211 this.targetDebugStream.write("".data);
215 foreach(var file in this.files) {
217 print("reading %s\n",file );
219 if (!FileUtils.test (file, FileTest.EXISTS) || FileUtils.test (file, FileTest.IS_DIR)) {
220 print("SKIP (is not a file) %s\n ", file);
224 var loaded_string = false;
225 string file_contents = "";
228 if (this.targetDebugStream !=null) {
230 FileUtils.get_contents(file,out file_contents);
231 this.targetDebugStream.write(file_contents.data);
232 loaded_string = false;
234 // it's a good idea to check with 0 compression to see if the code can parse!!
237 //File.append(dout, str +"\n");
241 var minfile = this.tmpDir + "/" + file.replace("/", ".");
244 // let's see if we have a min file already?
245 // this might happen if tmpDir is set ..
248 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
250 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
251 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
254 ot.set_time_val(otv);
256 mt.set_time_val(mtv);
257 //print("compare : " + mt + "=>" + ot);
258 if (mt.compare(ot) >= 0) {
259 continue; // file is newer or the same time..
265 print("COMPRESSING ");
266 //var codeComp = pack(str, 10, 0, 0);
267 if (FileUtils.test (minfile, FileTest.EXISTS)) {
268 FileUtils.remove(minfile);
270 if (!loaded_string) {
271 FileUtils.get_contents(file,out file_contents);
274 this.packFile(file_contents, file, minfile);
281 // if we are translating, write the translations strings at the top
284 if (this.translateJSON) {
287 print("MERGING LANGUAGE");
288 var out = "if (typeof(_T) == 'undefined') { _T={};}\n";
290 File.write(this.target, out);
295 File.write(this.translateJSON, "");
296 for(var i=0; i < this.files.length; i++) {
297 var file = this.files[i];
298 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
299 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
300 if (File.exists(transmd5)) {
301 var str = File.read(transmd5);
304 File.append(this.target, str + "\n");
306 this.out += str + "\n";
311 File.remove(transmd5);
314 if (File.exists(transfile)) {
315 var str = File.read(transfile);
317 File.append(this.translateJSON, str);
320 File.remove(transfile);
328 print("MERGING SOURCE");
330 for(var i=0; i < this.files.size; i++) {
331 var file = this.files[i];
332 var minfile = this.tmpDir + "/" + file.replace("/", ".");
335 if ( FileUtils.test(minfile, FileTest.EXISTS)) {
339 FileUtils.get_contents(minfile, out str);
340 print("using MIN FILE %s\n", minfile);
341 if (str.length > 0) {
342 if (this.targetStream != null) {
343 this.targetStream.write(("//" + file + "\n").data);
344 this.targetStream.write((str + "\n").data);
347 this.outstr += "//" + file + "\n";
348 this.outstr += str + "\n";
353 FileUtils.remove(minfile);
357 print("Output file: " + this.target);
358 if (this.targetDebug.length > 0) {
359 print("Output debug file: " + this.targetDebug);
367 * Core packing routine for a file
369 * @param str - str source text..
370 * @param fn - filename (for reference?)
371 * @param minfile - min file location...
375 private string packFile (string str,string fn, string minfile)
378 var tr = new TokenReader();
381 tr.keepComments = true;
383 tr.collapseWhite = false;
386 // we can load translation map here...
388 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
392 this.activeFile = fn;
394 // and replace if we are generating a different language..
397 //var ts = new TokenStream(toks);
398 //print(JSON.stringify(toks, null,4 )); Seed.quit();
399 var ts = new Collapse(toks.tokens);
400 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
402 var sp = new ScopeParser(ts);
405 sp.buildSymbolTree();
407 sp.mungeSymboltree();
409 //print(sp.warnings.join("\n"));
412 var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
417 if (outf.length > 0) {
418 FileUtils.set_contents(minfile, outf);
429 public string md5(string str)
432 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
436 //stringHandler : function(tok) -- not used...