+// <script type="text/javascript">
+XObject = imports.XObject.XObject;
+File = imports.File.File;
+
+TokenReader = imports['JSDOC/TokenReader.js']/TokenReader;
+/**
+ * @class JSDOC.Packer
+ * Create a new packer
+ *
+ * Usage:
+ * <code>
+ *
+var x = new JSDOC.Packer(
+ [ "/location/of/file1.js", "/location/of/file2.js", ... ],
+ "/location/of"
+);
+x.packFiles(
+ "/location/of/temp_batch_dir",
+ "/location/of/output-compacted-file.js",
+ "/location/of/output-debug-merged-file.js"
+);
+
+ *</code>
+ *
+ * Notes for improving compacting:
+ * if you add a jsdoc comment
+ * <code>
+ * /**
+ * eval:var:avarname
+ * eval:var:bvarname
+ * ....
+ * </code>
+ * directly before an eval statement, it will compress all the code around the eval,
+ * and not rename the variables 'avarname'
+ *
+ * Dont try running this on a merged uncompressed large file - it's horrifically slow.
+ * Best to use lot's of small classes, and use it to merge, as it will cache the compaction
+ *
+ *
+ *
+ * @param {Array} files List of Files - MUST BE WITH ABSOLUTE PATH eg. [ '/usr/src/xyz/abc.js', .... ]
+ * @param {String} source_path top level directory of source (used to work out the relative names for the minimized temp files)
+ */
+Packer = function(files, spath)
+{
+ this.files = files;
+ this.spath = spath; // source path
+ this.aliasList = { }; // list of maps Roo.asdfasdfasf => Roo.A1
+ this.timer = new Date() * 1;
+ this.translate = true;
+}
+Packer.prototype = {
+
+ bpath : '',
+
+ // set to false to stop translation happening..
+
+ /**
+ * Pack the files.
+ *
+ * @param {String} batch_path location of batched temporary min files.
+ * @param {String} compressed_file eg. roo-all.js
+ * @param {String} debug_file eg. roo-debug.js
+ *
+ */
+
+ packFiles : function(bpath, allfile, debugfile) {
+ var str;
+ var spath = this.spath;
+ var files = this.files;
+ this.bpath = bpath;
+ // old var names - fixme..
+ var dout = debugfile;
+ //File.write(dout, "");
+
+ var outpath = allfile;
+
+ var transfile = bpath + '/_translation_.js';
+ //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
+ //File.write(this.transfile, "");
+ File.write(dout, "");
+ File.write(allfile, "");
+ for(var i=0; i < files.length; i++) {
+
+ print("reading " +files[i] );
+ if (!File.exists(files[i])) {
+ print("SKIP (does not exist) " + files[i]);
+ continue;
+ }
+
+
+ File.append(dout, File.read(files[i]));
+ // it's a good idea to check with 0 compression to see if the code can parse!!
+
+ // debug file..
+ //File.append(dout, str +"\n");
+
+
+
+ var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
+ var transfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
+ // let's see if we have a min file already?
+ if (true && File.exists(minfile)) {
+ var mt = File.mtime(minfile);
+ var ot = File.mtime(files[i]);
+ print("compare : " + mt + "=>" + ot);
+ if (mt >= ot) {
+ continue;
+ /*
+ // then the min'files time is > than original..
+ var str = File.read(minfile);
+ print("using MIN FILE "+ minfile);
+ if (str.length) {
+ File.append(outpath, str + "\n");
+ }
+
+ continue;
+ */
+ }
+
+ }
+
+ print("COMPRESSING ");
+ //var codeComp = pack(str, 10, 0, 0);
+ var str = File.read(files[i]);
+ var str = this.pack(str, files[i], minfile);
+ if (str.length) {
+ File.write(minfile, str);
+ }
+
+
+
+ //var str = File.read(minfile);
+ //print("using MIN FILE "+ minfile);
+ //File.append(outpath, str + "\n");
+ //this.timerPrint("Wrote Files");
+ /*
+ if (codeComp.length) {
+ //print(codeComp);
+
+ File.append(outpath, codeComp+"\n");
+ File.write(minfile, codeComp);
+ }
+ */
+ //print(codeComp);
+ // if (i > 10) return;
+ }
+ if (this.translate) {
+
+
+ print("MERGING LANGUAGE");
+ File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
+
+ var transfileAll = bpath + '/_translation_.js';
+ File.write(transfileAll, "");
+ for(var i=0; i < files.length; i++) {
+ var transfile= bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang.trans';
+ var transmd5 = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';
+ if (File.exists(transmd5)) {
+ var str = File.read(transmd5);
+ if (str.length) {
+ File.append(outpath, str + "\n");
+ }
+ }
+ if (File.exists(transfile)) {
+ var str = File.read(transfile);
+ if (str.length) {
+ File.append(transfileAll, str);
+ }
+ }
+
+
+ }
+ }
+ print("MERGING SOURCE");
+
+ for(var i=0; i < files.length; i++) {
+
+ var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
+ if (!File.exists(minfile)) {
+ continue;
+ }
+ var str = File.read(minfile);
+ print("using MIN FILE "+ minfile);
+ if (str.length) {
+ File.append(outpath, str + "\n");
+ }
+ }
+
+
+ //File.append(dout, "\n");// end the function
+
+
+
+ },
+ /**
+ * Core packing routine for a file
+ *
+ * @param str - str source text..
+ * @param fn - filename (for reference?)
+ * @param minfile - min file location...
+ *
+ */
+
+ pack : function (str,fn,minfile)
+ {
+
+ var tr = new TokenReader();
+ this.timerPrint("START" + fn);
+
+ // we can load translation map here...
+
+ var toks = tr.tokenize(str,false); // dont merge xxx + . + yyyy etc.
+
+ // at this point we can write a language file...
+ if (this.translate) {
+ this.writeTranslateFile(fn, minfile, tr.translateMap);
+ }
+
+ this.activeFile = fn;
+
+ // and replace if we are generating a different language..
+
+
+
+
+ this.timerPrint("Tokenized");
+ //return;//
+ var sp = new JSDOC.ScopeParser(new JSDOC.TokenStream(toks, str.length));
+ this.timerPrint("Converted to Parser");
+ sp.packer = this;
+ sp.buildSymbolTree();
+ this.timerPrint("Built Sym tree");
+ sp.mungeSymboltree();
+ this.timerPrint("Munged Sym tree");
+ print(sp.warnings.join("\n"));
+ var out = JSDOC.CompressWhite(sp.ts, this);
+ this.timerPrint("Compressed");
+ return out;
+
+ },
+
+ timerPrint: function (str) {
+ var ntime = new Date() * 1;
+ var tdif = ntime -this.timer;
+ this.timer = ntime;
+ print('['+tdif+']'+str);
+ },
+
+ /**
+ *
+ * Translation concept...
+ * -> replace text strings with _T....
+ * -> this file will need inserting at the start of the application....
+ * -> we need to generate 2 files,
+ * -> a reference used to do the translation, and the _T file..
+ *
+ */
+
+ writeTranslateFile : function(fn, minfile, map)
+ {
+ var transfile = minfile + '.lang.trans';
+ var transmd5 = minfile + '.lang';
+ var i = 0;
+ var v = '';
+ if (File.exists(transfile)) {
+ File.remove(transfile);
+ }
+ if (File.exists(transmd5)) {
+ File.remove(transmd5);
+ }
+ for(v in map) { i++; break };
+ if (!i ) {
+ return; // no strings in file...
+ }
+ var ff = fn.split('/');
+ var ffn = ff[ff.length-1];
+
+
+ File.write(transfile, "\n" + ffn.toSource() + " : {");
+ var l = '';
+ var _tout = {}
+
+ File.write(transmd5, '');
+ for(v in map) {
+ File.append(transfile, l + "\n\t \"" + v + '" : "' + v + '"');
+ l = ',';
+ // strings are raw... - as the where encoded to start with!!!
+ File.append(transmd5, '_T[' + (ffn + '-' + v).md5().toSource() + ']="'+v+"\";\n");
+ }
+ File.append(transfile, "\n},"); // always one trailing..
+
+
+ },
+ stringHandler : function(tok)
+ {
+ //print("STRING HANDLER");
+ // callback when outputing compressed file,
+ if (!this.translate) {
+ // print("TURNED OFF");
+ return tok.outData;
+ }
+ if (tok.qc != '"') {
+ return tok.outData;
+ }
+ var sval = tok.data.substring(1,tok.data.length-1);
+ // blank with tabs or spaces..
+ //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
+ // return tok.outData;
+ // }
+
+
+
+
+ var ff = this.activeFile.split('/');
+ var ffn = ff[ff.length-1];
+ return '_T[' + (ffn + '-' + sval).md5().toSource() + ']';
+
+
+ }
+
+
+});