sync
[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                 public void spawnExecute(JsRender.JsRender file)
204                 {
205                         // race condition..
206                         if (this.compiler != null) { 
207                                 return;
208                         }
209                         if (!(file.project is Project.Gtk)) {
210                             return;
211                         }
212                         var pr = (Project.Gtk)(file.project);
213                         
214                         
215                         this.file = file;
216                         this.line_offset = 0;
217                           
218                         string[] args = {};
219                         args += BuilderApplication._self;
220                         args += "--project";
221                         args += this.file.project.fn;
222                         args += "--target";
223                         if (this.file.build_module.length > 0 ) {
224                             args += this.file.build_module;
225                         } else {
226                             args += pr.firstBuildModule();
227                         }
228                         //args += "--output"; -- set up by the module -- defaults to testrun
229                         //args += "/tmp/testrun";
230                         
231                         // assume code is in home...
232                         try {
233                             this.compiler = new Spawn( GLib.Environment.get_home_dir(), args);
234                             this.compiler.output_line.connect(compile_output_line);
235                             this.compiler.complete.connect(runResult);
236                             this.compiler.run(); 
237                                 this.children.add(this.compiler); //keep a reference...
238                          
239                         } catch (GLib.Error e) {
240                             GLib.debug(e.message);
241                             this.compiler = null;
242
243                         }
244                         return;
245                          
246                 }
247                 public void compile_output_line(   string str )
248                 {
249                         this.compile_output(str);
250                 }
251                 /**
252                 * Used to compile a non builder file..
253                 */
254                  
255                 public bool checkPlainFileSpawn(  JsRender.JsRender file, string contents )
256                 {
257                         // race condition..
258                         if (this.compiler != null) { 
259                                 return false;
260                         }
261                         var pr = (Project.Gtk)(file.project);
262                         
263                         var m = pr.firstBuildModule();
264                         var cg = pr.compilegroups.get(m);
265                         var foundit = false;
266                         for (var i = 0; i < cg.sources.size; i++) {
267                             var path = pr.resolve_path(
268                                     pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
269                             if (path == file.path) {
270                                 foundit = true;
271                                 break;
272                                         }
273                         
274                         }
275                         if (!foundit) {
276                           
277                             this.compiler = null;
278                         
279                             return false; // do not run the compile..
280                         }
281                         // is the file in the module?
282                         
283                         
284                         FileIOStream iostream;
285                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
286                         tmpfile.ref();
287
288                         OutputStream ostream = iostream.output_stream;
289                         DataOutputStream dostream = new DataOutputStream (ostream);
290                         dostream.put_string (contents);
291                         
292                         
293                         this.file = null;
294                         this.line_offset = 0;
295                           
296                         string[] args = {};
297                         args += BuilderApplication._self;
298                         args += "--project";
299                         args +=  file.project.fn;
300                         args += "--target";
301                         args += pr.firstBuildModule();
302                         args += "--add-file";
303                         args +=  tmpfile.get_path();
304                         args += "--skip-file";
305                         args += file.path;
306                          
307                         
308                         
309                         
310                         try {
311                             this.compiler = new Spawn("/tmp", args);
312                             this.compiler.complete.connect(spawnResult);
313                             this.compiler.run(); 
314                         } catch (GLib.Error e) {
315                             
316                             this.compiler = null;
317                             return false;
318                         }
319                         return true;
320                          
321                 }
322                 
323                 
324                 public void spawnResult(int res, string output, string stderr)
325                 {
326                          
327                                 
328                         try { 
329                                 //GLib.debug("GOT output %s", output);
330                                 
331                                 var pa = new Json.Parser();
332                                 pa.load_from_data(output);
333                                 var node = pa.get_root();
334
335                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
336                                         var ret = new Json.Object();
337                                         ret.set_boolean_member("success", false);
338                                         ret.set_string_member("message", 
339                                                 "Compiler returned Unexpected element type %s".printf( 
340                                                         node.type_name ()
341                                                 )
342                                         );
343                                         this.compiled(ret);
344                                         this.compiler = null;
345                                 }
346                                 var ret = node.get_object ();
347                                 ret.set_int_member("line_offset", this.line_offset);
348                                 
349                                 this.compiled(ret);
350                                 
351                                 
352                         } catch (GLib.Error e) {
353                                 var ret = new Json.Object();
354                                 ret.set_boolean_member("success", false);
355                                 ret.set_string_member("message", e.message);
356                                 this.compiled(ret);
357                         }
358                         this.compiler = null;
359                         //compiler.unref();
360                         //tmpfile.unref();
361                          
362                         
363                         
364                 }
365                 
366                 public void runResult(int res, string output, string stderr)
367                 {
368                         this.compiler = null;
369                         var exe = "/tmp/testrun";
370                         var mod = "";
371                         var pr = (Project.Gtk)(this.file.project);
372                         
373                         
374                         
375                         if (this.file.build_module.length > 0 ) {
376                     mod =  this.file.build_module;
377                         } else {
378                             mod =  pr.firstBuildModule();
379                         }
380                         if (mod.length < 1) {
381                                 return;
382                         }
383                         var cg =  pr.compilegroups.get(mod);
384                         if (cg.target_bin.length > 0) {
385                                 exe = cg.target_bin;
386                         }
387                         
388                         
389                         if (!GLib.FileUtils.test(exe, GLib.FileTest.EXISTS)) {
390                                 print("Missing output file: %s\n",exe);
391                                 return;
392                         }
393                         string[] args = "/usr/bin/gnome-terminal -x /usr/bin/gdb -ex=r --args".split(" ");
394
395                         
396                         // runs gnome-terminal, with gdb .. running the application..
397                         // fixme -- need a system/which
398                         
399                         args += exe;
400                         if (cg.execute_args.length > 0) {
401                                 var aa = cg.execute_args.split(" ");
402                                 for (var i =0; i < aa.length; i++) {
403                                         args += aa[i];
404                                 }
405                         }
406
407                     print("OUT: %s\n\n----\nERR:%s\n", output, stderr);
408                     
409                     // should be home directory...
410                     
411                     
412                     
413             var exec = new Spawn(GLib.Environment.get_home_dir() , args);
414             exec.detach = true;
415                     exec.run(); 
416                         
417                 }
418         }
419                  
420 }
421 /*
422 int main (string[] args) {
423
424         var a = new ValaSource(file);
425         a.create_valac_tree();
426         return 0;
427 }
428 */
429
430