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;
198 * Used to compile a non builder file..
201 public void checkPlainFileSpawn( JsRender.JsRender file, string contents )
204 if (this.compiler != null) {
207 var pr = (Project.Gtk)(file.project);
209 var m = pr.firstBuildModule();
210 var cg = pr.compilegroups.get(m);
212 for (var i = 0; i < cg.sources.size; i++) {
213 var path = pr.resolve_path(
214 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
215 if (path == file.path) {
222 var ret = new Json.Object();
223 ret.set_boolean_member("success", true);
224 ret.set_string_member("message", "no need to compile");
226 this.compiler = null;
228 return; // do not run the compile..
230 // is the file in the module?
233 FileIOStream iostream;
234 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
237 OutputStream ostream = iostream.output_stream;
238 DataOutputStream dostream = new DataOutputStream (ostream);
239 dostream.put_string (contents);
243 this.line_offset = 0;
246 args += BuilderApplication._self;
248 args += file.project.fn;
250 args += pr.firstBuildModule();
251 args += "--add-file";
252 args += tmpfile.get_path();
253 args += "--skip-file";
257 this.compiler = new Spawn("/tmp", args);
258 this.compiler.complete.connect(spawnResult);
262 } catch (GLib.SpawnError e) {
263 var ret = new Json.Object();
264 ret.set_boolean_member("success", false);
265 ret.set_string_member("message", e.message);
267 this.compiler = null;
273 public void spawnResult(int res, string output, string stderr)
278 GLib.debug("GOT output %s", output);
280 var pa = new Json.Parser();
281 pa.load_from_data(output);
282 var node = pa.get_root();
284 if (node.get_node_type () != Json.NodeType.OBJECT) {
285 var ret = new Json.Object();
286 ret.set_boolean_member("success", false);
287 ret.set_string_member("message",
288 "Compiler returned Unexpected element type %s".printf(
293 this.compiler = null;
295 var ret = node.get_object ();
296 ret.set_int_member("line_offset", this.line_offset);
302 var ret = new Json.Object();
303 ret.set_boolean_member("success", false);
304 ret.set_string_member("message", e.message);
307 this.compiler = null;
318 int main (string[] args) {
320 var a = new ValaSource(file);
321 a.create_valac_tree();