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 void checkFileWithNodePropChange(
56 JsRender.JsRender file,
66 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
67 var hash = ptype == "listener" ? node.listeners : node.props;
69 // untill we get a smarter renderer..
70 // we have some scenarios where changing the value does not work
71 if (prop == "* xns" || prop == "xtype") {
72 this.compiled(new Json.Object());
77 var old = hash.get(prop);
78 var newval = "/*--VALACHECK-START--*/ " + val ;
80 hash.set(prop, newval);
81 var tmpstring = JsRender.NodeToVala.mungeFile(file);
83 //print("%s\n", tmpstring);
84 var bits = tmpstring.split("/*--VALACHECK-START--*/");
86 if (bits.length > 0) {
87 offset = bits[0].split("\n").length +1;
90 this.line_offset = offset;
92 //this.dumpCode(tmpstring);
93 //print("offset %d\n", offset);
94 this.checkStringSpawn(tmpstring );
103 public void checkStringSpawn(
108 FileIOStream iostream;
109 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
112 OutputStream ostream = iostream.output_stream;
113 DataOutputStream dostream = new DataOutputStream (ostream);
114 dostream.put_string (contents);
118 var regex = new Regex("\\.bjs$");
120 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
121 } catch (GLib.RegexError e) {
122 var ret = new Json.Object();
123 ret.set_boolean_member("success", false);
124 ret.set_string_member("message", e.message);
130 args += BuilderApplication._self;
132 args += this.file.project.fn;
134 args += this.file.build_module;
135 args += "--add-file";
136 args += tmpfile.get_path();
137 args += "--skip-file";
142 this.compiler = new Spawn("/tmp", args);
143 this.compiler.complete.connect(spawnResult);
147 } catch (GLib.SpawnError e) {
148 var ret = new Json.Object();
149 ret.set_boolean_member("success", false);
150 ret.set_string_member("message", e.message);
152 this.compiler = null;
157 public void checkFileSpawn(JsRender.JsRender file )
160 if (this.compiler != null) {
165 this.line_offset = 0;
168 args += BuilderApplication._self;
170 args += this.file.project.fn;
172 args += this.file.build_module;
176 this.compiler = new Spawn("/tmp", args);
177 this.compiler.complete.connect(spawnResult);
181 } catch (GLib.SpawnError e) {
182 var ret = new Json.Object();
183 ret.set_boolean_member("success", false);
184 ret.set_string_member("message", e.message);
186 this.compiler = null;
194 public void spawnResult(int res, string output, string stderr)
199 GLib.debug("GOT output %s", output);
201 var pa = new Json.Parser();
202 pa.load_from_data(output);
203 var node = pa.get_root();
205 if (node.get_node_type () != Json.NodeType.OBJECT) {
206 var ret = new Json.Object();
207 ret.set_boolean_member("success", false);
208 ret.set_string_member("message", "Compiler returned Unexpected element type %s", node.type_name ());
210 this.compiler = null;
212 var ret = node.get_object ();
213 ret.set_int_member("line_offset", this.line_offset);
219 var ret = new Json.Object();
220 ret.set_boolean_member("success", false);
221 ret.set_string_member("message", e.message);
224 this.compiler = null;
235 int main (string[] args) {
237 var a = new ValaSource(file);
238 a.create_valac_tree();