1 //<Script type="text/javascript">
6 * Projects can only contain one directory... - it can import other projects..(later)
8 * we need to sort out that - paths is currently a key/value array..
10 * currently we store projects in ~/.Builder/{md5}.json
11 - then for Gtk projects we have a file config1.builder - which contains dependancies, and a list of files for each target
12 (for builder it's ended up in src/Builder/config1.builder? by accident.
13 - should really support something like
14 roobuilder --build {path_to_cfg} {target} - or local directory if not set..
15 roobuilder --build-errors {path_to_cfg} {target} - or local directory if not set..
20 should really store project data in the directory of the project?
24 // List of projects - just an array of paths in .Builder/Projects.json
26 // .roobuilder.jcfg << hidden file with project details?
35 public errordomain Error {
45 // static array of all projects.
46 private Gee.ArrayList<Project> projects;
50 public bool projects_loaded = false;
53 // used to pass around callbacks with project as a return
54 public class Callback : Object {
55 public signal void call(Project project);
58 public abstract class Project : Object {
60 public signal void on_changed ();
63 //public string fn = ""; // just a md5...
67 return GLib.Path.get_basename(path);
73 public string path = "";
74 private Gee.ArrayList<JsRender.JsRender> sub_paths;
76 private Gee.HashMap<string,JsRender.JsRender> files ; // contains full list of files.
80 //public Json.Object json_project_data;
82 public Palete.Palete palete;
84 private bool is_scanned = false;
85 public Gee.HashMap<string,Palete.GirObject> gir_cache = null; // used by Gir ??? is this used by Roo?
86 public Palete.ValaCompileRequest last_request = null;
88 protected Project (string path) {
91 //this.json_project_data = new Json.Object();
93 this.is_scanned = false;
94 this.sub_paths = new Gee.ArrayList<JsRender.JsRender>();
95 this.files = new Gee.HashMap<string,JsRender.JsRender>();
96 //XObject.extend(this, cfg);
107 public static void loadAll(bool force = false)
109 if (projects_loaded && !force) {
113 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
114 var dir = File.new_for_path(dirname);
115 if (!dir.query_exists()) {
117 dir.make_directory();
118 } catch(GLib.Error e) {
119 GLib.error("could not make builder directory");
123 projects = new Gee.ArrayList<Project>();
126 if (FileUtils.test(dirname + "/Projects.list", GLib.FileTest.IS_REGULAR)) {
128 projects_loaded = true;
131 convertOldProjects(); // this saves..
132 foreach(var p in projects) {
135 projects_loaded = true;
139 public static void remove(Project p) {
145 public static void saveProjectList()
147 var f = new Json.Object();
148 foreach(var p in projects) {
149 f.set_string_member(p.path, p.xtype);
152 var generator = new Json.Generator ();
153 var root = new Json.Node(Json.NodeType.OBJECT);
155 generator.set_root (root);
156 generator.pretty = true;
157 generator.indent = 4;
159 var data = generator.to_data (null);
160 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
161 GLib.debug("Write new Project list\n %s", data);
165 //FileUtils.set_contents(dirname + "/" + this.fn + ".json", s, s.length);
166 FileUtils.set_contents(dirname + "/Projects.list", data, data.length);
167 } catch (GLib.Error e) {
168 GLib.error("failed to save file %s", e.message);
175 public static void convertOldProjects()
178 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
179 var dir = File.new_for_path(dirname);
181 var file_enum = dir.enumerate_children(
182 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
183 GLib.FileQueryInfoFlags.NONE,
188 while ((next_file = file_enum.next_file(null)) != null) {
189 var fn = next_file.get_display_name();
190 if (!Regex.match_simple("\\.json$", fn)) {
193 Project.factoryFromFileOld(dirname + "/" + fn);
195 } catch(GLib.Error e) {
196 GLib.warning("oops - something went wrong scanning the projects\n");
198 GLib.debug("Loaded all old Projects - saving");
199 Project.saveProjectList();
203 public static Gee.ArrayList<Project> allProjectsByName()
210 public static Project? getProjectByName(string name)
213 foreach (var p in projects) {
214 if (p.name == name) {
222 public static Project? getProjectByPath(string path)
225 foreach (var p in projects) {
226 if (p.path == path) {
234 public static string listAllToString()
240 all.sort((fa,fb) => {
241 return ((Project)fa).name.collate(((Project)fb).name);
245 var iter = all.list_iterator();
246 var ret = "ID\tName\tDirectory\n";
247 while (iter.next()) {
248 ret += "%s\t%s\n".printf(
262 public static void loadIntoStore(GLib.ListStore st)
265 foreach (var p in projects) {
274 static void loadProjectList()
277 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
279 projects = new Gee.ArrayList<Project>();
281 var pa = new Json.Parser();
283 pa.load_from_file(dirname + "/Projects.list");
284 } catch (GLib.Error e) {
285 GLib.error("could not load json file %s", e.message);
287 var node = pa.get_root();
288 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
289 GLib.error( dirname + "/Projects.list - invalid format?");
294 var obj = node.get_object ();
295 obj.foreach_member((sobj, key, val) => {
296 GLib.debug("read ProjectList %s: %s", key, val.get_string());
297 // facotry adds project!
299 Project.factory(val.get_string(), key );
300 } catch (GLib.Error e ) {
301 GLib.debug("error createing project %s", e.message);
309 // load project data from project file.
310 public static void factoryFromFileOld(string jsonfile)
313 GLib.debug("parse %s", jsonfile);
315 var pa = new Json.Parser();
317 pa.load_from_file(jsonfile);
318 } catch (GLib.Error e) {
319 GLib.error("could not load json file %s", e.message);
321 var node = pa.get_root();
324 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
325 GLib.debug("SKIP " + jsonfile + " - invalid format?");
329 var obj = node.get_object ();
330 var xtype = obj.get_string_member("xtype");
333 var paths = obj.get_object_member("paths");
336 paths.foreach_member((sobj, key, val) => {
343 if (fpath.length < 0 || !FileUtils.test(fpath,FileTest.IS_DIR)) {
349 proj = factory(xtype, fpath);
351 GLib.debug("Skip file - invalid file type");
355 //proj.json_project_data = obj; // store the original object...
357 //proj.fn = Path.get_basename(jsonfile).split(".")[0];
362 proj.name = obj.get_string_member("name");
364 // used to load paths..
365 //proj.initSubDirectories();
368 //proj.initDatabase();
370 GLib.debug("Add Project %s", proj.name);
378 public static Project factory(string xtype, string path) throws Error
381 // check to see if it's already loaded..
383 foreach(var p in projects) {
384 if (p.path == path) {
392 var ret = new Gtk(path);
397 var ret = new Roo(path);
402 // return new Flutter(path);
404 throw new Error.INVALID_TYPE("invalid project type");
419 //var dirname = GLib.Environment.get_home_dir() + "/.Builder";
421 var s = this.toJSON();
422 GLib.debug("Save Project %s\n%s", this.name, s);
424 //FileUtils.set_contents(dirname + "/" + this.fn + ".json", s, s.length);
425 FileUtils.set_contents(this.path + "/.roobuilder.jcfg", s, s.length);
426 } catch (GLib.Error e) {
427 GLib.error("failed to save file %s", e.message);
435 public string toJSON( )
438 var obj = new Json.Object();
439 obj.set_string_member("xtype", this.xtype);
444 var generator = new Json.Generator ();
445 var root = new Json.Node(Json.NodeType.OBJECT);
446 root.init_object(obj);
447 generator.set_root (root);
449 generator.pretty = true;
450 generator.indent = 4;
453 return generator.to_data (null);
458 // used to check what type a project might be..
460 public static string peekProjectType(string fn)
462 var pa = new Json.Parser();
464 pa.load_from_file(fn);
465 } catch (GLib.Error e) {
466 GLib.debug("could not load json file %s", e.message);
470 var node = pa.get_root();
472 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
473 GLib.debug("SKIP %s/.roobuilder.jcfg - invalid format?",fn);
477 var obj = node.get_object ();
479 var xtype = obj.get_string_member("xtype");
480 return xtype == null ? "" : xtype;
485 // this will do a full scan - should only be done on viewing project..
486 // not initial load.. - may take time.
490 if (this.is_scanned) {
493 GLib.debug("load is_scanned = false");
495 if (FileUtils.test(this.path + "/.roobuilder.jcfg", FileTest.EXISTS)) {
497 var pa = new Json.Parser();
499 pa.load_from_file(this.path + "/.roobuilder.jcfg");
500 } catch (GLib.Error e) {
501 GLib.error("could not load json file %s", e.message);
503 var node = pa.get_root();
506 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
507 GLib.debug("SKIP %s/.roobuilder.jcfg - invalid format?",this.path);
511 var obj = node.get_object ();
515 // used to load paths..
516 this.sub_paths = new Gee.ArrayList<JsRender.JsRender>();
517 this.files = new Gee.HashMap<string,JsRender.JsRender>();
518 this.loadSubDirectories("", 0);
521 this.is_scanned = true; // loaded.. dont need to do it again..
522 GLib.debug("load is_scanned = true");
526 public abstract void loadJson(Json.Object obj);
527 public abstract void saveJson(Json.Object obj);
531 public string firstPath()
533 var iter = this.paths.map_iterator();
534 while (iter.next()) {
535 return iter.get_key();
541 public bool hasPath(string path)
543 var iter = this.paths.map_iterator();
544 while (iter.next()) {
545 if (iter.get_key() == path) {
554 // returns the first path
555 public string getName()
557 var iter = this.paths.map_iterator();
558 while (iter.next()) {
559 return GLib.Path.get_basename(iter.get_key());
566 public Gee.ArrayList<JsRender.JsRender> sortedFiles()
568 var files = new Gee.ArrayList<JsRender.JsRender>();
570 var fiter = this.files.map_iterator();
571 while(fiter.next()) {
572 files.add(fiter.get_value());
574 files.sort((fa,fb) => {
575 return ((JsRender.JsRender)fa).name.collate(((JsRender.JsRender)fb).name);
584 public string listAllFilesToString()
587 var iter = this.sortedFiles().list_iterator();
588 var ret = "ID\tName\tDirectory\n";
589 while (iter.next()) {
590 ret += "%s\n".printf(
606 public JsRender.JsRender? getByName(string name)
608 foreach(var f in this.files.values) {
609 if (f.name == name) {
615 // this get's a file using the full path ( replaces vala->bjs if they exist);
617 public JsRender.JsRender? getByPath(string path)
620 // keys are not paths...
621 foreach(var f in this.files.values) {
622 GLib.debug("check %s = %s ? %s", path, f.path, f.targetName());
623 if (f.path == path || f.targetName() == path) {
630 public JsRender.JsRender? getById(string id)
632 foreach(var f in this.files.values) {
640 // name should include extension.
642 public JsRender.JsRender? newFile (string xtype, string sub_dir, string name)
645 var fp = this.path + (sub_dir.length > 0 ? "/" : "") + sub_dir;
646 if (this.files.has_key(fp + "/" + name)) {
651 var ret = JsRender.JsRender.factory(xtype,
655 this.files.set(fp + "/" + name , ret);
657 } catch (JsRender.Error e) {
658 GLib.error("failed to create file %s", e.message);
663 public JsRender.JsRender loadFileOnly (string path)
667 return JsRender.JsRender.factory(xt, this, path);
668 } catch (JsRender.Error e) {
669 GLib.error("failed to create file %s", e.message);
675 public JsRender.JsRender create(string filename)
677 var ret = this.loadFileOnly(filename);
684 private void loadSubDirectories(string subdir, int dp)
687 //print("Project.Base: Running scandir on " + dir +"\n");
688 if (dp > 5) { // no more than 5 deep?
691 if (subdir == "build") { // cmake!
695 if (subdir == "autom4te.cache") { // automake?
698 if (subdir == "debian") { // debian!?
703 var dir = this.path + (subdir.length > 0 ? "/" : "") + subdir;
706 GLib.debug("Project %s Scan Dir: %s", this.name, dir);
707 var jsDir = new JsRender.Dir(this, dir);
708 this.sub_paths.add(jsDir); // might be ''...
711 // this should be done async -- but since we are getting the proto up ...
712 var other_files = new Gee.ArrayList<string>();
713 var bjs_files = new Gee.ArrayList<string>();
714 var vala_files = new Gee.ArrayList<string>();
715 var subs = new Gee.ArrayList<string>();
718 var f = File.new_for_path(dir);
720 var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
724 while ((next_file = file_enum.next_file(null)) != null) {
725 var fn = next_file.get_display_name();
728 //print("trying" + dir + "/" + fn +"\n");
730 if (fn[0] == '.') { // skip hidden
734 if (FileUtils.test(dir + "/" + fn, GLib.FileTest.IS_DIR)) {
735 subs.add(dir + "/" + fn);
738 if (Regex.match_simple("\\.(o|cache|gif|jpg|png|gif|out|stamp|~)$", fn)) { // object..
741 if (Regex.match_simple("^(config1.builder|a.out|stamp-h1|depcomp|config.log|config.status)$", fn)) { // object..
746 if (Regex.match_simple("\\.vala$", fn)) {
749 //print("no a bjs\n");
752 if (!Regex.match_simple("\\.bjs$", fn)) {
754 //print("no a bjs\n");
757 bjs_files.add(fn.substring(0, fn.length-4));
760 var el = JsRender.JsRender.factory(xt,this, dir + "/" + fn);
761 this.files.set( dir + "/" + fn, el);
762 jsDir.childfiles.append(el);
769 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
770 } catch (GLib.Error e) {
771 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
774 foreach(var fn in other_files) {
775 var dpos = fn.last_index_of(".");
776 var without_ext = fn.substring(0, dpos);
777 if (bjs_files.contains(without_ext)) { // will remove vala and c.
780 // c with a vala - skip
781 if (Regex.match_simple("\\.c$", fn) && vala_files.contains(without_ext + ".vala")) {
784 // Makefile (only allow am files at present.
785 if (without_ext == "Makefile") {
786 if (!Regex.match_simple("\\.am$", fn)) {
790 if (without_ext == "configure") {
791 if (!Regex.match_simple("\\.ac$", fn)) {
800 //GLib.debug("Could have added %s/%s", dir, fn);
802 var el = JsRender.JsRender.factory("PlainFile",this, dir + "/" + fn);
803 this.files.set( dir + "/" + fn, el);
804 jsDir.childfiles.append(el);
805 } catch (JsRender.Error e) {
806 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
810 foreach (var sd in subs) {
811 this.loadSubDirectories(sd.substring(this.path.length+1), dp+1);
817 // calle dfrom new file dialog
818 // add files to dires
822 public void addFile(JsRender.JsRender pfile)
823 { // add a single file, and trigger changed.
825 if (pfile.xtype == "Gtk" || pfile.xtype == "Roo" ) {
826 this.files.set(pfile.path, pfile); // duplicate check
828 if (pfile.xtype == "Gtk" && pfile.build_module != "") {
830 var gfile = (JsRender.Gtk) pfile;
831 gfile.updateCompileGroup("", pfile.build_module);
835 var sp = this.findDir(pfile.dir);
836 sp.childfiles.append(pfile);
844 public void deleteFile(JsRender.JsRender file)
846 if (file.xtype =="Dir") {
849 var sp = this.findDir(file.dir);
850 for(var i =0;i < sp.childfiles.n_items; i++) {
851 var jf = (JsRender.JsRender) sp.childfiles.get_item(i);
852 if (jf.path == file.path) {
853 sp.childfiles.remove(i);
857 if (this.files.has_key(file.path)) {
858 this.files.unset(file.path);
868 // but do not add it to our list.!!!
869 public void makeProjectSubdir(string name)
871 var dir = File.new_for_path(this.path + "/" + name);
872 if (FileUtils.test(this.path + "/" + name, FileTest.EXISTS)) {
877 dir.make_directory();
878 } catch (GLib.Error e) {
879 GLib.error("Failed to make directory %s", this.path + "/" + name);
883 public void createDir(string subdir) // add a single dir, and trigger changed.
885 if (subdir.strip() == "" || this.subpathsContains(subdir)) {
888 var dir= File.new_for_path(this.path + "/" + subdir);
890 if (!dir.query_exists()) {
894 dir.make_directory();
895 } catch (GLib.Error e) {
896 GLib.error("Failed to make directory %s", this.path + "/" + name);
900 this.sub_paths.add(new JsRender.Dir(this,this.path + "/" + subdir));
901 this.on_changed(); // not sure if it's needed - adding a dir doesnt really change much.
904 // this store is used in the icon view ?? do we need to store and update it?
905 public void loadFilesIntoStore(GLib.ListStore ls)
908 //GLib.debug("Load files (into grid) %s", this.name);
909 foreach(var f in this.files.values) {
910 // GLib.debug("Add file %s", f.name);
911 if (f.xtype == "PlainFile") {
918 public void loadDirsIntoStore(GLib.ListStore ls)
921 foreach(var f in this.sub_paths) {
922 //GLib.debug("Add %s", f.name);
928 public bool subpathsContains(string subpath)
930 foreach(var sp in this.sub_paths) {
932 if (sp.path == this.path + "/" + subpath) {
940 public void loadDirsToStringList( global::Gtk.StringList sl, string prefix)
943 while (sl.get_n_items() > 0) {
947 foreach(var sp in this.sub_paths) {
948 var add = sp.path == this.path ? "/" : sp.path.substring(this.path.length);
949 if (prefix.length > 0 && !add.has_prefix(prefix)) {
957 public JsRender.Dir? findDir(string path) {
959 foreach(var jdir in this.sub_paths) {
960 if (path == jdir.path) {
961 return (JsRender.Dir)jdir;
967 public string[] pathsMatching(string name, bool full_path)
971 foreach(var jdir in this.sub_paths) {
975 if (Path.get_basename (jdir.path) == name) {
976 GLib.debug("pathsMatching %s\n", jdir.path);
977 ret += full_path ? jdir.path : jdir.relpath;
984 public Gee.ArrayList<string> readArray(Json.Array ar)
986 var ret = new Gee.ArrayList<string>();
987 for(var i =0; i< ar.get_length(); i++) {
988 var add = ar.get_string_element(i);
989 if (ret.contains(add)) {
999 public abstract void initDatabase();
1000 public abstract void initialize(); // for new projects (make dirs?);