src/Palete/ValaSource.vala
[app.Builder.js] / src / Palete / ValaSource.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 namespace Palete {
5         
6         public errordomain ValaSourceError {
7                 INVALID_FORMAT 
8         }
9         
10         public delegate  void ValaSourceResult(Json.Object res);
11         
12          
13
14         public class ValaSource : Object {
15  
16
17                 Vala.CodeContext context;
18                  
19                 Project.Gtk project;
20                 public string build_module;
21                 public string filepath;
22                 public string original_filepath;
23                 public int line_offset = 0;
24                 
25                 // file.project , file.path, file.build_module, ""
26                 public ValaSource(Project.Gtk project, string filepath, string build_module, string original_filepath) {
27                         base();
28                         //this.file = file;
29                         this.filepath = filepath;
30                         this.build_module = build_module;
31                         this.original_filepath = original_filepath;
32                         this.project =  project;
33                         
34                 }
35                 public void dumpCode(string str) 
36                 {
37                         var ls = str.split("\n");
38                         for (var i=0;i < ls.length; i++) {
39                                 print("%d : %s\n", i+1, ls[i]);
40                         }
41                 }
42                 
43                 //public Gee.HashMap<int,string> checkFile()
44                 //{
45                 //      return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
46                 //}
47
48                 public void checkFileWithNodePropChange(
49                                         JsRender.JsRender file,
50                                         JsRender.Node node, 
51                                         string prop,
52                                         string ptype,
53                                         string val,
54                                         ValaSourceResult result_cb)
55                 {
56                         
57                         
58                         
59                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
60                         var hash = ptype == "listener" ? node.listeners : node.props;
61                         
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());
66                                 return ;
67                         }
68                                 
69                         
70                         var old = hash.get(prop);
71                         var newval = "/*--VALACHECK-START--*/ " + val ;
72                         
73                         hash.set(prop, newval);
74                         var tmpstring = JsRender.NodeToVala.mungeFile(file);
75                         hash.set(prop, old);
76                         //print("%s\n", tmpstring);
77                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
78                         var offset =0;
79                         if (bits.length > 0) {
80                                 offset = bits[0].split("\n").length +1;
81                         }
82                         
83                         this.line_offset = offset;
84                         
85                         //this.dumpCode(tmpstring);
86                         //print("offset %d\n", offset);
87                         this.result_callback = result_cb;
88                         this.checkStringSpawn(tmpstring );
89                         
90                         // modify report
91                         
92                         
93                         
94                 }
95                 Spawn compiler;
96                 ValaSourceResult result_callback;
97                 public void checkStringSpawn(
98                                         string contents 
99                                 )
100                 {
101                         
102                         FileIOStream iostream;
103                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
104                         tmpfile.ref();
105
106                         OutputStream ostream = iostream.output_stream;
107                         DataOutputStream dostream = new DataOutputStream (ostream);
108                         dostream.put_string (contents);
109                         
110                         
111                         string[] args = {};
112                         args += BuilderApplication._self;
113                         args += "--project";
114                         args += this.project.fn;
115                         args += "--target";
116                         args += this.build_module;
117                         args += "--add-file";
118                         args +=  tmpfile.get_path();
119                         args += "--skip-file";
120                         args += this.filepath;
121                         
122                          
123                         
124                         this.compiler = new Spawn("/tmp", args);
125                         
126                         try {
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);
133                         }
134                          
135                 }
136                 public void spawnResult(int res, string output, string stderr)
137                 {
138                          
139                                 
140                         try { 
141                                 GLib.debug("GOT output %s", output);
142                                 
143                                 var pa = new Json.Parser();
144                                 pa.load_from_data(output);
145                                 var node = pa.get_root();
146
147                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
148                                         throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
149                                 }
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?");
154                                         return;
155                                 }
156                                 this.result_callback(ret);
157                                 
158                                 
159                         } catch (Error e) {
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);
164                         }
165                         //compiler.unref();
166                         //tmpfile.unref();
167                          
168                         
169                         
170                 }
171                  
172 }
173 /*
174 int main (string[] args) {
175
176         var a = new ValaSource(file);
177         a.create_valac_tree();
178         return 0;
179 }
180 */
181
182