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);
32 public Xcls_MainWindow window;
34 JsRender.JsRender file;
35 public int line_offset = 0;
37 public Gee.ArrayList<Spawn> children;
38 public ValaSource( Xcls_MainWindow window )
43 this.children = new Gee.ArrayList<Spawn>();
46 public void dumpCode(string str)
48 var ls = str.split("\n");
49 for (var i=0;i < ls.length; i++) {
50 print("%d : %s\n", i+1, ls[i]);
54 //public Gee.HashMap<int,string> checkFile()
56 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
59 public bool checkFileWithNodePropChange(
61 JsRender.JsRender file,
70 if (this.compiler != null) {
75 var hash = ptype == "listener" ? node.listeners : node.props;
77 // untill we get a smarter renderer..
78 // we have some scenarios where changing the value does not work
79 if (prop == "* xns" || prop == "xtype") {
84 var old = hash.get(prop);
85 var newval = "/*--VALACHECK-START--*/ " + val ;
87 hash.set(prop, newval);
88 var tmpstring = JsRender.NodeToVala.mungeFile(file);
90 //print("%s\n", tmpstring);
91 var bits = tmpstring.split("/*--VALACHECK-START--*/");
93 if (bits.length > 0) {
94 offset = bits[0].split("\n").length +1;
97 this.line_offset = offset;
99 //this.dumpCode(tmpstring);
100 //print("offset %d\n", offset);
101 return this.checkStringSpawn(tmpstring );
110 public bool checkStringSpawn(
115 if (this.compiler != null) {
119 FileIOStream iostream;
120 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
123 OutputStream ostream = iostream.output_stream;
124 DataOutputStream dostream = new DataOutputStream (ostream);
125 dostream.put_string (contents);
129 var regex = new Regex("\\.bjs$");
131 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
132 } catch (GLib.RegexError e) {
138 args += BuilderApplication._self;
140 args += this.file.project.fn;
142 args += this.file.build_module;
143 args += "--add-file";
144 args += tmpfile.get_path();
145 args += "--skip-file";
150 this.compiler = new Spawn("/tmp", args);
151 this.compiler.complete.connect(spawnResult);
152 this.window.statusbar_compile_spinner.el.start();
155 } catch (GLib.SpawnError e) {
156 GLib.debug(e.message);
157 this.compiler = null;
165 public bool checkFileSpawn(JsRender.JsRender file )
168 if (this.compiler != null) {
173 this.line_offset = 0;
176 args += BuilderApplication._self;
178 args += this.file.project.fn;
180 args += this.file.build_module;
186 this.compiler = new Spawn("/tmp", args);
187 this.compiler.complete.connect(spawnResult);
192 } catch (GLib.Error e) {
193 GLib.debug(e.message);
194 this.compiler = null;
205 public void spawnExecute(JsRender.JsRender file)
208 if (this.compiler != null) {
211 if (!(file.project is Project.Gtk)) {
214 var pr = (Project.Gtk)(file.project);
218 this.line_offset = 0;
221 args += BuilderApplication._self;
223 args += this.file.project.fn;
225 if (this.file.build_module.length > 0 ) {
226 args += this.file.build_module;
228 args += pr.firstBuildModule();
230 //args += "--output"; -- set up by the module -- defaults to testrun
231 //args += "/tmp/testrun";
233 // assume code is in home...
235 this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
236 this.compiler.output_line.connect(compile_output_line);
237 this.compiler.complete.connect(runResult);
239 this.children.add(this.compiler); //keep a reference...
241 } catch (GLib.Error e) {
242 GLib.debug(e.message);
243 this.compiler = null;
249 public void compile_output_line( string str )
251 this.compile_output(str);
254 * Used to compile a non builder file..
258 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
262 if (this.compiler != null) {
265 var pr = (Project.Gtk)(file.project);
267 var m = pr.firstBuildModule();
268 var cg = pr.compilegroups.get(m);
270 for (var i = 0; i < cg.sources.size; i++) {
271 var path = pr.resolve_path(
272 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
273 if (path == file.path) {
281 this.compiler = null;
283 return false; // do not run the compile..
285 // is the file in the module?
288 FileIOStream iostream;
289 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
292 OutputStream ostream = iostream.output_stream;
293 DataOutputStream dostream = new DataOutputStream (ostream);
294 dostream.put_string (contents);
296 var target = pr.firstBuildModule();
297 if (target.length < 1) {
302 this.line_offset = 0;
305 args += BuilderApplication._self;
307 args += file.project.fn;
310 args += pr.firstBuildModule();
311 args += "--add-file";
312 args += tmpfile.get_path();
313 args += "--skip-file";
320 this.compiler = new Spawn("/tmp", args);
321 this.compiler.complete.connect(spawnResult);
323 } catch (GLib.Error e) {
325 this.compiler = null;
333 public void spawnResult(int res, string output, string stderr)
338 //GLib.debug("GOT output %s", output);
340 var pa = new Json.Parser();
341 pa.load_from_data(output);
342 var node = pa.get_root();
344 if (node.get_node_type () != Json.NodeType.OBJECT) {
345 var ret = new Json.Object();
346 ret.set_boolean_member("success", false);
347 ret.set_string_member("message",
348 "Compiler returned Unexpected element type %s".printf(
353 this.compiler = null;
355 var ret = node.get_object ();
356 ret.set_int_member("line_offset", this.line_offset);
361 } catch (GLib.Error e) {
362 var ret = new Json.Object();
363 ret.set_boolean_member("success", false);
364 ret.set_string_member("message", e.message);
367 this.compiler = null;
375 public void runResult(int res, string output, string stderr)
377 this.compiler = null;
378 var exe = "/tmp/testrun";
380 var pr = (Project.Gtk)(this.file.project);
384 if (this.file.build_module.length > 0 ) {
385 mod = this.file.build_module;
387 mod = pr.firstBuildModule();
389 if (mod.length < 1) {
392 var cg = pr.compilegroups.get(mod);
393 if (cg.target_bin.length > 0) {
398 if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
399 print("Missing output file: %s\n",exe);
402 string[] args = "/usr/bin/gnome-terminal -x /usr/bin/gdb -ex=r --args".split(" ");
405 // runs gnome-terminal, with gdb .. running the application..
406 // fixme -- need a system/which
409 if (cg.execute_args.length > 0) {
410 var aa = cg.execute_args.split(" ");
411 for (var i =0; i < aa.length; i++) {
416 print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
418 // should be home directory...
422 var exec = new Spawn(GLib.Environment.get_home_dir() , args);
431 int main (string[] args) {
433 var a = new ValaSource(file);
434 a.create_valac_tree();