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 JsRender.JsRender file;
37 public string build_module;
38 public string 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(
66 JsRender.JsRender file,
71 ValaSourceResult result_cb)
74 this.project = file.project;
75 this.filepath = file.path;
76 this.build_module = file.build_module;
79 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
80 var hash = ptype == "listener" ? node.listeners : node.props;
82 // untill we get a smarter renderer..
83 // we have some scenarios where changing the value does not work
84 if (prop == "* xns" || prop == "xtype") {
85 result_callback(new Json.Object());
90 var old = hash.get(prop);
91 var newval = "/*--VALACHECK-START--*/ " + val ;
93 hash.set(prop, newval);
94 var tmpstring = JsRender.NodeToVala.mungeFile(file);
96 //print("%s\n", tmpstring);
97 var bits = tmpstring.split("/*--VALACHECK-START--*/");
99 if (bits.length > 0) {
100 offset = bits[0].split("\n").length +1;
103 this.line_offset = offset;
105 //this.dumpCode(tmpstring);
106 //print("offset %d\n", offset);
107 this.result_callback = result_cb;
108 this.checkStringSpawn(tmpstring );
116 ValaSourceResult result_callback;
117 public void checkStringSpawn(
122 FileIOStream iostream;
123 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
126 OutputStream ostream = iostream.output_stream;
127 DataOutputStream dostream = new DataOutputStream (ostream);
128 dostream.put_string (contents);
132 args += BuilderApplication._self;
134 args += this.project.fn;
136 args += this.build_module;
137 args += "--add-file";
138 args += tmpfile.get_path();
139 args += "--skip-file";
140 args += this.filepath;
144 this.compiler = new Spawn("/tmp", args);
147 this.compiler.run(spawnResult);
148 } catch (GLib.SpawnError e) {
149 var ret = new Json.Object();
150 ret.set_boolean_member("success", false);
151 ret.set_string_member("message", e.message);
152 this.result_callback(ret);
156 public void spawnResult(int res, string output, string stderr)
161 GLib.debug("GOT output %s", output);
163 var pa = new Json.Parser();
164 pa.load_from_data(output);
165 var node = pa.get_root();
167 if (node.get_node_type () != Json.NodeType.OBJECT) {
168 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
170 var ret = node.get_object ();
171 ret.set_int_member("line_offset", this.line_offset);
172 if (result_callback == null) {
173 print ("no callback?");
176 this.result_callback(ret);
180 var ret = new Json.Object();
181 ret.set_boolean_member("success", false);
182 ret.set_string_member("message", e.message);
183 this.result_callback(ret);
194 int main (string[] args) {
196 var a = new ValaSource(file);
197 a.create_valac_tree();