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 int line_offset = 0;
40 // file.project , file.path, file.build_module, ""
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(
65 JsRender.JsRender file,
70 ValaSourceResult result_cb)
73 this.project = file.project;
74 this.filepath = file.path;
75 this.build_module = file.build_module;
78 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
79 var hash = ptype == "listener" ? node.listeners : node.props;
81 // untill we get a smarter renderer..
82 // we have some scenarios where changing the value does not work
83 if (prop == "* xns" || prop == "xtype") {
84 result_callback(new Json.Object());
89 var old = hash.get(prop);
90 var newval = "/*--VALACHECK-START--*/ " + val ;
92 hash.set(prop, newval);
93 var tmpstring = JsRender.NodeToVala.mungeFile(file);
95 //print("%s\n", tmpstring);
96 var bits = tmpstring.split("/*--VALACHECK-START--*/");
98 if (bits.length > 0) {
99 offset = bits[0].split("\n").length +1;
102 this.line_offset = offset;
104 //this.dumpCode(tmpstring);
105 //print("offset %d\n", offset);
106 this.result_callback = result_cb;
107 this.checkStringSpawn(tmpstring );
115 ValaSourceResult result_callback;
116 public void checkStringSpawn(
121 FileIOStream iostream;
122 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
125 OutputStream ostream = iostream.output_stream;
126 DataOutputStream dostream = new DataOutputStream (ostream);
127 dostream.put_string (contents);
131 args += BuilderApplication._self;
133 args += this.project.fn;
135 args += this.build_module;
136 args += "--add-file";
137 args += tmpfile.get_path();
138 args += "--skip-file";
139 args += this.filepath;
143 this.compiler = new Spawn("/tmp", args);
146 this.compiler.run(spawnResult);
147 } catch (GLib.SpawnError e) {
148 var ret = new Json.Object();
149 ret.set_boolean_member("success", false);
150 ret.set_string_member("message", e.message);
151 this.result_callback(ret);
155 public void spawnResult(int res, string output, string stderr)
160 GLib.debug("GOT output %s", output);
162 var pa = new Json.Parser();
163 pa.load_from_data(output);
164 var node = pa.get_root();
166 if (node.get_node_type () != Json.NodeType.OBJECT) {
167 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
169 var ret = node.get_object ();
170 ret.set_int_member("line_offset", this.line_offset);
171 if (result_callback == null) {
172 print ("no callback?");
175 this.result_callback(ret);
179 var ret = new Json.Object();
180 ret.set_boolean_member("success", false);
181 ret.set_string_member("message", e.message);
182 this.result_callback(ret);
193 int main (string[] args) {
195 var a = new ValaSource(file);
196 a.create_valac_tree();