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