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