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