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...
32 public Gee.HashMap<string,Palete.Gir> gir_cache = null;
34 public Gtk(string path) {
38 this.gir_cache = new Gee.HashMap<string,Palete.Gir>();
40 var gid = "project-gtk-%d".printf(gtk_id++);
44 } catch (GLib.Error e ) {
49 public Gee.HashMap<string,GtkValaSettings> compilegroups;
51 public void loadConfig() throws GLib.Error
53 // load a builder.config JSON file.
55 this.compilegroups = new Gee.HashMap<string,GtkValaSettings>();
58 var fn = this.firstPath() + "/config1.builder";
59 GLib.debug("load: " + fn );
61 if (!FileUtils.test(fn, FileTest.EXISTS)) {
62 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
66 var pa = new Json.Parser();
67 pa.load_from_file(fn);
68 var node = pa.get_root();
70 // should be an array really.
71 if (node.get_node_type () != Json.NodeType.ARRAY) {
72 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
75 var obj = node.get_array ();
76 for(var i= 0;i<obj.get_length();i++) {
77 var el = obj.get_object_element(i);
78 var vs = new GtkValaSettings.from_json(el);
80 print("problem loading json file");
83 if (vs.name != "_default_") {
84 vs.parent = this.compilegroups.get("_default_");
86 this.compilegroups.set(vs.name,vs);
88 GLib.debug("%s\n",this.configToString ());
91 public string configToString()
93 var ar = new Json.Array();
94 var iter = this.compilegroups.map_iterator();
97 ar.add_object_element(iter.get_value().toJson());
100 var generator = new Json.Generator ();
101 generator.indent = 4;
102 generator.pretty = true;
103 var node = new Json.Node(Json.NodeType.ARRAY);
105 generator.set_root(node);
106 return generator.to_data(null);
109 public void writeConfig()
111 var fn = this.firstPath() + "/config1.builder";
112 GLib.debug("write: " + fn );
115 var f = GLib.File.new_for_path(fn);
116 var data_out = new GLib.DataOutputStream(
117 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
119 data_out.put_string(this.configToString(), null);
120 data_out.close(null);
126 * perhaps we should select the default in the window somewhere...
128 public string firstBuildModule()
130 var iter = this.compilegroups.map_iterator();
133 if (iter.get_value().name == "_default_") {
137 return iter.get_value().name;
143 public string relPath(string target)
145 var basename = this.firstPath();
146 // eg. base = /home/xxx/fred/blogs
147 // target = /home/xxx/fred/jones
149 // this does not work correctly...
153 if ( bb.length < target.length &&
154 target.substring(0, bb.length) == bb) {
156 return prefix + target.substring(bb.length );
159 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
162 bb = GLib.Path.get_dirname(bb);
169 * get a list of files for a folder..
171 * - in the project manager this has to list all possible compilable
172 * files - eg. exclue XXX.vala.c or XXX.c with the same name as
173 * a vala file (so to ignore the generated files)
175 * - for the editor navigation - this should exclude all files that
176 * are vala based on a bjs file..
180 public Gee.ArrayList<string> filesAll(string in_path,bool abspath = true)
182 var ret = new Gee.ArrayList<string>();
184 var dirname = this.resolve_path(
185 this.resolve_path_combine_path(this.firstPath(),in_path));
187 GLib.debug("SCAN %s", dirname);
188 // scan the directory for files -- ending with vala || c
191 var dir = File.new_for_path(dirname);
192 if (!dir.query_exists()) {
193 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
196 var pathprefix = abspath ? dirname : in_path;
199 var file_enum = dir.enumerate_children(
201 GLib.FileQueryInfoFlags.NONE,
207 while ((next_file = file_enum.next_file(null)) != null) {
208 var fn = next_file.get_display_name();
210 if (next_file.get_file_type () == GLib.FileType.DIRECTORY) {
212 GLib.debug("SKIP %s not regular ", fn);
215 if (!Regex.match_simple("^text", next_file.get_content_type())) {
218 GLib.debug("SCAN ADD %s : %s", fn, next_file.get_content_type());
219 ret.add(pathprefix + "/" + fn);
221 // any other valid types???
226 GLib.warning("oops - something went wrong scanning the projects\n");
232 public Gee.ArrayList<string> filesForCompile(string in_path, bool abspath = true)
234 var allfiles = this.filesAll(in_path,abspath);
235 var ret = new Gee.ArrayList<string>();
238 for (var i = 0; i < allfiles.size; i ++) {
239 var fn = allfiles.get(i);
241 if (Regex.match_simple("\\.vala$", fn)) {
245 // vala.c -- ignore..
246 if (Regex.match_simple("\\.vala\\.c$", fn)) {
250 if (!Regex.match_simple("\\.c$", fn)) {
254 // is the c file the same as a vala file...
258 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
262 if (allfiles.index_of( vv) > -1) {
265 // add the 'c' file..
272 ret.sort((fa,fb) => {
273 return ((string)fa).collate((string) fb);
279 public Gee.ArrayList<string> filesForOpen(string in_path)
281 var allfiles = this.filesAll(in_path);
282 var ret = new Gee.ArrayList<string>();
283 GLib.debug("SCAN %s - %d files",in_path, allfiles.size);
285 for (var i = 0; i < allfiles.size; i ++) {
286 var fn = allfiles.get(i);
287 var bn = GLib.Path.get_basename(fn);
290 if (Regex.match_simple("\\.vala\\.c$", fn)) {
291 GLib.debug("SKIP %s - vala.c",fn);
296 if (Regex.match_simple("\\.bjs$", fn)) {
297 GLib.debug("SKIP %s - .bjs",fn);
301 if (Regex.match_simple("\\~$", fn)) {
302 GLib.debug("SKIP %s - ~",fn);
305 if (Regex.match_simple("\\.stamp$", fn)) {
306 GLib.debug("SKIP %s - .o",fn);
309 if ("stamp-h1" == bn) {
310 GLib.debug("SKIP %s - .o",fn);
315 if ("config.h" == bn || "config.h.in" == bn || "config.log" == bn || "configure" == bn ) {
316 if (allfiles.index_of( in_path +"/configure.ac") > -1) {
321 if ("Makefile" == bn || "Makefile.in" == bn ) {
322 if (allfiles.index_of( in_path +"/Makefile.am") > -1) {
327 if (Regex.match_simple("^\\.", bn)) {
328 GLib.debug("SKIP %s - hidden",fn);
331 if (Regex.match_simple("\\.vala$", fn)) {
332 var vv = (new Regex("\\.vala$")).replace( fn, fn.length, 0, ".bjs");
333 if (allfiles.index_of( vv) > -1) {
334 GLib.debug("SKIP %s - .vala (got bjs)",fn);
337 GLib.debug("ADD %s",fn);
341 // vala.c -- ignore..
344 if (Regex.match_simple("\\.c$", fn)) {
346 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
347 if (allfiles.index_of( vv) > -1) {
348 GLib.debug("SKIP %s - .c (got vala)",fn);
351 GLib.debug("ADD %s",fn);
356 if (GLib.Path.get_basename( fn) == "config1.builder") {
359 // not .c / not .vala /not .bjs.. -- other type of file..
361 GLib.debug("ADD %s",fn);
362 // add the 'c' file..
365 GLib.debug("Exception %s",e.message);
370 ret.sort((fa,fb) => {
371 return ((string)fa).collate((string) fb);
381 public string resolve_path_combine_path(string first, string second)
384 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
388 //print("combined path = %s", ret + second);
391 public string resolve_path_times(string part, int times, string? clue = null)
394 for (int i = 0; i < times; i++)
396 if (clue != null && i > 0)
404 public string resolve_path(string _path, string? relative = null)
407 if (relative != null)
409 path = this.resolve_path_combine_path(path, relative);
411 string[] parts = path.split("/");
413 int relative_parts = 0;
415 foreach (var part in parts)
417 if (part.length < 1 || part == ".")
426 ret = ret[0: ret.length -1];
438 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
439 if (_path.has_prefix("/"))
446 public string[] vapidirs()
449 var sources = this.compilegroups.get("_default_").sources;
450 for(var i =0; i< sources.size; i++) {
452 var path = this.resolve_path( this.firstPath(), sources.get(i));
454 if (Path.get_basename (path) == "vapi") {
455 GLib.debug("Adding VAPIDIR: %s\n", path);
463 public string[] sourcedirs()
466 var sources = this.compilegroups.get("_default_").sources;
467 ret += this.firstPath();
468 for(var i =0; i< sources.size; i++) {
470 var path = this.resolve_path( this.firstPath(), sources.get(i));
471 if (path == this.firstPath()) {
474 if (Path.get_basename (path) == "vapi") {
478 // GLib.debug("Adding VAPIDIR: %s\n", path);
486 // an object describing a build config (or generic ...)
487 public class GtkValaSettings : Object {
489 public GtkValaSettings? parent;
491 public string compile_flags; // generic to all.
492 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
493 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
494 public string target_bin;
496 public string execute_args;
499 public GtkValaSettings(string name)
502 this.compile_flags = "";
503 this.target_bin = "";
504 this.packages = new Gee.ArrayList<string>();
505 this.sources = new Gee.ArrayList<string>();
506 this.execute_args = "";
511 public GtkValaSettings.from_json(Json.Object el) {
514 this.name = el.get_string_member("name");
515 this.compile_flags = el.get_string_member("compile_flags");
516 if ( el.has_member("execute_args")) {
517 this.execute_args = el.get_string_member("execute_args");
519 this.execute_args = "";
521 this.target_bin = el.get_string_member("target_bin");
522 // sources and packages.
523 this.sources = this.readArray(el.get_array_member("sources"));
524 this.packages = this.readArray(el.get_array_member("packages"));
528 // why not array of strings?
530 public Gee.ArrayList<string> readArray(Json.Array ar)
532 var ret = new Gee.ArrayList<string>();
533 for(var i =0; i< ar.get_length(); i++) {
534 ret.add(ar.get_string_element(i));
539 public Json.Object toJson()
541 var ret = new Json.Object();
542 ret.set_string_member("name", this.name);
543 ret.set_string_member("compile_flags", this.compile_flags);
544 ret.set_string_member("execute_args", this.execute_args);
545 ret.set_string_member("target_bin", this.target_bin);
546 ret.set_array_member("sources", this.writeArray(this.sources));
547 ret.set_array_member("packages", this.writeArray(this.packages));
550 public Json.Array writeArray(Gee.ArrayList<string> ar) {
551 var ret = new Json.Array();
552 for(var i =0; i< ar.size; i++) {
553 ret.add_string_element(ar.get(i));