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 project, string filename, string contents )
235 if (this.compiler != null) {
239 FileIOStream iostream;
240 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
243 OutputStream ostream = iostream.output_stream;
244 DataOutputStream dostream = new DataOutputStream (ostream);
245 dostream.put_string (contents);
250 this.line_offset = 0;
253 args += BuilderApplication._self;
257 args += project.firstBuildModule();
258 args += "--add-file";
259 args += tmpfile.get_path();
260 args += "--skip-file";
265 this.compiler = new Spawn("/tmp", args);
266 this.compiler.complete.connect(spawnResult);
270 } catch (GLib.SpawnError e) {
271 var ret = new Json.Object();
272 ret.set_boolean_member("success", false);
273 ret.set_string_member("message", e.message);
275 this.compiler = null;
281 public void spawnResult(int res, string output, string stderr)
286 GLib.debug("GOT output %s", output);
288 var pa = new Json.Parser();
289 pa.load_from_data(output);
290 var node = pa.get_root();
292 if (node.get_node_type () != Json.NodeType.OBJECT) {
293 var ret = new Json.Object();
294 ret.set_boolean_member("success", false);
295 ret.set_string_member("message",
296 "Compiler returned Unexpected element type %s".printf(
301 this.compiler = null;
303 var ret = node.get_object ();
304 ret.set_int_member("line_offset", this.line_offset);
310 var ret = new Json.Object();
311 ret.set_boolean_member("success", false);
312 ret.set_string_member("message", e.message);
315 this.compiler = null;
326 int main (string[] args) {
328 var a = new ValaSource(file);
329 a.create_valac_tree();