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 namespace Palete {
5         
6         public class ValaSourceReport  : Vala.Report {
7
8
9                 
10                 public Gee.HashMap<int,string> line_warnings
11                 public Gee.HashMap<int,string> line_depr;
12                 public Gee.HashMap<int,string> line_errors;
13
14                 public ValaSourceReport()
15                 {
16                         base();
17                         this.line_errors = new Gee.HashMap<int,string> ();
18                         this.line_depr = new Gee.HashMap<int,string> ();
19                         this.line_warnings = new Gee.HashMap<int,string> ();
20                 }
21                 
22                 public override void warn (Vala.SourceReference? source, string message) {
23                         errors++;
24                         if (source == null) {
25                                 return;
26                                 //stderr.printf ("My error: %s\n", message);
27                         }
28                         if (source.file.filename != "~~~~~testfile.vala") {
29                                 print ("Warning: %d:  %s\n", source.begin.line, message);
30                                 return;
31                         }
32                         var pre = "";
33                         if (line_warnings.has_key(source.begin.line)) {
34                                 pre = line_warnings.get(source.begin.line) + "\n";
35                                 
36                         }
37                         line_warnings.set(source.begin.line, pre +  "Warn: " + message);
38                         print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
39                 }
40                 public override void depr (Vala.SourceReference? source, string message) {
41                         errors++;
42                         if (source == null) {
43                                 return;
44                                 //stderr.printf ("My error: %s\n", message);
45                         }
46                         if (source.file.filename != "~~~~~testfile.vala") {
47                                 print ("Depr: %d:  %s\n", source.begin.line, message);
48                                 return;
49                         }
50                         var pre = "";
51                         if (line_depr.has_key(source.begin.line)) {
52                                 pre = line_depr.get(source.begin.line) + "\n";
53                                 
54                         }
55                         line_depr.set(source.begin.line, pre +  "Warn: " + message);
56                         print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
57                 }
58                 
59                 public override void err (Vala.SourceReference? source, string message) {
60                         errors++;
61                         if (source == null) {
62                                 return;
63                                 //stderr.printf ("My error: %s\n", message);
64                         }
65                         if (source.file.filename != "~~~~~testfile.vala") {
66                                 print ("Other file: Got error error: %d:  %s\n", source.begin.line, message);
67                                 return;
68                         }
69                         var pre = "";
70                         if (line_errors.has_key(source.begin.line)) {
71                                 pre = line_errors.get(source.begin.line) + "\n";
72                                 
73                         }
74                         line_errors.set(source.begin.line, pre +  message);
75                         print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
76                 }
77                 public void dump()
78                 {
79                         var iter = this.line_errors.map_iterator();
80                         while (iter.next()) {
81                                 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
82                         }
83                 }
84
85         }
86
87         public class ValaSource : Vala.CodeVisitor {
88
89                 Vala.CodeContext context;
90                 ValaSourceReport report;
91                 JsRender.JsRender file; 
92                 public ValaSource(JsRender.JsRender file) {
93                         base();
94                         this.file = file;
95                         
96
97                 }
98                 public void dumpCode(string str) {
99                         var ls = str.split("\n");
100                         for (var i=0;i < ls.length; i++) {
101                                 print("%d : %s\n", i+1, ls[i]);
102                         }
103                 }
104                 
105                 public Gee.HashMap<int,string> checkFile()
106                 {
107                         return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
108                 }
109
110                 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
111                                         JsRender.Node node, 
112                                         string prop,
113                                         string ptype,
114                                         string val)
115                 {
116                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
117                         var hash = ptype == "listener" ? node.listeners : node.props;
118                         
119                         // untill we get a smarter renderer..
120                         // we have some scenarios where changing the value does not work
121                         if (prop == "* xns" || prop == "xtype") {
122                                 return ret;
123                         }
124                                 
125                         
126                         var old = hash.get(prop);
127                         var newval = "/*--VALACHECK-START--*/ " + val ;
128                         
129                         hash.set(prop, newval);
130                         var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
131                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
132                         var offset =0;
133                         if (bits.length > 0) {
134                                 offset = bits[0].split("\n").length +1;
135                         }
136                         //this.dumpCode(tmpstring);
137                         //print("offset %d\n", offset);
138                         yield this.checkStringThread(tmpstring);
139                         hash.set(prop, old);
140                         // modify report
141                         
142                         var iter = this.report.line_errors.map_iterator();
143                         while (iter.next()) {
144                                 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
145                                 // we have to prefix the error with the fake line number 
146                                 // so that it's a unique mark..
147                                  ret.set(iter.get_key() - offset, 
148                                        "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
149                         }
150                         return ret;
151                         
152                 }
153                 
154                 public async  Gee.HashMap<int,string> checkStringThread(string contents)
155                 {
156                         SourceFunc callback = checkStringThread.callback;
157                         var ret = new Gee.HashMap<int,string>();
158                         ThreadFunc<void*> run = () => {
159                                  
160                                 // Pass back result and schedule callback
161                                 ret = this.checkString(contents);
162                                 Idle.add((owned) callback);
163                                 return null;
164                         };
165                         Thread.create<void*>(run, false);
166
167                         // Wait for background thread to schedule our callback
168                         yield;
169                         return ret;
170                 }
171                 
172                 
173                 
174                 public Gee.HashMap<int,string> checkString(string contents)
175                 {
176                         // init context:
177                         var valac = "valac " ;
178                         
179                         context = new Vala.CodeContext ();
180                         Vala.CodeContext.push (context);
181                 
182                         context.experimental = false;
183                         context.experimental_non_null = false;
184                         
185 #if VALA_0_28
186                         var ver=28;
187 #elif VALA_0_26 
188                         var ver=26;
189 #elif VALA_0_24
190                         var ver=24;
191 #elif VALA_0_22 
192                         var ver=22;
193 #endif
194                         
195                         for (int i = 2; i <= ver; i += 2) {
196                                 context.add_define ("VALA_0_%d".printf (i));
197                         }
198                         
199                         
200                         
201                         
202                         
203                         
204                         
205                         var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
206                         // what's the current version of vala???
207                         
208                         
209                         vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
210                         
211                         for(var i =0 ; i < vapidirs.length; i++) {
212                                 valac += " --vapidir=" + vapidirs[i];
213                         }
214                                 
215                         
216                         // or context.get_vapi_path("glib-2.0"); // should return path..
217                         context.vapi_directories = vapidirs;
218                         context.report.enable_warnings = true;
219                         context.metadata_directories = { };
220                         context.gir_directories = {};
221                         context.thread = true;
222                         
223                         
224                         this.report = new ValaSourceReport();;
225                         context.report = this.report;
226                         
227                         
228                         context.basedir = "/tmp"; //Posix.realpath (".");
229                 
230                         context.directory = context.basedir;
231                 
232
233                         // add default packages:
234                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
235                         context.profile = Vala.Profile.GOBJECT;
236                          
237                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
238                         context.root.add_using_directive (ns_ref);
239
240                         var source_file = new Vala.SourceFile (
241                                 context, 
242                                 Vala.SourceFileType.SOURCE, 
243                                         "~~~~~testfile.vala",
244                                         contents
245                         );
246                         source_file.add_using_directive (ns_ref);
247                         context.add_source_file (source_file);
248                         
249                 // add all the files (except the current one) - this.file.path
250                 var pr = ((Project.Gtk)this.file.project);
251                 if (this.file.build_module.length > 0) {
252                                 var cg =  pr.compilegroups.get(this.file.build_module);
253                                 for (var i = 0; i < cg.sources.size; i++) {
254                                         var path = pr.resolve_path(
255                                 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
256                                 
257                         if (!FileUtils.test(path, FileTest.EXISTS)) {
258                                                 continue;
259                                         }       
260                          
261                                         if (path == this.file.path.replace(".bjs", ".vala")) {
262                                                 valac += " " + path;
263                                                 continue;
264                                         }
265                                         if (FileUtils.test(path, FileTest.IS_DIR)) {
266                                                 continue;
267                                         }
268                                         //print("Add source file %s\n", path);
269                                         
270                                         valac += " " + path;
271                                         
272                                         if (Regex.match_simple("\\.c$", path)) {
273                                                 context.add_c_source_file(path);
274                                                 continue;
275                                         }
276                                         
277                                         
278                                         var xsf = new Vala.SourceFile (
279                                                 context,
280                                                 Vala.SourceFileType.SOURCE, 
281                                                 path
282                                         );
283                                         xsf.add_using_directive (ns_ref);
284                                         context.add_source_file(xsf);
285                                         
286                                 }
287                         }
288                         // default.. packages..
289                         context.add_external_package ("glib-2.0"); 
290                         context.add_external_package ("gobject-2.0");
291                         // user defined ones..
292                         
293                 var dcg = pr.compilegroups.get("_default_");
294                 for (var i = 0; i < dcg.packages.size; i++) {
295                                 valac += " --pkg " + dcg.packages.get(i);
296                                 context.add_external_package (dcg.packages.get(i));
297                         }
298                 
299                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
300                         
301                         // add the modules...
302                         
303                         
304                         
305                         //context.add_external_package ("libvala-0.24");
306                         
307                         
308
309                 
310                         //add_documented_files (context, settings.source_files);
311                 
312                         Vala.Parser parser = new Vala.Parser ();
313                         parser.parse (context);
314                         //gir_parser.parse (context);
315                         if (context.report.get_errors () > 0) {
316                                 print("parse got errors");
317                                 ((ValaSourceReport)context.report).dump();
318                                 Vala.CodeContext.pop ();
319                                 return this.report.line_errors;
320                         }
321
322
323                         
324                         // check context:
325                         context.check ();
326                         if (context.report.get_errors () > 0) {
327                                 print("check got errors");
328                                 ((ValaSourceReport)context.report).dump();
329                                 Vala.CodeContext.pop ();
330                                 return this.report.line_errors;
331                                 
332                         }
333                         
334                         context.codegen = new Vala.GDBusServerModule ();
335                         
336                          
337                         context.output = "/tmp/testbuild";
338                         valac += " -o " +context.output;
339                         context.codegen.emit (context);
340                          
341                         var ccompiler = new Vala.CCodeCompiler ();
342                         var cc_command = Environment.get_variable ("CC");
343                         var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
344 #if VALA_0_28
345                         ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
346 #else
347                         ccompiler.compile (context, cc_command, new string[] { });
348 #endif
349
350  
351                         Vala.CodeContext.pop ();
352                         print("%s\n", valac);
353                         print("ALL OK?\n");
354                         return this.report.line_errors;
355                 }
356         //
357                 // startpoint:
358                 //
359          
360         }
361 }
362 /*
363 int main (string[] args) {
364
365         var a = new ValaSource(file);
366         a.create_valac_tree();
367         return 0;
368 }
369 */
370
371