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 Vala.CodeContext context;
36 public string build_module;
37 public string filepath;
38 public string original_filepath;
39 public int line_offset = 0;
41 // file.project , file.path, file.build_module, ""
51 public void dumpCode(string str)
53 var ls = str.split("\n");
54 for (var i=0;i < ls.length; i++) {
55 print("%d : %s\n", i+1, ls[i]);
59 //public Gee.HashMap<int,string> checkFile()
61 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
64 public void checkFileWithNodePropChange(
67 JsRender.JsRender file,
72 ValaSourceResult result_cb)
75 this.project = file.project;
76 this.filepath = file.path;
77 this.build_module = file.build_module;
80 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
81 var hash = ptype == "listener" ? node.listeners : node.props;
83 // untill we get a smarter renderer..
84 // we have some scenarios where changing the value does not work
85 if (prop == "* xns" || prop == "xtype") {
86 result_callback(new Json.Object());
91 var old = hash.get(prop);
92 var newval = "/*--VALACHECK-START--*/ " + val ;
94 hash.set(prop, newval);
95 var tmpstring = JsRender.NodeToVala.mungeFile(file);
97 //print("%s\n", tmpstring);
98 var bits = tmpstring.split("/*--VALACHECK-START--*/");
100 if (bits.length > 0) {
101 offset = bits[0].split("\n").length +1;
104 this.line_offset = offset;
106 //this.dumpCode(tmpstring);
107 //print("offset %d\n", offset);
108 this.result_callback = result_cb;
109 this.checkStringSpawn(tmpstring );
117 ValaSourceResult result_callback;
118 public void checkStringSpawn(
123 FileIOStream iostream;
124 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
127 OutputStream ostream = iostream.output_stream;
128 DataOutputStream dostream = new DataOutputStream (ostream);
129 dostream.put_string (contents);
133 args += BuilderApplication._self;
135 args += this.project.fn;
137 args += this.build_module;
138 args += "--add-file";
139 args += tmpfile.get_path();
140 args += "--skip-file";
141 args += this.filepath;
145 this.compiler = new Spawn("/tmp", args);
148 this.compiler.run(spawnResult);
149 } catch (GLib.SpawnError e) {
150 var ret = new Json.Object();
151 ret.set_boolean_member("success", false);
152 ret.set_string_member("message", e.message);
153 this.result_callback(ret);
157 public void spawnResult(int res, string output, string stderr)
162 GLib.debug("GOT output %s", output);
164 var pa = new Json.Parser();
165 pa.load_from_data(output);
166 var node = pa.get_root();
168 if (node.get_node_type () != Json.NodeType.OBJECT) {
169 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
171 var ret = node.get_object ();
172 ret.set_int_member("line_offset", this.line_offset);
173 if (result_callback == null) {
174 print ("no callback?");
177 this.result_callback(ret);
181 var ret = new Json.Object();
182 ret.set_boolean_member("success", false);
183 ret.set_string_member("message", e.message);
184 this.result_callback(ret);
195 int main (string[] args) {
197 var a = new ValaSource(file);
198 a.create_valac_tree();