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