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);
33 JsRender.JsRender file;
34 public int line_offset = 0;
41 public void dumpCode(string str)
43 var ls = str.split("\n");
44 for (var i=0;i < ls.length; i++) {
45 print("%d : %s\n", i+1, ls[i]);
49 //public Gee.HashMap<int,string> checkFile()
51 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
54 public bool checkFileWithNodePropChange(
56 JsRender.JsRender file,
65 if (this.compiler != null) {
70 var hash = ptype == "listener" ? node.listeners : node.props;
72 // untill we get a smarter renderer..
73 // we have some scenarios where changing the value does not work
74 if (prop == "* xns" || prop == "xtype") {
79 var old = hash.get(prop);
80 var newval = "/*--VALACHECK-START--*/ " + val ;
82 hash.set(prop, newval);
83 var tmpstring = JsRender.NodeToVala.mungeFile(file);
85 //print("%s\n", tmpstring);
86 var bits = tmpstring.split("/*--VALACHECK-START--*/");
88 if (bits.length > 0) {
89 offset = bits[0].split("\n").length +1;
92 this.line_offset = offset;
94 //this.dumpCode(tmpstring);
95 //print("offset %d\n", offset);
96 return this.checkStringSpawn(tmpstring );
105 public bool checkStringSpawn(
110 if (this.compiler != null) {
114 FileIOStream iostream;
115 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
118 OutputStream ostream = iostream.output_stream;
119 DataOutputStream dostream = new DataOutputStream (ostream);
120 dostream.put_string (contents);
124 var regex = new Regex("\\.bjs$");
126 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
127 } catch (GLib.RegexError e) {
133 args += BuilderApplication._self;
135 args += this.file.project.fn;
137 args += this.file.build_module;
138 args += "--add-file";
139 args += tmpfile.get_path();
140 args += "--skip-file";
145 this.compiler = new Spawn("/tmp", args);
146 this.compiler.complete.connect(spawnResult);
150 } catch (GLib.SpawnError e) {
151 GLib.debug(e.message);
152 this.compiler = null;
160 public bool checkFileSpawn(JsRender.JsRender file )
163 if (this.compiler != null) {
168 this.line_offset = 0;
171 args += BuilderApplication._self;
173 args += this.file.project.fn;
175 args += this.file.build_module;
181 this.compiler = new Spawn("/tmp", args);
182 this.compiler.complete.connect(spawnResult);
187 } catch (GLib.Error e) {
188 GLib.debug(e.message);
189 this.compiler = null;
196 public void spawnExecute(JsRender.JsRender file)
199 if (this.compiler != null) {
202 var pr = (Project.Gtk)(file.project);
206 this.line_offset = 0;
209 args += BuilderApplication._self;
211 args += this.file.project.fn;
213 if (this.file.build_module.length > 0 ) {
214 args += this.file.build_module;
216 args += pr.firstBuildModule();
225 this.compiler = new Spawn("/tmp", args);
226 this.compiler.complete.connect(spawnResult);
231 } catch (GLib.Error e) {
232 GLib.debug(e.message);
233 this.compiler = null;
242 * Used to compile a non builder file..
245 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
248 if (this.compiler != null) {
251 var pr = (Project.Gtk)(file.project);
253 var m = pr.firstBuildModule();
254 var cg = pr.compilegroups.get(m);
256 for (var i = 0; i < cg.sources.size; i++) {
257 var path = pr.resolve_path(
258 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
259 if (path == file.path) {
267 this.compiler = null;
269 return false; // do not run the compile..
271 // is the file in the module?
274 FileIOStream iostream;
275 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
278 OutputStream ostream = iostream.output_stream;
279 DataOutputStream dostream = new DataOutputStream (ostream);
280 dostream.put_string (contents);
284 this.line_offset = 0;
287 args += BuilderApplication._self;
289 args += file.project.fn;
291 args += pr.firstBuildModule();
292 args += "--add-file";
293 args += tmpfile.get_path();
294 args += "--skip-file";
301 this.compiler = new Spawn("/tmp", args);
302 this.compiler.complete.connect(spawnResult);
304 } catch (GLib.Error e) {
306 this.compiler = null;
314 public void spawnResult(int res, string output, string stderr)
319 //GLib.debug("GOT output %s", output);
321 var pa = new Json.Parser();
322 pa.load_from_data(output);
323 var node = pa.get_root();
325 if (node.get_node_type () != Json.NodeType.OBJECT) {
326 var ret = new Json.Object();
327 ret.set_boolean_member("success", false);
328 ret.set_string_member("message",
329 "Compiler returned Unexpected element type %s".printf(
334 this.compiler = null;
336 var ret = node.get_object ();
337 ret.set_int_member("line_offset", this.line_offset);
342 } catch (GLib.Error e) {
343 var ret = new Json.Object();
344 ret.set_boolean_member("success", false);
345 ret.set_string_member("message", e.message);
348 this.compiler = null;
359 int main (string[] args) {
361 var a = new ValaSource(file);
362 a.create_valac_tree();