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,
63 JsRender.NodeProp prop,
69 if (this.compiler != null) {
74 var hash = prop.ptype == JsRender.NodePropType.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.name == "xns" || prop.name == "xtype") {
86 prop.val = "/*--VALACHECK-START--*/ " + prop.val ;
89 var tmpstring = JsRender.NodeToVala.mungeFile(file);
92 //print("%s\n", tmpstring);
93 var bits = tmpstring.split("/*--VALACHECK-START--*/");
95 if (bits.length > 0) {
96 offset = bits[0].split("\n").length +1;
99 this.line_offset = offset;
101 //this.dumpCode(tmpstring);
102 //print("offset %d\n", offset);
103 return this.checkStringSpawn(tmpstring );
112 public bool checkStringSpawn(
117 if (this.compiler != null) {
121 FileIOStream iostream;
122 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
125 OutputStream ostream = iostream.output_stream;
126 DataOutputStream dostream = new DataOutputStream (ostream);
127 dostream.put_string (contents);
131 var regex = new Regex("\\.bjs$");
133 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
134 } catch (GLib.RegexError e) {
140 args += BuilderApplication._self;
142 args += this.file.project.fn;
144 args += this.file.build_module;
145 args += "--add-file";
146 args += tmpfile.get_path();
147 args += "--skip-file";
152 this.compiler = new Spawn("/tmp", args);
153 this.compiler.complete.connect(spawnResult);
154 this.window.statusbar_compile_spinner.start();
157 } catch (GLib.SpawnError e) {
158 GLib.debug(e.message);
159 this.window.statusbar_compile_spinner.stop();
160 this.compiler = null;
168 public bool checkFileSpawn(JsRender.JsRender file )
171 if (this.compiler != null) {
176 this.line_offset = 0;
179 args += BuilderApplication._self;
181 args += this.file.project.fn;
183 args += this.file.build_module;
189 this.compiler = new Spawn("/tmp", args);
190 this.compiler.complete.connect(spawnResult);
191 this.window.statusbar_compile_spinner.start();
195 } catch (GLib.Error e) {
196 GLib.debug(e.message);
197 this.window.statusbar_compile_spinner.stop();
198 this.compiler = null;
209 public void spawnExecute(JsRender.JsRender file)
212 if (this.compiler != null) {
215 if (!(file.project is Project.Gtk)) {
218 var pr = (Project.Gtk)(file.project);
222 this.line_offset = 0;
225 args += BuilderApplication._self;
227 args += this.file.project.fn;
229 if (this.file.build_module.length > 0 ) {
230 args += this.file.build_module;
232 args += pr.firstBuildModule();
234 //args += "--output"; -- set up by the module -- defaults to testrun
235 //args += "/tmp/testrun";
237 // assume code is in home...
239 this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
240 this.compiler.output_line.connect(compile_output_line);
241 this.compiler.complete.connect(runResult);
242 this.window.statusbar_compile_spinner.start();
244 this.children.add(this.compiler); //keep a reference...
246 } catch (GLib.Error e) {
247 this.window.statusbar_compile_spinner.stop();
248 GLib.debug(e.message);
249 this.compiler = null;
255 public void compile_output_line( string str )
257 this.compile_output(str);
260 * Used to compile a non builder file..
264 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
268 if (this.compiler != null) {
272 var pr = (Project.Gtk)(file.project);
274 var m = pr.firstBuildModule();
275 var cg = pr.compilegroups.get(m);
282 if (cg.sources == null) {
285 for (var i = 0; i < cg.sources.size; i++) {
286 var path = pr.resolve_path(
287 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
288 if (path == file.path) {
296 this.compiler = null;
298 return false; // do not run the compile..
300 // is the file in the module?
303 FileIOStream iostream;
304 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
307 OutputStream ostream = iostream.output_stream;
308 DataOutputStream dostream = new DataOutputStream (ostream);
309 dostream.put_string (contents);
311 var target = pr.firstBuildModule();
312 if (target.length < 1) {
317 this.line_offset = 0;
320 args += BuilderApplication._self;
322 args += file.project.fn;
325 args += pr.firstBuildModule();
326 args += "--add-file";
327 args += tmpfile.get_path();
328 args += "--skip-file";
335 this.compiler = new Spawn("/tmp", args);
336 this.compiler.complete.connect(spawnResult);
337 this.window.statusbar_compile_spinner.start();
339 } catch (GLib.Error e) {
340 this.window.statusbar_compile_spinner.stop();
341 this.compiler = null;
349 public void spawnResult(int res, string output, string stderr)
352 this.window.statusbar_compile_spinner.stop();
354 //GLib.debug("GOT output %s", output);
356 var pa = new Json.Parser();
357 pa.load_from_data(output);
358 var node = pa.get_root();
360 if (node.get_node_type () != Json.NodeType.OBJECT) {
361 var ret = new Json.Object();
362 ret.set_boolean_member("success", false);
363 ret.set_string_member("message",
364 "Compiler returned Unexpected element type %s".printf(
369 this.compiler = null;
371 var ret = node.get_object ();
372 ret.set_int_member("line_offset", this.line_offset);
377 } catch (GLib.Error e) {
378 var ret = new Json.Object();
379 ret.set_boolean_member("success", false);
380 ret.set_string_member("message", e.message);
383 this.compiler = null;
391 public void runResult(int res, string output, string stderr)
393 this.compiler = null;
394 var exe = "/tmp/testrun";
396 var pr = (Project.Gtk)(this.file.project);
400 if (this.file.build_module.length > 0 ) {
401 mod = this.file.build_module;
403 mod = pr.firstBuildModule();
405 if (mod.length < 1) {
408 var cg = pr.compilegroups.get(mod);
409 if (cg.target_bin.length > 0) {
414 if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
415 print("Missing output file: %s\n",exe);
418 string[] args = "/usr/bin/gnome-terminal -x /usr/bin/gdb -ex=r --args".split(" ");
421 // runs gnome-terminal, with gdb .. running the application..
422 // fixme -- need a system/which
425 if (cg.execute_args.length > 0) {
426 var aa = cg.execute_args.split(" ");
427 for (var i =0; i < aa.length; i++) {
432 print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
434 // should be home directory...
438 var exec = new Spawn(GLib.Environment.get_home_dir() , args);
447 int main (string[] args) {
449 var a = new ValaSource(file);
450 a.create_valac_tree();