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,
64 this.project = file.project;
65 this.filepath = file.path;
66 this.build_module = file.build_module;
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 result_callback(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.result_callback = result_cb;
98 this.checkStringSpawn(tmpstring );
106 ValaSourceResult result_callback;
107 public void checkStringSpawn(
112 FileIOStream iostream;
113 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
116 OutputStream ostream = iostream.output_stream;
117 DataOutputStream dostream = new DataOutputStream (ostream);
118 dostream.put_string (contents);
122 args += BuilderApplication._self;
124 args += this.project.fn;
126 args += this.build_module;
127 args += "--add-file";
128 args += tmpfile.get_path();
129 args += "--skip-file";
130 args += this.filepath;
134 this.compiler = new Spawn("/tmp", args);
137 this.compiler.run(spawnResult);
138 } catch (GLib.SpawnError e) {
139 var ret = new Json.Object();
140 ret.set_boolean_member("success", false);
141 ret.set_string_member("message", e.message);
142 this.result_callback(ret);
146 public void spawnResult(int res, string output, string stderr)
151 GLib.debug("GOT output %s", output);
153 var pa = new Json.Parser();
154 pa.load_from_data(output);
155 var node = pa.get_root();
157 if (node.get_node_type () != Json.NodeType.OBJECT) {
158 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
160 var ret = node.get_object ();
161 ret.set_int_member("line_offset", this.line_offset);
162 if (result_callback == null) {
163 print ("no callback?");
166 this.result_callback(ret);
170 var ret = new Json.Object();
171 ret.set_boolean_member("success", false);
172 ret.set_string_member("message", e.message);
173 this.result_callback(ret);
184 int main (string[] args) {
186 var a = new ValaSource(file);
187 a.create_valac_tree();