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>();
116 new Lang_Class(); ///initilizaze lang..
120 public void loadSourceIndexes(Gee.ArrayList<string> indexes)
122 foreach(var f in indexes) {
123 this.loadSourceIndex(f);
127 public void loadFiles(string[] fs)
129 foreach(var f in fs) {
130 GLib.debug("add File: %s", f);
131 this.files.add(f); //?? easier way?
134 public void loadFile(string f)
136 GLib.debug("add File: %s", f);
138 GLib.debug("FILE LEN: %d", this.files.size);
143 if (this.files.size < 1) {
144 throw new PackerError.ArgumentError("No Files loaded before pack() called");
146 if (this.target.length > 0 ) {
147 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
149 if (this.targetDebug.length > 0 ) {
150 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
163 * load a dependancy list -f option
164 * @param {String} srcfile sourcefile to parse
168 public void loadSourceIndex(string srcfile)
171 FileUtils.get_contents(srcfile,out str);
173 var lines = str.split("\n");
174 for(var i =0; i < lines.length;i++) {
176 var f = lines[i].strip();
178 Regex.match_simple ("^/", f) ||
179 !Regex.match_simple ("[a-zA-Z]+", f)
181 continue; // blank comment or not starting with a-z
184 if (Regex.match_simple ("\\.js$", f)) {
190 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
191 // should we prefix? =- or should this be done elsewhere?
193 var add = f.replace(".", "/") + ".js";
194 if (this.files.contains(add)) {
197 this.files.add( add );
203 private void packAll() // do the packing (run from constructor)
206 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
207 //File.write(this.transfile, "");
208 if (this.target.length > 0) {
209 this.targetStream.write("".data);
212 if (this.targetDebugStream != null) {
213 this.targetDebugStream.write("".data);
217 foreach(var file in this.files) {
219 print("reading %s\n",file );
221 if (!FileUtils.test (file, FileTest.EXISTS) || FileUtils.test (file, FileTest.IS_DIR)) {
222 print("SKIP (is not a file) %s\n ", file);
226 var loaded_string = false;
227 string file_contents = "";
230 if (this.targetDebugStream !=null) {
232 FileUtils.get_contents(file,out file_contents);
233 this.targetDebugStream.write(file_contents.data);
234 loaded_string = false;
236 // it's a good idea to check with 0 compression to see if the code can parse!!
239 //File.append(dout, str +"\n");
243 var minfile = this.tmpDir + "/" + file.replace("/", ".");
246 // let's see if we have a min file already?
247 // this might happen if tmpDir is set ..
250 if (true && FileUtils.test (minfile, FileTest.EXISTS)) {
252 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
253 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
256 ot.set_time_val(otv);
258 mt.set_time_val(mtv);
259 //print("compare : " + mt + "=>" + ot);
260 if (mt.compare(ot) >= 0) {
261 continue; // file is newer or the same time..
267 print("COMPRESSING ");
268 //var codeComp = pack(str, 10, 0, 0);
269 if (FileUtils.test (minfile, FileTest.EXISTS)) {
270 FileUtils.remove(minfile);
272 if (!loaded_string) {
273 FileUtils.get_contents(file,out file_contents);
276 this.packFile(file_contents, file, minfile);
282 print("MERGING SOURCE");
284 for(var i=0; i < this.files.size; i++) {
285 var file = this.files[i];
286 var minfile = this.tmpDir + "/" + file.replace("/", ".");
289 if ( FileUtils.test(minfile, FileTest.EXISTS)) {
293 FileUtils.get_contents(minfile, out str);
294 print("using MIN FILE %s\n", minfile);
295 if (str.length > 0) {
296 if (this.targetStream != null) {
297 this.targetStream.write(("//" + file + "\n").data);
298 this.targetStream.write((str + "\n").data);
301 this.outstr += "//" + file + "\n";
302 this.outstr += str + "\n";
307 FileUtils.remove(minfile);
311 print("Output file: " + this.target);
312 if (this.targetDebug.length > 0) {
313 print("Output debug file: " + this.targetDebug);
321 * Core packing routine for a file
323 * @param str - str source text..
324 * @param fn - filename (for reference?)
325 * @param minfile - min file location...
329 private string packFile (string str,string fn, string minfile)
332 var tr = new TokenReader();
335 tr.keepComments = true;
337 tr.collapseWhite = false;
340 // we can load translation map here...
342 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
346 this.activeFile = fn;
348 // and replace if we are generating a different language..
351 //var ts = new TokenStream(toks);
352 //print(JSON.stringify(toks, null,4 )); Seed.quit();
353 var ts = new Collapse(toks.tokens);
354 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
356 var sp = new ScopeParser(ts);
359 sp.buildSymbolTree();
361 sp.mungeSymboltree();
363 //print(sp.warnings.join("\n"));
366 var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
371 if (outf.length > 0) {
372 FileUtils.set_contents(minfile, outf);
383 public string md5(string str)
386 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
390 //stringHandler : function(tok) -- not used...