a7d9498d176b3eb3e7d9217d665f020370e4d958
[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 < allfiles.size; i ++) {
218                                 var fn = allfiles.get(i);
219                                 try {
220                                         if (Regex.match_simple("\\.vala$", fn)) {
221                                                 ret.add( fn);
222                                                 continue;
223                                         }
224                                         // vala.c -- ignore..
225                                         if (Regex.match_simple("\\.vala\\.c$", fn)) {
226                                                 continue;
227                                         }
228                                         // not a c file...
229                                         if (!Regex.match_simple("\\.c$", fn)) {
230                                                 continue;
231                                         }
232                                         
233                                         // is the c file the same as a vala file...
234                                         
235                                          
236                                         
237                                         var vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
238                                 
239                                         
240                                                 
241                                         if (allfiles.index_of( vv) > -1) {
242                                                 continue;
243                                         }
244                                         // add the 'c' file..
245                                         ret.add(fn);
246                                 } catch (Error e) {
247                                         continue;
248                                 }
249                         }
250                         // sort.
251                         ret.sort((fa,fb) => {
252                                 return ((string)fa).collate(string) fb);
253                         });
254                         return ret;
255                         
256                 }
257                 
258                 public Gee.ArrayList<string> filesForOpen(string in_path)
259                 {
260                         var allfiles = this.fileAll();
261                         var ret =  new Gee.ArrayList<string>();
262                         
263                         
264                         for (var i = 0; i < allfiles.size; i ++) {
265                                 var fn = cfiles.get(i);
266                                 try {
267                                         
268                                         if (Regex.match_simple("\\.vala\\.c$", fn)) {
269                                                 continue;
270                                         }
271                                         
272                                         if (Regex.match_simple("\\.bjs$", fn)) {
273                                                 continue;
274                                         }
275                                         
276                                         if (Regex.match_simple("\\.vala$", fn)) {
277                                                 var vv = (new Regex("\\.vala$")).replace( fn, fn, 0, ".bjs");
278                                                 if (allfiles.index_of( vv) > -1) {
279                                                         continue;
280                                                 }
281                                                 
282                                                 ret.add( fn);
283                                                 continue;
284                                         }
285                                         // vala.c -- ignore..
286                                         
287                                         // not a c file...
288                                         if (Regex.match_simple("\\.c$", fn)) {
289                                                 
290                                                 var vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
291                                                 if (allfiles.index_of( vv) > -1) {
292                                                         continue;
293                                                 }
294                                                 ret.add( fn);
295                                                 continue;
296                                         }
297                                         // not .c / not .vala /not .bjs.. -- other type of file..
298                                         // allow ???
299                                         
300                                 
301                                         
302                                                 
303                                         if (ret.index_of( vv) > -1) {
304                                                 continue;
305                                         }
306                                         // add the 'c' file..
307                                         ret.add(fn);
308                                 } catch (Error e) {
309                                         continue;
310                                 }
311                         }
312                         // sort.
313                         ret.sort((fa,fb) => {
314                                 return ((string)fa).collate(string) fb);
315                         });
316                         return ret;
317                         
318                 }
319                 
320                 
321                 
322                 
323                 public Gee.ArrayList<string> files(string in_path)
324                 {
325                         var ret =  new Gee.ArrayList<string>();
326                         var cfiles =  new Gee.ArrayList<string>();
327                         
328                         var dirname = this.resolve_path(
329                                 this.resolve_path_combine_path(this.firstPath(),in_path));
330                         
331                         GLib.debug("SCAN %s\n", dirname);
332                                 // scan the directory for files -- ending with vala || c
333                         
334
335                         var dir = File.new_for_path(dirname);
336                         if (!dir.query_exists()) {
337                                 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
338                                 return ret;
339                         }
340           
341            
342                         try {
343                                 var file_enum = dir.enumerate_children(
344                                         GLib.FileAttribute.STANDARD_DISPLAY_NAME, 
345                                         GLib.FileQueryInfoFlags.NONE, 
346                                         null
347                                 );
348                 
349                  
350                                 FileInfo next_file; 
351                                 while ((next_file = file_enum.next_file(null)) != null) {
352                                         var fn = next_file.get_display_name();
353                                         
354                                         GLib.debug("SCAN %s - checking %s\n", dirname, fn);
355                                         if (Regex.match_simple("\\.vala$", fn)) {
356                                                 ret.add(in_path + "/" + fn);
357                                                 continue;
358                                         }
359                                         
360                                         if (Regex.match_simple("\\.vala\\.c$", fn)) {
361                                                 continue;
362                                         }
363                                         if (Regex.match_simple("\\.c$", fn)) {
364                                                 
365                                                 // if we have a vala file with the same name 
366                                                 // then do not add it...
367                                                 
368                                                 cfiles.add(in_path + "/" + fn);
369                                                 continue;
370                                         }
371                                         // any other valid types???
372                                 
373                                 }
374                                 
375                         } catch(Error e) {
376                                 GLib.warning("oops - something went wrong scanning the projects\n");
377                         }       
378                          
379                                  
380                                 // add the cfiles to ret - if they do not have a vala...
381                         for (var i = 0; i < cfiles.size; i ++) {
382                                 
383                                 var fn = cfiles.get(i);
384                                 vv = fn;
385                                 try {
386                                         vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
387                                 } catch (Error e) {
388                                         continue;
389                                 }
390                                         
391                                                 
392                                 if (ret.index_of( vv) > -1) {
393                                         continue;
394                                 }
395                                 ret.add(fn);
396                         }
397                                                 
398                          
399                         GLib.debug("SCAN %s = returning %d", dirname, ret.size);
400                          
401                         return ret;
402                         
403
404                 }
405  
406
407                 public   string  resolve_path_combine_path(string first, string second)
408                 {
409                         string ret = first;
410                         if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
411                         {
412                                 ret += "/";
413                         }
414                         //print("combined path = %s",  ret + second);
415                         return ret + second;
416                 }
417                 public   string  resolve_path_times(string part, int times, string? clue = null)
418                 {
419                         string ret = "";
420                         for (int i = 0; i < times; i++)
421                         {
422                                 if (clue != null && i > 0)
423                                 {
424                                         ret += clue;
425                                 }
426                                 ret += part;
427                         }
428                         return ret;
429                 }
430                 public   string resolve_path(string _path, string? relative = null)
431                 {
432                         string path = _path;
433                         if (relative != null)
434                         {
435                                 path = this.resolve_path_combine_path(path, relative);
436                         }
437                         string[] parts = path.split("/");
438                         string[] ret = {};
439                         int relative_parts = 0;
440                                         
441                         foreach (var part in parts)
442                         {
443                                 if (part.length < 1 || part == ".")
444                                 {
445                                         continue;
446                                 }
447                                 
448                                 if (part == "..")
449                                 {
450                                         if (ret.length > 0)
451                                         {
452                                                 ret = ret[0: ret.length -1];
453                                         }
454                                         else
455                                         {
456                                                 relative_parts++;
457                                         }
458                                         continue;
459                                 }
460                                 
461                                 ret += part;
462                         }
463                         
464                         path =  this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
465                         if (_path.has_prefix("/"))
466                         {
467                                 path = "/" + path;
468                         }
469                         return path;
470                 }
471                 
472                 public string[] vapidirs()
473                 {
474                         string[] ret = {};
475                         var sources = this.compilegroups.get("_default_").sources;
476                         for(var i =0; i< sources.size; i++) {
477                                 
478                                 var path = this.resolve_path( this.firstPath(), sources.get(i));
479                                 
480                                 if (Path.get_basename (path) == "vapi") {
481                                         GLib.debug("Adding VAPIDIR: %s\n", path);
482                                         ret += path;
483                                 }
484                                 
485                         }
486                         return ret;
487                         
488                 }
489                         
490
491         }
492         // an object describing a build config (or generic ...)
493         public class GtkValaSettings : Object {
494                 public string name;
495                 public GtkValaSettings? parent;
496                 
497                 public string compile_flags; // generic to all.
498                 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
499                 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
500                 public string target_bin;
501
502
503                 public GtkValaSettings(string name) 
504                 {
505                         this.name = name;
506                         this.compile_flags = "";
507                         this.target_bin = "";
508                         this.packages = new Gee.ArrayList<string>();
509                         this.sources = new Gee.ArrayList<string>();
510                                 
511                 }
512                 
513                 
514                 public GtkValaSettings.from_json(Json.Object el) {
515
516                         
517                         this.name = el.get_string_member("name");
518                         this.compile_flags = el.get_string_member("compile_flags");
519                         this.target_bin = el.get_string_member("target_bin");
520                         // sources and packages.
521                         this.sources = this.readArray(el.get_array_member("sources"));
522                         this.packages = this.readArray(el.get_array_member("packages"));
523                         
524                 }
525                 
526                 
527                 
528                 public Gee.ArrayList<string> readArray(Json.Array ar) 
529                 {
530                         var ret = new Gee.ArrayList<string>();
531                         for(var i =0; i< ar.get_length(); i++) {
532                                 ret.add(ar.get_string_element(i));
533                         }
534                         return ret;
535                 }
536                 
537                 public Json.Object toJson()
538                 {
539                         var ret = new Json.Object();
540                         ret.set_string_member("name", this.name);
541                         ret.set_string_member("compile_flags", this.compile_flags);
542                         ret.set_string_member("target_bin", this.target_bin);
543                         ret.set_array_member("sources", this.writeArray(this.sources));
544                         ret.set_array_member("packages", this.writeArray(this.packages));
545                         return ret;
546                 }
547                 public Json.Array writeArray(Gee.ArrayList<string> ar) {
548                         var ret = new Json.Array();
549                         for(var i =0; i< ar.size; i++) {
550                                 ret.add_string_element(ar.get(i));
551                         }
552                         return ret;
553                 }
554                 
555                 
556         }
557  
558    
559 }