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);
31 public signal void compile_output(string str);
34 JsRender.JsRender file;
35 public int line_offset = 0;
42 public void dumpCode(string str)
44 var ls = str.split("\n");
45 for (var i=0;i < ls.length; i++) {
46 print("%d : %s\n", i+1, ls[i]);
50 //public Gee.HashMap<int,string> checkFile()
52 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
55 public bool checkFileWithNodePropChange(
57 JsRender.JsRender file,
66 if (this.compiler != null) {
71 var hash = ptype == "listener" ? node.listeners : node.props;
73 // untill we get a smarter renderer..
74 // we have some scenarios where changing the value does not work
75 if (prop == "* xns" || prop == "xtype") {
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 return this.checkStringSpawn(tmpstring );
106 public bool 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) {
134 args += BuilderApplication._self;
136 args += this.file.project.fn;
138 args += this.file.build_module;
139 args += "--add-file";
140 args += tmpfile.get_path();
141 args += "--skip-file";
146 this.compiler = new Spawn("/tmp", args);
147 this.compiler.complete.connect(spawnResult);
151 } catch (GLib.SpawnError e) {
152 GLib.debug(e.message);
153 this.compiler = null;
161 public bool checkFileSpawn(JsRender.JsRender file )
164 if (this.compiler != null) {
169 this.line_offset = 0;
172 args += BuilderApplication._self;
174 args += this.file.project.fn;
176 args += this.file.build_module;
182 this.compiler = new Spawn("/tmp", args);
183 this.compiler.complete.connect(spawnResult);
188 } catch (GLib.Error e) {
189 GLib.debug(e.message);
190 this.compiler = null;
197 public void spawnExecute(JsRender.JsRender file)
200 if (this.compiler != null) {
203 if (!(file.project is Project.Gtk)) {
206 var pr = (Project.Gtk)(file.project);
210 this.line_offset = 0;
213 args += BuilderApplication._self;
215 args += this.file.project.fn;
217 if (this.file.build_module.length > 0 ) {
218 args += this.file.build_module;
220 args += pr.firstBuildModule();
223 args += "/tmp/testrun";
225 // assume code is in home...
227 this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
228 this.compiler.output_line.connect(compile_output_line);
229 this.compiler.complete.connect(runResult);
233 } catch (GLib.Error e) {
234 GLib.debug(e.message);
235 this.compiler = null;
241 public void compile_output_line( string str )
243 this.compile_output(str);
246 * Used to compile a non builder file..
249 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
252 if (this.compiler != null) {
255 var pr = (Project.Gtk)(file.project);
257 var m = pr.firstBuildModule();
258 var cg = pr.compilegroups.get(m);
260 for (var i = 0; i < cg.sources.size; i++) {
261 var path = pr.resolve_path(
262 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
263 if (path == file.path) {
271 this.compiler = null;
273 return false; // do not run the compile..
275 // is the file in the module?
278 FileIOStream iostream;
279 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
282 OutputStream ostream = iostream.output_stream;
283 DataOutputStream dostream = new DataOutputStream (ostream);
284 dostream.put_string (contents);
288 this.line_offset = 0;
291 args += BuilderApplication._self;
293 args += file.project.fn;
295 args += pr.firstBuildModule();
296 args += "--add-file";
297 args += tmpfile.get_path();
298 args += "--skip-file";
305 this.compiler = new Spawn("/tmp", args);
306 this.compiler.complete.connect(spawnResult);
308 } catch (GLib.Error e) {
310 this.compiler = null;
318 public void spawnResult(int res, string output, string stderr)
323 //GLib.debug("GOT output %s", output);
325 var pa = new Json.Parser();
326 pa.load_from_data(output);
327 var node = pa.get_root();
329 if (node.get_node_type () != Json.NodeType.OBJECT) {
330 var ret = new Json.Object();
331 ret.set_boolean_member("success", false);
332 ret.set_string_member("message",
333 "Compiler returned Unexpected element type %s".printf(
338 this.compiler = null;
340 var ret = node.get_object ();
341 ret.set_int_member("line_offset", this.line_offset);
346 } catch (GLib.Error e) {
347 var ret = new Json.Object();
348 ret.set_boolean_member("success", false);
349 ret.set_string_member("message", e.message);
352 this.compiler = null;
360 public void runResult(int res, string output, string stderr)
362 this.compiler = null;
363 print("OUT: %s\n\n----\nERR:%s", output, stderr);
364 var exec = new Spawn("/tmp", { "/tmp/testrun" });
372 int main (string[] args) {
374 var a = new ValaSource(file);
375 a.create_valac_tree();