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;
38 // file.project , file.path, file.build_module, ""
48 public void dumpCode(string str)
50 var ls = str.split("\n");
51 for (var i=0;i < ls.length; i++) {
52 print("%d : %s\n", i+1, ls[i]);
56 //public Gee.HashMap<int,string> checkFile()
58 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
61 public void checkFileWithNodePropChange(
63 JsRender.JsRender file,
68 ValaSourceResult result_cb)
71 this.project = file.project;
72 this.filepath = file.path;
73 this.build_module = file.build_module;
76 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
77 var hash = ptype == "listener" ? node.listeners : node.props;
79 // untill we get a smarter renderer..
80 // we have some scenarios where changing the value does not work
81 if (prop == "* xns" || prop == "xtype") {
82 result_callback(new Json.Object());
87 var old = hash.get(prop);
88 var newval = "/*--VALACHECK-START--*/ " + val ;
90 hash.set(prop, newval);
91 var tmpstring = JsRender.NodeToVala.mungeFile(file);
93 //print("%s\n", tmpstring);
94 var bits = tmpstring.split("/*--VALACHECK-START--*/");
96 if (bits.length > 0) {
97 offset = bits[0].split("\n").length +1;
100 this.line_offset = offset;
102 //this.dumpCode(tmpstring);
103 //print("offset %d\n", offset);
104 this.result_callback = result_cb;
105 this.checkStringSpawn(tmpstring );
113 ValaSourceResult result_callback;
114 public void checkStringSpawn(
119 FileIOStream iostream;
120 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
123 OutputStream ostream = iostream.output_stream;
124 DataOutputStream dostream = new DataOutputStream (ostream);
125 dostream.put_string (contents);
129 args += BuilderApplication._self;
131 args += this.project.fn;
133 args += this.build_module;
134 args += "--add-file";
135 args += tmpfile.get_path();
136 args += "--skip-file";
137 args += this.filepath;
141 this.compiler = new Spawn("/tmp", args);
144 this.compiler.run(spawnResult);
145 } catch (GLib.SpawnError e) {
146 var ret = new Json.Object();
147 ret.set_boolean_member("success", false);
148 ret.set_string_member("message", e.message);
149 this.result_callback(ret);
153 public void spawnResult(int res, string output, string stderr)
158 GLib.debug("GOT output %s", output);
160 var pa = new Json.Parser();
161 pa.load_from_data(output);
162 var node = pa.get_root();
164 if (node.get_node_type () != Json.NodeType.OBJECT) {
165 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
167 var ret = node.get_object ();
168 ret.set_int_member("line_offset", this.line_offset);
169 if (result_callback == null) {
170 print ("no callback?");
173 this.result_callback(ret);
177 var ret = new Json.Object();
178 ret.set_boolean_member("success", false);
179 ret.set_string_member("message", e.message);
180 this.result_callback(ret);
191 int main (string[] args) {
193 var a = new ValaSource(file);
194 a.create_valac_tree();