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 GLib.debug("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 GLib.debug("%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 GLib.debug("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);
112 * perhaps we should select the default in the window somewhere...
114 public string firstBuildModule()
116 var iter = this.compilegroups.map_iterator();
119 if (iter.get_value().name == "__default__") {
123 return iter.get_value().name;
129 public string relPath(string target)
131 var basename = this.firstPath();
132 // eg. base = /home/xxx/fred/blogs
133 // target = /home/xxx/fred/jones
135 // this does not work correctly...
139 if ( bb.length < target.length &&
140 target.substring(0, bb.length) == bb) {
142 return prefix + target.substring(bb.length +1);
145 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
148 bb = GLib.Path.get_dirname(bb);
155 * get a list of files for a folder..
157 * - in the project manager this has to list all possible compilable
158 * files - eg. exclue XXX.vala.c or XXX.c with the same name as
159 * a vala file (so to ignore the generated files)
161 * - for the editor navigation - this should exclude all files that
162 * are vala based on a bjs file..
166 public Gee.ArrayList<string> filesAll(string in_path)
168 var ret = new Gee.ArrayList<string>();
170 var dirname = this.resolve_path(
171 this.resolve_path_combine_path(this.firstPath(),in_path));
173 GLib.debug("SCAN %s\n", dirname);
174 // scan the directory for files -- ending with vala || c
177 var dir = File.new_for_path(dirname);
178 if (!dir.query_exists()) {
179 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
185 var file_enum = dir.enumerate_children(
186 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
187 GLib.FileQueryInfoFlags.NONE,
193 while ((next_file = file_enum.next_file(null)) != null) {
194 var fn = next_file.get_display_name();
196 GLib.debug("SCAN %s - checking %s\n", dirname, fn);
198 ret.add(in_path + "/" + fn);
200 // any other valid types???
205 GLib.warning("oops - something went wrong scanning the projects\n");
211 public Gee.ArrayList<string> filesForCompile(string in_path)
213 var allfile = this.fileAll();
214 var ret = new Gee.ArrayList<string>();
217 for (var i = 0; i < cfiles.size; i ++) {
218 var fn = cfiles.get(i);
219 if (Regex.match_simple("\\.vala$", fn)) {
223 // vala.c -- ignore..
224 if (Regex.match_simple("\\.vala\\.c$", fn)) {
228 if (!Regex.match_simple("\\.c$", fn)) {
232 // got a file that is not
236 vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
240 if (Regex.match_simple("\\.vala\\.c$", fn)) {
244 if (ret.index_of( vv) > -1) {
253 public Gee.ArrayList<string> files(string in_path)
255 var ret = new Gee.ArrayList<string>();
256 var cfiles = new Gee.ArrayList<string>();
258 var dirname = this.resolve_path(
259 this.resolve_path_combine_path(this.firstPath(),in_path));
261 GLib.debug("SCAN %s\n", dirname);
262 // scan the directory for files -- ending with vala || c
265 var dir = File.new_for_path(dirname);
266 if (!dir.query_exists()) {
267 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
273 var file_enum = dir.enumerate_children(
274 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
275 GLib.FileQueryInfoFlags.NONE,
281 while ((next_file = file_enum.next_file(null)) != null) {
282 var fn = next_file.get_display_name();
284 GLib.debug("SCAN %s - checking %s\n", dirname, fn);
285 if (Regex.match_simple("\\.vala$", fn)) {
286 ret.add(in_path + "/" + fn);
290 if (Regex.match_simple("\\.vala\\.c$", fn)) {
293 if (Regex.match_simple("\\.c$", fn)) {
295 // if we have a vala file with the same name
296 // then do not add it...
298 cfiles.add(in_path + "/" + fn);
301 // any other valid types???
306 GLib.warning("oops - something went wrong scanning the projects\n");
310 // add the cfiles to ret - if they do not have a vala...
311 for (var i = 0; i < cfiles.size; i ++) {
313 var fn = cfiles.get(i);
316 vv = (new Regex("\\.c$")).replace( fn, fn, 0, ".vala");
322 if (ret.index_of( vv) > -1) {
329 GLib.debug("SCAN %s = returning %d", dirname, ret.size);
337 public string resolve_path_combine_path(string first, string second)
340 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
344 //print("combined path = %s", ret + second);
347 public string resolve_path_times(string part, int times, string? clue = null)
350 for (int i = 0; i < times; i++)
352 if (clue != null && i > 0)
360 public string resolve_path(string _path, string? relative = null)
363 if (relative != null)
365 path = this.resolve_path_combine_path(path, relative);
367 string[] parts = path.split("/");
369 int relative_parts = 0;
371 foreach (var part in parts)
373 if (part.length < 1 || part == ".")
382 ret = ret[0: ret.length -1];
394 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
395 if (_path.has_prefix("/"))
402 public string[] vapidirs()
405 var sources = this.compilegroups.get("_default_").sources;
406 for(var i =0; i< sources.size; i++) {
408 var path = this.resolve_path( this.firstPath(), sources.get(i));
410 if (Path.get_basename (path) == "vapi") {
411 GLib.debug("Adding VAPIDIR: %s\n", path);
422 // an object describing a build config (or generic ...)
423 public class GtkValaSettings : Object {
425 public GtkValaSettings? parent;
427 public string compile_flags; // generic to all.
428 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
429 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
430 public string target_bin;
433 public GtkValaSettings(string name)
436 this.compile_flags = "";
437 this.target_bin = "";
438 this.packages = new Gee.ArrayList<string>();
439 this.sources = new Gee.ArrayList<string>();
444 public GtkValaSettings.from_json(Json.Object el) {
447 this.name = el.get_string_member("name");
448 this.compile_flags = el.get_string_member("compile_flags");
449 this.target_bin = el.get_string_member("target_bin");
450 // sources and packages.
451 this.sources = this.readArray(el.get_array_member("sources"));
452 this.packages = this.readArray(el.get_array_member("packages"));
458 public Gee.ArrayList<string> readArray(Json.Array ar)
460 var ret = new Gee.ArrayList<string>();
461 for(var i =0; i< ar.get_length(); i++) {
462 ret.add(ar.get_string_element(i));
467 public Json.Object toJson()
469 var ret = new Json.Object();
470 ret.set_string_member("name", this.name);
471 ret.set_string_member("compile_flags", this.compile_flags);
472 ret.set_string_member("target_bin", this.target_bin);
473 ret.set_array_member("sources", this.writeArray(this.sources));
474 ret.set_array_member("packages", this.writeArray(this.packages));
477 public Json.Array writeArray(Gee.ArrayList<string> ar) {
478 var ret = new Json.Array();
479 for(var i =0; i< ar.size; i++) {
480 ret.add_string_element(ar.get(i));