Final User interface tweaks to basic commit code (shows dialogs while it does stuff)
[gitlive] / Spawn.js
index 084a7cb..473d66e 100644 (file)
--- a/Spawn.js
+++ b/Spawn.js
@@ -1,7 +1,7 @@
 ///<script type="text/javascript">
 
-Gio      = imports.gi.Gio;
-GLib      = imports.gi.GLib;
+var Gio      = imports.gi.Gio;
+var GLib      = imports.gi.GLib;
 
 
 /**
@@ -25,6 +25,46 @@ GLib      = imports.gi.GLib;
 *   }
 *  });
 * 
+*
+*
+
+*
+*
+*  CRITICAL - needs this change to gir in GLib-2.0.gir g_spawn_async_with_pipes
+*
+    <parameter name="argv" transfer-ownership="none">
+         <array c:type="gchar**">
+            <type name="utf8"/>
+          </array>
+        </parameter>
+        <parameter name="envp" transfer-ownership="none" allow-none="1">
+          <array c:type="gchar**">
+            <type name="utf8"/>
+          </array>
+        </parameter>
+*
+*
+*<method name="read_line"
+              c:identifier="g_io_channel_read_line"
+              throws="1">
+        <return-value transfer-ownership="none">
+          <type name="IOStatus" c:type="GIOStatus"/>
+        </return-value>
+        <parameters>
+          <parameter name="str_return" transfer-ownership="full" direction="out">
+            <type name="utf8" c:type="gchar**"/>
+          </parameter>
+          <parameter name="length" transfer-ownership="none" direction="out">
+            <type name="gsize" c:type="gsize*"/>
+          </parameter>
+          <parameter name="terminator_pos" transfer-ownership="none"  direction="out">
+            <type name="gsize" c:type="gsize*"/>
+          </parameter>
+        </parameters>
+      </method>
+*
+*
+*
 * 
 */
 
@@ -38,10 +78,11 @@ GLib      = imports.gi.GLib;
  * @arg listeners {Object} (optional) handlers for output, stderr, input
  *     stderr/output both receive output line as argument
  *     input should return any standard input
+ *     finish recieves result as argument.
  * @arg env {Array}             enviroment eg. [ 'GITDIR=/home/test' ]
  * @arg async {Boolean} (optional)return instantly, or wait for exit. (default no)
  * @arg exceptions {Boolean}    throw exception on failure (default no)
- * 
+ * @arg debug {Boolean}    print out what's going on.. (default no)
  * 
  */
 function Spawn(cfg) {
@@ -56,14 +97,18 @@ function Spawn(cfg) {
     }
     
 }
+
+
 Spawn.prototype = {
     
+    ctx : false, // the mainloop ctx.
     listeners : false,
     async : false,
     env : null,
     cwd: false,
     args: false,
     exceptions : false,
+    debug : true,
     /**
      * @property output {String} resulting output
      */
@@ -75,157 +120,306 @@ Spawn.prototype = {
      /**
      * @property result {Number} execution result.
      */
-    result: 0
+    result: 0,
     /**
      * @property pid {Number} pid of child process (of false if it's not running)
      */
     pid : false,
-    
     /**
-     * @property done {Boolean} has the process completed.
+     * @property in_ch {GLib.IOChannel} input io channel
      */
-    done : false,
-    
+    in_ch : false,
+    /**
+     * @property out_ch {GLib.IOChannel} output io channel
+     */
+    out_ch : false,
+    /**
+     * @property err_ch {GLib.IOChannel} stderr io channel
+     */
+    err_ch : false,
     /**
      * 
      * @method run
      * Run the configured command.
-     * 
+     * result is applied to object properties (eg. 'output' or 'stderr')
+     * @returns {Object} self.
      */
-    
-    
     run : function()
     {
+        
+        var _this = this;
+        
+        var err_src = false;
+        var out_src = false;
         var ret = {};
-        GLib.spawn_async_with_pipes(this.cwd, this.args, null, 
+        
+        if (this.debug) {
+           print("cd " + this.cwd +";" + this.args.join(" "));
+        }
+        
+        GLib.spawn_async_with_pipes(this.cwd, this.args, this.env, 
             GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH , 
             null, null, ret);
             
+       //print(JSON.stringify(ret));    
         this.pid = ret.child_pid;
         
-        var ctx = false; 
-       
-        var _this = this;
+        if (this.debug) {
+            print("PID: " + this.pid);
+        }
+        
         
+       
         GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.pid, function(pid, result) {
             _this.result = result;
+            if (_this.debug) {
+                print("child_watch_add : result: " + result);
+            }
+            _this.read(_this.out_ch);
+            _this.read(_this.err_ch);
             
             GLib.spawn_close_pid(_this.pid);
             _this.pid = false;
-            if (ctx) {
-                GLib.main_loop_quit(ctx);
+            if (_this.ctx) {
+                _this.ctx.quit();
+            }
+            tidyup();
+           //print("DONE TIDYUP");
+            if (_this.listeners.finish) {
+                _this.listeners.finish.call(this, _this.result);
             }
-            
         });
-        this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
-        var out_ch = GLib.io_channel_unix_new(ret.standard_output);
-        var err_ch = GLib.io_channel_unix_new(ret.standard_error);
-       
-        // make everything non-blocking!
-        GLib.io_channel_set_flags (this.in_ch,GLib.IOFlags.NONBLOCK);
-        GLib.io_channel_set_flags (out_ch,GLib.IOFlags.NONBLOCK);
-        GLib.io_channel_set_flags (err_ch,GLib.IOFlags.NONBLOCK);
-
-        function readstr(ch, prop) {
-            while (true) {
-                var x = new GLib.String();
-                var status = GLib.io_channel_read_line_string (ch, x);
-                switch(status) {
-                    case GLib.IOStatus.NORMAL:
-                        //write(fn, x.str);
-                        if (this.listeners[prop]) {
-                            this.listeners[prop].call(this, x.str);
-                        }
-                        _this[prop] += x.str;
-                       continue;
-                    case GLib.IOStatus.AGAIN:   
-                        break;
-                    case GLib.IOStatus.ERROR:    
-                    case GLib.IOStatus.EOF:   
-                       break;
-                    
-                }
-                break;
+        
+        function tidyup()
+        {
+            if (_this.pid) {
+                GLib.spawn_close_pid(_this.pid); // hopefully kills it..
+                _this.pid = false;
             }
+            if (_this.in_ch)  _this.in_ch.close();
+            if (_this.out_ch)  _this.out_ch.close();
+            if (_this.err_ch)  _this.err_ch.close();
+            // blank out channels
+            _this.in_ch = false;
+            _this.err_ch = false;
+            _this.out_ch = false;
+            // rmeove listeners !! important otherwise we kill the CPU
+            if (err_src !== false) GLib.source_remove(err_src);
+            if (out_src !== false) GLib.source_remove(out_src);
+            err_src = false;
+            out_src = false;
+            
         }
         
-        var out_src= GLib.io_add_watch(out_ch, GLib.PRIORITY_DEFAULT, 
-            GLib.IOCondition.OUT + GLib.IOCondition.IN  + GLib.IOCondition.PRI, function()
-        {
-            readstr(out_ch,  'output');
+        
+        this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
+        this.out_ch = GLib.io_channel_unix_new(ret.standard_output);
+        this.err_ch = GLib.io_channel_unix_new(ret.standard_error);
+        
+        // make everything non-blocking!
             
-        });
-        var err_src= GLib.io_add_watch(err_ch, GLib.PRIORITY_DEFAULT, 
-            GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT, 
+        
+        
+         // using NONBLOCKING only works if io_add_watch
+       //returns true/false in right conditions
+       this.in_ch.set_flags (GLib.IOFlags.NONBLOCK);
+       this.out_ch.set_flags (GLib.IOFlags.NONBLOCK);
+       this.err_ch.set_flags (GLib.IOFlags.NONBLOCK);
+       
+
+      
+        // add handlers for output and stderr.
+        out_src= GLib.io_add_watch(this.out_ch, GLib.PRIORITY_DEFAULT, 
+            GLib.IOCondition.OUT + GLib.IOCondition.IN  + GLib.IOCondition.PRI +  GLib.IOCondition.HUP +  GLib.IOCondition.ERR,
+            function() {
+            
+               return  _this.read(_this.out_ch);
+            
+            }
+        );
+        err_src= GLib.io_add_watch(this.err_ch, GLib.PRIORITY_DEFAULT, 
+            GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT +  GLib.IOCondition.HUP, 
             function()
         {
-             readstr(err_ch, 'stderr');
+            return _this.read(_this.err_ch);
              
         });
+        
+      
+        
+        // call input.. 
         if (this.pid !== false) {
             // child can exit before we get this far..
             if (this.listeners.input) {
-                this.write(this.listeners.input.call(this));
+               print("Trying to call listeners");
+                try {
+                    this.write(this.listeners.input.call(this));
+                    // this probably needs to be a bit smarter...
+                   //but... let's close input now..
+                   this.in_ch.close();
+                   _this.in_ch = false;
+                  
+                   
+                   
+                   
+                   
+                } catch (e) {
+                    tidyup();
+                    throw e;
+                    
+                }
+                
             }
         }
-        if (this.pid !== false && !this.async) {
-            
-            ctx = GLib.main_loop_new (null, false);
-            GLib.main_loop_run(ctx, false); // wait fore exit?
+        // async - if running - return..
+        if (this.async && this.pid) {
+            return this;
         }
-        // read any resulting data.
-        readstr(out_ch,  'output');
-        readstr(err_ch,  'error');
         
-        // clean up.
         
+        // start mainloop if not async..
+        
+        if (this.pid !== false) {
+            if (this.debug) {
+                print("starting main loop");
+            }
+           
+            this.ctx = new GLib.MainLoop.c_new (null, false);
+            this.ctx.run(false); // wait fore exit?
+            
+            //print("main_loop done!");
+        } else {
+            tidyup(); // tidyup get's called in main loop. 
+        }
         
-        GLib.io_channel_close(this.in_ch);
-        this.in_ch = false;
-        GLib.io_channel_close(out_ch);
-        GLib.io_channel_close(err_ch);
-        GLib.source_remove(err_src);
-        GLib.source_remove(out_src);
         if (this.exceptions && this.result != 0) {
-            throw this.stderr;
+            this.toString = function() { return this.stderr; };
+            throw this; // we throw self...
         }
+        
+        // finally throw, or return self..
+        
         return this;
     
     },
     /**
      * write to stdin of process
+     * @arg str {String} string to write to stdin of process
      * @returns GLib.IOStatus (0 == error, 1= NORMAL)
-     * 
      */
-    
     write : function(str) // write a line to 
     {
         if (!this.in_ch) {
-            return; // input is closed
+            return 0; // input is closed
         }
-        var ret = {};
-        var res = GLib.io_channel_write_chars(this.in_ch, str, str.length);
+       //print("write: " + str);
+       // NEEDS GIR FIX! for return value.. let's ignore for the time being..
+       //var ret = {};
+        //var res = this.in_ch.write_chars(str, str.length, ret);
+        var res = this.in_ch.write_chars(str, str.length);
+       
+       //print("write_char retunred:" + JSON.stringify(res) +  ' ' +JSON.stringify(ret)  );
+       
         if (res != GLib.IOStatus.NORMAL) {
             throw "Write failed";
         }
-        return ret.bytes_written;
+        //return ret.value;
+        return str.length;
         
+    },
+    
+    /**
+     * read from pipe and call appropriate listerner and add to output or stderr string.
+     * @arg giochannel to read from.
+     * @returns none
+     */
+    read: function(ch) 
+    {
+        var prop = ch == this.out_ch ? 'output' : 'stderr';
+       // print("prop: " + prop);
+        var _this = this;
+        
+       
+        //print(JSON.stringify(ch, null,4));
+        while (true) {
+            var x = {};
+            var status = ch.read_line(x);
+            // print('status: '  +JSON.stringify(status));
+            // print(JSON.stringify(x));
+            switch(status) {
+                case GLib.IOStatus.NORMAL:
+               
+                    //write(fn, x.str);
+                    if (this.listeners[prop]) {
+                        this.listeners[prop].call(this, x.str_return);
+                    }
+                    _this[prop] += x.str_return;
+                    if (_this.debug) {
+                        print(prop + ':' + x.str_return.replace(/\n/, ''));
+                    }
+                    if (this.async) {
+                        try {
+                            if (imports.gi.Gtk.events_pending()) {
+                                imports.gi.Gtk.main_iteration();
+                            }
+                        } catch(e) {
+                            
+                        }
+                    }
+                    
+                    //this.ctx.iteration(true);
+                   continue;
+                case GLib.IOStatus.AGAIN:   
+                   //print("Should be called again.. waiting for more data..");
+                   return true;
+                    break;
+                case GLib.IOStatus.ERROR:    
+                case GLib.IOStatus.EOF:   
+                   return false;
+                   break;
+                
+            }
+            break;
+        }
+       
+        //print("RETURNING");
+         return false; // allow it to be called again..
     }
     
 };
 /**
  * @function run 
  * 
- * simple run a process - returns result, or throws error..
+ * simple run a process - returns result, or throws stderr result...
  * @param cfg {Object}  see spawn
+ * @return {string} stdout output.
  */
 function run(cfg) {
     cfg.exceptions = true;
     cfg.async = false;
     var s = new Spawn(cfg);
     var ret = s.run();
-    return ret.output;
+    return s.output;
 }
+ /*
+// test
+try { 
+    Seed.print(run({
+        args: ['ls', '/tmp'],
+        debug : true
+    }));
+} catch (e) { print(JSON.stringify(e)); }
+var secs = (new Date()).getSeconds() 
 
+try {      
+Seed.print(run({
+    args: ['/bin/touch', '/tmp/spawntest-' + secs ],
+    debug : true
+}));
+} catch (e) { print( 'Error: ' + JSON.stringify(e)); }
 
-     
\ No newline at end of file
+ */