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
32                 
33                 JsRender.JsRender file;
34                 public int line_offset = 0;
35                 
36                 public ValaSource( ) 
37                 {
38                         base();
39                         this.compiler = null;
40                 }
41                 public void dumpCode(string str) 
42                 {
43                         var ls = str.split("\n");
44                         for (var i=0;i < ls.length; i++) {
45                                 print("%d : %s\n", i+1, ls[i]);
46                         }
47                 }
48                 
49                 //public Gee.HashMap<int,string> checkFile()
50                 //{
51                 //      return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
52                 //}
53
54                 public void checkFileWithNodePropChange(
55                   
56                                         JsRender.JsRender file,
57                                         JsRender.Node node, 
58                                         string prop,
59                                         string ptype,
60                                         string val
61                                  )
62                 {
63                         this.file = file;
64                         
65                         if (this.compiler != null) {
66                                 return;
67                         }
68                         
69                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
70                         var hash = ptype == "listener" ? node.listeners : node.props;
71                         
72                         // untill we get a smarter renderer..
73                         // we have some scenarios where changing the value does not work
74                         if (prop == "* xns" || prop == "xtype") {
75                                 this.compiled(new Json.Object());
76                                 return ;
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                         this.checkStringSpawn(tmpstring );
98                         
99                         // modify report
100                         
101                         
102                         
103                 }
104                 Spawn compiler;
105                  
106                 public void checkStringSpawn(
107                                         string contents 
108                                 )
109                 {
110                         
111                         
112                         
113                         FileIOStream iostream;
114                         var tmpfile = File.new_tmp ("test-XXXXXX.vala", out iostream);
115                         tmpfile.ref();
116
117                         OutputStream ostream = iostream.output_stream;
118                         DataOutputStream dostream = new DataOutputStream (ostream);
119                         dostream.put_string (contents);
120                         
121                         var valafn = "";
122                         try {             
123                            var  regex = new Regex("\\.bjs$");
124                         
125                                 valafn = regex.replace(this.file.path,this.file.path.length , 0 , ".vala");
126                          } catch (GLib.RegexError e) {
127                                 var ret = new Json.Object();
128                                 ret.set_boolean_member("success", false);
129                                 ret.set_string_member("message", e.message);
130                             this.compiled(ret);
131                             return;
132                         }   
133                         
134                         string[] args = {};
135                         args += BuilderApplication._self;
136                         args += "--project";
137                         args += this.file.project.fn;
138                         args += "--target";
139                         args += this.file.build_module;
140                         args += "--add-file";
141                         args +=  tmpfile.get_path();
142                         args += "--skip-file";
143                         args += valafn;
144                         
145                          
146                         
147                         this.compiler = new Spawn("/tmp", args);
148                         this.compiler.complete.connect(spawnResult);
149                         
150                         try {
151                                 this.compiler.run(); 
152                         } catch (GLib.SpawnError e) {
153                                 var ret = new Json.Object();
154                                 ret.set_boolean_member("success", false);
155                                 ret.set_string_member("message", e.message);
156                             this.compiled(ret);
157                             this.compiler = null;
158                         }
159                          
160                 }
161                 
162                 public void checkFileSpawn(JsRender.JsRender file )
163                 {
164                         // race condition..
165                         if (this.compiler != null) { 
166                                 return;
167                         }
168                         
169                         this.file = file;
170                         this.line_offset = 0;
171                           
172                         string[] args = {};
173                         args += BuilderApplication._self;
174                         args += "--project";
175                         args += this.file.project.fn;
176                         args += "--target";
177                         args += this.file.build_module;
178                          
179                          
180                         
181                         this.compiler = new Spawn("/tmp", args);
182                         this.compiler.complete.connect(spawnResult);
183                         
184                         try {
185                                 this.compiler.run(); 
186                         } catch (GLib.SpawnError e) {
187                                 var ret = new Json.Object();
188                                 ret.set_boolean_member("success", false);
189                                 ret.set_string_member("message", e.message);
190                             this.compiled(ret);
191                             this.compiler = null;
192                         }
193                          
194                 }
195                 
196                 
197                 
198                 
199                 public void spawnResult(int res, string output, string stderr)
200                 {
201                          
202                                 
203                         try { 
204                                 GLib.debug("GOT output %s", output);
205                                 
206                                 var pa = new Json.Parser();
207                                 pa.load_from_data(output);
208                                 var node = pa.get_root();
209
210                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
211                                         var ret = new Json.Object();
212                                         ret.set_boolean_member("success", false);
213                                         ret.set_string_member("message", "Compiler returned Unexpected element type %s", node.type_name ());
214                                         this.compiled(ret);
215                                         this.compiler = null;
216                                 }
217                                 var ret = node.get_object ();
218                                 ret.set_int_member("line_offset", this.line_offset);
219                                 
220                                 this.compiled(ret);
221                                 
222                                 
223                         } catch (Error e) {
224                                 var ret = new Json.Object();
225                                 ret.set_boolean_member("success", false);
226                                 ret.set_string_member("message", e.message);
227                                 this.compiled(ret);
228                         }
229                         this.compiler = null;
230                         //compiler.unref();
231                         //tmpfile.unref();
232                          
233                         
234                         
235                 }
236         }
237                  
238 }
239 /*
240 int main (string[] args) {
241
242         var a = new ValaSource(file);
243         a.create_valac_tree();
244         return 0;
245 }
246 */
247
248