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