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++);
38 } catch (GLib.Error e ) {
43 public Gee.HashMap<string,GtkValaSettings> compilegroups;
45 public void loadConfig() throws GLib.Error
47 // load a builder.config JSON file.
49 this.compilegroups = new Gee.HashMap<string,GtkValaSettings>();
52 var fn = this.firstPath() + "/config1.builder";
53 GLib.debug("load: " + fn );
55 if (!FileUtils.test(fn, FileTest.EXISTS)) {
56 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
60 var pa = new Json.Parser();
61 pa.load_from_file(fn);
62 var node = pa.get_root();
64 // should be an array really.
65 if (node.get_node_type () != Json.NodeType.ARRAY) {
66 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
69 var obj = node.get_array ();
70 for(var i= 0;i<obj.get_length();i++) {
71 var el = obj.get_object_element(i);
72 var vs = new GtkValaSettings.from_json(el);
74 print("problem loading json file");
77 if (vs.name != "_default_") {
78 vs.parent = this.compilegroups.get("_default_");
80 this.compilegroups.set(vs.name,vs);
82 GLib.debug("%s\n",this.configToString ());
85 public string configToString()
87 var ar = new Json.Array();
88 var iter = this.compilegroups.map_iterator();
91 ar.add_object_element(iter.get_value().toJson());
94 var generator = new Json.Generator ();
96 generator.pretty = true;
97 var node = new Json.Node(Json.NodeType.ARRAY);
99 generator.set_root(node);
100 return generator.to_data(null);
103 public void writeConfig()
105 var fn = this.firstPath() + "/config1.builder";
106 GLib.debug("write: " + fn );
109 var f = GLib.File.new_for_path(fn);
110 var data_out = new GLib.DataOutputStream(
111 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
113 data_out.put_string(this.configToString(), null);
114 data_out.close(null);
120 * perhaps we should select the default in the window somewhere...
122 public string firstBuildModule()
124 var iter = this.compilegroups.map_iterator();
127 if (iter.get_value().name == "_default_") {
131 return iter.get_value().name;
137 public string relPath(string target)
139 var basename = this.firstPath();
140 // eg. base = /home/xxx/fred/blogs
141 // target = /home/xxx/fred/jones
143 // this does not work correctly...
147 if ( bb.length < target.length &&
148 target.substring(0, bb.length) == bb) {
150 return prefix + target.substring(bb.length );
153 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
156 bb = GLib.Path.get_dirname(bb);
163 * get a list of files for a folder..
165 * - in the project manager this has to list all possible compilable
166 * files - eg. exclue XXX.vala.c or XXX.c with the same name as
167 * a vala file (so to ignore the generated files)
169 * - for the editor navigation - this should exclude all files that
170 * are vala based on a bjs file..
174 public Gee.ArrayList<string> filesAll(string in_path,bool abspath = true)
176 var ret = new Gee.ArrayList<string>();
178 var dirname = this.resolve_path(
179 this.resolve_path_combine_path(this.firstPath(),in_path));
181 GLib.debug("SCAN %s", dirname);
182 // scan the directory for files -- ending with vala || c
185 var dir = File.new_for_path(dirname);
186 if (!dir.query_exists()) {
187 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
190 var pathprefix = abspath ? dirname : in_path;
193 var file_enum = dir.enumerate_children(
195 GLib.FileQueryInfoFlags.NONE,
201 while ((next_file = file_enum.next_file(null)) != null) {
202 var fn = next_file.get_display_name();
204 if (next_file.get_file_type () == GLib.FileType.DIRECTORY) {
206 GLib.debug("SKIP %s not regular ", fn);
209 if (!Regex.match_simple("^text", next_file.get_content_type())) {
212 GLib.debug("SCAN ADD %s : %s", fn, next_file.get_content_type());
213 ret.add(pathprefix + "/" + fn);
215 // any other valid types???
220 GLib.warning("oops - something went wrong scanning the projects\n");
226 public Gee.ArrayList<string> filesForCompile(string in_path, bool abspath = true)
228 var allfiles = this.filesAll(in_path,abspath);
229 var ret = new Gee.ArrayList<string>();
232 for (var i = 0; i < allfiles.size; i ++) {
233 var fn = allfiles.get(i);
235 if (Regex.match_simple("\\.vala$", fn)) {
239 // vala.c -- ignore..
240 if (Regex.match_simple("\\.vala\\.c$", fn)) {
244 if (!Regex.match_simple("\\.c$", fn)) {
248 // is the c file the same as a vala file...
252 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
256 if (allfiles.index_of( vv) > -1) {
259 // add the 'c' file..
266 ret.sort((fa,fb) => {
267 return ((string)fa).collate((string) fb);
273 public Gee.ArrayList<string> filesForOpen(string in_path)
275 var allfiles = this.filesAll(in_path);
276 var ret = new Gee.ArrayList<string>();
277 GLib.debug("SCAN %s - %d files",in_path, allfiles.size);
279 for (var i = 0; i < allfiles.size; i ++) {
280 var fn = allfiles.get(i);
281 var bn = GLib.Path.get_basename(fn);
284 if (Regex.match_simple("\\.vala\\.c$", fn)) {
285 GLib.debug("SKIP %s - vala.c",fn);
290 if (Regex.match_simple("\\.bjs$", fn)) {
291 GLib.debug("SKIP %s - .bjs",fn);
295 if (Regex.match_simple("\\~$", fn)) {
296 GLib.debug("SKIP %s - ~",fn);
299 if (Regex.match_simple("\\.stamp$", fn)) {
300 GLib.debug("SKIP %s - .o",fn);
303 if ("stamp-h1" == bn) {
304 GLib.debug("SKIP %s - .o",fn);
309 if ("config.h" == bn || "config.h.in" == bn || "config.log" == bn || "configure" == bn ) {
310 if (allfiles.index_of( in_path +"/configure.ac") > -1) {
315 if ("Makefile" == bn || "Makefile.in" == bn ) {
316 if (allfiles.index_of( in_path +"/Makefile.am") > -1) {
321 if (Regex.match_simple("^\\.", bn)) {
322 GLib.debug("SKIP %s - hidden",fn);
325 if (Regex.match_simple("\\.vala$", fn)) {
326 var vv = (new Regex("\\.vala$")).replace( fn, fn.length, 0, ".bjs");
327 if (allfiles.index_of( vv) > -1) {
328 GLib.debug("SKIP %s - .vala (got bjs)",fn);
331 GLib.debug("ADD %s",fn);
335 // vala.c -- ignore..
338 if (Regex.match_simple("\\.c$", fn)) {
340 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
341 if (allfiles.index_of( vv) > -1) {
342 GLib.debug("SKIP %s - .c (got vala)",fn);
345 GLib.debug("ADD %s",fn);
350 if (GLib.Path.get_basename( fn) == "config1.builder") {
353 // not .c / not .vala /not .bjs.. -- other type of file..
355 GLib.debug("ADD %s",fn);
356 // add the 'c' file..
359 GLib.debug("Exception %s",e.message);
364 ret.sort((fa,fb) => {
365 return ((string)fa).collate((string) fb);
375 public string resolve_path_combine_path(string first, string second)
378 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
382 //print("combined path = %s", ret + second);
385 public string resolve_path_times(string part, int times, string? clue = null)
388 for (int i = 0; i < times; i++)
390 if (clue != null && i > 0)
398 public string resolve_path(string _path, string? relative = null)
401 if (relative != null)
403 path = this.resolve_path_combine_path(path, relative);
405 string[] parts = path.split("/");
407 int relative_parts = 0;
409 foreach (var part in parts)
411 if (part.length < 1 || part == ".")
420 ret = ret[0: ret.length -1];
432 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
433 if (_path.has_prefix("/"))
440 public string[] vapidirs()
443 var sources = this.compilegroups.get("_default_").sources;
444 for(var i =0; i< sources.size; i++) {
446 var path = this.resolve_path( this.firstPath(), sources.get(i));
448 if (Path.get_basename (path) == "vapi") {
449 GLib.debug("Adding VAPIDIR: %s\n", path);
457 public string[] sourcedirs()
460 var sources = this.compilegroups.get("_default_").sources;
461 ret += this.firstPath();
462 for(var i =0; i< sources.size; i++) {
464 var path = this.resolve_path( this.firstPath(), sources.get(i));
465 if (path == this.firstPath()) {
468 if (Path.get_basename (path) == "vapi") {
472 // GLib.debug("Adding VAPIDIR: %s\n", path);
480 // an object describing a build config (or generic ...)
481 public class GtkValaSettings : Object {
483 public GtkValaSettings? parent;
485 public string compile_flags; // generic to all.
486 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
487 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
488 public string target_bin;
490 public string execute_args;
493 public GtkValaSettings(string name)
496 this.compile_flags = "";
497 this.target_bin = "";
498 this.packages = new Gee.ArrayList<string>();
499 this.sources = new Gee.ArrayList<string>();
500 this.execute_args = "";
505 public GtkValaSettings.from_json(Json.Object el) {
508 this.name = el.get_string_member("name");
509 this.compile_flags = el.get_string_member("compile_flags");
510 if ( el.has_member("execute_args")) {
511 this.execute_args = el.get_string_member("execute_args");
513 this.execute_args = "";
515 this.target_bin = el.get_string_member("target_bin");
516 // sources and packages.
517 this.sources = this.readArray(el.get_array_member("sources"));
518 this.packages = this.readArray(el.get_array_member("packages"));
522 // why not array of strings?
524 public Gee.ArrayList<string> readArray(Json.Array ar)
526 var ret = new Gee.ArrayList<string>();
527 for(var i =0; i< ar.get_length(); i++) {
528 ret.add(ar.get_string_element(i));
533 public Json.Object toJson()
535 var ret = new Json.Object();
536 ret.set_string_member("name", this.name);
537 ret.set_string_member("compile_flags", this.compile_flags);
538 ret.set_string_member("execute_args", this.execute_args);
539 ret.set_string_member("target_bin", this.target_bin);
540 ret.set_array_member("sources", this.writeArray(this.sources));
541 ret.set_array_member("packages", this.writeArray(this.packages));
544 public Json.Array writeArray(Gee.ArrayList<string> ar) {
545 var ret = new Json.Array();
546 for(var i =0; i< ar.size; i++) {
547 ret.add_string_element(ar.get(i));