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++);
39 public Gee.HashMap<string,GtkValaSettings> compilegroups;
41 public void loadConfig() throws GLib.Error
43 // load a builder.config JSON file.
45 this.compilegroups = new Gee.HashMap<string,GtkValaSettings>();
48 var fn = this.firstPath() + "/config1.builder";
49 print("load: " + fn );
51 if (!FileUtils.test(fn, FileTest.EXISTS)) {
52 this.compilegroups.set("_default_", new GtkValaSettings("_default_") );
56 var pa = new Json.Parser();
57 pa.load_from_file(fn);
58 var node = pa.get_root();
60 // should be an array really.
61 if (node.get_node_type () != Json.NodeType.ARRAY) {
62 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
65 var obj = node.get_array ();
66 for(var i= 0;i<obj.get_length();i++) {
67 var el = obj.get_object_element(i);
68 var vs = new GtkValaSettings.from_json(el);
69 if (vs.name != "_default_") {
70 vs.parent = this.compilegroups.get("_default_");
72 this.compilegroups.set(vs.name,vs);
76 public string configToString()
78 var ar = new Json.Array();
79 var iter = this.compilegroups.map_iterator();
82 ar.add_object_element(iter.get_value().toJson());
85 var generator = new Json.Generator ();
87 generator.pretty = true;
88 var node = new Json.Node(Json.NodeType.ARRAY);
90 generator.set_root(node);
91 return generator.to_data(null);
94 public void writeConfig()
96 var fn = this.firstPath() + "/config1.builder";
97 print("write: " + fn );
102 var iter = this.compilegroups.map_iterator();
105 ar.add_object_element(iter.get_value().toJson());
108 var generator = new Json.Generator ();
109 generator.indent = 4;
110 generator.pretty = true;
111 var node = new Json.Node(Json.NodeType.ARRAY);
113 generator.set_root(node);
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);
127 public string relPath(string target)
129 var basename = this.firstPath();
130 // eg. base = /home/xxx/fred/blogs
131 // target = /home/xxx/fred/jones
135 if ( bb.length < target.length &&
136 target.substring(0, bb.length) == bb) {
137 return prefix + target.substring(bb.length +1);
140 throw new Error.INVALID_FORMAT ("Could not work out relative path %s to %s",
143 bb = GLib.Path.get_dirname(bb);
152 // an object describing a build config (or generic ...)
153 public class GtkValaSettings : Object {
155 public GtkValaSettings? parent;
157 public string compile_flags; // generic to all.
158 public Gee.ArrayList<string> packages; // list of packages?? some might be genericly named?
159 public Gee.ArrayList<string> sources; // list of files+dirs (relative to project)
160 public string target_bin;
163 public GtkValaSettings(string name)
166 this.compile_flags = "";
167 this.target_bin = "";
168 this.packages = new Gee.ArrayList<string>();
169 this.sources = new Gee.ArrayList<string>();
174 public GtkValaSettings.from_json(Json.Object el) {
177 this.name = el.get_string_member("name");
178 this.compile_flags = el.get_string_member("compile_flags");
179 this.target_bin = el.get_string_member("target_bin");
180 // sources and packages.
181 this.sources = this.readArray(el.get_array_member("sources"));
182 this.packages = this.readArray(el.get_array_member("packages"));
185 public Gee.ArrayList<string> readArray(Json.Array ar)
187 var ret = new Gee.ArrayList<string>();
188 for(var i =0; i< ar.get_length(); i++) {
189 ret.add(ar.get_string_element(i));
194 public Json.Object toJson()
196 var ret = new Json.Object();
197 ret.set_string_member("name", this.name);
198 ret.set_string_member("compile_flags", this.compile_flags);
199 ret.set_string_member("target_bin", this.target_bin);
200 ret.set_array_member("sources", this.writeArray(this.sources));
201 ret.set_array_member("packages", this.writeArray(this.packages));
204 public Json.Array writeArray(Gee.ArrayList<string> ar) {
205 var ret = new Json.Array();
206 for(var i =0; i< ar.size; i++) {
207 ret.add_string_element(ar.get(i));