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 * Gir cache - it's local as we might want clear it if we modify the packages...
31 Gee.HashMap<string,Gir> cache = null;
33 public Gtk(string path) {
38 var gid = "project-gtk-%d".printf(gtk_id++);
42 } catch (GLib.Error e ) {
47 public Gee.HashMap<string,GtkValaSettings> compilegroups;
49 public void loadConfig() throws GLib.Error
51 // load a builder.config JSON file.
53 this.compilegroups = new Gee.HashMap<string,GtkValaSettings>();
56 var fn = this.firstPath() + "/config1.builder";
57 GLib.debug("load: " + fn );
59 if (!FileUtils.test(fn, FileTest.EXISTS)) {
60 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
64 var pa = new Json.Parser();
65 pa.load_from_file(fn);
66 var node = pa.get_root();
68 // should be an array really.
69 if (node.get_node_type () != Json.NodeType.ARRAY) {
70 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
73 var obj = node.get_array ();
74 for(var i= 0;i<obj.get_length();i++) {
75 var el = obj.get_object_element(i);
76 var vs = new GtkValaSettings.from_json(el);
78 print("problem loading json file");
81 if (vs.name != "_default_") {
82 vs.parent = this.compilegroups.get("_default_");
84 this.compilegroups.set(vs.name,vs);
86 GLib.debug("%s\n",this.configToString ());
89 public string configToString()
91 var ar = new Json.Array();
92 var iter = this.compilegroups.map_iterator();
95 ar.add_object_element(iter.get_value().toJson());
98 var generator = new Json.Generator ();
100 generator.pretty = true;
101 var node = new Json.Node(Json.NodeType.ARRAY);
103 generator.set_root(node);
104 return generator.to_data(null);
107 public void writeConfig()
109 var fn = this.firstPath() + "/config1.builder";
110 GLib.debug("write: " + fn );
113 var f = GLib.File.new_for_path(fn);
114 var data_out = new GLib.DataOutputStream(
115 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
117 data_out.put_string(this.configToString(), null);
118 data_out.close(null);
124 * perhaps we should select the default in the window somewhere...
126 public string firstBuildModule()
128 var iter = this.compilegroups.map_iterator();
131 if (iter.get_value().name == "_default_") {
135 return iter.get_value().name;
141 public string relPath(string target)
143 var basename = this.firstPath();
144 // eg. base = /home/xxx/fred/blogs
145 // target = /home/xxx/fred/jones
147 // this does not work correctly...
151 if ( bb.length < target.length &&
152 target.substring(0, bb.length) == bb) {
154 return prefix + target.substring(bb.length );
157 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
160 bb = GLib.Path.get_dirname(bb);
167 * get a list of files for a folder..
169 * - in the project manager this has to list all possible compilable
170 * files - eg. exclue XXX.vala.c or XXX.c with the same name as
171 * a vala file (so to ignore the generated files)
173 * - for the editor navigation - this should exclude all files that
174 * are vala based on a bjs file..
178 public Gee.ArrayList<string> filesAll(string in_path,bool abspath = true)
180 var ret = new Gee.ArrayList<string>();
182 var dirname = this.resolve_path(
183 this.resolve_path_combine_path(this.firstPath(),in_path));
185 GLib.debug("SCAN %s", dirname);
186 // scan the directory for files -- ending with vala || c
189 var dir = File.new_for_path(dirname);
190 if (!dir.query_exists()) {
191 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
194 var pathprefix = abspath ? dirname : in_path;
197 var file_enum = dir.enumerate_children(
199 GLib.FileQueryInfoFlags.NONE,
205 while ((next_file = file_enum.next_file(null)) != null) {
206 var fn = next_file.get_display_name();
208 if (next_file.get_file_type () == GLib.FileType.DIRECTORY) {
210 GLib.debug("SKIP %s not regular ", fn);
213 if (!Regex.match_simple("^text", next_file.get_content_type())) {
216 GLib.debug("SCAN ADD %s : %s", fn, next_file.get_content_type());
217 ret.add(pathprefix + "/" + fn);
219 // any other valid types???
224 GLib.warning("oops - something went wrong scanning the projects\n");
230 public Gee.ArrayList<string> filesForCompile(string in_path, bool abspath = true)
232 var allfiles = this.filesAll(in_path,abspath);
233 var ret = new Gee.ArrayList<string>();
236 for (var i = 0; i < allfiles.size; i ++) {
237 var fn = allfiles.get(i);
239 if (Regex.match_simple("\\.vala$", fn)) {
243 // vala.c -- ignore..
244 if (Regex.match_simple("\\.vala\\.c$", fn)) {
248 if (!Regex.match_simple("\\.c$", fn)) {
252 // is the c file the same as a vala file...
256 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
260 if (allfiles.index_of( vv) > -1) {
263 // add the 'c' file..
270 ret.sort((fa,fb) => {
271 return ((string)fa).collate((string) fb);
277 public Gee.ArrayList<string> filesForOpen(string in_path)
279 var allfiles = this.filesAll(in_path);
280 var ret = new Gee.ArrayList<string>();
281 GLib.debug("SCAN %s - %d files",in_path, allfiles.size);
283 for (var i = 0; i < allfiles.size; i ++) {
284 var fn = allfiles.get(i);
285 var bn = GLib.Path.get_basename(fn);
288 if (Regex.match_simple("\\.vala\\.c$", fn)) {
289 GLib.debug("SKIP %s - vala.c",fn);
294 if (Regex.match_simple("\\.bjs$", fn)) {
295 GLib.debug("SKIP %s - .bjs",fn);
299 if (Regex.match_simple("\\~$", fn)) {
300 GLib.debug("SKIP %s - ~",fn);
303 if (Regex.match_simple("\\.stamp$", fn)) {
304 GLib.debug("SKIP %s - .o",fn);
307 if ("stamp-h1" == bn) {
308 GLib.debug("SKIP %s - .o",fn);
313 if ("config.h" == bn || "config.h.in" == bn || "config.log" == bn || "configure" == bn ) {
314 if (allfiles.index_of( in_path +"/configure.ac") > -1) {
319 if ("Makefile" == bn || "Makefile.in" == bn ) {
320 if (allfiles.index_of( in_path +"/Makefile.am") > -1) {
325 if (Regex.match_simple("^\\.", bn)) {
326 GLib.debug("SKIP %s - hidden",fn);
329 if (Regex.match_simple("\\.vala$", fn)) {
330 var vv = (new Regex("\\.vala$")).replace( fn, fn.length, 0, ".bjs");
331 if (allfiles.index_of( vv) > -1) {
332 GLib.debug("SKIP %s - .vala (got bjs)",fn);
335 GLib.debug("ADD %s",fn);
339 // vala.c -- ignore..
342 if (Regex.match_simple("\\.c$", fn)) {
344 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
345 if (allfiles.index_of( vv) > -1) {
346 GLib.debug("SKIP %s - .c (got vala)",fn);
349 GLib.debug("ADD %s",fn);
354 if (GLib.Path.get_basename( fn) == "config1.builder") {
357 // not .c / not .vala /not .bjs.. -- other type of file..
359 GLib.debug("ADD %s",fn);
360 // add the 'c' file..
363 GLib.debug("Exception %s",e.message);
368 ret.sort((fa,fb) => {
369 return ((string)fa).collate((string) fb);
379 public string resolve_path_combine_path(string first, string second)
382 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
386 //print("combined path = %s", ret + second);
389 public string resolve_path_times(string part, int times, string? clue = null)
392 for (int i = 0; i < times; i++)
394 if (clue != null && i > 0)
402 public string resolve_path(string _path, string? relative = null)
405 if (relative != null)
407 path = this.resolve_path_combine_path(path, relative);
409 string[] parts = path.split("/");
411 int relative_parts = 0;
413 foreach (var part in parts)
415 if (part.length < 1 || part == ".")
424 ret = ret[0: ret.length -1];
436 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
437 if (_path.has_prefix("/"))
444 public string[] vapidirs()
447 var sources = this.compilegroups.get("_default_").sources;
448 for(var i =0; i< sources.size; i++) {
450 var path = this.resolve_path( this.firstPath(), sources.get(i));
452 if (Path.get_basename (path) == "vapi") {
453 GLib.debug("Adding VAPIDIR: %s\n", path);
461 public string[] sourcedirs()
464 var sources = this.compilegroups.get("_default_").sources;
465 ret += this.firstPath();
466 for(var i =0; i< sources.size; i++) {
468 var path = this.resolve_path( this.firstPath(), sources.get(i));
469 if (path == this.firstPath()) {
472 if (Path.get_basename (path) == "vapi") {
476 // GLib.debug("Adding VAPIDIR: %s\n", path);
484 // an object describing a build config (or generic ...)
485 public class GtkValaSettings : Object {
487 public GtkValaSettings? parent;
489 public string compile_flags; // generic to all.
490 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
491 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
492 public string target_bin;
494 public string execute_args;
497 public GtkValaSettings(string name)
500 this.compile_flags = "";
501 this.target_bin = "";
502 this.packages = new Gee.ArrayList<string>();
503 this.sources = new Gee.ArrayList<string>();
504 this.execute_args = "";
509 public GtkValaSettings.from_json(Json.Object el) {
512 this.name = el.get_string_member("name");
513 this.compile_flags = el.get_string_member("compile_flags");
514 if ( el.has_member("execute_args")) {
515 this.execute_args = el.get_string_member("execute_args");
517 this.execute_args = "";
519 this.target_bin = el.get_string_member("target_bin");
520 // sources and packages.
521 this.sources = this.readArray(el.get_array_member("sources"));
522 this.packages = this.readArray(el.get_array_member("packages"));
526 // why not array of strings?
528 public Gee.ArrayList<string> readArray(Json.Array ar)
530 var ret = new Gee.ArrayList<string>();
531 for(var i =0; i< ar.get_length(); i++) {
532 ret.add(ar.get_string_element(i));
537 public Json.Object toJson()
539 var ret = new Json.Object();
540 ret.set_string_member("name", this.name);
541 ret.set_string_member("compile_flags", this.compile_flags);
542 ret.set_string_member("execute_args", this.execute_args);
543 ret.set_string_member("target_bin", this.target_bin);
544 ret.set_array_member("sources", this.writeArray(this.sources));
545 ret.set_array_member("packages", this.writeArray(this.packages));
548 public Json.Array writeArray(Gee.ArrayList<string> ar) {
549 var ret = new Json.Array();
550 for(var i =0; i< ar.size; i++) {
551 ret.add_string_element(ar.get(i));