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;
101 public bool debug = false;
103 public string activeFile = "";
106 public string outstr = ""; // if no target is specified - then this will contain the result
108 public Packer(string target, string targetDebug = "")
110 this.target = target;
111 this.targetDebug = targetDebug;
115 public void loadSourceIndexes(Gee.ArrayList<string> indexes)
117 foreach(var f in indexes) {
118 this.loadSourceIndex(f);
122 public void loadFiles(string[] fs)
124 foreach(var f in fs) {
125 this.files.add(f); //?? easier way?
132 if (this.files.size < 1) {
133 throw new PackerError.ArgumentError("No Files loaded before pack() called");
135 if (this.target.length > 0 ) {
136 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
138 if (this.targetDebug.length > 0 ) {
139 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
152 * load a dependancy list -f option
153 * @param {String} srcfile sourcefile to parse
157 public void loadSourceIndex(string srcfile)
160 FileUtils.get_contents(srcfile,out str);
162 var lines = str.split("\n");
163 for(var i =0; i < lines.length;i++) {
165 var f = lines[i].strip();
167 Regex.match_simple ("^/", f) ||
168 !Regex.match_simple ("[a-zA-Z]+", f)
170 continue; // blank comment or not starting with a-z
173 if (Regex.match_simple ("\\.js$", f)) {
179 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
180 // should we prefix? =- or should this be done elsewhere?
182 var add = f.replace(".", "/") + ".js";
183 if (this.files.contains(add)) {
186 this.files.add( add );
192 private void packAll() // do the packing (run from constructor)
195 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
196 //File.write(this.transfile, "");
197 if (this.target.length > 0) {
198 this.targetStream.write("".data);
201 if (this.targetDebugStream != null) {
202 this.targetDebugStream.write("".data);
206 foreach(var file in this.files) {
208 print("reading %s\n",file );
210 if (FileUtils.test (file, FileTest.EXISTS) && ! FileUtils.test (file, FileTest.IS_DIR)) {
211 print("SKIP (is not a file) %s\n ", file);
215 var loaded_string = false;
216 string file_contents = "";
219 if (this.targetDebugStream !=null) {
221 FileUtils.get_contents(file,out file_contents);
222 this.targetDebugStream.write(file_contents.data);
223 loaded_string = false;
225 // it's a good idea to check with 0 compression to see if the code can parse!!
228 //File.append(dout, str +"\n");
232 var minfile = this.tmpDir + "/" + file.replace("/", ".");
235 // let's see if we have a min file already?
236 // this might happen if tmpDir is set ..
239 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
241 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
242 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
245 ot.set_time_val(otv);
247 mt.set_time_val(mtv);
248 //print("compare : " + mt + "=>" + ot);
249 if (mt.compare(ot) >= 0) {
250 continue; // file is newer or the same time..
256 print("COMPRESSING ");
257 //var codeComp = pack(str, 10, 0, 0);
258 if (FileUtils.test (minfile, FileTest.EXISTS)) {
259 FileUtils.remove(minfile);
261 if (!loaded_string) {
262 FileUtils.get_contents(file,out file_contents);
265 this.packFile(file_contents, file, minfile);
272 // if we are translating, write the translations strings at the top
275 if (this.translateJSON) {
278 print("MERGING LANGUAGE");
279 var out = "if (typeof(_T) == 'undefined') { _T={};}\n";
281 File.write(this.target, out);
286 File.write(this.translateJSON, "");
287 for(var i=0; i < this.files.length; i++) {
288 var file = this.files[i];
289 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
290 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
291 if (File.exists(transmd5)) {
292 var str = File.read(transmd5);
295 File.append(this.target, str + "\n");
297 this.out += str + "\n";
302 File.remove(transmd5);
305 if (File.exists(transfile)) {
306 var str = File.read(transfile);
308 File.append(this.translateJSON, str);
311 File.remove(transfile);
319 print("MERGING SOURCE");
321 for(var i=0; i < this.files.size; i++) {
322 var file = this.files[i];
323 var minfile = this.tmpDir + "/" + file.replace("/", ".");
326 if ( FileUtils.test(minfile, FileTest.EXISTS)) {
330 FileUtils.get_contents(minfile, out str);
331 print("using MIN FILE %s\n", minfile);
332 if (str.length > 0) {
333 if (this.targetStream != null) {
334 this.targetStream.write(("//" + file + "\n").data);
335 this.targetStream.write((str + "\n").data);
338 this.outstr += "//" + file + "\n";
339 this.outstr += str + "\n";
344 FileUtils.remove(minfile);
348 print("Output file: " + this.target);
349 if (this.targetDebug.length > 0) {
350 print("Output debug file: " + this.targetDebug);
358 * Core packing routine for a file
360 * @param str - str source text..
361 * @param fn - filename (for reference?)
362 * @param minfile - min file location...
366 private string packFile (string str,string fn, string minfile)
369 var tr = new TokenReader();
372 tr.keepComments = true;
374 tr.collapseWhite = false;
377 // we can load translation map here...
379 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
383 this.activeFile = fn;
385 // and replace if we are generating a different language..
388 //var ts = new TokenStream(toks);
389 //print(JSON.stringify(toks, null,4 )); Seed.quit();
390 var ts = new Collapse(toks.tokens);
391 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
393 var sp = new ScopeParser(ts);
396 sp.buildSymbolTree();
398 sp.mungeSymboltree();
400 //print(sp.warnings.join("\n"));
403 var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
408 if (outf.length > 0) {
409 FileUtils.set_contents(minfile, outf);
420 public string md5(string str)
423 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
427 //stringHandler : function(tok) -- not used...