783070e4239d74c9eb95f4940ccd11a43affd63b
[app.Builder.js] / 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
102                          
103
104                         var f = GLib.File.new_for_path(fn);
105                         var data_out = new GLib.DataOutputStream(
106                                           f.replace(null, false, GLib.FileCreateFlags.NONE, null)
107                         );
108                         data_out.put_string(this.configToString(), null);
109                         data_out.close(null);
110                         
111                         return ;
112                         
113                         
114
115                 }
116                 public string relPath(string target)
117                 {
118                         var basename = this.firstPath();
119                         // eg. base = /home/xxx/fred/blogs
120                         // target = /home/xxx/fred/jones
121                         var bb = basename;
122                         var prefix = "";
123                         while (true) {
124                                 if (    bb.length < target.length &&
125                                         target.substring(0, bb.length) == bb) {
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                 public Gee.ArrayList<string> files(string in_path)
140                 {
141                         var ret = new Gee.ArrayList<string>(); 
142                         var dirname = this.resolve_path(
143                                 this.resolve_path_combine_path(this.firstPath(),in_path));
144                                 // scan the directory for files -- ending with vala || c
145                                 
146  
147                                 var dir = File.new_for_path(dirname);
148                                 if (!dir.query_exists()) {
149                                         return ret;
150                                 }
151                   
152                    
153                                 try {
154                                         var file_enum = dir.enumerate_children(
155                                                 GLib.FileAttribute.STANDARD_DISPLAY_NAME, 
156                                                 GLib.FileQueryInfoFlags.NONE, 
157                                                 null
158                                         );
159                         
160                          
161                                         FileInfo next_file; 
162                                         while ((next_file = file_enum.next_file(null)) != null) {
163                                                 var fn = next_file.get_display_name();
164                                                 if (!Regex.match_simple("\\.vala$", fn)) {
165                                                         continue;
166                                                 }
167                                         ret.add(dirname + "/" + fn);
168                                         }       
169                                 } catch(Error e) {
170                                         print("oops - something went wrong scanning the projects\n");
171                                 }
172
173                         }
174                         return ret;
175                         
176
177                 }
178
179                 public Gee.ArrayList<string> files(string path)
180                 {
181                         var ret = new Gee.ArrayList<string>();
182
183
184
185                         return ret;
186
187                 }
188                 
189
190                 public   string  resolve_path_combine_path(string first, string second)
191                 {
192                         string ret = first;
193                         if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
194                         {
195                                 ret += "/";
196                         }
197                         return ret + second;
198                 }
199                 public   string  resolve_path_times(string part, int times, string? clue = null)
200                 {
201                         string ret = "";
202                         for (int i = 0; i < times; i++)
203                         {
204                                 if (clue != null && i > 0)
205                                 {
206                                         ret += clue;
207                                 }
208                                 ret += part;
209                         }
210                         return ret;
211                 }
212                 public   string resolve_path(string _path, string? relative = null)
213                 {
214                         string path = _path;
215                         if (relative != null)
216                         {
217                                 path = this.resolve_path_combine_path(path, relative);
218                         }
219                         string[] parts = path.split("/");
220                         string[] ret = {};
221                         int relative_parts = 0;
222                                         
223                         foreach (var part in parts)
224                         {
225                                 if (part.length < 1 || part == ".")
226                                 {
227                                         continue;
228                                 }
229                                 
230                                 if (part == "..")
231                                 {
232                                         if (ret.length > 0)
233                                         {
234                                                 ret = ret[0: ret.length -1];
235                                         }
236                                         else
237                                         {
238                                                 relative_parts++;
239                                         }
240                                         continue;
241                                 }
242                                 
243                                 ret += part;
244                         }
245                         
246                         path =  this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
247                         if (_path.has_prefix("/"))
248                         {
249                                 path = "/" + path;
250                         }
251                         return path;
252                 }
253                 
254                 
255                         
256
257         }
258         // an object describing a build config (or generic ...)
259         public class GtkValaSettings : Object {
260                 public string name;
261                 public GtkValaSettings? parent;
262                 
263                 public string compile_flags; // generic to all.
264                 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
265                 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
266                 public string target_bin;
267
268
269                 public GtkValaSettings(string name) 
270                 {
271                         this.name = name;
272                         this.compile_flags = "";
273                         this.target_bin = "";
274                         this.packages = new Gee.ArrayList<string>();
275                         this.sources = new Gee.ArrayList<string>();
276                                 
277                 }
278                 
279                 
280                 public GtkValaSettings.from_json(Json.Object el) {
281
282                         
283                         this.name = el.get_string_member("name");
284                         this.compile_flags = el.get_string_member("compile_flags");
285                         this.target_bin = el.get_string_member("target_bin");
286                         // sources and packages.
287                         this.sources = this.readArray(el.get_array_member("sources"));
288                         this.packages = this.readArray(el.get_array_member("packages"));
289                         
290                 }
291                 public Gee.ArrayList<string> readArray(Json.Array ar) 
292                 {
293                         var ret = new Gee.ArrayList<string>();
294                         for(var i =0; i< ar.get_length(); i++) {
295                                 ret.add(ar.get_string_element(i));
296                         }
297                         return ret;
298                 }
299                 
300                 public Json.Object toJson()
301                 {
302                         var ret = new Json.Object();
303                         ret.set_string_member("name", this.name);
304                         ret.set_string_member("compile_flags", this.compile_flags);
305                         ret.set_string_member("target_bin", this.target_bin);
306                         ret.set_array_member("sources", this.writeArray(this.sources));
307                         ret.set_array_member("packages", this.writeArray(this.packages));
308                         return ret;
309                 }
310                 public Json.Array writeArray(Gee.ArrayList<string> ar) {
311                         var ret = new Json.Array();
312                         for(var i =0; i< ar.size; i++) {
313                                 ret.add_string_element(ar.get(i));
314                         }
315                         return ret;
316                 }
317                 
318                 
319         }
320  
321    
322 }