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... );
18 public errordomain ValaSourceError {
22 //public delegate void ValaSourceResult(Json.Object res);
24 public signal void compiled(Json.Object res);
27 public class ValaSource : Object {
32 Vala.CodeContext context;
35 public string build_module;
36 public string filepath;
37 public string original_filepath;
38 public int line_offset = 0;
40 // file.project , file.path, file.build_module, ""
48 string original_filepath) {
51 this.filepath = filepath;
52 this.build_module = build_module;
53 this.original_filepath = original_filepath;
54 this.project = project;
57 public void dumpCode(string str)
59 var ls = str.split("\n");
60 for (var i=0;i < ls.length; i++) {
61 print("%d : %s\n", i+1, ls[i]);
65 //public Gee.HashMap<int,string> checkFile()
67 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
70 public void checkFileWithNodePropChange(
74 JsRender.JsRender file,
79 ValaSourceResult result_cb)
84 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
85 var hash = ptype == "listener" ? node.listeners : node.props;
87 // untill we get a smarter renderer..
88 // we have some scenarios where changing the value does not work
89 if (prop == "* xns" || prop == "xtype") {
90 result_callback(new Json.Object());
95 var old = hash.get(prop);
96 var newval = "/*--VALACHECK-START--*/ " + val ;
98 hash.set(prop, newval);
99 var tmpstring = JsRender.NodeToVala.mungeFile(file);
101 //print("%s\n", tmpstring);
102 var bits = tmpstring.split("/*--VALACHECK-START--*/");
104 if (bits.length > 0) {
105 offset = bits[0].split("\n").length +1;
108 this.line_offset = offset;
110 //this.dumpCode(tmpstring);
111 //print("offset %d\n", offset);
112 this.result_callback = result_cb;
113 this.checkStringSpawn(tmpstring );
121 ValaSourceResult result_callback;
122 public void checkStringSpawn(
127 FileIOStream iostream;
128 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
131 OutputStream ostream = iostream.output_stream;
132 DataOutputStream dostream = new DataOutputStream (ostream);
133 dostream.put_string (contents);
137 args += BuilderApplication._self;
139 args += this.project.fn;
141 args += this.build_module;
142 args += "--add-file";
143 args += tmpfile.get_path();
144 args += "--skip-file";
145 args += this.filepath;
149 this.compiler = new Spawn("/tmp", args);
152 this.compiler.run(spawnResult);
153 } catch (GLib.SpawnError e) {
154 var ret = new Json.Object();
155 ret.set_boolean_member("success", false);
156 ret.set_string_member("message", e.message);
157 this.result_callback(ret);
161 public void spawnResult(int res, string output, string stderr)
166 GLib.debug("GOT output %s", output);
168 var pa = new Json.Parser();
169 pa.load_from_data(output);
170 var node = pa.get_root();
172 if (node.get_node_type () != Json.NodeType.OBJECT) {
173 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
175 var ret = node.get_object ();
176 ret.set_int_member("line_offset", this.line_offset);
177 if (result_callback == null) {
178 print ("no callback?");
181 this.result_callback(ret);
185 var ret = new Json.Object();
186 ret.set_boolean_member("success", false);
187 ret.set_string_member("message", e.message);
188 this.result_callback(ret);
199 int main (string[] args) {
201 var a = new ValaSource(file);
202 a.create_valac_tree();