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                          
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                         return 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 bool checkFileSpawn(JsRender.JsRender file )
161                 {
162                         // race condition..
163                         if (this.compiler != null) { 
164                                 return false;
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                         
180                         try {
181                             this.compiler = new Spawn("/tmp", args);
182                             this.compiler.complete.connect(spawnResult);
183                         
184                             this.compiler.run(); 
185                         
186                          
187                         } catch (GLib.Error e) {
188                             GLib.debug(e.message);
189                             this.compiler = null;
190                             return false;
191                         }
192                         return true;
193                          
194                 }
195                 
196                 public void spawnExecute(JsRender.JsRender file)
197                 {
198                         // race condition..
199                         if (this.compiler != null) { 
200                                 return false;
201                         }
202                         var pr = (Project.Gtk)(file.project);
203                         
204                         
205                         this.file = file;
206                         this.line_offset = 0;
207                           
208                         string[] args = {};
209                         args += BuilderApplication._self;
210                         args += "--project";
211                         args += this.file.project.fn;
212                         args += "--target";
213                         if (this.file.build_module.length > 0 ) {
214                             args += this.file.build_module;
215                         } else {
216                             args += pr.firstBuildModule();
217                         }
218                         args += "--output";
219                         args += "/tmp/testrun";
220                         
221                          
222                         try {
223                             this.compiler = new Spawn(pr.firstPath(), args);
224                             this.compiler.complete.connect(spawnResult);
225                         
226                             this.compiler.run(); 
227                         
228                          
229                         } catch (GLib.Error e) {
230                             GLib.debug(e.message);
231                             this.compiler = null;
232                             return false;
233                         }
234                         return true;
235                          
236                 }
237                 
238                 
239                 /**
240                 * Used to compile a non builder file..
241                 */
242                  
243                 public bool checkPlainFileSpawn(  JsRender.JsRender file, string contents )
244                 {
245                         // race condition..
246                         if (this.compiler != null) { 
247                                 return false;
248                         }
249                         var pr = (Project.Gtk)(file.project);
250                         
251                         var m = pr.firstBuildModule();
252                         var cg = pr.compilegroups.get(m);
253                         var foundit = false;
254                         for (var i = 0; i < cg.sources.size; i++) {
255                             var path = pr.resolve_path(
256                                     pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
257                             if (path == file.path) {
258                                 foundit = true;
259                                 break;
260                                         }
261                         
262                         }
263                         if (!foundit) {
264                           
265                             this.compiler = null;
266                         
267                             return false; // do not run the compile..
268                         }
269                         // is the file in the module?
270                         
271                         
272                         FileIOStream iostream;
273                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
274                         tmpfile.ref();
275
276                         OutputStream ostream = iostream.output_stream;
277                         DataOutputStream dostream = new DataOutputStream (ostream);
278                         dostream.put_string (contents);
279                         
280                         
281                         this.file = null;
282                         this.line_offset = 0;
283                           
284                         string[] args = {};
285                         args += BuilderApplication._self;
286                         args += "--project";
287                         args +=  file.project.fn;
288                         args += "--target";
289                         args += pr.firstBuildModule();
290                         args += "--add-file";
291                         args +=  tmpfile.get_path();
292                         args += "--skip-file";
293                         args += file.path;
294                          
295                         
296                         
297                         
298                         try {
299                             this.compiler = new Spawn("/tmp", args);
300                             this.compiler.complete.connect(spawnResult);
301                             this.compiler.run(); 
302                         } catch (GLib.Error e) {
303                             
304                             this.compiler = null;
305                             return false;
306                         }
307                         return true;
308                          
309                 }
310                 
311                 
312                 public void spawnResult(int res, string output, string stderr)
313                 {
314                          
315                                 
316                         try { 
317                                 //GLib.debug("GOT output %s", output);
318                                 
319                                 var pa = new Json.Parser();
320                                 pa.load_from_data(output);
321                                 var node = pa.get_root();
322
323                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
324                                         var ret = new Json.Object();
325                                         ret.set_boolean_member("success", false);
326                                         ret.set_string_member("message", 
327                                                 "Compiler returned Unexpected element type %s".printf( 
328                                                         node.type_name ()
329                                                 )
330                                         );
331                                         this.compiled(ret);
332                                         this.compiler = null;
333                                 }
334                                 var ret = node.get_object ();
335                                 ret.set_int_member("line_offset", this.line_offset);
336                                 
337                                 this.compiled(ret);
338                                 
339                                 
340                         } catch (GLib.Error e) {
341                                 var ret = new Json.Object();
342                                 ret.set_boolean_member("success", false);
343                                 ret.set_string_member("message", e.message);
344                                 this.compiled(ret);
345                         }
346                         this.compiler = null;
347                         //compiler.unref();
348                         //tmpfile.unref();
349                          
350                         
351                         
352                 }
353         }
354                  
355 }
356 /*
357 int main (string[] args) {
358
359         var a = new ValaSource(file);
360         a.create_valac_tree();
361         return 0;
362 }
363 */
364
365