2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
6 public errordomain ValaSourceError {
10 public delegate void ValaSourceResult(Json.Object res);
14 public class ValaSource : Object {
17 Vala.CodeContext context;
20 public string build_module;
21 public string filepath;
22 public string original_filepath;
23 public int line_offset = 0;
25 // file.project , file.path, file.build_module, ""
26 public ValaSource(Project.Gtk project, string filepath, string build_module, string original_filepath) {
29 this.filepath = filepath;
30 this.build_module = build_module;
31 this.original_filepath = original_filepath;
32 this.project = project;
35 public void dumpCode(string str)
37 var ls = str.split("\n");
38 for (var i=0;i < ls.length; i++) {
39 print("%d : %s\n", i+1, ls[i]);
43 //public Gee.HashMap<int,string> checkFile()
45 // return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
48 public void checkFileWithNodePropChange(
49 JsRender.JsRender file,
54 ValaSourceResult result_cb)
59 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
60 var hash = ptype == "listener" ? node.listeners : node.props;
62 // untill we get a smarter renderer..
63 // we have some scenarios where changing the value does not work
64 if (prop == "* xns" || prop == "xtype") {
65 result_callback(new Json.Object());
70 var old = hash.get(prop);
71 var newval = "/*--VALACHECK-START--*/ " + val ;
73 hash.set(prop, newval);
74 var tmpstring = JsRender.NodeToVala.mungeFile(file);
76 //print("%s\n", tmpstring);
77 var bits = tmpstring.split("/*--VALACHECK-START--*/");
79 if (bits.length > 0) {
80 offset = bits[0].split("\n").length +1;
83 this.line_offset = offset;
85 //this.dumpCode(tmpstring);
86 //print("offset %d\n", offset);
87 this.result_callback = result_cb;
88 this.checkStringSpawn(tmpstring );
96 ValaSourceResult result_callback;
97 public void checkStringSpawn(
102 FileIOStream iostream;
103 var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
106 OutputStream ostream = iostream.output_stream;
107 DataOutputStream dostream = new DataOutputStream (ostream);
108 dostream.put_string (contents);
112 args += BuilderApplication._self;
114 args += this.project.fn;
116 args += this.build_module;
117 args += "--add-file";
118 args += tmpfile.get_path();
119 args += "--skip-file";
120 args += this.filepath;
124 this.compiler = new Spawn("/tmp", args);
127 this.compiler.run(spawnResult);
128 } catch (GLib.SpawnError e) {
129 var ret = new Json.Object();
130 ret.set_boolean_member("success", false);
131 ret.set_string_member("message", e.message);
132 this.result_callback(ret);
136 public void spawnResult(int res, string output, string stderr)
141 GLib.debug("GOT output %s", output);
143 var pa = new Json.Parser();
144 pa.load_from_data(output);
145 var node = pa.get_root();
147 if (node.get_node_type () != Json.NodeType.OBJECT) {
148 throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
150 var ret = node.get_object ();
151 ret.set_int_member("line_offset", this.line_offset);
152 if (result_callback == null) {
153 print ("no callback?");
156 this.result_callback(ret);
160 var ret = new Json.Object();
161 ret.set_boolean_member("success", false);
162 ret.set_string_member("message", e.message);
163 this.result_callback(ret);
174 int main (string[] args) {
176 var a = new ValaSource(file);
177 a.create_valac_tree();