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 = "") throws PackerError
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();
190 * load a dependancy list -f option
191 * @param {String} srcfile sourcefile to parse
195 public void loadSourceIndex(string in_srcfile)
198 var srcfile = in_srcfile;
199 if (srcfile[0] != '/') {
200 srcfile = this.baseDir + in_srcfile;
203 FileUtils.get_contents(srcfile,out str);
205 var lines = str.split("\n");
206 for(var i =0; i < lines.length;i++) {
208 var f = lines[i].strip();
210 Regex.match_simple ("^/", f) ||
211 !Regex.match_simple ("[a-zA-Z]+", f)
213 continue; // blank comment or not starting with a-z
216 if (Regex.match_simple ("\\.js$", f)) {
222 // this maps Roo.bootstrap.XXX to Roo/bootstrap/xxx.js
223 // should we prefix? =- or should this be done elsewhere?
225 var add = f.replace(".", "/") + ".js";
228 add = this.baseDir + add;
231 if (this.files.contains(add)) {
237 this.files.add( add );
243 private string packAll() // do the packing (run from constructor)
246 //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
247 //File.write(this.transfile, "");
248 if (this.target.length > 0) {
249 this.targetStream.write("".data);
252 if (this.targetDebugStream != null) {
253 this.targetDebugStream.write("".data);
257 foreach(var file in this.files) {
259 print("reading %s\n",file );
261 if (!FileUtils.test (file, FileTest.EXISTS) || FileUtils.test (file, FileTest.IS_DIR)) {
262 print("SKIP (is not a file) %s\n ", file);
266 var loaded_string = false;
267 string file_contents = "";
270 if (this.targetDebugStream !=null) {
272 FileUtils.get_contents(file,out file_contents);
273 this.targetDebugStream.write(file_contents.data);
274 loaded_string = false;
276 // it's a good idea to check with 0 compression to see if the code can parse!!
279 //File.append(dout, str +"\n");
283 var minfile = this.tmpDir + "/" + file.replace("/", ".");
286 // let's see if we have a min file already?
287 // this might happen if tmpDir is set ..
290 if ( FileUtils.test (minfile, FileTest.EXISTS)) {
292 var otv = File.new_for_path(file).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
293 var mtv = File.new_for_path(minfile).query_info (FileAttribute.TIME_MODIFIED, 0).get_modification_time();
296 // print("%s : compare : Cache file %s to Orignal Time %s\n", file, mtv.to_iso8601(), otv.to_iso8601());
297 if (mtv.tv_usec > otv.tv_usec) {
298 continue; // file is newer or the same time..
304 print("COMPRESSING to %s\n", minfile);
305 //var codeComp = pack(str, 10, 0, 0);
306 if (this.cleanup && FileUtils.test (minfile, FileTest.EXISTS)) {
307 FileUtils.remove(minfile);
309 if (!loaded_string) {
310 FileUtils.get_contents(file,out file_contents);
313 this.packFile(file_contents, file, minfile);
318 if (this.dumpTokens) {
320 GLib.Process.exit(0);
322 print("MERGING SOURCE\n");
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)) {
330 print("skipping source %s - does not exist\n", minfile);
334 FileUtils.get_contents(minfile, out str);
335 print("using MIN FILE %s\n", minfile);
336 if (str.length > 0) {
337 if (this.targetStream != null) {
338 this.targetStream.write(("// " +
339 ( (file.length > this.baseDir.length) ? file.substring(this.baseDir.length) : file ) +
341 this.targetStream.write((str + "\n").data);
344 this.outstr += "//" +
345 ( (file.length > this.baseDir.length) ? file.substring(this.baseDir.length) : file ) + "\n";
346 this.outstr += str + "\n";
351 FileUtils.remove(minfile);
355 if (this.target.length > 0 ) {
356 print("Output file: " + this.target);
358 if (this.targetDebug.length > 0) {
359 print("Output debug file: %s\n" , this.targetDebug);
362 // OUTPUT should be handled by PackerRun (so that this can be used as a library...)
363 if (this.outstr.length > 0 ) {
365 // stdout.printf ("%s", this.outstr);
372 * Core packing routine for a file
374 * @param str - str source text..
375 * @param fn - filename (for reference?)
376 * @param minfile - min file location...
380 private string packFile (string str,string fn, string minfile) throws TokenReaderError.Syntax,
383 var tr = new TokenReader();
386 tr.keepComments = true;
388 tr.collapseWhite = false;
391 // we can load translation map here...
393 TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
395 if (this.dumpTokens) {
398 //GLib.Process.exit(0);
401 this.activeFile = fn;
403 // and replace if we are generating a different language..
406 //var ts = new TokenStream(toks);
407 //print(JSON.stringify(toks, null,4 )); Seed.quit();
408 var ts = new Collapse(toks.tokens);
410 //ts.dumpAll(""); print("Done collaps"); Process.exit(1);
412 // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
414 if (!this.skipScope) {
415 var sp = new ScopeParser(ts);
418 sp.buildSymbolTree();
419 sp.mungeSymboltree();
426 //print(sp.warnings.join("\n"));
427 //(new TokenStream(toks.tokens)).dumpAll(""); GLib.Process.exit(1);
428 // compress works on the original array - in theory the replacements have already been done by now
429 var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
432 debug("RESULT: \n %s\n", outf);
434 if (outf.length > 0) {
435 FileUtils.set_contents(minfile, outf);
447 public string md5(string str)
450 return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
454 //stringHandler : function(tok) -- not used...