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,
65 if (this.compiler != null) {
69 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
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") {
75 this.compiled(new Json.Object());
80 var old = hash.get(prop);
81 var newval = "/*--VALACHECK-START--*/ " + val ;
83 hash.set(prop, newval);
84 var tmpstring = JsRender.NodeToVala.mungeFile(file);
86 //print("%s\n", tmpstring);
87 var bits = tmpstring.split("/*--VALACHECK-START--*/");
89 if (bits.length > 0) {
90 offset = bits[0].split("\n").length +1;
93 this.line_offset = offset;
95 //this.dumpCode(tmpstring);
96 //print("offset %d\n", offset);
97 this.checkStringSpawn(tmpstring );
106 public void checkStringSpawn(
111 if (this.compiler != null) {
115 FileIOStream iostream;
116 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
119 OutputStream ostream = iostream.output_stream;
120 DataOutputStream dostream = new DataOutputStream (ostream);
121 dostream.put_string (contents);
125 var regex = new Regex("\\.bjs$");
127 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
128 } catch (GLib.RegexError e) {
129 var ret = new Json.Object();
130 ret.set_boolean_member("success", false);
131 ret.set_string_member("message", e.message);
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 var ret = new Json.Object();
156 ret.set_boolean_member("success", false);
157 ret.set_string_member("message", e.message);
159 this.compiler = null;
164 public void 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;
183 this.compiler = new Spawn("/tmp", args);
184 this.compiler.complete.connect(spawnResult);
188 } catch (GLib.SpawnError e) {
189 var ret = new Json.Object();
190 ret.set_boolean_member("success", false);
191 ret.set_string_member("message", e.message);
193 this.compiler = null;
199 public void checkFileSpawn(JsRender.JsRender file )
202 if (this.compiler != null) {
207 this.line_offset = 0;
210 args += BuilderApplication._self;
212 args += this.file.project.fn;
214 args += this.file.build_module;
218 this.compiler = new Spawn("/tmp", args);
219 this.compiler.complete.connect(spawnResult);
223 } catch (GLib.SpawnError e) {
224 var ret = new Json.Object();
225 ret.set_boolean_member("success", false);
226 ret.set_string_member("message", e.message);
228 this.compiler = null;
232 public void checkFilePlainSpawn(Project.Project proj, string filename, string contents )
235 if (this.compiler != null) {
240 this.line_offset = 0;
243 args += BuilderApplication._self;
245 args += this.file.project.fn;
247 args += this.file.build_module;
251 this.compiler = new Spawn("/tmp", args);
252 this.compiler.complete.connect(spawnResult);
256 } catch (GLib.SpawnError e) {
257 var ret = new Json.Object();
258 ret.set_boolean_member("success", false);
259 ret.set_string_member("message", e.message);
261 this.compiler = null;
267 public void spawnResult(int res, string output, string stderr)
272 GLib.debug("GOT output %s", output);
274 var pa = new Json.Parser();
275 pa.load_from_data(output);
276 var node = pa.get_root();
278 if (node.get_node_type () != Json.NodeType.OBJECT) {
279 var ret = new Json.Object();
280 ret.set_boolean_member("success", false);
281 ret.set_string_member("message",
282 "Compiler returned Unexpected element type %s".printf(
287 this.compiler = null;
289 var ret = node.get_object ();
290 ret.set_int_member("line_offset", this.line_offset);
296 var ret = new Json.Object();
297 ret.set_boolean_member("success", false);
298 ret.set_string_member("message", e.message);
301 this.compiler = null;
312 int main (string[] args) {
314 var a = new ValaSource(file);
315 a.create_valac_tree();