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.complete.connect(runResult);
229                             this.compiler.run(); 
230                         
231                          
232                         } catch (GLib.Error e) {
233                             GLib.debug(e.message);
234                             this.compiler = null;
235
236                         }
237                         return;
238                          
239                 }
240                 
241                 
242                 /**
243                 * Used to compile a non builder file..
244                 */
245                  
246                 public bool checkPlainFileSpawn(  JsRender.JsRender file, string contents )
247                 {
248                         // race condition..
249                         if (this.compiler != null) { 
250                                 return false;
251                         }
252                         var pr = (Project.Gtk)(file.project);
253                         
254                         var m = pr.firstBuildModule();
255                         var cg = pr.compilegroups.get(m);
256                         var foundit = false;
257                         for (var i = 0; i < cg.sources.size; i++) {
258                             var path = pr.resolve_path(
259                                     pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
260                             if (path == file.path) {
261                                 foundit = true;
262                                 break;
263                                         }
264                         
265                         }
266                         if (!foundit) {
267                           
268                             this.compiler = null;
269                         
270                             return false; // do not run the compile..
271                         }
272                         // is the file in the module?
273                         
274                         
275                         FileIOStream iostream;
276                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
277                         tmpfile.ref();
278
279                         OutputStream ostream = iostream.output_stream;
280                         DataOutputStream dostream = new DataOutputStream (ostream);
281                         dostream.put_string (contents);
282                         
283                         
284                         this.file = null;
285                         this.line_offset = 0;
286                           
287                         string[] args = {};
288                         args += BuilderApplication._self;
289                         args += "--project";
290                         args +=  file.project.fn;
291                         args += "--target";
292                         args += pr.firstBuildModule();
293                         args += "--add-file";
294                         args +=  tmpfile.get_path();
295                         args += "--skip-file";
296                         args += file.path;
297                          
298                         
299                         
300                         
301                         try {
302                             this.compiler = new Spawn("/tmp", args);
303                             this.compiler.complete.connect(spawnResult);
304                             this.compiler.run(); 
305                         } catch (GLib.Error e) {
306                             
307                             this.compiler = null;
308                             return false;
309                         }
310                         return true;
311                          
312                 }
313                 
314                 
315                 public void spawnResult(int res, string output, string stderr)
316                 {
317                          
318                                 
319                         try { 
320                                 //GLib.debug("GOT output %s", output);
321                                 
322                                 var pa = new Json.Parser();
323                                 pa.load_from_data(output);
324                                 var node = pa.get_root();
325
326                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
327                                         var ret = new Json.Object();
328                                         ret.set_boolean_member("success", false);
329                                         ret.set_string_member("message", 
330                                                 "Compiler returned Unexpected element type %s".printf( 
331                                                         node.type_name ()
332                                                 )
333                                         );
334                                         this.compiled(ret);
335                                         this.compiler = null;
336                                 }
337                                 var ret = node.get_object ();
338                                 ret.set_int_member("line_offset", this.line_offset);
339                                 
340                                 this.compiled(ret);
341                                 
342                                 
343                         } catch (GLib.Error e) {
344                                 var ret = new Json.Object();
345                                 ret.set_boolean_member("success", false);
346                                 ret.set_string_member("message", e.message);
347                                 this.compiled(ret);
348                         }
349                         this.compiler = null;
350                         //compiler.unref();
351                         //tmpfile.unref();
352                          
353                         
354                         
355                 }
356                 
357                 public void runResult(int res, string output, string stderr)
358                 {
359                         this.compiler = null;
360                     print("OUT: %s\n\n----\nERR:%s", output, stderr);
361                     var exec = new Spawn("/tmp", { "/tmp/testrun" });
362                     exec.run(); 
363
364                 }
365         }
366                  
367 }
368 /*
369 int main (string[] args) {
370
371         var a = new ValaSource(file);
372         a.create_valac_tree();
373         return 0;
374 }
375 */
376
377