1 //<Script type="text/javascript">
3 * Gtk projects - normally vala based now..
5 * should have a few extra features..
9 * different versions (eg. different files can compile different versions - eg. for testing.
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..
23 static int gtk_id = 1;
26 public class Gtk : Project
29 public Gtk(string path) {
34 var gid = "project-gtk-%d".printf(gtk_id++);
39 public Gee.HashMap<string,GtkValaSettings> compilegroups;
41 public void loadConfig() throws GLib.Error
43 // load a builder.config JSON file.
45 this.compilegroups = new Gee.HashMap<string,GtkValaSettings>();
48 var fn = this.firstPath() + "/config1.builder";
49 print("load: " + fn );
51 if (!FileUtils.test(fn, FileTest.EXISTS)) {
52 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
56 var pa = new Json.Parser();
57 pa.load_from_file(fn);
58 var node = pa.get_root();
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 ());
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_");
72 this.compilegroups.set(vs.name,vs);
74 print("%s\n",this.configToString ());
77 public string configToString()
79 var ar = new Json.Array();
80 var iter = this.compilegroups.map_iterator();
83 ar.add_object_element(iter.get_value().toJson());
86 var generator = new Json.Generator ();
88 generator.pretty = true;
89 var node = new Json.Node(Json.NodeType.ARRAY);
91 generator.set_root(node);
92 return generator.to_data(null);
95 public void writeConfig()
97 var fn = this.firstPath() + "/config1.builder";
98 print("write: " + fn );
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)
105 data_out.put_string(this.configToString(), null);
106 data_out.close(null);
113 public string relPath(string target)
115 var basename = this.firstPath();
116 // eg. base = /home/xxx/fred/blogs
117 // target = /home/xxx/fred/jones
121 if ( bb.length < target.length &&
122 target.substring(0, bb.length) == bb) {
123 return prefix + target.substring(bb.length +1);
126 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
129 bb = GLib.Path.get_dirname(bb);
137 public Gee.ArrayList<string> files(string in_path)
139 var ret = new Gee.ArrayList<string>();
140 var cfiles = new Gee.ArrayList<string>();
142 var dirname = this.resolve_path(
143 this.resolve_path_combine_path(this.firstPath(),in_path));
145 print("SCAN %s\n", dirname);
146 // scan the directory for files -- ending with vala || c
149 var dir = File.new_for_path(dirname);
150 if (!dir.query_exists()) {
151 print("SCAN %s - skip - does not exist\n", dirname);
157 var file_enum = dir.enumerate_children(
158 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
159 GLib.FileQueryInfoFlags.NONE,
165 while ((next_file = file_enum.next_file(null)) != null) {
166 var fn = next_file.get_display_name();
168 print("SCAN %s - checking %s\n", dirname, fn);
169 if (Regex.match_simple("\\.vala$", fn)) {
170 ret.add(in_path + "/" + fn);
176 if (Regex.match_simple("\\.c$", fn)) {
178 // if we have a vala file with the same name
179 // then do not add it...
180 var vv = (new Regex("\\.c$")).replace(
181 fn, fn.length, 0, ".vala");
183 if (ret.index_of(in_path + "/" + vv) > -1) {
187 cfiles.add(in_path + "/" + fn);
190 // any other valid types???
193 // add the cfiles to ret - if they do not have a vala...
197 print("oops - something went wrong scanning the projects\n");
199 print("SCAN %s = returning %d", dirname, ret.size);
207 public string resolve_path_combine_path(string first, string second)
210 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
216 public string resolve_path_times(string part, int times, string? clue = null)
219 for (int i = 0; i < times; i++)
221 if (clue != null && i > 0)
229 public string resolve_path(string _path, string? relative = null)
232 if (relative != null)
234 path = this.resolve_path_combine_path(path, relative);
236 string[] parts = path.split("/");
238 int relative_parts = 0;
240 foreach (var part in parts)
242 if (part.length < 1 || part == ".")
251 ret = ret[0: ret.length -1];
263 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
264 if (_path.has_prefix("/"))
275 // an object describing a build config (or generic ...)
276 public class GtkValaSettings : Object {
278 public GtkValaSettings? parent;
280 public string compile_flags; // generic to all.
281 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
282 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
283 public string target_bin;
286 public GtkValaSettings(string name)
289 this.compile_flags = "";
290 this.target_bin = "";
291 this.packages = new Gee.ArrayList<string>();
292 this.sources = new Gee.ArrayList<string>();
297 public GtkValaSettings.from_json(Json.Object el) {
300 this.name = el.get_string_member("name");
301 this.compile_flags = el.get_string_member("compile_flags");
302 this.target_bin = el.get_string_member("target_bin");
303 // sources and packages.
304 this.sources = this.readArray(el.get_array_member("sources"));
305 this.packages = this.readArray(el.get_array_member("packages"));
308 public Gee.ArrayList<string> readArray(Json.Array ar)
310 var ret = new Gee.ArrayList<string>();
311 for(var i =0; i< ar.get_length(); i++) {
312 ret.add(ar.get_string_element(i));
317 public Json.Object toJson()
319 var ret = new Json.Object();
320 ret.set_string_member("name", this.name);
321 ret.set_string_member("compile_flags", this.compile_flags);
322 ret.set_string_member("target_bin", this.target_bin);
323 ret.set_array_member("sources", this.writeArray(this.sources));
324 ret.set_array_member("packages", this.writeArray(this.packages));
327 public Json.Array writeArray(Gee.ArrayList<string> ar) {
328 var ret = new Json.Array();
329 for(var i =0; i< ar.size; i++) {
330 ret.add_string_element(ar.get(i));