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                         this.compiler = null;
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                         if (this.compiler != null) {
66                                 return;
67                         }
68                         
69                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
70                         var hash = ptype == "listener" ? node.listeners : node.props;
71                         
72                         // untill we get a smarter renderer..
73                         // we have some scenarios where changing the value does not work
74                         if (prop == "* xns" || prop == "xtype") {
75                                 this.compiled(new Json.Object());
76                                 return ;
77                         }
78                                 
79                         
80                         var old = hash.get(prop);
81                         var newval = "/*--VALACHECK-START--*/ " + val ;
82                         
83                         hash.set(prop, newval);
84                         var tmpstring = JsRender.NodeToVala.mungeFile(file);
85                         hash.set(prop, old);
86                         //print("%s\n", tmpstring);
87                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
88                         var offset =0;
89                         if (bits.length > 0) {
90                                 offset = bits[0].split("\n").length +1;
91                         }
92                         
93                         this.line_offset = offset;
94                         
95                         //this.dumpCode(tmpstring);
96                         //print("offset %d\n", offset);
97                         this.checkStringSpawn(tmpstring );
98                         
99                         // modify report
100                         
101                         
102                         
103                 }
104                 Spawn compiler;
105                  
106                 public void checkStringSpawn(
107                                 string contents 
108                         )
109                 {
110                         
111                         if (this.compiler != null) {
112                                 return;
113                         }
114                         
115                         FileIOStream iostream;
116                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
117                         tmpfile.ref();
118
119                         OutputStream ostream = iostream.output_stream;
120                         DataOutputStream dostream = new DataOutputStream (ostream);
121                         dostream.put_string (contents);
122                         
123                         var valafn = "";
124                         try {             
125                            var  regex = new Regex("\\.bjs$");
126                         
127                                 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
128                          } catch (GLib.RegexError e) {
129                                 var ret = new Json.Object();
130                                 ret.set_boolean_member("success", false);
131                                 ret.set_string_member("message", e.message);
132                             this.compiled(ret);
133                             return;
134                         }   
135                         
136                         string[] args = {};
137                         args += BuilderApplication._self;
138                         args += "--project";
139                         args += this.file.project.fn;
140                         args += "--target";
141                         args += this.file.build_module;
142                         args += "--add-file";
143                         args +=  tmpfile.get_path();
144                         args += "--skip-file";
145                         args += valafn;
146                         
147                          
148                         
149                         this.compiler = new Spawn("/tmp", args);
150                         this.compiler.complete.connect(spawnResult);
151                         
152                         try {
153                                 this.compiler.run(); 
154                         } catch (GLib.SpawnError e) {
155                                 var ret = new Json.Object();
156                                 ret.set_boolean_member("success", false);
157                                 ret.set_string_member("message", e.message);
158                             this.compiled(ret);
159                             this.compiler = null;
160                         }
161                          
162                 }
163                 
164                 public void checkFileSpawn(JsRender.JsRender file )
165                 {
166                         // race condition..
167                         if (this.compiler != null) { 
168                                 return;
169                         }
170                         
171                         this.file = file;
172                         this.line_offset = 0;
173                           
174                         string[] args = {};
175                         args += BuilderApplication._self;
176                         args += "--project";
177                         args += this.file.project.fn;
178                         args += "--target";
179                         args += this.file.build_module;
180                          
181                          
182                         
183                         this.compiler = new Spawn("/tmp", args);
184                         this.compiler.complete.connect(spawnResult);
185                         
186                         try {
187                                 this.compiler.run(); 
188                         } catch (GLib.SpawnError e) {
189                                 var ret = new Json.Object();
190                                 ret.set_boolean_member("success", false);
191                                 ret.set_string_member("message", e.message);
192                             this.compiled(ret);
193                             this.compiler = null;
194                         }
195                          
196                 }
197                 /**
198                 * Used to compile a non builder file..
199                 */
200                  
201                 public void checkPlainFileSpawn(  JsRender.JsRender file, string contents )
202                 {
203                         // race condition..
204                         if (this.compiler != null) { 
205                                 return;
206                         }
207                         var pr = (Project.Gtk)(file.project);
208                         
209                         var m = pr.firstBuildModule();
210                         var cg = pr.compilegroups.get(m);
211                         var foundit = false;
212                         for (var i = 0; i < cg.sources.size; i++) {
213                             var path = pr.resolve_path(
214                                     pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
215                             if (path == file.path) {
216                                 foundit = true;
217                                 break;
218                             }
219                         
220                         }
221                         if (!foundit) {
222                             return; // do not run the compile..
223                         }
224                         // is the file in the module?
225                         
226                         
227                         FileIOStream iostream;
228                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
229                         tmpfile.ref();
230
231                         OutputStream ostream = iostream.output_stream;
232                         DataOutputStream dostream = new DataOutputStream (ostream);
233                         dostream.put_string (contents);
234                         
235                         
236                         this.file = null;
237                         this.line_offset = 0;
238                           
239                         string[] args = {};
240                         args += BuilderApplication._self;
241                         args += "--project";
242                         args +=  file.project.fn;
243                         args += "--target";
244                         args += pr.firstBuildModule();
245                         args += "--add-file";
246                         args +=  tmpfile.get_path();
247                         args += "--skip-file";
248                         args += file.path;
249                          
250                         
251                         this.compiler = new Spawn("/tmp", args);
252                         this.compiler.complete.connect(spawnResult);
253                         
254                         try {
255                                 this.compiler.run(); 
256                         } catch (GLib.SpawnError e) {
257                                 var ret = new Json.Object();
258                                 ret.set_boolean_member("success", false);
259                                 ret.set_string_member("message", e.message);
260                             this.compiled(ret);
261                             this.compiler = null;
262                         }
263                          
264                 }
265                 
266                 
267                 public void spawnResult(int res, string output, string stderr)
268                 {
269                          
270                                 
271                         try { 
272                                 GLib.debug("GOT output %s", output);
273                                 
274                                 var pa = new Json.Parser();
275                                 pa.load_from_data(output);
276                                 var node = pa.get_root();
277
278                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
279                                         var ret = new Json.Object();
280                                         ret.set_boolean_member("success", false);
281                                         ret.set_string_member("message", 
282                                                 "Compiler returned Unexpected element type %s".printf( 
283                                                         node.type_name ()
284                                                 )
285                                         );
286                                         this.compiled(ret);
287                                         this.compiler = null;
288                                 }
289                                 var ret = node.get_object ();
290                                 ret.set_int_member("line_offset", this.line_offset);
291                                 
292                                 this.compiled(ret);
293                                 
294                                 
295                         } catch (Error e) {
296                                 var ret = new Json.Object();
297                                 ret.set_boolean_member("success", false);
298                                 ret.set_string_member("message", e.message);
299                                 this.compiled(ret);
300                         }
301                         this.compiler = null;
302                         //compiler.unref();
303                         //tmpfile.unref();
304                          
305                         
306                         
307                 }
308         }
309                  
310 }
311 /*
312 int main (string[] args) {
313
314         var a = new ValaSource(file);
315         a.create_valac_tree();
316         return 0;
317 }
318 */
319
320