src/Project/Gtk.vala
[roobuilder] / 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                 * Gir cache - it's local as we might want clear it if we modify the packages...
30                 *
31                 */
32                 public Gee.HashMap<string,Palete.Gir> gir_cache = null;
33           
34                 public Gtk(string path) {
35                   
36                   
37                         base(path);
38                         this.gir_cache = new Gee.HashMap<string,Palete.Gir>();
39                         this.xtype = "Gtk";
40                         var gid = "project-gtk-%d".printf(gtk_id++);
41                         this.id = gid;
42                         try {
43                                 this.loadConfig();
44                         } catch (GLib.Error e )  {
45                                 // is tihs ok?
46                         }
47                 
48                 }
49                 public Gee.HashMap<string,GtkValaSettings> compilegroups;
50                 
51                 public void loadConfig() throws GLib.Error 
52                 {
53                         // load a builder.config JSON file.
54                         // 
55                         this.compilegroups = new  Gee.HashMap<string,GtkValaSettings>();
56                         
57                         
58                         var fn = this.firstPath() + "/config1.builder";
59                         GLib.debug("load: " + fn );
60                         
61                         if (!FileUtils.test(fn, FileTest.EXISTS)) {
62                                 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
63                                 return;
64                         }
65
66                         var pa = new Json.Parser();
67                         pa.load_from_file(fn);
68                         var node = pa.get_root();
69
70                         // should be an array really.
71                         if (node.get_node_type () != Json.NodeType.ARRAY) {
72                                 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
73                         }
74                         
75                         var obj = node.get_array ();
76                         for(var i= 0;i<obj.get_length();i++) {
77                                 var el = obj.get_object_element(i);
78                                 var vs = new GtkValaSettings.from_json(el);
79                 if (vs == null) {
80                     print("problem loading json file");
81                     continue;
82                 }
83                                 if (vs.name != "_default_") {
84                                         vs.parent = this.compilegroups.get("_default_");
85                                 }
86                                 this.compilegroups.set(vs.name,vs);
87                         }
88                         GLib.debug("%s\n",this.configToString ());
89                         
90                 }
91                 public string configToString()
92                 {
93                         var ar = new Json.Array();
94                         var iter = this.compilegroups.map_iterator();
95                         while(iter.next()) {
96                                  
97                                 ar.add_object_element(iter.get_value().toJson());
98                         }
99
100                         var generator = new Json.Generator ();
101                         generator.indent = 4;
102                         generator.pretty = true;
103                         var node = new Json.Node(Json.NodeType.ARRAY);
104                         node.set_array(ar);
105                         generator.set_root(node);
106                         return generator.to_data(null);
107                 }
108                 
109                 public void writeConfig()
110                 {
111                         var fn = this.firstPath() + "/config1.builder";
112                         GLib.debug("write: " + fn );
113
114                          
115                         var f = GLib.File.new_for_path(fn);
116                         var data_out = new GLib.DataOutputStream(
117                                         f.replace(null, false, GLib.FileCreateFlags.NONE, null)
118                         );
119                         data_out.put_string(this.configToString(), null);
120                         data_out.close(null);
121                         
122                         return ;
123                          
124                 }
125                 /**
126                  *  perhaps we should select the default in the window somewhere...
127                  */ 
128                 public string firstBuildModule()
129                 {
130                         var iter = this.compilegroups.map_iterator();
131                         while(iter.next()) {
132                                  
133                                  if (iter.get_value().name == "_default_") {
134                                          continue;
135                                  }
136                                  
137                                  return iter.get_value().name;
138                         }
139                         return "";
140                 }
141                 
142                 
143                 public string relPath(string target)
144                 {
145                         var basename = this.firstPath();
146                         // eg. base = /home/xxx/fred/blogs
147                         // target = /home/xxx/fred/jones
148                         
149                         // this does not work correctly...
150                         var bb = basename;
151                         var prefix = "";
152                         while (true) {
153                                 if (    bb.length < target.length &&
154                                         target.substring(0, bb.length) == bb) {
155                                         
156                                         return prefix + target.substring(bb.length );
157                                 }
158                                 if (bb.length < 1) {
159                                         throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
160                                                                         basename, target);
161                                 }
162                                 bb = GLib.Path.get_dirname(bb);
163                                 prefix += "../";
164                                 
165                         }
166          
167                 }
168                 /**
169                  * get a list of files for a folder..
170                  * 
171                  * - in the project manager this has to list all possible compilable 
172                  *   files  - eg. exclue XXX.vala.c or XXX.c with the same name as 
173                  *   a vala file (so to ignore the generated files)
174                  * 
175                  * - for the editor navigation - this should exclude all files that
176                  *   are vala based on a bjs file..
177                  *  
178                  */
179                 
180                 public Gee.ArrayList<string> filesAll(string in_path,bool abspath = true)
181                 {
182                         var ret =  new Gee.ArrayList<string>();
183                         
184                         var dirname = this.resolve_path(
185                                 this.resolve_path_combine_path(this.firstPath(),in_path));
186                         
187                         GLib.debug("SCAN %s", dirname);
188                                 // scan the directory for files -- ending with vala || c
189                         
190
191                         var dir = File.new_for_path(dirname);
192                         if (!dir.query_exists()) {
193                                 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
194                                 return ret;
195                         }
196                         var pathprefix = abspath ? dirname : in_path;
197            
198                         try {
199                                 var file_enum = dir.enumerate_children(
200                                         "standard::*", 
201                                         GLib.FileQueryInfoFlags.NONE, 
202                                         null
203                                 );
204                 
205                  
206                                 FileInfo next_file; 
207                                 while ((next_file = file_enum.next_file(null)) != null) {
208                                         var fn = next_file.get_display_name();
209                                         
210                                         if (next_file.get_file_type () == GLib.FileType.DIRECTORY) {
211                                          
212                                                 GLib.debug("SKIP %s not regular  ", fn);
213                                                 continue;
214                                         }
215                                         if (!Regex.match_simple("^text", next_file.get_content_type())) {
216                                                 continue;
217                                         }
218                                         GLib.debug("SCAN ADD %s : %s", fn, next_file.get_content_type());
219                                         ret.add(pathprefix + "/" + fn);
220                                          
221                                         // any other valid types???
222                                 
223                                 }
224                                 
225                         } catch(Error e) {
226                                 GLib.warning("oops - something went wrong scanning the projects\n");
227                         }       
228                         
229                         return ret;
230                 }
231                 
232                 public Gee.ArrayList<string> filesForCompile(string in_path, bool abspath = true)
233                 {
234                         var allfiles = this.filesAll(in_path,abspath);
235                         var ret =  new Gee.ArrayList<string>();
236                         
237                         
238                         for (var i = 0; i < allfiles.size; i ++) {
239                                 var fn = allfiles.get(i);
240                                 try {
241                                         if (Regex.match_simple("\\.vala$", fn)) {
242                                                 ret.add( fn);
243                                                 continue;
244                                         }
245                                         // vala.c -- ignore..
246                                         if (Regex.match_simple("\\.vala\\.c$", fn)) {
247                                                 continue;
248                                         }
249                                         // not a c file...
250                                         if (!Regex.match_simple("\\.c$", fn)) {
251                                                 continue;
252                                         }
253                                         
254                                         // is the c file the same as a vala file...
255                                         
256                                          
257                                         
258                                         var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
259                                 
260                                         
261                                                 
262                                         if (allfiles.index_of( vv) > -1) {
263                                                 continue;
264                                         }
265                                         // add the 'c' file..
266                                         ret.add(fn);
267                                 } catch (Error e) {
268                                         continue;
269                                 }
270                         }
271                         // sort.
272                         ret.sort((fa,fb) => {
273                                 return ((string)fa).collate((string) fb);
274                         });
275                         return ret;
276                         
277                 }
278                 
279                 public Gee.ArrayList<string> filesForOpen(string in_path)
280                 {
281                         var allfiles = this.filesAll(in_path);
282                         var ret =  new Gee.ArrayList<string>();
283                         GLib.debug("SCAN %s - %d files",in_path, allfiles.size);
284                         
285                         for (var i = 0; i < allfiles.size; i ++) {
286                                 var fn = allfiles.get(i);
287                                 var bn  = GLib.Path.get_basename(fn);
288                                 try {
289                                         
290                                         if (Regex.match_simple("\\.vala\\.c$", fn)) {
291                                                 GLib.debug("SKIP %s - vala.c",fn);
292
293                                                 continue;
294                                         }
295                                         
296                                         if (Regex.match_simple("\\.bjs$", fn)) {
297                                                 GLib.debug("SKIP %s - .bjs",fn);
298                                                 continue;
299                                         }
300                                         
301                                         if (Regex.match_simple("\\~$", fn)) {
302                                                 GLib.debug("SKIP %s - ~",fn);
303                                                 continue;
304                                         }
305                                         if (Regex.match_simple("\\.stamp$", fn)) {
306                                                 GLib.debug("SKIP %s - .o",fn);
307                                                 continue;
308                                         }
309                                         if ("stamp-h1" == bn) {
310                                                 GLib.debug("SKIP %s - .o",fn);
311                                                 continue;
312                                         }
313                                         
314                                         // confgure.am
315                                         if ("config.h" == bn || "config.h.in" == bn || "config.log" == bn  || "configure" == bn ) {
316                                                 if (allfiles.index_of( in_path +"/configure.ac") > -1) {
317                                                         continue;
318                                                 }
319                                         }
320                                         // makefile
321                                         if ("Makefile" == bn || "Makefile.in" == bn ) {
322                                                 if (allfiles.index_of( in_path +"/Makefile.am") > -1) {
323                                                         continue;
324                                                 }
325                                         }
326                                         
327                                         if (Regex.match_simple("^\\.", bn)) {
328                                                 GLib.debug("SKIP %s - hidden",fn);
329                                                 continue;
330                                         }
331                                         if (Regex.match_simple("\\.vala$", fn)) {
332                                                 var vv = (new Regex("\\.vala$")).replace( fn, fn.length, 0, ".bjs");
333                                                 if (allfiles.index_of( vv) > -1) {
334                                                         GLib.debug("SKIP %s - .vala (got bjs)",fn);
335                                                         continue;
336                                                 }
337                                                 GLib.debug("ADD %s",fn);
338                                                 ret.add( fn);
339                                                 continue;
340                                         }
341                                         // vala.c -- ignore..
342                                         
343                                         // not a c file...
344                                         if (Regex.match_simple("\\.c$", fn)) {
345                                                 
346                                                 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
347                                                 if (allfiles.index_of( vv) > -1) {
348                                                         GLib.debug("SKIP %s - .c (got vala)",fn);
349                                                         continue;
350                                                 }
351                                                 GLib.debug("ADD %s",fn);                                                
352                                                 ret.add( fn);
353                                                 continue;
354                                         }
355                                         
356                                         if (GLib.Path.get_basename( fn) == "config1.builder") {
357                                                 continue;
358                                         }
359                                         // not .c / not .vala /not .bjs.. -- other type of file..
360                                         // allow ???
361                                         GLib.debug("ADD %s",fn);
362                                         // add the 'c' file..
363                                         ret.add(fn);
364                                 } catch (Error e) {
365                                         GLib.debug("Exception %s",e.message);
366                                         continue;
367                                 }
368                         }
369                         // sort.
370                         ret.sort((fa,fb) => {
371                                 return ((string)fa).collate((string) fb);
372                         });
373                         return ret;
374                         
375                 }
376                 
377                 
378                  
379  
380
381                 public   string  resolve_path_combine_path(string first, string second)
382                 {
383                         string ret = first;
384                         if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
385                         {
386                                 ret += "/";
387                         }
388                         //print("combined path = %s",  ret + second);
389                         return ret + second;
390                 }
391                 public   string  resolve_path_times(string part, int times, string? clue = null)
392                 {
393                         string ret = "";
394                         for (int i = 0; i < times; i++)
395                         {
396                                 if (clue != null && i > 0)
397                                 {
398                                         ret += clue;
399                                 }
400                                 ret += part;
401                         }
402                         return ret;
403                 }
404                 public   string resolve_path(string _path, string? relative = null)
405                 {
406                         string path = _path;
407                         if (relative != null)
408                         {
409                                 path = this.resolve_path_combine_path(path, relative);
410                         }
411                         string[] parts = path.split("/");
412                         string[] ret = {};
413                         int relative_parts = 0;
414                                         
415                         foreach (var part in parts)
416                         {
417                                 if (part.length < 1 || part == ".")
418                                 {
419                                         continue;
420                                 }
421                                 
422                                 if (part == "..")
423                                 {
424                                         if (ret.length > 0)
425                                         {
426                                                 ret = ret[0: ret.length -1];
427                                         }
428                                         else
429                                         {
430                                                 relative_parts++;
431                                         }
432                                         continue;
433                                 }
434                                 
435                                 ret += part;
436                         }
437                         
438                         path =  this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
439                         if (_path.has_prefix("/"))
440                         {
441                                 path = "/" + path;
442                         }
443                         return path;
444                 }
445                 
446                 public string[] vapidirs()
447                 {
448                         string[] ret = {};
449                         var sources = this.compilegroups.get("_default_").sources;
450                         for(var i =0; i< sources.size; i++) {
451                                 
452                                 var path = this.resolve_path( this.firstPath(), sources.get(i));
453                                 
454                                 if (Path.get_basename (path) == "vapi") {
455                                         GLib.debug("Adding VAPIDIR: %s\n", path);
456                                         ret += path;
457                                 }
458                                 
459                         }
460                         return ret;
461                         
462                 }
463                 public string[] sourcedirs()
464                 {
465                         string[] ret = {};
466                         var sources = this.compilegroups.get("_default_").sources;
467                         ret += this.firstPath();  
468                         for(var i =0; i< sources.size; i++) {
469                                 
470                                 var path = this.resolve_path( this.firstPath(), sources.get(i));
471                                 if (path == this.firstPath()) {
472                                         continue;
473                                 }
474                                 if (Path.get_basename (path) == "vapi") {
475                                         continue;
476                 
477                                 }
478                 //                      GLib.debug("Adding VAPIDIR: %s\n", path);
479                                 ret += path;            
480                         }
481                         return ret;
482                         
483                 }       
484
485         }
486         // an object describing a build config (or generic ...)
487         public class GtkValaSettings : Object {
488                 public string name;
489                 public GtkValaSettings? parent;
490                 
491                 public string compile_flags; // generic to all.
492                 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
493                 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
494                 public string target_bin;
495
496                 public string execute_args;
497                 
498                 
499                 public GtkValaSettings(string name) 
500                 {
501                         this.name = name;
502                         this.compile_flags = "";
503                         this.target_bin = "";
504                         this.packages = new Gee.ArrayList<string>();
505                         this.sources = new Gee.ArrayList<string>();
506                         this.execute_args = "";
507                                 
508                 }
509                 
510                 
511                 public GtkValaSettings.from_json(Json.Object el) {
512
513                         
514                         this.name = el.get_string_member("name");
515                         this.compile_flags = el.get_string_member("compile_flags");
516                         if ( el.has_member("execute_args")) {
517                                 this.execute_args = el.get_string_member("execute_args");
518                         } else {
519                                 this.execute_args = "";
520                         }
521                         this.target_bin = el.get_string_member("target_bin");
522                         // sources and packages.
523                         this.sources = this.readArray(el.get_array_member("sources"));
524                         this.packages = this.readArray(el.get_array_member("packages"));
525
526                 }
527                 
528                 // why not array of strings?
529                 
530                 public Gee.ArrayList<string> readArray(Json.Array ar) 
531                 {
532                         var ret = new Gee.ArrayList<string>();
533                         for(var i =0; i< ar.get_length(); i++) {
534                                 ret.add(ar.get_string_element(i));
535                         }
536                         return ret;
537                 }
538                 
539                 public Json.Object toJson()
540                 {
541                         var ret = new Json.Object();
542                         ret.set_string_member("name", this.name);
543                         ret.set_string_member("compile_flags", this.compile_flags);
544                         ret.set_string_member("execute_args", this.execute_args);
545                         ret.set_string_member("target_bin", this.target_bin);
546                         ret.set_array_member("sources", this.writeArray(this.sources));
547                         ret.set_array_member("packages", this.writeArray(this.packages));
548                         return ret;
549                 }
550                 public Json.Array writeArray(Gee.ArrayList<string> ar) {
551                         var ret = new Json.Array();
552                         for(var i =0; i< ar.size; i++) {
553                                 ret.add_string_element(ar.get(i));
554                         }
555                         return ret;
556                 }
557                 
558                 
559         }
560  
561    
562 }