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