resources/RooUsage.txt
[app.Builder.js] / src / Spawn.vala
index b239ab7..d3b9428 100644 (file)
@@ -7,12 +7,17 @@ using GLib;
 /**
  * Revised version?
  * 
- * x = new Spawn( working dir, args)
+ * x = new Spawn( "/tmp", {"ls", "-l" })
+ * 
+ * // these are optionall..
  * x.env = ..... (if you need to set one...
- * x.output_line.connect((string str) => { ... });
+ * x.output_line.connect((string str) => { 
+ *             if ( Gtk.events_pending()) { Gtk.main_iteration(); } 
+ * });
  * x.input_line.connect(() => { return string });
- * x.finish.connect((int res, string output, string stderr) => { ... });
- * x.run();
+ * 
+ * x.run((int res, string output, string stderr) => { ... });
+
  * 
  * 
  */
@@ -38,10 +43,7 @@ public errordomain SpawnError {
  
  * 
  */
-void SpawnFinish (int res, string str, string stderr);
-
+  
 public class Spawn : Object
 {
        /**
@@ -49,6 +51,11 @@ public class Spawn : Object
         * @return the string or null 
         */
        public signal string? input();
+       /**
+        * @signal complete called at when the command has completed.
+        * 
+        */
+       public signal void complete(int res, string str, string stderr);
        /**
         * @signal output_line called when a line is recieved from the process.
         * Note you may want to connect this and run 
@@ -64,6 +71,7 @@ public class Spawn : Object
        
        public bool is_async = true;
        public bool throw_exceptions = false;
+       public bool detach = false;
 
     public Spawn(string cwd, string[] args) throws Error
     {
@@ -132,7 +140,7 @@ public class Spawn : Object
      * result is applied to object properties (eg. '?' or 'stderr')
      * @returns {Object} self.
      */
-       public void run(SpawnFinish finish) throws SpawnError, GLib.SpawnError, GLib.IOChannelError
+       public void run( ) throws SpawnError, GLib.SpawnError, GLib.IOChannelError
        {
                
                 
@@ -146,11 +154,27 @@ public class Spawn : Object
                 
                GLib.debug("cd %s; %s" , this.cwd , string.joinv(" ", this.args));
                
-               
+               if (this.detach) { 
+                       Process.spawn_async_with_pipes (
+                               this.cwd,
+                               this.args,
+                               this.env.length > 0 ? this.env : null,
+                               SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
+                               null,
+                               out this.pid);
+                               ChildWatch.add (this.pid, (pid, status) => {
+                                       // Triggered when the child indicated by child_pid exits
+                                       Process.close_pid (pid);
+                                        
+                               });
+                               
+                               return;
+
+               }
                Process.spawn_async_with_pipes (
                                this.cwd,
                                this.args,
-                               this.env,
+                               this.env.length > 0 ? this.env : null,
                                SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
                                null,
                                out this.pid,
@@ -159,7 +183,7 @@ public class Spawn : Object
                                out standard_error);
 
                // stdout:
-
+                
                        
                //print(JSON.stringify(gret));    
                 
@@ -202,7 +226,7 @@ public class Spawn : Object
                        this.tidyup();
                        //print("DONE TIDYUP");
                        
-                       finish(this.result, this.output, this.stderr);
+                       this.complete(this.result, this.output, this.stderr);
                        
                });
            
@@ -347,7 +371,7 @@ public class Spawn : Object
      */
     private bool read(IOChannel ch) 
     {
-        string prop = (ch == this.out_ch) ? "output" : "stderr";
+       // string prop = (ch == this.out_ch) ? "output" : "stderr";
        // print("prop: " + prop);
 
         
@@ -381,10 +405,11 @@ public class Spawn : Object
                         
                     } else {
                         this.stderr += buffer;
+                        this.output_line(  buffer); 
                     }
                     //_this[prop] += x.str_return;
                     //if (this.cfg.debug) {
-                        GLib.debug("%s : %s", prop , buffer);
+                        //GLib.debug("%s : %s", prop , buffer);
                     //}
                     if (this.is_async) {
                          
@@ -414,6 +439,7 @@ public class Spawn : Object
     }
     
 }
+/*
  
 int main (string[] args) {
        GLib.Log.set_handler(null, 
@@ -434,4 +460,4 @@ int main (string[] args) {
             
        return 0;
 }
+ */