2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
6 * This just deals with spawning the compiler and getting the results.
8 * each window should have one of these...
10 * x = new ValaSource();
11 * x.connect.compiled(... do something with results... );
14 THIS IS NOT USED ?? - replaced with valacompilerequest
20 public errordomain ValaSourceError {
24 //public delegate void ValaSourceResult(Json.Object res);
28 public class ValaSourceOLD : Object {
30 public Json.Object? last_result = null;
31 public signal void compiled(Json.Object res);
32 public signal void compile_output(string str);
35 public JsRender.JsRender file;
36 public int line_offset = 0;
38 public Gee.ArrayList<Spawn> children;
41 public string tmpfile_path = "";
44 public int terminal_pid = 0;
46 public ValaSourceOLD( )
51 this.children = new Gee.ArrayList<Spawn>();
54 public void dumpCode(string str)
56 var ls = str.split("\n");
57 for (var i=0;i < ls.length; i++) {
58 print("%d : %s\n", i+1, ls[i]);
62 //public Gee.HashMap<int,string> checkFile()
64 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
67 public bool checkFileWithNodePropChange(
69 JsRender.JsRender file,
71 JsRender.NodeProp prop,
77 if (this.compiler != null) {
78 //this.compiler.tidyup();
79 //this.spawnResult(0,"","");
84 // untill we get a smarter renderer..
85 // we have some scenarios where changing the value does not work
86 if (prop.name == "xns" || prop.name == "xtype") {
94 prop.val = "/*--VALACHECK-START--*/ " + prop.val ;
97 var tmpstring = JsRender.NodeToVala.mungeFile(file);
100 //print("%s\n", tmpstring);
101 var bits = tmpstring.split("/*--VALACHECK-START--*/");
103 if (bits.length > 0) {
104 offset = bits[0].split("\n").length +1;
107 this.line_offset = offset;
109 //this.dumpCode(tmpstring);
110 //print("offset %d\n", offset);
111 return this.checkStringSpawn(tmpstring );
120 private bool checkStringSpawn( string contents )
123 if (this.compiler != null) {
124 this.compiler.isZombie();
125 //this.compiler.tidyup();
126 //this.spawnResult(-2,"","");
129 var pr = (Project.Gtk)(file.project);
131 var tmpfilename = pr.path + "/build/tmp-%u.vala".printf( (uint) GLib.get_real_time()) ;
133 GLib.FileUtils.set_contents(tmpfilename,contents);
134 } catch (GLib.FileError e) {
135 GLib.debug("Error creating temp build file %s : %s", tmpfilename, e.message);
143 var regex = new Regex("\\.bjs$");
145 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
146 } catch (GLib.RegexError e) {
154 args += BuilderApplication._self;
155 args += "--skip-linking";
157 args += this.file.project.path;
159 args += pr.firstBuildModuleWith(this.file);
160 args += "--add-file";
162 args += "--skip-file";
165 this.tmpfile_path = tmpfilename;
167 this.compiler = new Spawn(pr.path + "/build", args);
168 } catch (GLib.Error e) {
169 GLib.debug("Spawn failed: %s", e.message);
172 this.compiler.complete.connect(spawnResult);
173 BuilderApplication.showSpinner("spinner", "compiling file");
176 } catch (GLib.Error e) {
177 GLib.debug("Error %s",e.message);
178 BuilderApplication.showSpinner("face-sad", "compile failed %s".printf(e.message));
179 this.compiler = null;
191 public bool checkFileSpawn(JsRender.JsRender file )
194 if (this.compiler != null) {
195 this.compiler.isZombie();
196 //this.compiler.tidyup();
197 //this.spawnResult(-2,"","");
202 var pr = (Project.Gtk)(file.project);
203 this.line_offset = 0;
206 args += BuilderApplication._self;
207 args += "--skip-linking";
209 args += this.file.project.path;
211 args += pr.firstBuildModuleWith(this.file);
218 this.compiler = new Spawn(pr.path+"/build", args);
219 this.compiler.output_line.connect(this.compile_output_line);
220 this.compiler.complete.connect(spawnResult);
225 } catch (GLib.Error e) {
226 GLib.debug(e.message);
228 this.compiler = null;
239 public void spawnExecute(JsRender.JsRender file)
242 if (this.compiler != null) {
243 this.compiler.isZombie();
244 this.compiler.tidyup();
245 this.spawnResult(-2,"","");
248 if (!(file.project is Project.Gtk)) {
251 var pr = (Project.Gtk)(file.project);
255 this.line_offset = 0;
260 args += BuilderApplication._self;
265 args += this.file.project.path;
267 args += pr.firstBuildModuleWith(this.file);
269 //args += "--output"; -- set up by the module -- defaults to testrun
270 //args += "/tmp/testrun";
272 // assume code is in home...
274 this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
275 this.compiler.output_line.connect(this.compile_output_line);
276 this.compiler.complete.connect(runResult);
279 this.children.add(this.compiler); //keep a reference...
281 } catch (GLib.Error e) {
283 GLib.debug(e.message);
284 this.compiler = null;
290 public void compile_output_line( string str )
292 GLib.debug("%s", str);
293 this.compile_output(str);
296 * Used to compile a non builder file..
300 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
304 if (this.compiler != null) {
305 this.compiler.isZombie();
310 var pr = (Project.Gtk)(file.project);
312 var m = pr.firstBuildModuleWith(file);
313 var cg = pr.compilegroups.get(m);
320 if (cg.sources == null) {
323 for (var i = 0; i < cg.sources.size; i++) {
324 var path = pr.path + "/" + cg.sources.get(i);
325 if (path == file.path) {
333 this.compiler = null;
335 return false; // do not run the compile..
337 // is the file in the module?
339 var tmpfilename = pr.path + "/build/tmp-%u.vala".printf( (uint) GLib.get_real_time()) ;
341 GLib.FileUtils.set_contents(tmpfilename,contents);
342 } catch (GLib.FileError e) {
343 GLib.debug("Error creating temp build file %s : %s", tmpfilename, e.message);
347 var target = pr.firstBuildModule();
348 if (target.length < 1) {
352 // this.file = null; << /why
353 this.line_offset = 0;
356 args += BuilderApplication._self;
357 args += "--skip-linking";
359 args += file.project.path;
362 args += pr.firstBuildModuleWith(this.file);
363 args += "--add-file";
365 args += "--skip-file";
368 this.tmpfile_path = tmpfilename;
372 this.compiler = new Spawn("/tmp", args);
373 this.compiler.output_line.connect(this.compile_output_line);
374 this.compiler.complete.connect(spawnResult);
377 } catch (GLib.Error e) {
379 this.compiler = null;
389 public void deleteTemp()
391 if (this.tmpfile_path == "") {
394 if (GLib.FileUtils.test(this.tmpfile_path, GLib.FileTest.EXISTS)) {
395 GLib.FileUtils.unlink(this.tmpfile_path);
397 var cf = this.tmpfile_path.substring(0, this.tmpfile_path.length-4) + "c";
398 GLib.debug("try remove %s",cf);
399 if (GLib.FileUtils.test(cf, GLib.FileTest.EXISTS)) {
400 GLib.FileUtils.unlink(cf);
402 var ccf = GLib.Path.get_dirname(cf) + "/build/" + GLib.Path.get_basename(cf);
403 GLib.debug("try remove %s",ccf);
404 if (GLib.FileUtils.test(ccf, GLib.FileTest.EXISTS)) {
405 GLib.FileUtils.unlink(ccf);
407 this.tmpfile_path = "";
409 // update the compiler results into the lists.
412 // what to do when we have finished running..
413 // call this.compiled(result) (handled by windowstate?)
414 public void spawnResult(int res, string output, string stderr)
418 var ret = new Json.Object();
419 ret.set_boolean_member("success", false);
420 ret.set_string_member("message","killed");
422 this.compiler.isZombie();
423 this.compiler = null;
429 //GLib.debug("GOT output %s", output);
431 var pa = new Json.Parser();
432 pa.load_from_data(output);
433 var node = pa.get_root();
435 if (node.get_node_type () != Json.NodeType.OBJECT) {
436 var ret = new Json.Object();
437 ret.set_boolean_member("success", false);
438 ret.set_string_member("message",
439 "Compiler returned Unexpected element type %s".printf(
444 this.compiler = null;
446 var ret = node.get_object ();
447 ret.set_int_member("line_offset", this.line_offset);
448 this.last_result = ret;
452 } catch (GLib.Error e) {
453 var ret = new Json.Object();
454 ret.set_boolean_member("success", false);
455 ret.set_string_member("message", e.message);
458 this.compiler.isZombie();
459 this.compiler = null;
469 public void killChildren(int pid)
474 var cn = "/proc/%d/task/%d/children".printf(pid,pid);
475 if (!FileUtils.test(cn, GLib.FileTest.EXISTS)) {
476 GLib.debug("%s doesnt exist - killing %d", cn, pid);
482 FileUtils.get_contents(cn, out cpids);
485 if (cpids.length > 0) {
486 this.killChildren(int.parse(cpids));
489 } catch (GLib.FileError e) {
492 GLib.debug("killing %d", pid);
497 public void runResult(int res, string output, string stderr)
499 this.compiler = null;
502 GLib.debug("run result last pid = %d", this.terminal_pid );
504 this.killChildren(this.terminal_pid);
505 this.terminal_pid = 0;
512 var pr = (Project.Gtk)(this.file.project);
516 if (this.file.build_module.length > 0 ) {
517 mod = this.file.build_module;
519 mod = pr.firstBuildModule();
521 if (mod.length < 1) {
522 GLib.debug("missing compilegroup module");
525 var cg = pr.compilegroups.get(mod);
526 var exe = pr.path + "/build/" + cg.name;
530 if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
531 print("Missing output file: %s\n",exe);
534 var gdb_cfg= pr.path + "/build/.gdb-script";
535 if (!GLib.FileUtils.test(gdb_cfg, GLib.FileTest.EXISTS)) {
536 pr.writeFile("build/.gdb-script", "set debuginfod enabled off\nr");
541 string[] args = "/usr/bin/gnome-terminal --disable-factory --wait -- /usr/bin/gdb -x".split(" ");
542 //string[] args = "/usr/bin/xterm -e /usr/bin/gdb -x".split(" ");
547 // runs gnome-terminal, with gdb .. running the application..
548 // fixme -- need a system/which
551 if (cg.execute_args.length > 0) {
553 var aa = cg.execute_args.split(" ");
554 for (var i =0; i < aa.length; i++) {
559 print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
561 // should be home directory...
566 var exec = new Spawn(pr.path , args);
567 exec.env = GLib.Environ.get();
569 "PATH=" + GLib.Environment.get_variable("PATH"),
570 "SHELL=" + GLib.Environment.get_variable("SHELL"),
571 "DISPLAY=" + GLib.Environment.get_variable("DISPLAY"),
573 "USER=" + GLib.Environment.get_variable("USER"),
574 "DBUS_SESSION_BUS_ADDRESS="+ GLib.Environment.get_variable("DBUS_SESSION_BUS_ADDRESS"),
575 "XDG_SESSION_PATH="+ GLib.Environment.get_variable("XDG_SESSION_PATH"),
576 "SESSION_MANAGER="+ GLib.Environment.get_variable("SESSION_MANAGER"),
577 "XDG_SESSION_CLASS="+ GLib.Environment.get_variable("XDG_SESSION_CLASS"),
578 "XDG_SESSION_DESKTOP="+ GLib.Environment.get_variable("XDG_SESSION_DESKTOP"),
579 "XDG_SESSION_TYPE="+ GLib.Environment.get_variable("XDG_SESSION_TYPE")
585 this.terminal_pid = exec.pid;
586 GLib.debug("Child PID = %d", this.terminal_pid);
588 } catch(GLib.Error e) {
589 GLib.debug("Failed to spawn: %s", e.message);
598 int main (string[] args) {
600 var a = new ValaSource(file);
601 a.create_valac_tree();