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 /**
5  * 
6  *  This just deals with spawning the compiler and getting the results.
7  * 
8  *  each window should have one of these...
9  * 
10  *  x = new ValaSource();
11  *  x.connect.compiled(... do something with results... );
12  *  
13  * x.
14  * 
15  */
16
17 namespace Palete {
18         
19         public errordomain ValaSourceError {
20                 INVALID_FORMAT 
21         }
22         
23         //public delegate  void ValaSourceResult(Json.Object res);
24         
25          
26
27         public class ValaSource : Object {
28  
29                 
30                 public signal void compiled(Json.Object res);
31
32                 
33                 JsRender.JsRender file;
34                 public int line_offset = 0;
35                 
36                 public ValaSource( ) 
37                 {
38                         base();
39                  
40                 }
41                 public void dumpCode(string str) 
42                 {
43                         var ls = str.split("\n");
44                         for (var i=0;i < ls.length; i++) {
45                                 print("%d : %s\n", i+1, ls[i]);
46                         }
47                 }
48                 
49                 //public Gee.HashMap<int,string> checkFile()
50                 //{
51                 //      return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
52                 //}
53
54                 public void checkFileWithNodePropChange(
55                   
56                                         JsRender.JsRender file,
57                                         JsRender.Node node, 
58                                         string prop,
59                                         string ptype,
60                                         string val
61                                  )
62                 {
63                         this.file = file;
64                         
65                         
66                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
67                         var hash = ptype == "listener" ? node.listeners : node.props;
68                         
69                         // untill we get a smarter renderer..
70                         // we have some scenarios where changing the value does not work
71                         if (prop == "* xns" || prop == "xtype") {
72                                 this.compiled(new Json.Object());
73                                 return ;
74                         }
75                                 
76                         
77                         var old = hash.get(prop);
78                         var newval = "/*--VALACHECK-START--*/ " + val ;
79                         
80                         hash.set(prop, newval);
81                         var tmpstring = JsRender.NodeToVala.mungeFile(file);
82                         hash.set(prop, old);
83                         //print("%s\n", tmpstring);
84                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
85                         var offset =0;
86                         if (bits.length > 0) {
87                                 offset = bits[0].split("\n").length +1;
88                         }
89                         
90                         this.line_offset = offset;
91                         
92                         //this.dumpCode(tmpstring);
93                         //print("offset %d\n", offset);
94                         this.checkStringSpawn(tmpstring );
95                         
96                         // modify report
97                         
98                         
99                         
100                 }
101                 Spawn compiler;
102                  
103                 public void checkStringSpawn(
104                                         string contents 
105                                 )
106                 {
107                         
108                         FileIOStream iostream;
109                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
110                         tmpfile.ref();
111
112                         OutputStream ostream = iostream.output_stream;
113                         DataOutputStream dostream = new DataOutputStream (ostream);
114                         dostream.put_string (contents);
115                         
116                         var valafn = "";
117                         try {             
118                            var  regex = new Regex("\\.bjs$");
119                         
120                                 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
121                          } catch (GLib.RegexError e) {
122                                 var ret = new Json.Object();
123                                 ret.set_boolean_member("success", false);
124                                 ret.set_string_member("message", e.message);
125                             this.compiled(ret);
126                             return;
127                         }   
128                         
129                         string[] args = {};
130                         args += BuilderApplication._self;
131                         args += "--project";
132                         args += this.file.project.fn;
133                         args += "--target";
134                         args += this.file.build_module;
135                         args += "--add-file";
136                         args +=  tmpfile.get_path();
137                         args += "--skip-file";
138                         args += valafn;
139                         
140                          
141                         
142                         this.compiler = new Spawn("/tmp", args);
143                         this.compiler.complete.connect(spawnResult);
144                         
145                         try {
146                                 this.compiler.run(); 
147                         } catch (GLib.SpawnError e) {
148                                 var ret = new Json.Object();
149                                 ret.set_boolean_member("success", false);
150                                 ret.set_string_member("message", e.message);
151                             this.compiled(ret);
152                         }
153                          
154                 }
155                 
156                 public void checkSpawn( )
157                 {
158                         
159                         FileIOStream iostream;
160                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
161                         tmpfile.ref();
162
163                         OutputStream ostream = iostream.output_stream;
164                         DataOutputStream dostream = new DataOutputStream (ostream);
165                         dostream.put_string (contents);
166                         
167                         var valafn = "";
168                         try {             
169                            var  regex = new Regex("\\.bjs$");
170                         
171                                 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
172                          } catch (GLib.RegexError e) {
173                                 var ret = new Json.Object();
174                                 ret.set_boolean_member("success", false);
175                                 ret.set_string_member("message", e.message);
176                             this.compiled(ret);
177                             return;
178                         }   
179                         
180                         string[] args = {};
181                         args += BuilderApplication._self;
182                         args += "--project";
183                         args += this.file.project.fn;
184                         args += "--target";
185                         args += this.file.build_module;
186                         args += "--add-file";
187                         args +=  tmpfile.get_path();
188                         args += "--skip-file";
189                         args += valafn;
190                         
191                          
192                         
193                         this.compiler = new Spawn("/tmp", args);
194                         this.compiler.complete.connect(spawnResult);
195                         
196                         try {
197                                 this.compiler.run(); 
198                         } catch (GLib.SpawnError e) {
199                                 var ret = new Json.Object();
200                                 ret.set_boolean_member("success", false);
201                                 ret.set_string_member("message", e.message);
202                             this.compiled(ret);
203                         }
204                          
205                 }
206                 
207                 
208                 
209                 
210                 public void spawnResult(int res, string output, string stderr)
211                 {
212                          
213                                 
214                         try { 
215                                 GLib.debug("GOT output %s", output);
216                                 
217                                 var pa = new Json.Parser();
218                                 pa.load_from_data(output);
219                                 var node = pa.get_root();
220
221                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
222                                         throw new ValaSourceError.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
223                                 }
224                                 var ret = node.get_object ();
225                                 ret.set_int_member("line_offset", this.line_offset);
226                                 
227                                 this.compiled(ret);
228                                 
229                                 
230                         } catch (Error e) {
231                                 var ret = new Json.Object();
232                                 ret.set_boolean_member("success", false);
233                                 ret.set_string_member("message", e.message);
234                                 this.compiled(ret);
235                         }
236                         //compiler.unref();
237                         //tmpfile.unref();
238                          
239                         
240                         
241                 }
242         }
243                  
244 }
245 /*
246 int main (string[] args) {
247
248         var a = new ValaSource(file);
249         a.create_valac_tree();
250         return 0;
251 }
252 */
253
254