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.
104 public bool debug = false;
106 public string activeFile = "";
109 public string outstr = ""; // if no target is specified - then this will contain the result
111 public Packer(string target, string targetDebug = "")
113 this.target = target;
114 this.targetDebug = targetDebug;
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 this.files.add(f); //?? easier way?
135 if (this.files.size < 1) {
136 throw new PackerError.ArgumentError("No Files loaded before pack() called");
138 if (this.target.length > 0 ) {
139 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
141 if (this.targetDebug.length > 0 ) {
142 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
155 * load a dependancy list -f option
156 * @param {String} srcfile sourcefile to parse
160 public void loadSourceIndex(string srcfile)
163 FileUtils.get_contents(srcfile,out str);
165 var lines = str.split("\n");
166 for(var i =0; i < lines.length;i++) {
168 var f = lines[i].strip();
170 Regex.match_simple ("^/", f) ||
171 !Regex.match_simple ("[a-zA-Z]+", f)
173 continue; // blank comment or not starting with a-z
176 if (Regex.match_simple ("\\.js$", f)) {
182 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
183 // should we prefix? =- or should this be done elsewhere?
185 var add = f.replace(".", "/") + ".js";
186 if (this.files.contains(add)) {
189 this.files.add( add );
195 private void packAll() // do the packing (run from constructor)
198 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
199 //File.write(this.transfile, "");
200 if (this.target.length > 0) {
201 this.targetStream.write("".data);
204 if (this.targetDebugStream != null) {
205 this.targetDebugStream.write("".data);
209 foreach(var file in this.files) {
211 print("reading %s\n",file );
213 if (FileUtils.test (file, FileTest.EXISTS) && ! FileUtils.test (file, FileTest.IS_DIR)) {
214 print("SKIP (is not a file) %s\n ", file);
218 var loaded_string = false;
219 string file_contents = "";
222 if (this.targetDebugStream !=null) {
224 FileUtils.get_contents(file,out file_contents);
225 this.targetDebugStream.write(file_contents.data);
226 loaded_string = false;
228 // it's a good idea to check with 0 compression to see if the code can parse!!
231 //File.append(dout, str +"\n");
235 var minfile = this.tmpDir + "/" + file.replace("/", ".");
238 // let's see if we have a min file already?
239 // this might happen if tmpDir is set ..
242 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
244 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
245 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
248 ot.set_time_val(otv);
250 mt.set_time_val(mtv);
251 //print("compare : " + mt + "=>" + ot);
252 if (mt.compare(ot) >= 0) {
253 continue; // file is newer or the same time..
259 print("COMPRESSING ");
260 //var codeComp = pack(str, 10, 0, 0);
261 if (FileUtils.test (minfile, FileTest.EXISTS)) {
262 FileUtils.remove(minfile);
264 if (!loaded_string) {
265 FileUtils.get_contents(file,out file_contents);
268 this.packFile(file_contents, file, minfile);
275 // if we are translating, write the translations strings at the top
278 if (this.translateJSON) {
281 print("MERGING LANGUAGE");
282 var out = "if (typeof(_T) == 'undefined') { _T={};}\n";
284 File.write(this.target, out);
289 File.write(this.translateJSON, "");
290 for(var i=0; i < this.files.length; i++) {
291 var file = this.files[i];
292 var transfile= this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang.trans';
293 var transmd5 = this.tmpDir + '/' +file.replace(/\//g, '.') +'.lang';
294 if (File.exists(transmd5)) {
295 var str = File.read(transmd5);
298 File.append(this.target, str + "\n");
300 this.out += str + "\n";
305 File.remove(transmd5);
308 if (File.exists(transfile)) {
309 var str = File.read(transfile);
311 File.append(this.translateJSON, str);
314 File.remove(transfile);
322 print("MERGING SOURCE");
324 for(var i=0; i < this.files.size; i++) {
325 var file = this.files[i];
326 var minfile = this.tmpDir + "/" + file.replace("/", ".");
329 if ( FileUtils.test(minfile, FileTest.EXISTS)) {
333 FileUtils.get_contents(minfile, out str);
334 print("using MIN FILE %s\n", minfile);
335 if (str.length > 0) {
336 if (this.targetStream != null) {
337 this.targetStream.write(("//" + file + "\n").data);
338 this.targetStream.write((str + "\n").data);
341 this.outstr += "//" + file + "\n";
342 this.outstr += str + "\n";
347 FileUtils.remove(minfile);
351 print("Output file: " + this.target);
352 if (this.targetDebug.length > 0) {
353 print("Output debug file: " + this.targetDebug);
361 * Core packing routine for a file
363 * @param str - str source text..
364 * @param fn - filename (for reference?)
365 * @param minfile - min file location...
369 private string packFile (string str,string fn, string minfile)
372 var tr = new TokenReader();
375 tr.keepComments = true;
377 tr.collapseWhite = false;
380 // we can load translation map here...
382 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
386 this.activeFile = fn;
388 // and replace if we are generating a different language..
391 //var ts = new TokenStream(toks);
392 //print(JSON.stringify(toks, null,4 )); Seed.quit();
393 var ts = new Collapse(toks.tokens);
394 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
396 var sp = new ScopeParser(ts);
399 sp.buildSymbolTree();
401 sp.mungeSymboltree();
403 //print(sp.warnings.join("\n"));
406 var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
411 if (outf.length > 0) {
412 FileUtils.set_contents(minfile, outf);
423 public string md5(string str)
426 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
430 //stringHandler : function(tok) -- not used...