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