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