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..
24 static int gtk_id = 1;
27 public class Gtk : Project
30 * Gir cache - it's local as we might want clear it if we modify the packages...
33 public Gee.HashMap<string,Palete.Gir> gir_cache = null;
34 public bool gir_cache_loaded = false;
37 public Gtk(string path) {
41 this.palete = new Palete.Gtk(this);
43 this.gir_cache = new Gee.HashMap<string,Palete.Gir>();
45 var gid = "project-gtk-%d".printf(gtk_id++);
49 } catch (GLib.Error e ) {
54 public Gee.HashMap<string,GtkValaSettings> compilegroups;
56 public void loadConfig() throws GLib.Error
58 // load a builder.config JSON file.
60 this.compilegroups = new Gee.HashMap<string,GtkValaSettings>();
63 var fn = this.firstPath() + "/config1.builder";
64 GLib.debug("load: " + fn );
66 if (!FileUtils.test(fn, FileTest.EXISTS)) {
67 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
71 var pa = new Json.Parser();
72 pa.load_from_file(fn);
73 var node = pa.get_root();
75 // should be an array really.
76 if (node.get_node_type () != Json.NodeType.ARRAY) {
77 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
80 var obj = node.get_array ();
81 for(var i= 0;i<obj.get_length();i++) {
82 var el = obj.get_object_element(i);
83 var vs = new GtkValaSettings.from_json(el);
85 print("problem loading json file");
88 if (vs.name != "_default_") {
89 vs.parent = this.compilegroups.get("_default_");
91 this.compilegroups.set(vs.name,vs);
93 GLib.debug("%s\n",this.configToString ());
96 public string configToString()
98 var ar = new Json.Array();
99 var iter = this.compilegroups.map_iterator();
102 ar.add_object_element(iter.get_value().toJson());
105 var generator = new Json.Generator ();
106 generator.indent = 4;
107 generator.pretty = true;
108 var node = new Json.Node(Json.NodeType.ARRAY);
110 generator.set_root(node);
111 return generator.to_data(null);
114 public void writeConfig()
116 var fn = this.firstPath() + "/config1.builder";
117 GLib.debug("write: " + fn );
120 var f = GLib.File.new_for_path(fn);
121 var data_out = new GLib.DataOutputStream(
122 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
124 data_out.put_string(this.configToString(), null);
125 data_out.close(null);
126 this.gir_cache_loaded = false; // force a reload.
131 * perhaps we should select the default in the window somewhere...
133 public string firstBuildModule()
135 var iter = this.compilegroups.map_iterator();
138 if (iter.get_value().name == "_default_") {
142 return iter.get_value().name;
148 public string relPath(string target)
150 var basename = this.firstPath();
151 // eg. base = /home/xxx/fred/blogs
152 // target = /home/xxx/fred/jones
154 // this does not work correctly...
158 if ( bb.length < target.length &&
159 target.substring(0, bb.length) == bb) {
161 return prefix + target.substring(bb.length );
164 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
167 bb = GLib.Path.get_dirname(bb);
174 * get a list of files for a folder..
176 * - in the project manager this has to list all possible compilable
177 * files - eg. exclue XXX.vala.c or XXX.c with the same name as
178 * a vala file (so to ignore the generated files)
180 * - for the editor navigation - this should exclude all files that
181 * are vala based on a bjs file..
185 public Gee.ArrayList<string> filesAll(string in_path,bool abspath = true)
187 var ret = new Gee.ArrayList<string>();
189 var dirname = this.resolve_path(
190 this.resolve_path_combine_path(this.firstPath(),in_path));
192 GLib.debug("SCAN %s", dirname);
193 // scan the directory for files -- ending with vala || c
196 var dir = File.new_for_path(dirname);
197 if (!dir.query_exists()) {
198 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
201 var pathprefix = abspath ? dirname : in_path;
204 var file_enum = dir.enumerate_children(
206 GLib.FileQueryInfoFlags.NONE,
212 while ((next_file = file_enum.next_file(null)) != null) {
213 var fn = next_file.get_display_name();
215 if (next_file.get_file_type () == GLib.FileType.DIRECTORY) {
217 GLib.debug("SKIP %s not regular ", fn);
220 if (!Regex.match_simple("^text", next_file.get_content_type())) {
223 GLib.debug("SCAN ADD %s : %s", fn, next_file.get_content_type());
224 ret.add(pathprefix + "/" + fn);
226 // any other valid types???
231 GLib.warning("oops - something went wrong scanning the projects\n");
237 public Gee.ArrayList<string> filesForCompile(string in_path, bool abspath = true)
239 var allfiles = this.filesAll(in_path,abspath);
240 var ret = new Gee.ArrayList<string>();
243 for (var i = 0; i < allfiles.size; i ++) {
244 var fn = allfiles.get(i);
246 if (Regex.match_simple("\\.vala$", fn)) {
250 // vala.c -- ignore..
251 if (Regex.match_simple("\\.vala\\.c$", fn)) {
255 if (!Regex.match_simple("\\.c$", fn)) {
259 // is the c file the same as a vala file...
263 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
267 if (allfiles.index_of( vv) > -1) {
270 // add the 'c' file..
277 ret.sort((fa,fb) => {
278 return ((string)fa).collate((string) fb);
284 public Gee.ArrayList<string> filesForOpen(string in_path)
286 var allfiles = this.filesAll(in_path);
287 var ret = new Gee.ArrayList<string>();
288 GLib.debug("SCAN %s - %d files",in_path, allfiles.size);
290 for (var i = 0; i < allfiles.size; i ++) {
291 var fn = allfiles.get(i);
292 var bn = GLib.Path.get_basename(fn);
295 if (Regex.match_simple("\\.vala\\.c$", fn)) {
296 GLib.debug("SKIP %s - vala.c",fn);
301 if (Regex.match_simple("\\.bjs$", fn)) {
302 GLib.debug("SKIP %s - .bjs",fn);
306 if (Regex.match_simple("\\~$", fn)) {
307 GLib.debug("SKIP %s - ~",fn);
310 if (Regex.match_simple("\\.stamp$", fn)) {
311 GLib.debug("SKIP %s - .o",fn);
314 if ("stamp-h1" == bn) {
315 GLib.debug("SKIP %s - .o",fn);
320 if ("config.h" == bn || "config.h.in" == bn || "config.log" == bn || "configure" == bn ) {
321 if (allfiles.index_of( in_path +"/configure.ac") > -1) {
326 if ("Makefile" == bn || "Makefile.in" == bn ) {
327 if (allfiles.index_of( in_path +"/Makefile.am") > -1) {
332 if (Regex.match_simple("^\\.", bn)) {
333 GLib.debug("SKIP %s - hidden",fn);
336 if (Regex.match_simple("\\.vala$", fn)) {
337 var vv = (new Regex("\\.vala$")).replace( fn, fn.length, 0, ".bjs");
338 if (allfiles.index_of( vv) > -1) {
339 GLib.debug("SKIP %s - .vala (got bjs)",fn);
342 GLib.debug("ADD %s",fn);
346 // vala.c -- ignore..
349 if (Regex.match_simple("\\.c$", fn)) {
351 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
352 if (allfiles.index_of( vv) > -1) {
353 GLib.debug("SKIP %s - .c (got vala)",fn);
356 GLib.debug("ADD %s",fn);
361 if (GLib.Path.get_basename( fn) == "config1.builder") {
364 // not .c / not .vala /not .bjs.. -- other type of file..
366 GLib.debug("ADD %s",fn);
367 // add the 'c' file..
370 GLib.debug("Exception %s",e.message);
375 ret.sort((fa,fb) => {
376 return ((string)fa).collate((string) fb);
386 public string resolve_path_combine_path(string first, string second)
389 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
393 //print("combined path = %s", ret + second);
396 public string resolve_path_times(string part, int times, string? clue = null)
399 for (int i = 0; i < times; i++)
401 if (clue != null && i > 0)
409 public string resolve_path(string _path, string? relative = null)
412 if (relative != null)
414 path = this.resolve_path_combine_path(path, relative);
416 string[] parts = path.split("/");
418 int relative_parts = 0;
420 foreach (var part in parts)
422 if (part.length < 1 || part == ".")
431 ret = ret[0: ret.length -1];
443 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
444 if (_path.has_prefix("/"))
451 public string[] vapidirs()
454 var sources = this.compilegroups.get("_default_").sources;
455 for(var i =0; i< sources.size; i++) {
457 var path = this.resolve_path( this.firstPath(), sources.get(i));
459 if (Path.get_basename (path) == "vapi") {
460 GLib.debug("Adding VAPIDIR: %s\n", path);
468 public string[] sourcedirs()
471 var sources = this.compilegroups.get("_default_").sources;
472 ret += this.firstPath();
473 for(var i =0; i< sources.size; i++) {
475 var path = this.resolve_path( this.firstPath(), sources.get(i));
476 if (path == this.firstPath()) {
479 if (Path.get_basename (path) == "vapi") {
483 // GLib.debug("Adding VAPIDIR: %s\n", path);
491 // an object describing a build config (or generic ...)
492 public class GtkValaSettings : Object {
494 public GtkValaSettings? parent;
496 public string compile_flags; // generic to all.
497 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
498 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
499 public string target_bin;
501 public string execute_args;
504 public GtkValaSettings(string name)
507 this.compile_flags = "";
508 this.target_bin = "";
509 this.packages = new Gee.ArrayList<string>();
510 this.sources = new Gee.ArrayList<string>();
511 this.execute_args = "";
516 public GtkValaSettings.from_json(Json.Object el) {
519 this.name = el.get_string_member("name");
520 this.compile_flags = el.get_string_member("compile_flags");
521 if ( el.has_member("execute_args")) {
522 this.execute_args = el.get_string_member("execute_args");
524 this.execute_args = "";
526 this.target_bin = el.get_string_member("target_bin");
527 // sources and packages.
528 this.sources = this.readArray(el.get_array_member("sources"));
529 this.packages = this.readArray(el.get_array_member("packages"));
533 // why not array of strings?
535 public Gee.ArrayList<string> readArray(Json.Array ar)
537 var ret = new Gee.ArrayList<string>();
538 for(var i =0; i< ar.get_length(); i++) {
539 ret.add(ar.get_string_element(i));
544 public Json.Object toJson()
546 var ret = new Json.Object();
547 ret.set_string_member("name", this.name);
548 ret.set_string_member("compile_flags", this.compile_flags);
549 ret.set_string_member("execute_args", this.execute_args);
550 ret.set_string_member("target_bin", this.target_bin);
551 ret.set_array_member("sources", this.writeArray(this.sources));
552 ret.set_array_member("packages", this.writeArray(this.packages));
555 public Json.Array writeArray(Gee.ArrayList<string> ar) {
556 var ret = new Json.Array();
557 for(var i =0; i< ar.size; i++) {
558 ret.add_string_element(ar.get(i));