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