sync
[app.Builder.js] / src / Palete / ValaSource.vala
index e3c89b2..f8d05b2 100644 (file)
@@ -28,25 +28,19 @@ namespace Palete {
  
                
                public signal void compiled(Json.Object res);
+               public signal void compile_output(string str);
 
                
-               Vala.CodeContext context;
-                
-               Project.Gtk project;
-               public string build_module;
-               public string filepath;
-               public string original_filepath;
-               public int line_offset = 0;
-               
-               // file.project , file.path, file.build_module, ""
-               public ValaSource(
-                       
-               
+               JsRender.JsRender file;
+               public int line_offset = 0;
                
-                       ) {
+               public Gee.ArrayList<Spawn> children;
+               public ValaSource( ) 
+               {
                        base();
-                       //this.file = file;
-                
+                       this.compiler = null;
+                       this.children = new Gee.ArrayList<Spawn>();
+                       
                }
                public void dumpCode(string str) 
                {
@@ -61,30 +55,28 @@ namespace Palete {
                //      return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
                //}
 
-               public void checkFileWithNodePropChange(
-                
-                                 
+               public bool checkFileWithNodePropChange(
+                 
                                        JsRender.JsRender file,
                                        JsRender.Node node, 
                                        string prop,
                                        string ptype,
-                                       string val,
-                                       ValaSourceResult result_cb)
+                                       string val
+                                )
                {
+                       this.file = file;
+                       
+                       if (this.compiler != null) {
+                               return false;
+                       }
                        
-                       this.project = file.project;
-                       this.filepath = file.path;
-                       this.build_module = file.build_module;
-                       
-                       
-                       Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
+                        
                        var hash = ptype == "listener" ? node.listeners : node.props;
                        
                        // untill we get a smarter renderer..
                        // we have some scenarios where changing the value does not work
                        if (prop == "* xns" || prop == "xtype") {
-                               result_callback(new Json.Object());
-                               return ;
+                               return  false;
                        }
                                
                        
@@ -105,8 +97,7 @@ namespace Palete {
                        
                        //this.dumpCode(tmpstring);
                        //print("offset %d\n", offset);
-                       this.result_callback = result_cb;
-                       this.checkStringSpawn(tmpstring );
+                       return this.checkStringSpawn(tmpstring );
                        
                        // modify report
                        
@@ -114,12 +105,16 @@ namespace Palete {
                        
                }
                Spawn compiler;
-               ValaSourceResult result_callback;
-               public void checkStringSpawn(
-                                       string contents 
-                               )
+                
+               public bool checkStringSpawn(
+                               string contents 
+                       )
                {
                        
+                       if (this.compiler != null) {
+                               return false;
+                       }
+                       
                        FileIOStream iostream;
                        var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
                        tmpfile.ref();
@@ -128,67 +123,299 @@ namespace Palete {
                        DataOutputStream dostream = new DataOutputStream (ostream);
                        dostream.put_string (contents);
                        
+                       var valafn = "";
+                       try {             
+                          var  regex = new Regex("\\.bjs$");
+                       
+                               valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
+                        } catch (GLib.RegexError e) {
+                                
+                           return false;
+                       }   
                        
                        string[] args = {};
                        args += BuilderApplication._self;
                        args += "--project";
-                       args += this.project.fn;
+                       args += this.file.project.fn;
                        args += "--target";
-                       args += this.build_module;
+                       args += this.file.build_module;
                        args += "--add-file";
                        args +=  tmpfile.get_path();
                        args += "--skip-file";
-                       args += this.filepath;
+                       args += valafn;
                        
                         
                        
                        this.compiler = new Spawn("/tmp", args);
+                       this.compiler.complete.connect(spawnResult);
                        
                        try {
-                               this.compiler.run(spawnResult); 
+                               this.compiler.run(); 
                        } catch (GLib.SpawnError e) {
-                               var ret = new Json.Object();
-                               ret.set_boolean_member("success", false);
-                               ret.set_string_member("message", e.message);
-                               this.result_callback(ret);
+                               GLib.debug(e.message);
+                               this.compiler = null;
+                               return false;
+
+                       }
+                       return true;
+                        
+               }
+               
+               public bool checkFileSpawn(JsRender.JsRender file )
+               {
+                       // race condition..
+                       if (this.compiler != null) { 
+                               return false;
+                       }
+                       
+                       this.file = file;
+                       this.line_offset = 0;
+                         
+                       string[] args = {};
+                       args += BuilderApplication._self;
+                       args += "--project";
+                       args += this.file.project.fn;
+                       args += "--target";
+                       args += this.file.build_module;
+                        
+                        
+                       
+                       
+                       try {
+                           this.compiler = new Spawn("/tmp", args);
+                           this.compiler.complete.connect(spawnResult);
+                       
+                           this.compiler.run(); 
+                       
+                        
+                       } catch (GLib.Error e) {
+                           GLib.debug(e.message);
+                           this.compiler = null;
+                           return false;
+                       }
+                       return true;
+                        
+               }
+               
+
+               
+               
+               public void spawnExecute(JsRender.JsRender file)
+               {
+                       // race condition..
+                       if (this.compiler != null) { 
+                               return;
+                       }
+                       if (!(file.project is Project.Gtk)) {
+                           return;
+                       }
+                       var pr = (Project.Gtk)(file.project);
+                       
+                       
+                       this.file = file;
+                       this.line_offset = 0;
+                         
+                       string[] args = {};
+                       args += BuilderApplication._self;
+                       args += "--project";
+                       args += this.file.project.fn;
+                       args += "--target";
+                       if (this.file.build_module.length > 0 ) {
+                           args += this.file.build_module;
+                       } else {
+                           args += pr.firstBuildModule();
+                       }
+                       //args += "--output"; -- set up by the module -- defaults to testrun
+                       //args += "/tmp/testrun";
+                       
+                       // assume code is in home...
+                       try {
+                           this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
+                           this.compiler.output_line.connect(compile_output_line);
+                           this.compiler.complete.connect(runResult);
+                           this.compiler.run(); 
+                               this.children.add(this.compiler); //keep a reference...
+                        
+                       } catch (GLib.Error e) {
+                           GLib.debug(e.message);
+                           this.compiler = null;
+
+                       }
+                       return;
+                        
+               }
+               public void compile_output_line(   string str )
+               {
+                       this.compile_output(str);
+               }
+               /**
+               * Used to compile a non builder file..
+               */
+                
+               public bool checkPlainFileSpawn(  JsRender.JsRender file, string contents )
+               {
+                       // race condition..
+                       if (this.compiler != null) { 
+                               return false;
+                       }
+                       var pr = (Project.Gtk)(file.project);
+                       
+                       var m = pr.firstBuildModule();
+                       var cg = pr.compilegroups.get(m);
+                       var foundit = false;
+                       for (var i = 0; i < cg.sources.size; i++) {
+                           var path = pr.resolve_path(
+                                   pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
+                           if (path == file.path) {
+                               foundit = true;
+                               break;
+                                       }
+                       
+                       }
+                       if (!foundit) {
+                         
+                           this.compiler = null;
+                       
+                           return false; // do not run the compile..
+                       }
+                       // is the file in the module?
+                       
+                       
+                       FileIOStream iostream;
+                       var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
+                       tmpfile.ref();
+
+                       OutputStream ostream = iostream.output_stream;
+                       DataOutputStream dostream = new DataOutputStream (ostream);
+                       dostream.put_string (contents);
+                       
+                       
+                       this.file = null;
+                       this.line_offset = 0;
+                         
+                       string[] args = {};
+                       args += BuilderApplication._self;
+                       args += "--project";
+                       args +=  file.project.fn;
+                       args += "--target";
+                       args += pr.firstBuildModule();
+                       args += "--add-file";
+                       args +=  tmpfile.get_path();
+                       args += "--skip-file";
+                       args += file.path;
+                        
+                       
+                       
+                       
+                       try {
+                           this.compiler = new Spawn("/tmp", args);
+                           this.compiler.complete.connect(spawnResult);
+                           this.compiler.run(); 
+                       } catch (GLib.Error e) {
+                           
+                           this.compiler = null;
+                           return false;
                        }
+                       return true;
                         
                }
+               
+               
                public void spawnResult(int res, string output, string stderr)
                {
                         
                                
                        try { 
-                               GLib.debug("GOT output %s", output);
+                               //GLib.debug("GOT output %s", output);
                                
                                var pa = new Json.Parser();
                                pa.load_from_data(output);
                                var node = pa.get_root();
 
                                if (node.get_node_type () != Json.NodeType.OBJECT) {
-                                       throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
+                                       var ret = new Json.Object();
+                                       ret.set_boolean_member("success", false);
+                                       ret.set_string_member("message", 
+                                               "Compiler returned Unexpected element type %s".printf( 
+                                                       node.type_name ()
+                                               )
+                                       );
+                                       this.compiled(ret);
+                                       this.compiler = null;
                                }
                                var ret = node.get_object ();
                                ret.set_int_member("line_offset", this.line_offset);
-                               if (result_callback == null) {
-                                       print ("no callback?");
-                                       return;
-                               }
-                               this.result_callback(ret);
                                
+                               this.compiled(ret);
                                
-                       } catch (Error e) {
+                               
+                       } catch (GLib.Error e) {
                                var ret = new Json.Object();
                                ret.set_boolean_member("success", false);
                                ret.set_string_member("message", e.message);
-                               this.result_callback(ret);
+                               this.compiled(ret);
                        }
+                       this.compiler = null;
                        //compiler.unref();
                        //tmpfile.unref();
                         
                        
                        
                }
+               
+               public void runResult(int res, string output, string stderr)
+               {
+                       this.compiler = null;
+                       var exe = "/tmp/testrun";
+                       var mod = "";
+                       var pr = (Project.Gtk)(this.file.project);
+                       
+                       
+                       
+                       if (this.file.build_module.length > 0 ) {
+                   mod =  this.file.build_module;
+                       } else {
+                           mod =  pr.firstBuildModule();
+                       }
+                       if (mod.length < 1) {
+                               return;
+                       }
+                       var cg =  pr.compilegroups.get(mod);
+                       if (cg.target_bin.length > 0) {
+                               exe = cg.target_bin;
+                       }
+                       
+                       
+                       if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
+                               print("Missing output file: %s\n",exe);
+                               return;
+                       }
+                       string[] args = "/usr/bin/gnome-terminal -x /usr/bin/gdb -ex=r --args".split(" ");
+
+                       
+                       // runs gnome-terminal, with gdb .. running the application..
+                       // fixme -- need a system/which
+                       
+                       args += exe;
+                       if (cg.execute_args.length > 0) {
+                               var aa = cg.execute_args.split(" ");
+                               for (var i =0; i < aa.length; i++) {
+                                       args += aa[i];
+                               }
+                       }
+
+                   print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
+                   
+                   // should be home directory...
+                   
+                   
+                   
+            var exec = new Spawn(GLib.Environment.get_home_dir() , args);
+            exec.detach = true;
+                   exec.run(); 
+                       
+               }
+       }
                 
 }
 /*