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... );
19 public errordomain ValaSourceError {
23 //public delegate void ValaSourceResult(Json.Object res);
27 public class ValaSource : Object {
30 public signal void compiled(Json.Object res);
31 public signal void compile_output(string str);
34 JsRender.JsRender file;
35 public int line_offset = 0;
37 public Gee.ArrayList<Spawn> children;
42 this.children = new Gee.ArrayList<Spawn>();
45 public void dumpCode(string str)
47 var ls = str.split("\n");
48 for (var i=0;i < ls.length; i++) {
49 print("%d : %s\n", i+1, ls[i]);
53 //public Gee.HashMap<int,string> checkFile()
55 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
58 public bool checkFileWithNodePropChange(
60 JsRender.JsRender file,
69 if (this.compiler != null) {
74 var hash = ptype == "listener" ? node.listeners : node.props;
76 // untill we get a smarter renderer..
77 // we have some scenarios where changing the value does not work
78 if (prop == "* xns" || prop == "xtype") {
83 var old = hash.get(prop);
84 var newval = "/*--VALACHECK-START--*/ " + val ;
86 hash.set(prop, newval);
87 var tmpstring = JsRender.NodeToVala.mungeFile(file);
89 //print("%s\n", tmpstring);
90 var bits = tmpstring.split("/*--VALACHECK-START--*/");
92 if (bits.length > 0) {
93 offset = bits[0].split("\n").length +1;
96 this.line_offset = offset;
98 //this.dumpCode(tmpstring);
99 //print("offset %d\n", offset);
100 return this.checkStringSpawn(tmpstring );
109 public bool checkStringSpawn(
114 if (this.compiler != null) {
118 FileIOStream iostream;
119 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
122 OutputStream ostream = iostream.output_stream;
123 DataOutputStream dostream = new DataOutputStream (ostream);
124 dostream.put_string (contents);
128 var regex = new Regex("\\.bjs$");
130 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
131 } catch (GLib.RegexError e) {
137 args += BuilderApplication._self;
139 args += this.file.project.fn;
141 args += this.file.build_module;
142 args += "--add-file";
143 args += tmpfile.get_path();
144 args += "--skip-file";
149 this.compiler = new Spawn("/tmp", args);
150 this.compiler.complete.connect(spawnResult);
154 } catch (GLib.SpawnError e) {
155 GLib.debug(e.message);
156 this.compiler = null;
164 public bool checkFileSpawn(JsRender.JsRender file )
167 if (this.compiler != null) {
172 this.line_offset = 0;
175 args += BuilderApplication._self;
177 args += this.file.project.fn;
179 args += this.file.build_module;
185 this.compiler = new Spawn("/tmp", args);
186 this.compiler.complete.connect(spawnResult);
191 } catch (GLib.Error e) {
192 GLib.debug(e.message);
193 this.compiler = null;
204 public void spawnExecute(JsRender.JsRender file)
207 if (this.compiler != null) {
210 if (!(file.project is Project.Gtk)) {
213 var pr = (Project.Gtk)(file.project);
217 this.line_offset = 0;
220 args += BuilderApplication._self;
222 args += this.file.project.fn;
224 if (this.file.build_module.length > 0 ) {
225 args += this.file.build_module;
227 args += pr.firstBuildModule();
229 //args += "--output"; -- set up by the module -- defaults to testrun
230 //args += "/tmp/testrun";
232 // assume code is in home...
234 this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
235 this.compiler.output_line.connect(compile_output_line);
236 this.compiler.complete.connect(runResult);
238 this.children.add(this.compiler); //keep a reference...
240 } catch (GLib.Error e) {
241 GLib.debug(e.message);
242 this.compiler = null;
248 public void compile_output_line( string str )
250 this.compile_output(str);
253 * Used to compile a non builder file..
257 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
261 if (this.compiler != null) {
264 var pr = (Project.Gtk)(file.project);
266 var m = pr.firstBuildModule();
267 var cg = pr.compilegroups.get(m);
269 for (var i = 0; i < cg.sources.size; i++) {
270 var path = pr.resolve_path(
271 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
272 if (path == file.path) {
280 this.compiler = null;
282 return false; // do not run the compile..
284 // is the file in the module?
287 FileIOStream iostream;
288 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
291 OutputStream ostream = iostream.output_stream;
292 DataOutputStream dostream = new DataOutputStream (ostream);
293 dostream.put_string (contents);
295 var target = pr.firstBuildModule();
296 if (target.length < 1) {
301 this.line_offset = 0;
304 args += BuilderApplication._self;
306 args += file.project.fn;
309 args += pr.firstBuildModule();
310 args += "--add-file";
311 args += tmpfile.get_path();
312 args += "--skip-file";
319 this.compiler = new Spawn("/tmp", args);
320 this.compiler.complete.connect(spawnResult);
322 } catch (GLib.Error e) {
324 this.compiler = null;
332 public void spawnResult(int res, string output, string stderr)
337 //GLib.debug("GOT output %s", output);
339 var pa = new Json.Parser();
340 pa.load_from_data(output);
341 var node = pa.get_root();
343 if (node.get_node_type () != Json.NodeType.OBJECT) {
344 var ret = new Json.Object();
345 ret.set_boolean_member("success", false);
346 ret.set_string_member("message",
347 "Compiler returned Unexpected element type %s".printf(
352 this.compiler = null;
354 var ret = node.get_object ();
355 ret.set_int_member("line_offset", this.line_offset);
360 } catch (GLib.Error e) {
361 var ret = new Json.Object();
362 ret.set_boolean_member("success", false);
363 ret.set_string_member("message", e.message);
366 this.compiler = null;
374 public void runResult(int res, string output, string stderr)
376 this.compiler = null;
377 var exe = "/tmp/testrun";
379 var pr = (Project.Gtk)(this.file.project);
383 if (this.file.build_module.length > 0 ) {
384 mod = this.file.build_module;
386 mod = pr.firstBuildModule();
388 if (mod.length < 1) {
391 var cg = pr.compilegroups.get(mod);
392 if (cg.target_bin.length > 0) {
397 if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
398 print("Missing output file: %s\n",exe);
401 string[] args = "/usr/bin/gnome-terminal -x /usr/bin/gdb -ex=r --args".split(" ");
404 // runs gnome-terminal, with gdb .. running the application..
405 // fixme -- need a system/which
408 if (cg.execute_args.length > 0) {
409 var aa = cg.execute_args.split(" ");
410 for (var i =0; i < aa.length; i++) {
415 print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
417 // should be home directory...
421 var exec = new Spawn(GLib.Environment.get_home_dir() , args);
430 int main (string[] args) {
432 var a = new ValaSource(file);
433 a.create_valac_tree();