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