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);
73 if (vs.name != "_default_") {
74 vs.parent = this.compilegroups.get("_default_");
76 this.compilegroups.set(vs.name,vs);
78 GLib.debug("%s\n",this.configToString ());
81 public string configToString()
83 var ar = new Json.Array();
84 var iter = this.compilegroups.map_iterator();
87 ar.add_object_element(iter.get_value().toJson());
90 var generator = new Json.Generator ();
92 generator.pretty = true;
93 var node = new Json.Node(Json.NodeType.ARRAY);
95 generator.set_root(node);
96 return generator.to_data(null);
99 public void writeConfig()
101 var fn = this.firstPath() + "/config1.builder";
102 GLib.debug("write: " + fn );
105 var f = GLib.File.new_for_path(fn);
106 var data_out = new GLib.DataOutputStream(
107 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
109 data_out.put_string(this.configToString(), null);
110 data_out.close(null);
116 * perhaps we should select the default in the window somewhere...
118 public string firstBuildModule()
120 var iter = this.compilegroups.map_iterator();
123 if (iter.get_value().name == "__default__") {
127 return iter.get_value().name;
133 public string relPath(string target)
135 var basename = this.firstPath();
136 // eg. base = /home/xxx/fred/blogs
137 // target = /home/xxx/fred/jones
139 // this does not work correctly...
143 if ( bb.length < target.length &&
144 target.substring(0, bb.length) == bb) {
146 return prefix + target.substring(bb.length );
149 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
152 bb = GLib.Path.get_dirname(bb);
159 * get a list of files for a folder..
161 * - in the project manager this has to list all possible compilable
162 * files - eg. exclue XXX.vala.c or XXX.c with the same name as
163 * a vala file (so to ignore the generated files)
165 * - for the editor navigation - this should exclude all files that
166 * are vala based on a bjs file..
170 public Gee.ArrayList<string> filesAll(string in_path,bool abspath = true)
172 var ret = new Gee.ArrayList<string>();
174 var dirname = this.resolve_path(
175 this.resolve_path_combine_path(this.firstPath(),in_path));
177 GLib.debug("SCAN %s", dirname);
178 // scan the directory for files -- ending with vala || c
181 var dir = File.new_for_path(dirname);
182 if (!dir.query_exists()) {
183 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
186 var pathprefix = abspath ? dirname : in_path;
189 var file_enum = dir.enumerate_children(
191 GLib.FileQueryInfoFlags.NONE,
197 while ((next_file = file_enum.next_file(null)) != null) {
198 var fn = next_file.get_display_name();
200 if (next_file.get_file_type () == GLib.FileType.DIRECTORY) {
202 GLib.debug("SKIP %s not regular ", fn);
205 if (!Regex.match_simple("^text", next_file.get_content_type())) {
208 GLib.debug("SCAN ADD %s : %s", fn, next_file.get_content_type());
209 ret.add(pathprefix + "/" + fn);
211 // any other valid types???
216 GLib.warning("oops - something went wrong scanning the projects\n");
222 public Gee.ArrayList<string> filesForCompile(string in_path, bool abspath = true)
224 var allfiles = this.filesAll(in_path,abspath);
225 var ret = new Gee.ArrayList<string>();
228 for (var i = 0; i < allfiles.size; i ++) {
229 var fn = allfiles.get(i);
231 if (Regex.match_simple("\\.vala$", fn)) {
235 // vala.c -- ignore..
236 if (Regex.match_simple("\\.vala\\.c$", fn)) {
240 if (!Regex.match_simple("\\.c$", fn)) {
244 // is the c file the same as a vala file...
248 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
252 if (allfiles.index_of( vv) > -1) {
255 // add the 'c' file..
262 ret.sort((fa,fb) => {
263 return ((string)fa).collate((string) fb);
269 public Gee.ArrayList<string> filesForOpen(string in_path)
271 var allfiles = this.filesAll(in_path);
272 var ret = new Gee.ArrayList<string>();
273 GLib.debug("SCAN %s - %d files",in_path, allfiles.size);
275 for (var i = 0; i < allfiles.size; i ++) {
276 var fn = allfiles.get(i);
277 var bn = GLib.Path.get_basename(fn);
280 if (Regex.match_simple("\\.vala\\.c$", fn)) {
281 GLib.debug("SKIP %s - vala.c",fn);
286 if (Regex.match_simple("\\.bjs$", fn)) {
287 GLib.debug("SKIP %s - .bjs",fn);
291 if (Regex.match_simple("\\~$", fn)) {
292 GLib.debug("SKIP %s - ~",fn);
295 if (Regex.match_simple("\\.stamp$", fn)) {
296 GLib.debug("SKIP %s - .o",fn);
299 if ("stamp-h1" == bn) {
300 GLib.debug("SKIP %s - .o",fn);
305 if ("config.h" == bn || "config.h.in" == bn || "config.log" == bn || "configure" == bn ) {
306 if (allfiles.index_of( in_path +"/configure.ac") > -1) {
311 if ("Makefile" == bn || "Makefile.in" == bn ) {
312 if (allfiles.index_of( in_path +"/Makefile.am") > -1) {
317 if (Regex.match_simple("^\\.", bn)) {
318 GLib.debug("SKIP %s - hidden",fn);
321 if (Regex.match_simple("\\.vala$", fn)) {
322 var vv = (new Regex("\\.vala$")).replace( fn, fn.length, 0, ".bjs");
323 if (allfiles.index_of( vv) > -1) {
324 GLib.debug("SKIP %s - .vala (got bjs)",fn);
327 GLib.debug("ADD %s",fn);
331 // vala.c -- ignore..
334 if (Regex.match_simple("\\.c$", fn)) {
336 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
337 if (allfiles.index_of( vv) > -1) {
338 GLib.debug("SKIP %s - .c (got vala)",fn);
341 GLib.debug("ADD %s",fn);
346 if (GLib.Path.get_basename( fn) == "config1.builder") {
349 // not .c / not .vala /not .bjs.. -- other type of file..
351 GLib.debug("ADD %s",fn);
352 // add the 'c' file..
355 GLib.debug("Exception %s",e.message);
360 ret.sort((fa,fb) => {
361 return ((string)fa).collate((string) fb);
371 public string resolve_path_combine_path(string first, string second)
374 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
378 //print("combined path = %s", ret + second);
381 public string resolve_path_times(string part, int times, string? clue = null)
384 for (int i = 0; i < times; i++)
386 if (clue != null && i > 0)
394 public string resolve_path(string _path, string? relative = null)
397 if (relative != null)
399 path = this.resolve_path_combine_path(path, relative);
401 string[] parts = path.split("/");
403 int relative_parts = 0;
405 foreach (var part in parts)
407 if (part.length < 1 || part == ".")
416 ret = ret[0: ret.length -1];
428 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
429 if (_path.has_prefix("/"))
436 public string[] vapidirs()
439 var sources = this.compilegroups.get("_default_").sources;
440 for(var i =0; i< sources.size; i++) {
442 var path = this.resolve_path( this.firstPath(), sources.get(i));
444 if (Path.get_basename (path) == "vapi") {
445 GLib.debug("Adding VAPIDIR: %s\n", path);
453 public string[] sourcedirs()
456 var sources = this.compilegroups.get("_default_").sources;
457 ret += this.firstPath();
458 for(var i =0; i< sources.size; i++) {
460 var path = this.resolve_path( this.firstPath(), sources.get(i));
461 if (path == this.firstPath()) {
464 if (Path.get_basename (path) == "vapi") {
468 // GLib.debug("Adding VAPIDIR: %s\n", path);
476 // an object describing a build config (or generic ...)
477 public class GtkValaSettings : Object {
479 public GtkValaSettings? parent;
481 public string compile_flags; // generic to all.
482 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
483 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
484 public string target_bin;
486 public string execute_args;
489 public GtkValaSettings(string name)
492 this.compile_flags = "";
493 this.target_bin = "";
494 this.packages = new Gee.ArrayList<string>();
495 this.sources = new Gee.ArrayList<string>();
496 this.execute_args = "";
501 public GtkValaSettings.from_json(Json.Object el) {
504 this.name = el.get_string_member("name");
505 this.compile_flags = el.get_string_member("compile_flags");
506 if ( el.has_member("execute_args")) {
507 this.execute_args = el.get_string_member("execute_args");
509 this.execute_args = "";
511 this.target_bin = el.get_string_member("target_bin");
512 // sources and packages.
513 this.sources = this.readArray(el.get_array_member("sources"));
514 this.packages = this.readArray(el.get_array_member("packages"));
518 // why not array of strings?
520 public Gee.ArrayList<string> readArray(Json.Array ar)
522 var ret = new Gee.ArrayList<string>();
523 for(var i =0; i< ar.get_length(); i++) {
524 ret.add(ar.get_string_element(i));
529 public Json.Object toJson()
531 var ret = new Json.Object();
532 ret.set_string_member("name", this.name);
533 ret.set_string_member("compile_flags", this.compile_flags);
534 ret.set_string_member("execute_args", this.execute_args);
535 ret.set_string_member("target_bin", this.target_bin);
536 ret.set_array_member("sources", this.writeArray(this.sources));
537 ret.set_array_member("packages", this.writeArray(this.packages));
540 public Json.Array writeArray(Gee.ArrayList<string> ar) {
541 var ret = new Json.Array();
542 for(var i =0; i< ar.size; i++) {
543 ret.add_string_element(ar.get(i));