JSDOC/Packer.vala
[gnome.introspection-doc-generator] / JSDOC / Packer.vala
index 6a81d27..21782f1 100644 (file)
@@ -56,7 +56,9 @@ x.packAll();  // writes files  etc..
  */
 namespace JSDOC 
 {
-
+       public errordomain PackerError {
+            ArgumentError
+    }
 
        public class Packer : Object 
        {
@@ -64,14 +66,14 @@ namespace JSDOC
                * @cfg {String} target to write files to - must be full path.
                */
                string target;
-               FileOutputStream targetStream = null;
+               GLib.FileOutputStream targetStream = null;
                /**
                 * @cfg {String} debugTarget target to write files debug version to (uncompacted)- must be full path.
                 */
                string targetDebug;
                
 
-               FileOutputStream targetDebugStream  = null;
+               GLib.FileOutputStream targetDebugStream  = null;
                /**
                 * @cfg {String} tmpDir  (optional) where to put the temporary files. 
                 *      if you set this, then files will not be cleaned up
@@ -98,6 +100,11 @@ namespace JSDOC
                // list of files to compile...
                Gee.ArrayList<string> files;
                
+               
+               
+               public string activeFile = "";
+               
+               
                public  string out = ""; // if no target is specified - then this will contain the result
     
                public Packer(string target, string targetDebug = "")
@@ -124,8 +131,8 @@ namespace JSDOC
                
                public void pack()
                {
-                   if (!this.files) {
-                               throw new Packer.ArgumentError("No Files loaded before pack() called");
+                   if (this.files.size < 1) {
+                               throw new PackerError.ArgumentError("No Files loaded before pack() called");
                        }
                        if (this.target.length > 0 ) {
                                this.targetStream = File.new_for_path(this.target).replace(null, false,FileCreateFlags.NONE);
@@ -175,10 +182,10 @@ namespace JSDOC
                                // should we prefix? =- or should this be done elsewhere?
                                
                        var add = f.replace(".", "/") + ".js";
-                       if (_this.files.contains(add)) {
+                       if (this.files.contains(add)) {
                            continue;
                        }
-                       _this.files.add( add );
+                       this.files.add( add );
                        
                    }
                }
@@ -190,12 +197,14 @@ namespace JSDOC
                    //this.transOrigFile= bpath + '/../lang.en.js'; // needs better naming...
                    //File.write(this.transfile, "");
                    if (this.target.length > 0) {
-                       this.targetStream.write("");
+                       this.targetStream.write("".data);
                    }
                    
-                   if (this.debugTarget > 0) {
-                           this.targetDebugStream.write("");
+                   if (this.targetDebugStream != null) {
+                           this.targetDebugStream.write("".data);
                    }
+                   
+                   
                    foreach(var file in this.files) {
                        
                        print("reading %s\n",file );
@@ -212,7 +221,7 @@ namespace JSDOC
                        if (this.targetDebugStream !=null) {
                                
                                FileUtils.get_contents(file,out file_contents);
-                           this.targetDebugStream.write(file_contents);
+                           this.targetDebugStream.write(file_contents.data);
                            loaded_string = false;
                        }
                        // it's a good idea to check with 0 compression to see if the code can parse!!
@@ -222,7 +231,7 @@ namespace JSDOC
                        
                   
                        
-                       var minfile = this.tmpDir + '/' + file.replace("/", '.');
+                       var minfile = this.tmpDir + "/" + file.replace("/", ".");
                        
                        
                        // let's see if we have a min file already?
@@ -311,20 +320,21 @@ namespace JSDOC
                    */
                    print("MERGING SOURCE");
                    
-                   for(var i=0; i < this.files.length; i++)  {
+                   for(var i=0; i < this.files.size; i++)  {
                        var file = this.files[i];
-                       var minfile = this.tmpDir + '/' + file.replace('/', '.');
+                       var minfile = this.tmpDir + "/" + file.replace("/", ".");
                        
                        
-                       if (!File.exists(minfile)) {
+                       if ( FileUtils.test(minfile, FileTest.EXISTS)) {
                            continue;
                        }
-                       var str = File.read(minfile);
-                       print("using MIN FILE  "+ minfile);
-                       if (str.length) {
+                       string str;
+                       FileUtils.get_contents(minfile, out str);
+                       print("using MIN FILE  %s\n", minfile);
+                       if (str.length > 0) {
                            if (this.targetStream != null) {
-                                       this.targetStream.write("//" + file + "\n"); 
-                                       this.targetStream.write(str + "\n"); 
+                                       this.targetStream.write(("//" + file + "\n").data); 
+                                       this.targetStream.write((str + "\n").data); 
 
                            } else {
                                this.out += "//" + file + "\n";
@@ -338,123 +348,85 @@ namespace JSDOC
                        
                    }
                    print("Output file: " + this.target);
-                   if (this.debugTarget) print("Output debug file: " + this.debugTarget);
+                   if (this.targetDebug.length > 0) {
+                                print("Output debug file: " + this.targetDebug);
+                        }
                    
                     
                
                
                }
-    /**
-     * Core packing routine  for a file
-     * 
-     * @param str - str source text..
-     * @param fn - filename (for reference?)
-     * @param minfile - min file location...
-     * 
-     */
-    
-    packFile : function (str,fn,minfile)
-    {
-    
-        var tr = new  TokenReader(  { 
-            keepDocs :true, 
-            keepWhite : true,  
-            keepComments : true, 
-            sepIdents : true,
-            collapseWhite : false,
-            filename : fn
-        });
-        this.timerPrint("START" + fn);
-        
-        // we can load translation map here...
-        
-        var toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
-        
-        // at this point we can write a language file...
-        if (this.translateJSON) {
-            
-            this.writeTranslateFile(fn, minfile, toks);
-        }
-        
-        this.activeFile = fn;
-        
-        // and replace if we are generating a different language..
-        
-        this.timerPrint("Tokenized");
-        //var ts = new TokenStream(toks);
-        //print(JSON.stringify(toks, null,4 )); Seed.quit();
-        var ts = new Collapse(toks);
-       // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
-        //return;//
-        var sp = new ScopeParser(ts);
-        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"));
-        this.timerPrint("Compressed");
-        
-        var out = CompressWhite(new TokenStream(toks), this, this.keepWhite); // do not kill whitespace..
-        
-        
-        this.timerPrint("Compressed");
-        
-         if (out.length) {
-            File.write(minfile, out);
-            this.timerPrint("Write (" + out.length + "bytes) " + minfile);
-        }
-        
-        return out;
-        
-        
-         
-    },
-     
+               /**
+                * Core packing routine  for a file
+                * 
+                * @param str - str source text..
+                * @param fn - filename (for reference?)
+                * @param minfile - min file location...
+                * 
+                */
+
+               private string packFile  (string str,string fn, string minfile)
+               {
+
+                       var tr = new  TokenReader();
+                       tr.keepDocs =true;
+                       tr.keepWhite = true;
+                       tr.keepComments = true;
+                       tr.sepIdents = true;
+                       tr.collapseWhite = false;
+                       tr.filename = fn;
+                       // we can load translation map here...
+               
+                       TokenArray toks = tr.tokenize(new TextStream(str)); // dont merge xxx + . + yyyy etc.
+               
+               
+               
+                       this.activeFile = fn;
+               
+                       // and replace if we are generating a different language..
+               
+
+                       //var ts = new TokenStream(toks);
+                       //print(JSON.stringify(toks, null,4 )); Seed.quit();
+                       var ts = new Collapse(toks.tokens);
+                  // print(JSON.stringify(ts.tokens, null,4 )); Seed.quit();
+                       //return;//
+                       var sp = new ScopeParser(ts);
+                       //sp.packer = this;
+                       sp.buildSymbolTree();
+
+                       sp.mungeSymboltree();
+                       sp.printWarnings();
+                       //print(sp.warnings.join("\n"));
+
+               
+                       var outf = CompressWhite(new TokenStream(toks.tokens), this, this.keepWhite); // do not kill whitespace..
+               
+               
+               
+               
+                        if (outf.length > 0) {
+                               FileUtils.set_contents(minfile, outf);
+                                
+                       }
+               
+                       return out;
+               
+               
+                        
+               }
+                
+
+               public string md5(string str)
+               {
+               
+                       return GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, str);
+               
+               }
     
-    md5 : function (string)
-    {
-        
-        return GLib.compute_checksum_for_string(GLib.ChecksumType.MD5, string, string.length);
-        
-    },
-    stringHandler : function(tok)
-    {
-        //print("STRING HANDLER");
-       // callback when outputing compressed file, 
-       var data = tok.data;
-        if (!this.translateJSON) {
-         //   print("TURNED OFF");
-            return data;
-        }
-        if (tok.name == 'SINGLE_QUOTE') {
-            return data;
-        }
-        
-        if (typeof(tok.trsum) == 'undefined') {
-            return data;
-        }
-        
-        return '_T["' + tok.trsum + '"]';
-        
-        var sval = data.substring(1,data.length-1);
-        // we do not clean up... quoting here!??!!?!?!?!?
-        
-        
-        // blank with tabs or spaces..
-        //if (!sval.replace(new RegExp("(\\\\n|\\\\t| )+",'g'), '').length) {
-       //     return tok.outData;
-       // }
-        
-        var sval = tok.data.substring(1,data.length-1);
-        var fn = this.activeFile.substring(this.prefix.length);
-        
-        
-        return '_T["' + this.md5(fn + '-' + sval) + '"]';
-        
-        
+        //stringHandler : function(tok) -- not used...
     }
     
-    
-};
+}