src/Project/Gtk.vala
[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                 
167                 public Gee.ArrayList<string> files(string in_path)
168                 {
169                         var ret =  new Gee.ArrayList<string>();
170                         var cfiles =  new Gee.ArrayList<string>();
171                         
172                         var dirname = this.resolve_path(
173                                 this.resolve_path_combine_path(this.firstPath(),in_path));
174                         
175                         GLib.debug("SCAN %s\n", dirname);
176                                 // scan the directory for files -- ending with vala || c
177                         
178
179                         var dir = File.new_for_path(dirname);
180                         if (!dir.query_exists()) {
181                                 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
182                                 return ret;
183                         }
184           
185            
186                         try {
187                                 var file_enum = dir.enumerate_children(
188                                         GLib.FileAttribute.STANDARD_DISPLAY_NAME, 
189                                         GLib.FileQueryInfoFlags.NONE, 
190                                         null
191                                 );
192                 
193                  
194                                 FileInfo next_file; 
195                                 while ((next_file = file_enum.next_file(null)) != null) {
196                                         var fn = next_file.get_display_name();
197                                         
198                                         GLib.debug("SCAN %s - checking %s\n", dirname, fn);
199                                         if (Regex.match_simple("\\.vala$", fn)) {
200                                                 ret.add(in_path + "/" + fn);
201                                                 continue;
202                                         }
203                                         
204                                         if (Regex.match_simple("\\.vala\\.c$", fn)) {
205                                                 continue;
206                                         }
207                                         if (Regex.match_simple("\\.c$", fn)) {
208                                                 
209                                                 // if we have a vala file with the same name 
210                                                 // then do not add it...
211                                                 
212                                                 cfiles.add(in_path + "/" + fn);
213                                                 continue;
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                                 // add the cfiles to ret - if they do not have a vala...
224                         for (var i = 0; i < cfiles.size; i ++) {
225                                 
226                                 var fn = cfiles.get(i);
227                                 vv = fn;
228                                 try {
229                                         vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
230                                 } catch (Error e) {
231                                         continue;
232                                 }
233                                         
234                                                 
235                                 if (ret.index_of( vv) > -1) {
236                                         continue;
237                                 }
238                                 ret.add(fn);
239                         }
240                                                 
241                          
242                         GLib.debug("SCAN %s = returning %d", dirname, ret.size);
243                          
244                         return ret;
245                         
246
247                 }
248  
249
250                 public   string  resolve_path_combine_path(string first, string second)
251                 {
252                         string ret = first;
253                         if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
254                         {
255                                 ret += "/";
256                         }
257                         //print("combined path = %s",  ret + second);
258                         return ret + second;
259                 }
260                 public   string  resolve_path_times(string part, int times, string? clue = null)
261                 {
262                         string ret = "";
263                         for (int i = 0; i < times; i++)
264                         {
265                                 if (clue != null && i > 0)
266                                 {
267                                         ret += clue;
268                                 }
269                                 ret += part;
270                         }
271                         return ret;
272                 }
273                 public   string resolve_path(string _path, string? relative = null)
274                 {
275                         string path = _path;
276                         if (relative != null)
277                         {
278                                 path = this.resolve_path_combine_path(path, relative);
279                         }
280                         string[] parts = path.split("/");
281                         string[] ret = {};
282                         int relative_parts = 0;
283                                         
284                         foreach (var part in parts)
285                         {
286                                 if (part.length < 1 || part == ".")
287                                 {
288                                         continue;
289                                 }
290                                 
291                                 if (part == "..")
292                                 {
293                                         if (ret.length > 0)
294                                         {
295                                                 ret = ret[0: ret.length -1];
296                                         }
297                                         else
298                                         {
299                                                 relative_parts++;
300                                         }
301                                         continue;
302                                 }
303                                 
304                                 ret += part;
305                         }
306                         
307                         path =  this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
308                         if (_path.has_prefix("/"))
309                         {
310                                 path = "/" + path;
311                         }
312                         return path;
313                 }
314                 
315                 public string[] vapidirs()
316                 {
317                         string[] ret = {};
318                         var sources = this.compilegroups.get("_default_").sources;
319                         for(var i =0; i< sources.size; i++) {
320                                 
321                                 var path = this.resolve_path( this.firstPath(), sources.get(i));
322                                 
323                                 if (Path.get_basename (path) == "vapi") {
324                                         GLib.debug("Adding VAPIDIR: %s\n", path);
325                                         ret += path;
326                                 }
327                                 
328                         }
329                         return ret;
330                         
331                 }
332                         
333
334         }
335         // an object describing a build config (or generic ...)
336         public class GtkValaSettings : Object {
337                 public string name;
338                 public GtkValaSettings? parent;
339                 
340                 public string compile_flags; // generic to all.
341                 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
342                 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
343                 public string target_bin;
344
345
346                 public GtkValaSettings(string name) 
347                 {
348                         this.name = name;
349                         this.compile_flags = "";
350                         this.target_bin = "";
351                         this.packages = new Gee.ArrayList<string>();
352                         this.sources = new Gee.ArrayList<string>();
353                                 
354                 }
355                 
356                 
357                 public GtkValaSettings.from_json(Json.Object el) {
358
359                         
360                         this.name = el.get_string_member("name");
361                         this.compile_flags = el.get_string_member("compile_flags");
362                         this.target_bin = el.get_string_member("target_bin");
363                         // sources and packages.
364                         this.sources = this.readArray(el.get_array_member("sources"));
365                         this.packages = this.readArray(el.get_array_member("packages"));
366                         
367                 }
368                 
369                 
370                 
371                 public Gee.ArrayList<string> readArray(Json.Array ar) 
372                 {
373                         var ret = new Gee.ArrayList<string>();
374                         for(var i =0; i< ar.get_length(); i++) {
375                                 ret.add(ar.get_string_element(i));
376                         }
377                         return ret;
378                 }
379                 
380                 public Json.Object toJson()
381                 {
382                         var ret = new Json.Object();
383                         ret.set_string_member("name", this.name);
384                         ret.set_string_member("compile_flags", this.compile_flags);
385                         ret.set_string_member("target_bin", this.target_bin);
386                         ret.set_array_member("sources", this.writeArray(this.sources));
387                         ret.set_array_member("packages", this.writeArray(this.packages));
388                         return ret;
389                 }
390                 public Json.Array writeArray(Gee.ArrayList<string> ar) {
391                         var ret = new Json.Array();
392                         for(var i =0; i< ar.size; i++) {
393                                 ret.add_string_element(ar.get(i));
394                         }
395                         return ret;
396                 }
397                 
398                 
399         }
400  
401    
402 }