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...
14 public errordomain ValaSourceError {
18 public delegate void ValaSourceResult(Json.Object res);
22 public class ValaSource : Object {
25 Vala.CodeContext context;
28 public string build_module;
29 public string filepath;
30 public string original_filepath;
31 public int line_offset = 0;
33 // file.project , file.path, file.build_module, ""
41 string original_filepath) {
44 this.filepath = filepath;
45 this.build_module = build_module;
46 this.original_filepath = original_filepath;
47 this.project = project;
50 public void dumpCode(string str)
52 var ls = str.split("\n");
53 for (var i=0;i < ls.length; i++) {
54 print("%d : %s\n", i+1, ls[i]);
58 //public Gee.HashMap<int,string> checkFile()
60 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
63 public void checkFileWithNodePropChange(
64 JsRender.JsRender file,
69 ValaSourceResult result_cb)
74 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
75 var hash = ptype == "listener" ? node.listeners : node.props;
77 // untill we get a smarter renderer..
78 // we have some scenarios where changing the value does not work
79 if (prop == "* xns" || prop == "xtype") {
80 result_callback(new Json.Object());
85 var old = hash.get(prop);
86 var newval = "/*--VALACHECK-START--*/ " + val ;
88 hash.set(prop, newval);
89 var tmpstring = JsRender.NodeToVala.mungeFile(file);
91 //print("%s\n", tmpstring);
92 var bits = tmpstring.split("/*--VALACHECK-START--*/");
94 if (bits.length > 0) {
95 offset = bits[0].split("\n").length +1;
98 this.line_offset = offset;
100 //this.dumpCode(tmpstring);
101 //print("offset %d\n", offset);
102 this.result_callback = result_cb;
103 this.checkStringSpawn(tmpstring );
111 ValaSourceResult result_callback;
112 public void checkStringSpawn(
117 FileIOStream iostream;
118 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
121 OutputStream ostream = iostream.output_stream;
122 DataOutputStream dostream = new DataOutputStream (ostream);
123 dostream.put_string (contents);
127 args += BuilderApplication._self;
129 args += this.project.fn;
131 args += this.build_module;
132 args += "--add-file";
133 args += tmpfile.get_path();
134 args += "--skip-file";
135 args += this.filepath;
139 this.compiler = new Spawn("/tmp", args);
142 this.compiler.run(spawnResult);
143 } catch (GLib.SpawnError e) {
144 var ret = new Json.Object();
145 ret.set_boolean_member("success", false);
146 ret.set_string_member("message", e.message);
147 this.result_callback(ret);
151 public void spawnResult(int res, string output, string stderr)
156 GLib.debug("GOT output %s", output);
158 var pa = new Json.Parser();
159 pa.load_from_data(output);
160 var node = pa.get_root();
162 if (node.get_node_type () != Json.NodeType.OBJECT) {
163 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
165 var ret = node.get_object ();
166 ret.set_int_member("line_offset", this.line_offset);
167 if (result_callback == null) {
168 print ("no callback?");
171 this.result_callback(ret);
175 var ret = new Json.Object();
176 ret.set_boolean_member("success", false);
177 ret.set_string_member("message", e.message);
178 this.result_callback(ret);
189 int main (string[] args) {
191 var a = new ValaSource(file);
192 a.create_valac_tree();