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...
17 public errordomain ValaSourceError {
21 //public delegate void ValaSourceResult(Json.Object res);
23 public signal void compiled(Json.Object res);
26 public class ValaSource : Object {
31 Vala.CodeContext context;
34 public string build_module;
35 public string filepath;
36 public string original_filepath;
37 public int line_offset = 0;
39 // file.project , file.path, file.build_module, ""
47 string original_filepath) {
50 this.filepath = filepath;
51 this.build_module = build_module;
52 this.original_filepath = original_filepath;
53 this.project = project;
56 public void dumpCode(string str)
58 var ls = str.split("\n");
59 for (var i=0;i < ls.length; i++) {
60 print("%d : %s\n", i+1, ls[i]);
64 //public Gee.HashMap<int,string> checkFile()
66 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
69 public void checkFileWithNodePropChange(
73 JsRender.JsRender file,
78 ValaSourceResult result_cb)
83 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
84 var hash = ptype == "listener" ? node.listeners : node.props;
86 // untill we get a smarter renderer..
87 // we have some scenarios where changing the value does not work
88 if (prop == "* xns" || prop == "xtype") {
89 result_callback(new Json.Object());
94 var old = hash.get(prop);
95 var newval = "/*--VALACHECK-START--*/ " + val ;
97 hash.set(prop, newval);
98 var tmpstring = JsRender.NodeToVala.mungeFile(file);
100 //print("%s\n", tmpstring);
101 var bits = tmpstring.split("/*--VALACHECK-START--*/");
103 if (bits.length > 0) {
104 offset = bits[0].split("\n").length +1;
107 this.line_offset = offset;
109 //this.dumpCode(tmpstring);
110 //print("offset %d\n", offset);
111 this.result_callback = result_cb;
112 this.checkStringSpawn(tmpstring );
120 ValaSourceResult result_callback;
121 public void checkStringSpawn(
126 FileIOStream iostream;
127 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
130 OutputStream ostream = iostream.output_stream;
131 DataOutputStream dostream = new DataOutputStream (ostream);
132 dostream.put_string (contents);
136 args += BuilderApplication._self;
138 args += this.project.fn;
140 args += this.build_module;
141 args += "--add-file";
142 args += tmpfile.get_path();
143 args += "--skip-file";
144 args += this.filepath;
148 this.compiler = new Spawn("/tmp", args);
151 this.compiler.run(spawnResult);
152 } catch (GLib.SpawnError e) {
153 var ret = new Json.Object();
154 ret.set_boolean_member("success", false);
155 ret.set_string_member("message", e.message);
156 this.result_callback(ret);
160 public void spawnResult(int res, string output, string stderr)
165 GLib.debug("GOT output %s", output);
167 var pa = new Json.Parser();
168 pa.load_from_data(output);
169 var node = pa.get_root();
171 if (node.get_node_type () != Json.NodeType.OBJECT) {
172 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
174 var ret = node.get_object ();
175 ret.set_int_member("line_offset", this.line_offset);
176 if (result_callback == null) {
177 print ("no callback?");
180 this.result_callback(ret);
184 var ret = new Json.Object();
185 ret.set_boolean_member("success", false);
186 ret.set_string_member("message", e.message);
187 this.result_callback(ret);
198 int main (string[] args) {
200 var a = new ValaSource(file);
201 a.create_valac_tree();