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 {
62 public class Packer : Object
65 * @cfg {String} target to write files to - must be full path.
68 GLib.FileOutputStream targetStream = null;
70 * @cfg {String} debugTarget target to write files debug version to (uncompacted)- must be full path.
72 string targetDebug = "";
75 GLib.FileOutputStream targetDebugStream = null;
77 * @cfg {String} tmpDir (optional) where to put the temporary files.
78 * if you set this, then files will not be cleaned up
80 * at present we need tmpfiles - as we compile multiple files into one.
81 * we could do this in memory now, as I suspect vala will not be as bad as javascript for leakage...
84 //public string tmpDir = "/tmp"; // FIXME??? in ctor?
89 * @cfg {Boolean} cleanup (optional) clean up temp files after done -
90 * Defaults to false if you set tmpDir, otherwise true.
92 public bool cleanup = false;
95 // list of files to compile...
96 Gee.ArrayList<string> files;
99 * @cfg activeFile ??? used???
102 public string activeFile = "";
106 * @cfg baseDir -- prefix the files listed in indexfiles with this.
109 public string baseDir = "";
112 public string outstr = ""; // if no target is specified - then this will contain the result
120 this.result = new Json.Object();
122 this.result_count = new Gee.HashMap <string,int>();
124 this.result = new Gee.HashMap<
125 string /* errtype*/ , Gee.HashMap<string /*fn*/, Gee.HashMap<int /*line*/, Gee.ArrayList<string>>>
129 this.files = new Gee.ArrayList<string>();
131 new Lang_Class(); ///initilizaze lang..
133 //this.tmp = Glib.get_tmp_dir(); // do we have to delete this?
139 // this could be another class really..
141 public enum ResultType {
144 public string to_string() {
146 case err: return "ERR";
147 case warn: return "WARN";
148 default: assert_not_reached();
154 * result of complication - a JSON object containing warnings / errors etc..
156 * warn-TOTAL : X (number of warnings.
157 * err-TOTAL: X (number of errors) << this indicates failure...
160 * line : [ Errors,Errors,.... ]
168 public Json.Object result; // output - what's the complication result
170 public void logError(ResultType type, string filename, int line, string message) {
172 if (!this.result.has_member(type.to_string()+"-TOTAL")) {
173 this.result.set_int_member(type.to_string()+"-TOTAL", 1);
175 this.result.set_int_member(type.to_string()+"-TOTAL",
176 this.result.get_int_member(type.to_string()+"-TOTAL") +1
181 if (!this.result.has_member(type.to_string())) {
182 this.result.set_object_member(type.to_string(), new Json.Object());
184 var t = this.result.get_object_member(type.to_string());
185 if (!t.has_member(filename)) {
186 t.set_object_member(filename, new Json.Object());
188 var tt = t.get_object_member(filename);
189 if (!tt.has_member(line.to_string())) {
190 tt.set_array_member(line.to_string(), new Json.Array());
192 var tl = tt.get_array_member(line.to_string());
193 tl.add_string_element(message);
197 public bool hasErrors(string fn)
199 if (!this.result.has_member(ResultType.err.to_string())) {
206 var t = this.result.get_object_member(ResultType.err.to_string());
208 if (t.has_member(fn)) {
213 public void dumpErrors(ResultType type)
215 if (!this.result.has_member(type.to_string())) {
218 var t = this.result.get_object_member(type.to_string());
219 t.foreach_member((obj, filename, node) => {
220 var linelist = node.dup_object();
221 linelist.foreach_member((linelistobj, linestr, nodear) => {
222 var errors= nodear.dup_array();
223 errors.foreach_element((errorar, ignore, nodestr) => {
224 print("%s: %s:%s %s\n", type.to_string(), filename, linestr, nodestr.get_string());
231 public Gee.HashMap <string,int> result_count; // output - what's the complication result
234 string /* errtype*/ , Gee.HashMap<string /*fn*/, Gee.HashMap<int /*line*/, Gee.ArrayList<string>>>
237 public void logError(ResultType type, string filename, int line, string message) {
240 if (!this.result_count.has_key(type.to_string()+"-TOTAL")) {
241 this.result_count.set(type.to_string()+"-TOTAL", 1);
243 this.result_count.set(type.to_string()+"-TOTAL",
244 this.result_count.get(type.to_string()+"-TOTAL") +1
250 if (!this.result.has_key(type.to_string())) {
251 this.result.set(type.to_string(),
252 new Gee.HashMap<string /*fn*/, Gee.HashMap<int /*line*/, Gee.ArrayList<string>>>()
255 var t = this.result.get(type.to_string());
256 if (!t.has_key(filename)) {
257 t.set(filename, new Gee.HashMap<int /*line*/, Gee.ArrayList<string>>());
259 var tt = t.get(filename);
260 if (!tt.has_key(line)) {
261 tt.set(line, new Gee.ArrayList<string>());
263 var tl = tt.get(line);
268 public bool hasErrors(string fn)
270 if (!this.result.has_key(ResultType.err.to_string())) {
277 var t = this.result.get(ResultType.err.to_string());
284 public void dumpErrors(ResultType type)
286 if (!this.result.has_key(type.to_string())) {
289 var t = this.result.get(type.to_string());
290 foreach(string filename in t.keys) {
291 var node = t.get(filename);
292 foreach(int line in node.keys) {
293 var errors = node.get(line);
294 foreach(string errstr in errors) {
295 print("%s: %s:%d %s\n", type.to_string(), filename, line, errstr);
307 public void loadSourceIndexes(Gee.ArrayList<string> indexes)
309 foreach(var f in indexes) {
310 this.loadSourceIndex(f);
314 public void loadFiles(string[] fs)
316 // fixme -- prefix baseDir?
317 foreach(var f in fs) {
318 GLib.debug("add File: %s", f);
319 this.files.add(f); //?? easier way?
322 public void loadFile(string f)
324 // fixme -- prefix baseDir?
325 GLib.debug("add File: %s", f);
327 GLib.debug("FILE LEN: %d", this.files.size);
331 public string pack(string target, string targetDebug = "") throws PackerError
333 this.target = target;
334 this.targetDebug = targetDebug;
336 if (this.files.size < 1) {
337 throw new PackerError.ArgumentError("No Files loaded before pack() called");
339 if (this.target.length > 0 ) {
340 this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
342 if (this.targetDebug.length > 0 ) {
343 this.targetDebugStream = File.new_for_path(this.targetDebug).replace(null, false,FileCreateFlags.NONE);
345 return this.packAll();
353 * load a dependancy list -f option
354 * @param {String} srcfile sourcefile to parse
358 public void loadSourceIndex(string in_srcfile)
361 var srcfile = in_srcfile;
362 if (srcfile[0] != '/') {
363 srcfile = this.baseDir + in_srcfile;
366 FileUtils.get_contents(srcfile,out str);
368 var lines = str.split("\n");
369 for(var i =0; i < lines.length;i++) {
371 var f = lines[i].strip();
373 Regex.match_simple ("^/", f) ||
374 !Regex.match_simple ("[a-zA-Z]+", f)
376 continue; // blank comment or not starting with a-z
379 if (Regex.match_simple ("\\.js$", f)) {
385 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
386 // should we prefix? =- or should this be done elsewhere?
388 var add = f.replace(".", "/") + ".js";
391 add = this.baseDir + add;
394 if (this.files.contains(add)) {
400 this.files.add( add );
406 private string packAll() // do the packing (run from constructor)
409 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
410 //File.write(this.transfile, "");
411 if (this.target.length > 0) {
412 this.targetStream.write("".data);
415 if (this.targetDebugStream != null) {
416 this.targetDebugStream.write("".data);
420 var tmpDir = GLib.DirUtils.make_tmp("roojspacker_XXXXXX");
422 foreach(var file in this.files) {
424 print("reading %s\n",file );
426 if (!FileUtils.test (file, FileTest.EXISTS) || FileUtils.test (file, FileTest.IS_DIR)) {
427 print("SKIP (is not a file) %s\n ", file);
431 var loaded_string = false;
432 string file_contents = "";
435 if (this.targetDebugStream !=null) {
437 FileUtils.get_contents(file,out file_contents);
438 this.targetDebugStream.write(file_contents.data);
439 loaded_string = false;
441 // it's a good idea to check with 0 compression to see if the code can parse!!
444 //File.append(dout, str +"\n");
448 var minfile = tmpDir + "/" + file.replace("/", ".");
451 // let's see if we have a min file already?
452 // this might happen if tmpDir is set ..
455 if ( FileUtils.test (minfile, FileTest.EXISTS)) {
457 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
458 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
461 // print("%s : compare : Cache file %s to Orignal Time %s\n", file, mtv.to_iso8601(), otv.to_iso8601());
462 if (mtv.tv_usec > otv.tv_usec) {
463 continue; // file is newer or the same time..
469 print("COMPRESSING to %s\n", minfile);
470 //var codeComp = pack(str, 10, 0, 0);
471 if (this.cleanup && FileUtils.test (minfile, FileTest.EXISTS)) {
472 FileUtils.remove(minfile);
474 if (!loaded_string) {
475 FileUtils.get_contents(file,out file_contents);
478 this.packFile(file_contents, file, minfile);
483 // at this point if we have errors, we should stop..
486 this.dumpErrors(ResultType.warn);
487 this.dumpErrors(ResultType.err); // since they are fatal - display them last...
492 if (PackerRun.opt_dump_tokens || this.hasErrors("")) {
494 GLib.Process.exit(0);
496 print("MERGING SOURCE\n");
498 for(var i=0; i < this.files.size; i++) {
499 var file = this.files[i];
500 var minfile = tmpDir + "/" + file.replace("/", ".");
503 if ( !FileUtils.test(minfile, FileTest.EXISTS)) {
504 print("skipping source %s - does not exist\n", minfile);
508 FileUtils.get_contents(minfile, out str);
509 print("using MIN FILE %s\n", minfile);
510 if (str.length > 0) {
511 if (this.targetStream != null) {
512 this.targetStream.write(("// " +
513 ( (file.length > this.baseDir.length) ? file.substring(this.baseDir.length) : file ) +
515 this.targetStream.write((str + "\n").data);
518 this.outstr += "//" +
519 ( (file.length > this.baseDir.length) ? file.substring(this.baseDir.length) : file ) + "\n";
520 this.outstr += str + "\n";
525 FileUtils.remove(minfile);
530 FileUtils.remove(tmpDir);
533 if (this.target.length > 0 ) {
534 print("Output file: " + this.target);
536 if (this.targetDebug.length > 0) {
537 print("Output debug file: %s\n" , this.targetDebug);
542 // OUTPUT should be handled by PackerRun (so that this can be used as a library...)
543 if (this.outstr.length > 0 ) {
545 // stdout.printf ("%s", this.outstr);
552 * Core packing routine for a file
554 * @param str - str source text..
555 * @param fn - filename (for reference?)
556 * @param minfile - min file location...
560 public string packFile (string str,string fn, string minfile)
563 var tr = new TokenReader(this);
566 tr.keepComments = true;
568 tr.collapseWhite = false;
571 // we can load translation map here...
573 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
575 if (PackerRun.opt_dump_tokens) {
578 //GLib.Process.exit(0);
581 this.activeFile = fn;
583 // and replace if we are generating a different language..
586 //var ts = new TokenStream(toks);
587 //print(JSON.stringify(toks, null,4 )); Seed.quit();
588 var ts = new Collapse(toks.tokens, this, fn);
590 //ts.dumpAll(""); print("Done collaps"); Process.exit(1);
592 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
594 if (!PackerRun.opt_skip_scope) {
595 var sp = new ScopeParser(ts, this, fn);
598 sp.buildSymbolTree();
599 sp.mungeSymboltree();
606 //print(sp.warnings.join("\n"));
607 //(new TokenStream(toks.tokens)).dumpAll(""); GLib.Process.exit(1);
608 // compress works on the original array - in theory the replacements have already been done by now
609 var outf = CompressWhite(new TokenStream(toks.tokens), this, PackerRun.opt_keep_whitespace); // do not kill whitespace..
612 debug("RESULT: \n %s\n", outf);
616 if (outf.length > 0 && minfile.length > 0 && !this.hasErrors(fn)) {
617 FileUtils.set_contents(minfile, outf);
629 public string md5(string str)
632 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
636 //stringHandler : function(tok) -- not used...