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