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.
12 public errordomain ValaSourceError {
16 public delegate void ValaSourceResult(Json.Object res);
20 public class ValaSource : Object {
23 Vala.CodeContext context;
26 public string build_module;
27 public string filepath;
28 public string original_filepath;
29 public int line_offset = 0;
31 // file.project , file.path, file.build_module, ""
32 public ValaSource(Project.Gtk project, string filepath, string build_module, string original_filepath) {
35 this.filepath = filepath;
36 this.build_module = build_module;
37 this.original_filepath = original_filepath;
38 this.project = project;
41 public void dumpCode(string str)
43 var ls = str.split("\n");
44 for (var i=0;i < ls.length; i++) {
45 print("%d : %s\n", i+1, ls[i]);
49 //public Gee.HashMap<int,string> checkFile()
51 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
54 public void checkFileWithNodePropChange(
55 JsRender.JsRender file,
60 ValaSourceResult result_cb)
65 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
66 var hash = ptype == "listener" ? node.listeners : node.props;
68 // untill we get a smarter renderer..
69 // we have some scenarios where changing the value does not work
70 if (prop == "* xns" || prop == "xtype") {
71 result_callback(new Json.Object());
76 var old = hash.get(prop);
77 var newval = "/*--VALACHECK-START--*/ " + val ;
79 hash.set(prop, newval);
80 var tmpstring = JsRender.NodeToVala.mungeFile(file);
82 //print("%s\n", tmpstring);
83 var bits = tmpstring.split("/*--VALACHECK-START--*/");
85 if (bits.length > 0) {
86 offset = bits[0].split("\n").length +1;
89 this.line_offset = offset;
91 //this.dumpCode(tmpstring);
92 //print("offset %d\n", offset);
93 this.result_callback = result_cb;
94 this.checkStringSpawn(tmpstring );
102 ValaSourceResult result_callback;
103 public void checkStringSpawn(
108 FileIOStream iostream;
109 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
112 OutputStream ostream = iostream.output_stream;
113 DataOutputStream dostream = new DataOutputStream (ostream);
114 dostream.put_string (contents);
118 args += BuilderApplication._self;
120 args += this.project.fn;
122 args += this.build_module;
123 args += "--add-file";
124 args += tmpfile.get_path();
125 args += "--skip-file";
126 args += this.filepath;
130 this.compiler = new Spawn("/tmp", args);
133 this.compiler.run(spawnResult);
134 } catch (GLib.SpawnError e) {
135 var ret = new Json.Object();
136 ret.set_boolean_member("success", false);
137 ret.set_string_member("message", e.message);
138 this.result_callback(ret);
142 public void spawnResult(int res, string output, string stderr)
147 GLib.debug("GOT output %s", output);
149 var pa = new Json.Parser();
150 pa.load_from_data(output);
151 var node = pa.get_root();
153 if (node.get_node_type () != Json.NodeType.OBJECT) {
154 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
156 var ret = node.get_object ();
157 ret.set_int_member("line_offset", this.line_offset);
158 if (result_callback == null) {
159 print ("no callback?");
162 this.result_callback(ret);
166 var ret = new Json.Object();
167 ret.set_boolean_member("success", false);
168 ret.set_string_member("message", e.message);
169 this.result_callback(ret);
180 int main (string[] args) {
182 var a = new ValaSource(file);
183 a.create_valac_tree();