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, ""
49 this.filepath = filepath;
50 this.build_module = build_module;
51 this.original_filepath = original_filepath;
52 this.project = project;
55 public void dumpCode(string str)
57 var ls = str.split("\n");
58 for (var i=0;i < ls.length; i++) {
59 print("%d : %s\n", i+1, ls[i]);
63 //public Gee.HashMap<int,string> checkFile()
65 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
68 public void checkFileWithNodePropChange(
72 string original_filepath
74 JsRender.JsRender file,
79 ValaSourceResult result_cb)
82 this.project = file.project;
83 this.filepath = file.path;
84 this.build_module = file.build_module;
87 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
88 var hash = ptype == "listener" ? node.listeners : node.props;
90 // untill we get a smarter renderer..
91 // we have some scenarios where changing the value does not work
92 if (prop == "* xns" || prop == "xtype") {
93 result_callback(new Json.Object());
98 var old = hash.get(prop);
99 var newval = "/*--VALACHECK-START--*/ " + val ;
101 hash.set(prop, newval);
102 var tmpstring = JsRender.NodeToVala.mungeFile(file);
104 //print("%s\n", tmpstring);
105 var bits = tmpstring.split("/*--VALACHECK-START--*/");
107 if (bits.length > 0) {
108 offset = bits[0].split("\n").length +1;
111 this.line_offset = offset;
113 //this.dumpCode(tmpstring);
114 //print("offset %d\n", offset);
115 this.result_callback = result_cb;
116 this.checkStringSpawn(tmpstring );
124 ValaSourceResult result_callback;
125 public void checkStringSpawn(
130 FileIOStream iostream;
131 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
134 OutputStream ostream = iostream.output_stream;
135 DataOutputStream dostream = new DataOutputStream (ostream);
136 dostream.put_string (contents);
140 args += BuilderApplication._self;
142 args += this.project.fn;
144 args += this.build_module;
145 args += "--add-file";
146 args += tmpfile.get_path();
147 args += "--skip-file";
148 args += this.filepath;
152 this.compiler = new Spawn("/tmp", args);
155 this.compiler.run(spawnResult);
156 } catch (GLib.SpawnError e) {
157 var ret = new Json.Object();
158 ret.set_boolean_member("success", false);
159 ret.set_string_member("message", e.message);
160 this.result_callback(ret);
164 public void spawnResult(int res, string output, string stderr)
169 GLib.debug("GOT output %s", output);
171 var pa = new Json.Parser();
172 pa.load_from_data(output);
173 var node = pa.get_root();
175 if (node.get_node_type () != Json.NodeType.OBJECT) {
176 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
178 var ret = node.get_object ();
179 ret.set_int_member("line_offset", this.line_offset);
180 if (result_callback == null) {
181 print ("no callback?");
184 this.result_callback(ret);
188 var ret = new Json.Object();
189 ret.set_boolean_member("success", false);
190 ret.set_string_member("message", e.message);
191 this.result_callback(ret);
202 int main (string[] args) {
204 var a = new ValaSource(file);
205 a.create_valac_tree();