src/Project/Gtk.vala
[app.Builder.js] / src / Project / Gtk.vala
1 //<Script type="text/javascript">
2 /**
3  * Gtk projects - normally vala based now..
4  * 
5  * should have a few extra features..
6  * 
7  * like:
8  *   compile flags etc..
9  *   different versions (eg. different files can compile different versions - eg. for testing.
10  *   
11  * If we model this like adjuta - then we would need a 'project' file that is actually in 
12  * the directory somewhere... - and is revision controlled etc..
13  * 
14  * builder.config ??
15  * 
16  * 
17  * 
18  * 
19  */
20  
21
22 namespace Project {
23         static int gtk_id = 1;
24  
25
26         public class Gtk : Project
27         {
28           
29                 public Gtk(string path) {
30                   
31                   
32                         base(path);
33                         this.xtype = "Gtk";
34                         var gid = "project-gtk-%d".printf(gtk_id++);
35                         this.id = gid;
36                         this.loadConfig();
37                 
38                 }
39                 public Gee.HashMap<string,GtkValaSettings> compilegroups;
40                 
41                 public void loadConfig() throws GLib.Error 
42                 {
43                         // load a builder.config JSON file.
44                         // 
45                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
46                         
47                         
48                         var fn = this.firstPath() + "/config1.builder";
49                         GLib.debug("load: " + fn );
50                         
51                         if (!FileUtils.test(fn, FileTest.EXISTS)) {
52                                 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
53                                 return;
54                         }
55
56                         var pa = new Json.Parser();
57                         pa.load_from_file(fn);
58                         var node = pa.get_root();
59
60                         // should be an array really.
61                         if (node.get_node_type () != Json.NodeType.ARRAY) {
62                                 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
63                         }
64                         
65                         var obj = node.get_array ();
66                         for(var i= 0;i<obj.get_length();i++) {
67                                 var el = obj.get_object_element(i);
68                                 var vs = new GtkValaSettings.from_json(el);
69                                 if (vs.name != "_default_") {
70                                         vs.parent = this.compilegroups.get("_default_");
71                                 }
72                                 this.compilegroups.set(vs.name,vs);
73                         }
74                         GLib.debug("%s\n",this.configToString ());
75                         
76                 }
77                 public string configToString()
78                 {
79                         var ar = new Json.Array();
80                         var iter = this.compilegroups.map_iterator();
81                         while(iter.next()) {
82                                  
83                                 ar.add_object_element(iter.get_value().toJson());
84                         }
85
86                         var generator = new Json.Generator ();
87                         generator.indent = 4;
88                         generator.pretty = true;
89                         var node = new Json.Node(Json.NodeType.ARRAY);
90                         node.set_array(ar);
91                         generator.set_root(node);
92                         return generator.to_data(null);
93                 }
94                 
95                 public void writeConfig()
96                 {
97                         var fn = this.firstPath() + "/config1.builder";
98                         GLib.debug("write: " + fn );
99
100                          
101                         var f = GLib.File.new_for_path(fn);
102                         var data_out = new GLib.DataOutputStream(
103                                         f.replace(null, false, GLib.FileCreateFlags.NONE, null)
104                         );
105                         data_out.put_string(this.configToString(), null);
106                         data_out.close(null);
107                         
108                         return ;
109                          
110                 }
111                 /**
112                  *  perhaps we should select the default in the window somewhere...
113                  */ 
114                 public string firstBuildModule()
115                 {
116                         var iter = this.compilegroups.map_iterator();
117                         while(iter.next()) {
118                                  
119                                  if (iter.get_value().name == "__default__") {
120                                          continue;
121                                  }
122                                  
123                                  return iter.get_value().name;
124                         }
125                         return "";
126                 }
127                 
128                 
129                 public string relPath(string target)
130                 {
131                         var basename = this.firstPath();
132                         // eg. base = /home/xxx/fred/blogs
133                         // target = /home/xxx/fred/jones
134                         
135                         // this does not work correctly...
136                         var bb = basename;
137                         var prefix = "";
138                         while (true) {
139                                 if (    bb.length < target.length &&
140                                         target.substring(0, bb.length) == bb) {
141                                         
142                                         return prefix + target.substring(bb.length +1);
143                                 }
144                                 if (bb.length < 1) {
145                                         throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
146                                                                         basename, target);
147                                 }
148                                 bb = GLib.Path.get_dirname(bb);
149                                 prefix += "../";
150                                 
151                         }
152          
153                 }
154                 /**
155                  * get a list of files for a folder..
156                  * 
157                  * - in the project manager this has to list all possible compilable 
158                  *   files  - eg. exclue XXX.vala.c or XXX.c with the same name as 
159                  *   a vala file (so to ignore the generated files)
160                  * 
161                  * - for the editor navigation - this should exclude all files that
162                  *   are vala based on a bjs file..
163                  *  
164                  */
165                 
166                 public Gee.ArrayList<string> filesAll(string in_path)
167                 {
168                         var ret =  new Gee.ArrayList<string>();
169                         
170                         var dirname = this.resolve_path(
171                                 this.resolve_path_combine_path(this.firstPath(),in_path));
172                         
173                         GLib.debug("SCAN %s\n", dirname);
174                                 // scan the directory for files -- ending with vala || c
175                         
176
177                         var dir = File.new_for_path(dirname);
178                         if (!dir.query_exists()) {
179                                 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
180                                 return ret;
181                         }
182           
183            
184                         try {
185                                 var file_enum = dir.enumerate_children(
186                                         GLib.FileAttribute.STANDARD_DISPLAY_NAME, 
187                                         GLib.FileQueryInfoFlags.NONE, 
188                                         null
189                                 );
190                 
191                  
192                                 FileInfo next_file; 
193                                 while ((next_file = file_enum.next_file(null)) != null) {
194                                         var fn = next_file.get_display_name();
195                                         
196                                         GLib.debug("SCAN %s - checking %s\n", dirname, fn);
197                                          
198                                         ret.add(in_path + "/" + fn);
199                                          
200                                         // any other valid types???
201                                 
202                                 }
203                                 
204                         } catch(Error e) {
205                                 GLib.warning("oops - something went wrong scanning the projects\n");
206                         }       
207                         
208                         return ret;
209                 }
210                 
211                 public Gee.ArrayList<string> filesForCompile(string in_path)
212                 {
213                         var allfile = this.fileAll();
214                         var ret =  new Gee.ArrayList<string>();
215                         
216                         
217                         for (var i = 0; i < cfiles.size; i ++) {
218                                 var fn = cfiles.get(i);
219                                 if (Regex.match_simple("\\.vala$", fn)) {
220                                         ret.add( fn);
221                                         continue;
222                                 }
223                                 
224                                 // got a file that is not 
225                                 
226                                 vv = fn;
227                                 try {
228                                         vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
229                                 } catch (Error e) {
230                                         continue;
231                                 }
232                                 if (Regex.match_simple("\\.vala\\.c$", fn)) {
233                                                 continue;
234                                         }       
235                                                 
236                                 if (ret.index_of( vv) > -1) {
237                                         continue;
238                                 }
239                                 ret.add(fn);
240                         }
241                         
242                 }
243                 
244                 
245                 public Gee.ArrayList<string> files(string in_path)
246                 {
247                         var ret =  new Gee.ArrayList<string>();
248                         var cfiles =  new Gee.ArrayList<string>();
249                         
250                         var dirname = this.resolve_path(
251                                 this.resolve_path_combine_path(this.firstPath(),in_path));
252                         
253                         GLib.debug("SCAN %s\n", dirname);
254                                 // scan the directory for files -- ending with vala || c
255                         
256
257                         var dir = File.new_for_path(dirname);
258                         if (!dir.query_exists()) {
259                                 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
260                                 return ret;
261                         }
262           
263            
264                         try {
265                                 var file_enum = dir.enumerate_children(
266                                         GLib.FileAttribute.STANDARD_DISPLAY_NAME, 
267                                         GLib.FileQueryInfoFlags.NONE, 
268                                         null
269                                 );
270                 
271                  
272                                 FileInfo next_file; 
273                                 while ((next_file = file_enum.next_file(null)) != null) {
274                                         var fn = next_file.get_display_name();
275                                         
276                                         GLib.debug("SCAN %s - checking %s\n", dirname, fn);
277                                         if (Regex.match_simple("\\.vala$", fn)) {
278                                                 ret.add(in_path + "/" + fn);
279                                                 continue;
280                                         }
281                                         
282                                         if (Regex.match_simple("\\.vala\\.c$", fn)) {
283                                                 continue;
284                                         }
285                                         if (Regex.match_simple("\\.c$", fn)) {
286                                                 
287                                                 // if we have a vala file with the same name 
288                                                 // then do not add it...
289                                                 
290                                                 cfiles.add(in_path + "/" + fn);
291                                                 continue;
292                                         }
293                                         // any other valid types???
294                                 
295                                 }
296                                 
297                         } catch(Error e) {
298                                 GLib.warning("oops - something went wrong scanning the projects\n");
299                         }       
300                          
301                                  
302                                 // add the cfiles to ret - if they do not have a vala...
303                         for (var i = 0; i < cfiles.size; i ++) {
304                                 
305                                 var fn = cfiles.get(i);
306                                 vv = fn;
307                                 try {
308                                         vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
309                                 } catch (Error e) {
310                                         continue;
311                                 }
312                                         
313                                                 
314                                 if (ret.index_of( vv) > -1) {
315                                         continue;
316                                 }
317                                 ret.add(fn);
318                         }
319                                                 
320                          
321                         GLib.debug("SCAN %s = returning %d", dirname, ret.size);
322                          
323                         return ret;
324                         
325
326                 }
327  
328
329                 public   string  resolve_path_combine_path(string first, string second)
330                 {
331                         string ret = first;
332                         if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
333                         {
334                                 ret += "/";
335                         }
336                         //print("combined path = %s",  ret + second);
337                         return ret + second;
338                 }
339                 public   string  resolve_path_times(string part, int times, string? clue = null)
340                 {
341                         string ret = "";
342                         for (int i = 0; i < times; i++)
343                         {
344                                 if (clue != null && i > 0)
345                                 {
346                                         ret += clue;
347                                 }
348                                 ret += part;
349                         }
350                         return ret;
351                 }
352                 public   string resolve_path(string _path, string? relative = null)
353                 {
354                         string path = _path;
355                         if (relative != null)
356                         {
357                                 path = this.resolve_path_combine_path(path, relative);
358                         }
359                         string[] parts = path.split("/");
360                         string[] ret = {};
361                         int relative_parts = 0;
362                                         
363                         foreach (var part in parts)
364                         {
365                                 if (part.length < 1 || part == ".")
366                                 {
367                                         continue;
368                                 }
369                                 
370                                 if (part == "..")
371                                 {
372                                         if (ret.length > 0)
373                                         {
374                                                 ret = ret[0: ret.length -1];
375                                         }
376                                         else
377                                         {
378                                                 relative_parts++;
379                                         }
380                                         continue;
381                                 }
382                                 
383                                 ret += part;
384                         }
385                         
386                         path =  this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
387                         if (_path.has_prefix("/"))
388                         {
389                                 path = "/" + path;
390                         }
391                         return path;
392                 }
393                 
394                 public string[] vapidirs()
395                 {
396                         string[] ret = {};
397                         var sources = this.compilegroups.get("_default_").sources;
398                         for(var i =0; i< sources.size; i++) {
399                                 
400                                 var path = this.resolve_path( this.firstPath(), sources.get(i));
401                                 
402                                 if (Path.get_basename (path) == "vapi") {
403                                         GLib.debug("Adding VAPIDIR: %s\n", path);
404                                         ret += path;
405                                 }
406                                 
407                         }
408                         return ret;
409                         
410                 }
411                         
412
413         }
414         // an object describing a build config (or generic ...)
415         public class GtkValaSettings : Object {
416                 public string name;
417                 public GtkValaSettings? parent;
418                 
419                 public string compile_flags; // generic to all.
420                 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
421                 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
422                 public string target_bin;
423
424
425                 public GtkValaSettings(string name) 
426                 {
427                         this.name = name;
428                         this.compile_flags = "";
429                         this.target_bin = "";
430                         this.packages = new Gee.ArrayList<string>();
431                         this.sources = new Gee.ArrayList<string>();
432                                 
433                 }
434                 
435                 
436                 public GtkValaSettings.from_json(Json.Object el) {
437
438                         
439                         this.name = el.get_string_member("name");
440                         this.compile_flags = el.get_string_member("compile_flags");
441                         this.target_bin = el.get_string_member("target_bin");
442                         // sources and packages.
443                         this.sources = this.readArray(el.get_array_member("sources"));
444                         this.packages = this.readArray(el.get_array_member("packages"));
445                         
446                 }
447                 
448                 
449                 
450                 public Gee.ArrayList<string> readArray(Json.Array ar) 
451                 {
452                         var ret = new Gee.ArrayList<string>();
453                         for(var i =0; i< ar.get_length(); i++) {
454                                 ret.add(ar.get_string_element(i));
455                         }
456                         return ret;
457                 }
458                 
459                 public Json.Object toJson()
460                 {
461                         var ret = new Json.Object();
462                         ret.set_string_member("name", this.name);
463                         ret.set_string_member("compile_flags", this.compile_flags);
464                         ret.set_string_member("target_bin", this.target_bin);
465                         ret.set_array_member("sources", this.writeArray(this.sources));
466                         ret.set_array_member("packages", this.writeArray(this.packages));
467                         return ret;
468                 }
469                 public Json.Array writeArray(Gee.ArrayList<string> ar) {
470                         var ret = new Json.Array();
471                         for(var i =0; i< ar.size; i++) {
472                                 ret.add_string_element(ar.get(i));
473                         }
474                         return ret;
475                 }
476                 
477                 
478         }
479  
480    
481 }