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.
71 string targetDebug = "";
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 * at present we need tmpfiles - as we compile multiple files into one.
80 * we could do this in memory now, as I suspect vala will not be as bad as javascript for leakage...
83 public string tmpDir = "/tmp"; // FIXME??? in ctor?
88 * @cfg {Boolean} cleanup (optional) clean up temp files after done -
89 * Defaults to false if you set tmpDir, otherwise true.
91 public bool cleanup = false;
95 * @cfg {Boolean} keepWhite (optional) do not remove white space in output.
96 * usefull for debugging compressed files.
99 public bool keepWhite = false;
102 * @cfg {Boolean} skipScope (optional) skip Scope parsing and replacement.
103 * usefull for debugging...
106 public bool skipScope = false;
110 * @cfg {Boolean} dumpTokens (optional) read the first file and dump the tokens.
111 * usefull for debugging...
114 public bool dumpTokens = false;
116 // list of files to compile...
117 Gee.ArrayList<string> files;
120 * @cfg activeFile ??? used???
123 public string activeFile = "";
127 * @cfg baseDir -- prefix the files listed in indexfiles with this.
130 public string baseDir = "";
133 public string outstr = ""; // if no target is specified - then this will contain the result
138 this.files = new Gee.ArrayList<string>();
140 new Lang_Class(); ///initilizaze lang..
144 public void loadSourceIndexes(Gee.ArrayList<string> indexes)
146 foreach(var f in indexes) {
147 this.loadSourceIndex(f);
151 public void loadFiles(string[] fs)
153 // fixme -- prefix baseDir?
154 foreach(var f in fs) {
155 GLib.debug("add File: %s", f);
156 this.files.add(f); //?? easier way?
159 public void loadFile(string f)
161 // fixme -- prefix baseDir?
162 GLib.debug("add File: %s", f);
164 GLib.debug("FILE LEN: %d", this.files.size);
168 public string pack(string target, string targetDebug = "")
170 this.target = target;
171 this.targetDebug = targetDebug;
173 if (this.files.size < 1) {
174 throw new PackerError.ArgumentError("No Files loaded before pack() called");
176 if (this.target.length > 0 ) {
177 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
179 if (this.targetDebug.length > 0 ) {
180 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
182 return this.packAll();
193 * load a dependancy list -f option
194 * @param {String} srcfile sourcefile to parse
198 public void loadSourceIndex(string in_srcfile)
201 var srcfile = in_srcfile;
202 if (srcfile[0] != '/') {
203 srcfile = this.baseDir + in_srcfile;
206 FileUtils.get_contents(srcfile,out str);
208 var lines = str.split("\n");
209 for(var i =0; i < lines.length;i++) {
211 var f = lines[i].strip();
213 Regex.match_simple ("^/", f) ||
214 !Regex.match_simple ("[a-zA-Z]+", f)
216 continue; // blank comment or not starting with a-z
219 if (Regex.match_simple ("\\.js$", f)) {
225 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
226 // should we prefix? =- or should this be done elsewhere?
228 var add = f.replace(".", "/") + ".js";
231 add = this.baseDir + add;
234 if (this.files.contains(add)) {
240 this.files.add( add );
246 private string packAll() // do the packing (run from constructor)
249 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
250 //File.write(this.transfile, "");
251 if (this.target.length > 0) {
252 this.targetStream.write("".data);
255 if (this.targetDebugStream != null) {
256 this.targetDebugStream.write("".data);
260 foreach(var file in this.files) {
262 print("reading %s\n",file );
264 if (!FileUtils.test (file, FileTest.EXISTS) || FileUtils.test (file, FileTest.IS_DIR)) {
265 print("SKIP (is not a file) %s\n ", file);
269 var loaded_string = false;
270 string file_contents = "";
273 if (this.targetDebugStream !=null) {
275 FileUtils.get_contents(file,out file_contents);
276 this.targetDebugStream.write(file_contents.data);
277 loaded_string = false;
279 // it's a good idea to check with 0 compression to see if the code can parse!!
282 //File.append(dout, str +"\n");
286 var minfile = this.tmpDir + "/" + file.replace("/", ".");
289 // let's see if we have a min file already?
290 // this might happen if tmpDir is set ..
293 if ( FileUtils.test (minfile, FileTest.EXISTS)) {
295 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
296 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
299 // print("%s : compare : Cache file %s to Orignal Time %s\n", file, mtv.to_iso8601(), otv.to_iso8601());
300 if (mtv.tv_usec > otv.tv_usec) {
301 continue; // file is newer or the same time..
307 print("COMPRESSING to %s\n", minfile);
308 //var codeComp = pack(str, 10, 0, 0);
309 if (this.cleanup && FileUtils.test (minfile, FileTest.EXISTS)) {
310 FileUtils.remove(minfile);
312 if (!loaded_string) {
313 FileUtils.get_contents(file,out file_contents);
316 this.packFile(file_contents, file, minfile);
321 if (this.dumpTokens) {
323 GLib.Process.exit(0);
325 print("MERGING SOURCE\n");
327 for(var i=0; i < this.files.size; i++) {
328 var file = this.files[i];
329 var minfile = this.tmpDir + "/" + file.replace("/", ".");
332 if ( !FileUtils.test(minfile, FileTest.EXISTS)) {
333 print("skipping source %s - does not exist\n", minfile);
337 FileUtils.get_contents(minfile, out str);
338 print("using MIN FILE %s\n", minfile);
339 if (str.length > 0) {
340 if (this.targetStream != null) {
341 this.targetStream.write(("// " +
342 ( (file.length > this.baseDir.length) ? file.substring(this.baseDir.length) : file ) +
344 this.targetStream.write((str + "\n").data);
347 this.outstr += "//" +
348 ( (file.length > this.baseDir.length) ? file.substring(this.baseDir.length) : file ) + "\n";
349 this.outstr += str + "\n";
354 FileUtils.remove(minfile);
358 if (this.target.length > 0 ) {
359 print("Output file: " + this.target);
361 if (this.targetDebug.length > 0) {
362 print("Output debug file: %s\n" , this.targetDebug);
365 // OUTPUT should be handled by PackerRun (so that this can be used as a library...)
366 if (this.outstr.length > 0 ) {
368 // stdout.printf ("%s", this.outstr);
375 * Core packing routine for a file
377 * @param str - str source text..
378 * @param fn - filename (for reference?)
379 * @param minfile - min file location...
383 private string packFile (string str,string fn, string minfile)
386 var tr = new TokenReader();
389 tr.keepComments = true;
391 tr.collapseWhite = false;
394 // we can load translation map here...
396 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
398 if (this.dumpTokens) {
401 //GLib.Process.exit(0);
404 this.activeFile = fn;
406 // and replace if we are generating a different language..
409 //var ts = new TokenStream(toks);
410 //print(JSON.stringify(toks, null,4 )); Seed.quit();
411 var ts = new Collapse(toks.tokens);
413 //ts.dumpAll(""); print("Done collaps"); Process.exit(1);
415 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
417 if (!this.skipScope) {
418 var sp = new ScopeParser(ts);
421 sp.buildSymbolTree();
422 sp.mungeSymboltree();
429 //print(sp.warnings.join("\n"));
430 //(new TokenStream(toks.tokens)).dumpAll(""); GLib.Process.exit(1);
431 // compress works on the original array - in theory the replacements have already been done by now
432 var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
435 debug("RESULT: \n %s\n", outf);
437 if (outf.length > 0) {
438 FileUtils.set_contents(minfile, outf);
450 public string md5(string str)
453 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
457 //stringHandler : function(tok) -- not used...