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.start();
155 } catch (GLib.SpawnError e) {
156 GLib.debug(e.message);
157 this.window.statusbar_compile_spinner.stop();
158 this.compiler = null;
166 public bool checkFileSpawn(JsRender.JsRender file )
169 if (this.compiler != null) {
174 this.line_offset = 0;
177 args += BuilderApplication._self;
179 args += this.file.project.fn;
181 args += this.file.build_module;
187 this.compiler = new Spawn("/tmp", args);
188 this.compiler.complete.connect(spawnResult);
189 this.window.statusbar_compile_spinner.start();
193 } catch (GLib.Error e) {
194 GLib.debug(e.message);
195 this.window.statusbar_compile_spinner.stop();
196 this.compiler = null;
207 public void spawnExecute(JsRender.JsRender file)
210 if (this.compiler != null) {
213 if (!(file.project is Project.Gtk)) {
216 var pr = (Project.Gtk)(file.project);
220 this.line_offset = 0;
223 args += BuilderApplication._self;
225 args += this.file.project.fn;
227 if (this.file.build_module.length > 0 ) {
228 args += this.file.build_module;
230 args += pr.firstBuildModule();
232 //args += "--output"; -- set up by the module -- defaults to testrun
233 //args += "/tmp/testrun";
235 // assume code is in home...
237 this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
238 this.compiler.output_line.connect(compile_output_line);
239 this.compiler.complete.connect(runResult);
240 this.window.statusbar_compile_spinner.start();
242 this.children.add(this.compiler); //keep a reference...
244 } catch (GLib.Error e) {
245 this.window.statusbar_compile_spinner.stop();
246 GLib.debug(e.message);
247 this.compiler = null;
253 public void compile_output_line( string str )
255 this.compile_output(str);
258 * Used to compile a non builder file..
262 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
266 if (this.compiler != null) {
270 var pr = (Project.Gtk)(file.project);
272 var m = pr.firstBuildModule();
273 var cg = pr.compilegroups.get(m);
280 if (cg.sources is null) {
283 for (var i = 0; i < cg.sources.size; i++) {
284 var path = pr.resolve_path(
285 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
286 if (path == file.path) {
294 this.compiler = null;
296 return false; // do not run the compile..
298 // is the file in the module?
301 FileIOStream iostream;
302 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
305 OutputStream ostream = iostream.output_stream;
306 DataOutputStream dostream = new DataOutputStream (ostream);
307 dostream.put_string (contents);
309 var target = pr.firstBuildModule();
310 if (target.length < 1) {
315 this.line_offset = 0;
318 args += BuilderApplication._self;
320 args += file.project.fn;
323 args += pr.firstBuildModule();
324 args += "--add-file";
325 args += tmpfile.get_path();
326 args += "--skip-file";
333 this.compiler = new Spawn("/tmp", args);
334 this.compiler.complete.connect(spawnResult);
335 this.window.statusbar_compile_spinner.start();
337 } catch (GLib.Error e) {
338 this.window.statusbar_compile_spinner.stop();
339 this.compiler = null;
347 public void spawnResult(int res, string output, string stderr)
350 this.window.statusbar_compile_spinner.stop();
352 //GLib.debug("GOT output %s", output);
354 var pa = new Json.Parser();
355 pa.load_from_data(output);
356 var node = pa.get_root();
358 if (node.get_node_type () != Json.NodeType.OBJECT) {
359 var ret = new Json.Object();
360 ret.set_boolean_member("success", false);
361 ret.set_string_member("message",
362 "Compiler returned Unexpected element type %s".printf(
367 this.compiler = null;
369 var ret = node.get_object ();
370 ret.set_int_member("line_offset", this.line_offset);
375 } catch (GLib.Error e) {
376 var ret = new Json.Object();
377 ret.set_boolean_member("success", false);
378 ret.set_string_member("message", e.message);
381 this.compiler = null;
389 public void runResult(int res, string output, string stderr)
391 this.compiler = null;
392 var exe = "/tmp/testrun";
394 var pr = (Project.Gtk)(this.file.project);
398 if (this.file.build_module.length > 0 ) {
399 mod = this.file.build_module;
401 mod = pr.firstBuildModule();
403 if (mod.length < 1) {
406 var cg = pr.compilegroups.get(mod);
407 if (cg.target_bin.length > 0) {
412 if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
413 print("Missing output file: %s\n",exe);
416 string[] args = "/usr/bin/gnome-terminal -x /usr/bin/gdb -ex=r --args".split(" ");
419 // runs gnome-terminal, with gdb .. running the application..
420 // fixme -- need a system/which
423 if (cg.execute_args.length > 0) {
424 var aa = cg.execute_args.split(" ");
425 for (var i =0; i < aa.length; i++) {
430 print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
432 // should be home directory...
436 var exec = new Spawn(GLib.Environment.get_home_dir() , args);
445 int main (string[] args) {
447 var a = new ValaSource(file);
448 a.create_valac_tree();