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;
203 public void spawnExecute(JsRender.JsRender file)
206 if (this.compiler != null) {
209 if (!(file.project is Project.Gtk)) {
212 var pr = (Project.Gtk)(file.project);
216 this.line_offset = 0;
219 args += BuilderApplication._self;
221 args += this.file.project.fn;
223 if (this.file.build_module.length > 0 ) {
224 args += this.file.build_module;
226 args += pr.firstBuildModule();
229 args += "/tmp/testrun";
231 // assume code is in home...
233 this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
234 this.compiler.output_line.connect(compile_output_line);
235 this.compiler.complete.connect(runResult);
237 this.children.add(this.compiler); //keep a reference...
239 } catch (GLib.Error e) {
240 GLib.debug(e.message);
241 this.compiler = null;
247 public void compile_output_line( string str )
249 this.compile_output(str);
252 * Used to compile a non builder file..
255 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
258 if (this.compiler != null) {
261 var pr = (Project.Gtk)(file.project);
263 var m = pr.firstBuildModule();
264 var cg = pr.compilegroups.get(m);
266 for (var i = 0; i < cg.sources.size; i++) {
267 var path = pr.resolve_path(
268 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
269 if (path == file.path) {
277 this.compiler = null;
279 return false; // do not run the compile..
281 // is the file in the module?
284 FileIOStream iostream;
285 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
288 OutputStream ostream = iostream.output_stream;
289 DataOutputStream dostream = new DataOutputStream (ostream);
290 dostream.put_string (contents);
294 this.line_offset = 0;
297 args += BuilderApplication._self;
299 args += file.project.fn;
301 args += pr.firstBuildModule();
302 args += "--add-file";
303 args += tmpfile.get_path();
304 args += "--skip-file";
311 this.compiler = new Spawn("/tmp", args);
312 this.compiler.complete.connect(spawnResult);
314 } catch (GLib.Error e) {
316 this.compiler = null;
324 public void spawnResult(int res, string output, string stderr)
329 //GLib.debug("GOT output %s", output);
331 var pa = new Json.Parser();
332 pa.load_from_data(output);
333 var node = pa.get_root();
335 if (node.get_node_type () != Json.NodeType.OBJECT) {
336 var ret = new Json.Object();
337 ret.set_boolean_member("success", false);
338 ret.set_string_member("message",
339 "Compiler returned Unexpected element type %s".printf(
344 this.compiler = null;
346 var ret = node.get_object ();
347 ret.set_int_member("line_offset", this.line_offset);
352 } catch (GLib.Error e) {
353 var ret = new Json.Object();
354 ret.set_boolean_member("success", false);
355 ret.set_string_member("message", e.message);
358 this.compiler = null;
366 public void runResult(int res, string output, string stderr)
368 this.compiler = null;
369 print("OUT: %s\n\n----\nERR:%s", output, stderr);
370 var exec = new Spawn("/tmp", { "/tmp/testrun" });
379 int main (string[] args) {
381 var a = new ValaSource(file);
382 a.create_valac_tree();