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