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);
35 JsRender.JsRender file;
36 public int line_offset = 0;
43 public void dumpCode(string str)
45 var ls = str.split("\n");
46 for (var i=0;i < ls.length; i++) {
47 print("%d : %s\n", i+1, ls[i]);
51 //public Gee.HashMap<int,string> checkFile()
53 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
56 public void checkFileWithNodePropChange(
58 JsRender.JsRender file,
63 ValaSourceResult result_cb)
66 this.project = file.project;
67 this.filepath = file.path;
68 this.build_module = file.build_module;
71 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
72 var hash = ptype == "listener" ? node.listeners : node.props;
74 // untill we get a smarter renderer..
75 // we have some scenarios where changing the value does not work
76 if (prop == "* xns" || prop == "xtype") {
77 result_callback(new Json.Object());
82 var old = hash.get(prop);
83 var newval = "/*--VALACHECK-START--*/ " + val ;
85 hash.set(prop, newval);
86 var tmpstring = JsRender.NodeToVala.mungeFile(file);
88 //print("%s\n", tmpstring);
89 var bits = tmpstring.split("/*--VALACHECK-START--*/");
91 if (bits.length > 0) {
92 offset = bits[0].split("\n").length +1;
95 this.line_offset = offset;
97 //this.dumpCode(tmpstring);
98 //print("offset %d\n", offset);
99 this.result_callback = result_cb;
100 this.checkStringSpawn(tmpstring );
108 ValaSourceResult result_callback;
109 public void checkStringSpawn(
114 FileIOStream iostream;
115 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
118 OutputStream ostream = iostream.output_stream;
119 DataOutputStream dostream = new DataOutputStream (ostream);
120 dostream.put_string (contents);
124 args += BuilderApplication._self;
126 args += this.project.fn;
128 args += this.build_module;
129 args += "--add-file";
130 args += tmpfile.get_path();
131 args += "--skip-file";
132 args += this.filepath;
136 this.compiler = new Spawn("/tmp", args);
139 this.compiler.run(spawnResult);
140 } catch (GLib.SpawnError e) {
141 var ret = new Json.Object();
142 ret.set_boolean_member("success", false);
143 ret.set_string_member("message", e.message);
144 this.result_callback(ret);
148 public void spawnResult(int res, string output, string stderr)
153 GLib.debug("GOT output %s", output);
155 var pa = new Json.Parser();
156 pa.load_from_data(output);
157 var node = pa.get_root();
159 if (node.get_node_type () != Json.NodeType.OBJECT) {
160 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
162 var ret = node.get_object ();
163 ret.set_int_member("line_offset", this.line_offset);
164 if (result_callback == null) {
165 print ("no callback?");
168 this.result_callback(ret);
172 var ret = new Json.Object();
173 ret.set_boolean_member("success", false);
174 ret.set_string_member("message", e.message);
175 this.result_callback(ret);
186 int main (string[] args) {
188 var a = new ValaSource(file);
189 a.create_valac_tree();