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