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;
146 public string firstBuildModuleWith(JsRender.JsRender file)
149 var iter = this.compilegroups.map_iterator();
152 if (iter.get_value().name == "_default_") {
155 if (iter.get_value().has_file(file)) {
156 return iter.get_value().name;
164 public string relPath(string target)
166 var basename = this.firstPath();
167 // eg. base = /home/xxx/fred/blogs
168 // target = /home/xxx/fred/jones
170 // this does not work correctly...
174 if ( bb.length < target.length &&
175 target.substring(0, bb.length) == bb) {
177 return prefix + target.substring(bb.length );
180 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
183 bb = GLib.Path.get_dirname(bb);
190 * get a list of files for a folder..
192 * - in the project manager this has to list all possible compilable
193 * files - eg. exclue XXX.vala.c or XXX.c with the same name as
194 * a vala file (so to ignore the generated files)
196 * - for the editor navigation - this should exclude all files that
197 * are vala based on a bjs file..
201 public Gee.ArrayList<string> filesAll(string in_path,bool abspath = true)
203 var ret = new Gee.ArrayList<string>();
205 var dirname = this.resolve_path(
206 this.resolve_path_combine_path(this.firstPath(),in_path));
208 GLib.debug("SCAN %s", dirname);
209 // scan the directory for files -- ending with vala || c
212 var dir = File.new_for_path(dirname);
213 if (!dir.query_exists()) {
214 GLib.debug("SCAN %s - skip - does not exist\n", dirname);
217 var pathprefix = abspath ? dirname : in_path;
220 var file_enum = dir.enumerate_children(
222 GLib.FileQueryInfoFlags.NONE,
228 while ((next_file = file_enum.next_file(null)) != null) {
229 var fn = next_file.get_display_name();
231 if (next_file.get_file_type () == GLib.FileType.DIRECTORY) {
233 GLib.debug("SKIP %s not regular ", fn);
236 if (!Regex.match_simple("^text", next_file.get_content_type())) {
239 GLib.debug("SCAN ADD %s : %s", fn, next_file.get_content_type());
240 ret.add(pathprefix + "/" + fn);
242 // any other valid types???
247 GLib.warning("oops - something went wrong scanning the projects\n");
253 public Gee.ArrayList<string> filesForCompile(string in_path, bool abspath = true)
255 var allfiles = this.filesAll(in_path,abspath);
256 var ret = new Gee.ArrayList<string>();
259 for (var i = 0; i < allfiles.size; i ++) {
260 var fn = allfiles.get(i);
262 if (Regex.match_simple("\\.vala$", fn)) {
266 // vala.c -- ignore..
267 if (Regex.match_simple("\\.vala\\.c$", fn)) {
271 if (!Regex.match_simple("\\.c$", fn)) {
275 // is the c file the same as a vala file...
279 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
283 if (allfiles.index_of( vv) > -1) {
286 // add the 'c' file..
293 ret.sort((fa,fb) => {
294 return ((string)fa).collate((string) fb);
300 public Gee.ArrayList<string> filesForOpen(string in_path)
302 var allfiles = this.filesAll(in_path);
303 var ret = new Gee.ArrayList<string>();
304 GLib.debug("SCAN %s - %d files",in_path, allfiles.size);
306 for (var i = 0; i < allfiles.size; i ++) {
307 var fn = allfiles.get(i);
308 var bn = GLib.Path.get_basename(fn);
311 if (Regex.match_simple("\\.vala\\.c$", fn)) {
312 GLib.debug("SKIP %s - vala.c",fn);
317 if (Regex.match_simple("\\.bjs$", fn)) {
318 GLib.debug("SKIP %s - .bjs",fn);
322 if (Regex.match_simple("\\~$", fn)) {
323 GLib.debug("SKIP %s - ~",fn);
326 if (Regex.match_simple("\\.stamp$", fn)) {
327 GLib.debug("SKIP %s - .o",fn);
330 if ("stamp-h1" == bn) {
331 GLib.debug("SKIP %s - .o",fn);
336 if ("config.h" == bn || "config.h.in" == bn || "config.log" == bn || "configure" == bn ) {
337 if (allfiles.index_of( in_path +"/configure.ac") > -1) {
342 if ("Makefile" == bn || "Makefile.in" == bn ) {
343 if (allfiles.index_of( in_path +"/Makefile.am") > -1) {
348 if (Regex.match_simple("^\\.", bn)) {
349 GLib.debug("SKIP %s - hidden",fn);
352 if (Regex.match_simple("\\.vala$", fn)) {
353 var vv = (new Regex("\\.vala$")).replace( fn, fn.length, 0, ".bjs");
354 if (allfiles.index_of( vv) > -1) {
355 GLib.debug("SKIP %s - .vala (got bjs)",fn);
358 GLib.debug("ADD %s",fn);
362 // vala.c -- ignore..
365 if (Regex.match_simple("\\.c$", fn)) {
367 var vv = (new Regex("\\.c$")).replace( fn, fn.length, 0, ".vala");
368 if (allfiles.index_of( vv) > -1) {
369 GLib.debug("SKIP %s - .c (got vala)",fn);
372 GLib.debug("ADD %s",fn);
377 if (GLib.Path.get_basename( fn) == "config1.builder") {
380 // not .c / not .vala /not .bjs.. -- other type of file..
382 GLib.debug("ADD %s",fn);
383 // add the 'c' file..
386 GLib.debug("Exception %s",e.message);
391 ret.sort((fa,fb) => {
392 return ((string)fa).collate((string) fb);
402 public string resolve_path_combine_path(string first, string second)
405 if (first.length > 0 && second.length > 0 && !first.has_suffix("/") && !second.has_prefix("/"))
409 //print("combined path = %s", ret + second);
412 public string resolve_path_times(string part, int times, string? clue = null)
415 for (int i = 0; i < times; i++)
417 if (clue != null && i > 0)
425 public string resolve_path(string _path, string? relative = null)
428 if (relative != null)
430 path = this.resolve_path_combine_path(path, relative);
432 string[] parts = path.split("/");
434 int relative_parts = 0;
436 foreach (var part in parts)
438 if (part.length < 1 || part == ".")
447 ret = ret[0: ret.length -1];
459 path = this.resolve_path_combine_path(this.resolve_path_times("..", relative_parts, "/"), string.joinv("/", ret));
460 if (_path.has_prefix("/"))
467 public string[] vapidirs()
470 var sources = this.compilegroups.get("_default_").sources;
471 for(var i =0; i< sources.size; i++) {
473 var path = this.resolve_path( this.firstPath(), sources.get(i));
475 if (Path.get_basename (path) == "vapi") {
476 GLib.debug("Adding VAPIDIR: %s\n", path);
484 public string[] sourcedirs()
487 var sources = this.compilegroups.get("_default_").sources;
488 ret += this.firstPath();
489 for(var i =0; i< sources.size; i++) {
491 var path = this.resolve_path( this.firstPath(), sources.get(i));
492 if (path == this.firstPath()) {
495 if (Path.get_basename (path) == "vapi") {
499 // GLib.debug("Adding VAPIDIR: %s\n", path);