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