resources/RooUsage.txt
[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                 public signal void compile_output(string str);
32
33                 
34                 JsRender.JsRender file;
35                 public int line_offset = 0;
36                 
37                 public Gee.ArrayList<Spawn> children;
38                 public ValaSource( ) 
39                 {
40                         base();
41                         this.compiler = null;
42                         this.children = new Gee.ArrayList<Spawn>();
43                         
44                 }
45                 public void dumpCode(string str) 
46                 {
47                         var ls = str.split("\n");
48                         for (var i=0;i < ls.length; i++) {
49                                 print("%d : %s\n", i+1, ls[i]);
50                         }
51                 }
52                 
53                 //public Gee.HashMap<int,string> checkFile()
54                 //{
55                 //      return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
56                 //}
57
58                 public bool checkFileWithNodePropChange(
59                   
60                                         JsRender.JsRender file,
61                                         JsRender.Node node, 
62                                         string prop,
63                                         string ptype,
64                                         string val
65                                  )
66                 {
67                         this.file = file;
68                         
69                         if (this.compiler != null) {
70                                 return false;
71                         }
72                         
73                          
74                         var hash = ptype == "listener" ? node.listeners : node.props;
75                         
76                         // untill we get a smarter renderer..
77                         // we have some scenarios where changing the value does not work
78                         if (prop == "* xns" || prop == "xtype") {
79                                 return  false;
80                         }
81                                 
82                         
83                         var old = hash.get(prop);
84                         var newval = "/*--VALACHECK-START--*/ " + val ;
85                         
86                         hash.set(prop, newval);
87                         var tmpstring = JsRender.NodeToVala.mungeFile(file);
88                         hash.set(prop, old);
89                         //print("%s\n", tmpstring);
90                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
91                         var offset =0;
92                         if (bits.length > 0) {
93                                 offset = bits[0].split("\n").length +1;
94                         }
95                         
96                         this.line_offset = offset;
97                         
98                         //this.dumpCode(tmpstring);
99                         //print("offset %d\n", offset);
100                         return this.checkStringSpawn(tmpstring );
101                         
102                         // modify report
103                         
104                         
105                         
106                 }
107                 Spawn compiler;
108                  
109                 public bool checkStringSpawn(
110                                 string contents 
111                         )
112                 {
113                         
114                         if (this.compiler != null) {
115                                 return false;
116                         }
117                         
118                         FileIOStream iostream;
119                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
120                         tmpfile.ref();
121
122                         OutputStream ostream = iostream.output_stream;
123                         DataOutputStream dostream = new DataOutputStream (ostream);
124                         dostream.put_string (contents);
125                         
126                         var valafn = "";
127                         try {             
128                            var  regex = new Regex("\\.bjs$");
129                         
130                                 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
131                          } catch (GLib.RegexError e) {
132                                  
133                             return false;
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                                 GLib.debug(e.message);
156                                 this.compiler = null;
157                                 return false;
158
159                         }
160                         return true;
161                          
162                 }
163                 
164                 public bool checkFileSpawn(JsRender.JsRender file )
165                 {
166                         // race condition..
167                         if (this.compiler != null) { 
168                                 return false;
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                         
184                         try {
185                             this.compiler = new Spawn("/tmp", args);
186                             this.compiler.complete.connect(spawnResult);
187                         
188                             this.compiler.run(); 
189                         
190                          
191                         } catch (GLib.Error e) {
192                             GLib.debug(e.message);
193                             this.compiler = null;
194                             return false;
195                         }
196                         return true;
197                          
198                 }
199                 
200  
201
202                 
203                 
204                 public void spawnExecute(JsRender.JsRender file)
205                 {
206                         // race condition..
207                         if (this.compiler != null) { 
208                                 return;
209                         }
210                         if (!(file.project is Project.Gtk)) {
211                             return;
212                         }
213                         var pr = (Project.Gtk)(file.project);
214                         
215                         
216                         this.file = file;
217                         this.line_offset = 0;
218                           
219                         string[] args = {};
220                         args += BuilderApplication._self;
221                         args += "--project";
222                         args += this.file.project.fn;
223                         args += "--target";
224                         if (this.file.build_module.length > 0 ) {
225                             args += this.file.build_module;
226                         } else {
227                             args += pr.firstBuildModule();
228                         }
229                         //args += "--output"; -- set up by the module -- defaults to testrun
230                         //args += "/tmp/testrun";
231                         
232                         // assume code is in home...
233                         try {
234                             this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
235                             this.compiler.output_line.connect(compile_output_line);
236                             this.compiler.complete.connect(runResult);
237                             this.compiler.run(); 
238                                 this.children.add(this.compiler); //keep a reference...
239                          
240                         } catch (GLib.Error e) {
241                             GLib.debug(e.message);
242                             this.compiler = null;
243
244                         }
245                         return;
246                          
247                 }
248                 public void compile_output_line(   string str )
249                 {
250                         this.compile_output(str);
251                 }
252                 /**
253                 * Used to compile a non builder file..
254                 */
255                  
256                  
257                 public bool checkPlainFileSpawn( JsRender.JsRender file, string contents )
258  
259                 {
260                         // race condition..
261                         if (this.compiler != null) { 
262                                 return false;
263                         }
264                         var pr = (Project.Gtk)(file.project);
265                         
266                         var m = pr.firstBuildModule();
267                         var cg = pr.compilegroups.get(m);
268                         var foundit = false;
269                         for (var i = 0; i < cg.sources.size; i++) {
270                             var path = pr.resolve_path(
271                                     pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
272                             if (path == file.path) {
273                                 foundit = true;
274                                 break;
275                                         }
276                         
277                         }
278                         if (!foundit) {
279                           
280                             this.compiler = null;
281                         
282                             return false; // do not run the compile..
283                         }
284                         // is the file in the module?
285                         
286                         
287                         FileIOStream iostream;
288                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
289                         tmpfile.ref();
290
291                         OutputStream ostream = iostream.output_stream;
292                         DataOutputStream dostream = new DataOutputStream (ostream);
293                         dostream.put_string (contents);
294                         
295                         var target = pr.firstBuildModule();
296                         if (target.length < 1) {
297                                 return false;
298                         }
299                         
300                         this.file = null;
301                         this.line_offset = 0;
302                           
303                         string[] args = {};
304                         args += BuilderApplication._self;
305                         args += "--project";
306                         args +=  file.project.fn;
307                         args += "--target";
308  
309                         args += pr.firstBuildModule();
310                         args += "--add-file";
311                         args +=  tmpfile.get_path();
312                         args += "--skip-file";
313                         args += file.path;
314                          
315                         
316                         
317                         
318                         try {
319                             this.compiler = new Spawn("/tmp", args);
320                             this.compiler.complete.connect(spawnResult);
321                             this.compiler.run(); 
322                         } catch (GLib.Error e) {
323                             
324                             this.compiler = null;
325                             return false;
326                         }
327                         return true;
328                          
329                 }
330                  
331                 
332                 public void spawnResult(int res, string output, string stderr)
333                 {
334                          
335                                 
336                         try { 
337                                 //GLib.debug("GOT output %s", output);
338                                 
339                                 var pa = new Json.Parser();
340                                 pa.load_from_data(output);
341                                 var node = pa.get_root();
342
343                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
344                                         var ret = new Json.Object();
345                                         ret.set_boolean_member("success", false);
346                                         ret.set_string_member("message", 
347                                                 "Compiler returned Unexpected element type %s".printf( 
348                                                         node.type_name ()
349                                                 )
350                                         );
351                                         this.compiled(ret);
352                                         this.compiler = null;
353                                 }
354                                 var ret = node.get_object ();
355                                 ret.set_int_member("line_offset", this.line_offset);
356                                 
357                                 this.compiled(ret);
358                                 
359                                 
360                         } catch (GLib.Error e) {
361                                 var ret = new Json.Object();
362                                 ret.set_boolean_member("success", false);
363                                 ret.set_string_member("message", e.message);
364                                 this.compiled(ret);
365                         }
366                         this.compiler = null;
367                         //compiler.unref();
368                         //tmpfile.unref();
369                          
370                         
371                         
372                 }
373                 
374                 public void runResult(int res, string output, string stderr)
375                 {
376                         this.compiler = null;
377                         var exe = "/tmp/testrun";
378                         var mod = "";
379                         var pr = (Project.Gtk)(this.file.project);
380                         
381                         
382                         
383                         if (this.file.build_module.length > 0 ) {
384                     mod =  this.file.build_module;
385                         } else {
386                             mod =  pr.firstBuildModule();
387                         }
388                         if (mod.length < 1) {
389                                 return;
390                         }
391                         var cg =  pr.compilegroups.get(mod);
392                         if (cg.target_bin.length > 0) {
393                                 exe = cg.target_bin;
394                         }
395                         
396                         
397                         if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
398                                 print("Missing output file: %s\n",exe);
399                                 return;
400                         }
401                         string[] args = "/usr/bin/gnome-terminal -x /usr/bin/gdb -ex=r --args".split(" ");
402
403                         
404                         // runs gnome-terminal, with gdb .. running the application..
405                         // fixme -- need a system/which
406                         
407                         args += exe;
408                         if (cg.execute_args.length > 0) {
409                                 var aa = cg.execute_args.split(" ");
410                                 for (var i =0; i < aa.length; i++) {
411                                         args += aa[i];
412                                 }
413                         }
414
415                     print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
416                     
417                     // should be home directory...
418                     
419                     
420                     
421             var exec = new Spawn(GLib.Environment.get_home_dir() , args);
422             exec.detach = true;
423                     exec.run(); 
424                         
425                 }
426         }
427                  
428 }
429 /*
430 int main (string[] args) {
431
432         var a = new ValaSource(file);
433         a.create_valac_tree();
434         return 0;
435 }
436 */
437
438