JSDOC/Packer.js
[gnome.introspection-doc-generator] / JSDOC / Packer.js
index 6f0c2e0..5e24914 100644 (file)
@@ -1,21 +1,33 @@
 // <script type="text/javascript">
-XObject = imports.XObject.XObject;
-File = imports.File.File;
+XObject         = imports.XObject.XObject;
+File            = imports.File.File;
 
-TokenReader = imports['JSDOC/TokenReader.js'].TokenReader;
-ScopeParser = imports['JSDOC/ScopeParser.js'].ScopeParser;
-TokenStream = imports['JSDOC/TokenStream.js'].TokenStream;
+TokenReader     = imports['JSDOC/TokenReader.js'].TokenReader;
+ScopeParser     = imports['JSDOC/ScopeParser.js'].ScopeParser;
+TokenStream     = imports['JSDOC/TokenStream.js'].TokenStream;
+CompressWhite   = imports['JSDOC/CompressWhite.js'].CompressWhite;
+
+GLib = imports.gi.GLib;
 /**
  * @namespace JSDOC
  * @class  Packer
  * Create a new packer
  * 
+ * Use with pack.js 
+ * 
+ * 
  * Usage:
  * <code>
  *
-var x = new JSDOC.Packer(
-    [ "/location/of/file1.js", "/location/of/file2.js", ... ],
-    "/location/of"
+Packer = imports['JSDOC/Packer.js'].Packer;
+var x = new  Packer({
+    
+    files : [ "/location/of/file1.js", "/location/of/file2.js", ... ],
+    target : "/tmp/output.js",
+    debugTarget : "/tmp/output.debug.js", // merged file without compression.
+    translateJson : "/tmp/translate.json",
+    
+    
 );
 x.packFiles(
     "/location/of/temp_batch_dir", 
@@ -36,63 +48,91 @@ x.packFiles(
  * 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.
+ * Dont try running this on a merged uncompressed large file - it's used to be horrifically slow. not sure about now..
  * 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)
+ * Notes for translation
+ *  - translation relies on you using double quotes for strings if they need translating
+ *  - single quoted strings are ignored.
+ * 
+ * Generation of indexFiles
+ *   - translateIndex = the indexfile
+ * 
+ * 
+ * 
+ * 
+
  */
-Packer = function(files, spath)
+Packer = function(cfg)
 {
-    this.files = files;
-    this.spath  = spath; // source path
-    this.aliasList = { }; // list of maps Roo.asdfasdfasf => Roo.A1
+    
+    XObject.extend(this, cfg);
+    if (!this.files) {
+        throw "No Files";
+    }
+    if (!this.target) {
+        throw "No Target";
+    }
+    if ((typeof(cfg.tmpDir) != 'undefined') && (!cfg.cleanup)) {
+        this.cleanup = false; // do not clean up files.. = as tmpdir is set.
+    }
+    this.tmpFiles = [];
     this.timer =  new Date() * 1;
-    this.translate = true;
+    this.packAll();
 }
 Packer.prototype = {
+    /**
+     * @prop files {Array} list of files to compress (must be full path)
+     */
+    files : false,
+    /**
+     * @prop target {String} target to write files to - must be full path.
+     */
+    target : '',
+    /**
+     * @prop debugTarget {String} target to write files debug version to (uncompacted)- must be full path.
+     */
+    debugTarget : '', // merged file without compression.
+    /**
+     * @prop tmpDir {String} (optional) where to put the temporary files. 
+     *      if you set this, then files will not be cleaned up
+     */
+    tmpDir : '/tmp',
     
-    bpath : '',
-    
-    // set to false to stop translation happening..
-    
+    translateJson : '', // json based list of strings in all files.
+   
+    tmpFiles : false, // list of temporary files - cleaned up at end..
     /**
-     * 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
-     * 
+     * @prop cleanup {Boolean} (optional) clean up temp files after done - 
+     *    Defaults to false if you set tmpDir, otherwise true.
      */
+    cleanup : true, //
     
-    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, "");
+    packAll : function()  // do the packing (run from constructor)
+    {
         
-        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++)  {
+        File.write(this.target, "");
+        if (this.debugTarget) {
+            File.write(this.debugTarget, "");
+        }
+        
+        for(var i=0; i < this.files.length; i++)  {
+            var file = this.files[i];
             
-            print("reading " +files[i] );
-            if (!File.exists(files[i])) {
-                print("SKIP (does not exist) " + files[i]);
+            print("reading " +file );
+            if (!File.exists(file)) {
+                print("SKIP (does not exist) " + file);
                 continue;
             }
            
-            
-            File.append(dout, File.read(files[i]));
+            if (this.debugTarget) {
+                File.append(this.debugTarget, File.read(files[i]));
+            }
             // it's a good idea to check with 0 compression to see if the code can parse!!
             
             // debug file..
@@ -100,12 +140,14 @@ Packer.prototype = {
             
        
             
-            var minfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.');
-            var transfile = bpath + '/' +files[i].substr(spath.length+1).replace(/\//g, '.') +'.lang';        
+            var minfile = this.tmpDir + '/' +files.replace(/\//g, '.');
+            
+            
             // let's see if we have a min file already?
+            // this might happen if tmpDir is set .. 
             if (true && File.exists(minfile)) {
                 var mt = File.mtime(minfile);
-                var ot = File.mtime(files[i]);
+                var ot = File.mtime(files);
                 print("compare : " + mt + "=>" + ot);
                 if (mt >= ot) {
                     continue;
@@ -125,39 +167,30 @@ Packer.prototype = {
             
             print("COMPRESSING ");
             //var codeComp = pack(str, 10, 0, 0);
-            var str = File.read(files[i]);
-            var str = this.pack(str, files[i], minfile);
+            if (File.exists(minfile)) {
+                File.remove(minfile);
+            }
+            var str = File.read(files);
+            var str = this.pack(str, files, minfile);
             if (str.length) {
-                File.write(minfile, str);   
+                File.write(minfile, str);
+                this.tmpFiles.push(minfile);
             }
             
              
-            
-            //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) {
+        if (this.translateJson) {
             
                
             print("MERGING LANGUAGE");
-            File.write(outpath, "if (typeof(_T) == 'undefined') { _T={};}\n");
+            File.write(this.target, "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';
+            File.write(this.translateJson, "");
+            for(var i=0; i < this.files.length; i++)  {
+                var file = this.files[i];
+                var transfile= this.tmpDir + '/' +file.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);
@@ -168,7 +201,7 @@ Packer.prototype = {
                 if (File.exists(transfile)) {
                     var str = File.read(transfile);
                     if (str.length) {
-                        File.append(transfileAll, str);
+                        File.append(this.translateJson, str);
                     }
                 }
                 
@@ -300,7 +333,7 @@ Packer.prototype = {
         
         return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
         
-    }
+    },
     stringHandler : function(tok)
     {
         //print("STRING HANDLER");
@@ -329,4 +362,4 @@ Packer.prototype = {
     }
     
     
-});
+};