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");
414 var s = this.toJSON();
415 GLib.debug("Save Project %s\n%s", this.name, s);
417 //FileUtils.set_contents(dirname + "/" + this.fn + ".json", s, s.length);
418 FileUtils.set_contents(this.path + "/.roobuilder.jcfg", s, s.length);
419 } catch (GLib.Error e) {
420 GLib.error("failed to save file %s", e.message);
429 public string toJSON( )
432 var obj = new Json.Object();
433 obj.set_string_member("xtype", this.xtype);
438 var generator = new Json.Generator ();
439 var root = new Json.Node(Json.NodeType.OBJECT);
440 root.init_object(obj);
441 generator.set_root (root);
443 generator.pretty = true;
444 generator.indent = 4;
447 return generator.to_data (null);
452 // used to check what type a project might be..
454 public static string peekProjectType(string fn)
456 var pa = new Json.Parser();
458 pa.load_from_file(fn);
459 } catch (GLib.Error e) {
460 GLib.debug("could not load json file %s", e.message);
464 var node = pa.get_root();
466 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
467 GLib.debug("SKIP %s/.roobuilder.jcfg - invalid format?",fn);
471 var obj = node.get_object ();
473 var xtype = obj.get_string_member("xtype");
474 return xtype == null ? "" : xtype;
479 // this will do a full scan - should only be done on viewing project..
480 // not initial load.. - may take time.
484 if (this.is_scanned) {
487 GLib.debug("load is_scanned = false");
489 if (FileUtils.test(this.path + "/.roobuilder.jcfg", FileTest.EXISTS)) {
491 var pa = new Json.Parser();
493 pa.load_from_file(this.path + "/.roobuilder.jcfg");
494 } catch (GLib.Error e) {
495 GLib.error("could not load json file %s", e.message);
497 var node = pa.get_root();
500 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
501 GLib.debug("SKIP %s/.roobuilder.jcfg - invalid format?",this.path);
505 var obj = node.get_object ();
509 // used to load paths..
510 this.sub_paths = new Gee.ArrayList<JsRender.JsRender>();
511 this.files = new Gee.HashMap<string,JsRender.JsRender>();
512 this.loadSubDirectories("", 0);
515 this.is_scanned = true; // loaded.. dont need to do it again..
516 GLib.debug("load is_scanned = true");
523 public string firstPath()
525 var iter = this.paths.map_iterator();
526 while (iter.next()) {
527 return iter.get_key();
533 public bool hasPath(string path)
535 var iter = this.paths.map_iterator();
536 while (iter.next()) {
537 if (iter.get_key() == path) {
546 // returns the first path
547 public string getName()
549 var iter = this.paths.map_iterator();
550 while (iter.next()) {
551 return GLib.Path.get_basename(iter.get_key());
558 public Gee.ArrayList<JsRender.JsRender> sortedFiles()
560 var files = new Gee.ArrayList<JsRender.JsRender>();
562 var fiter = this.files.map_iterator();
563 while(fiter.next()) {
564 files.add(fiter.get_value());
566 files.sort((fa,fb) => {
567 return ((JsRender.JsRender)fa).name.collate(((JsRender.JsRender)fb).name);
576 public string listAllFilesToString()
579 var iter = this.sortedFiles().list_iterator();
580 var ret = "ID\tName\tDirectory\n";
581 while (iter.next()) {
582 ret += "%s\n".printf(
598 public JsRender.JsRender? getByName(string name)
600 foreach(var f in this.files.values) {
601 if (f.name == name) {
607 // this get's a file using the full path ( replaces vala->bjs if they exist);
609 public JsRender.JsRender? getByPath(string path)
612 // keys are not paths...
613 foreach(var f in this.files.values) {
614 GLib.debug("check %s = %s ? %s", path, f.path, f.targetName());
615 if (f.path == path || f.targetName() == path) {
622 public JsRender.JsRender? getById(string id)
624 foreach(var f in this.files.values) {
632 // name should include extension.
634 public JsRender.JsRender? newFile (string xtype, string sub_dir, string name)
637 var fp = this.path + (sub_dir.length > 0 ? "/" : "") + sub_dir;
638 if (this.files.has_key(fp + "/" + name)) {
643 var ret = JsRender.JsRender.factory(xtype,
647 this.files.set(fp + "/" + name , ret);
649 } catch (JsRender.Error e) {
650 GLib.error("failed to create file %s", e.message);
655 public JsRender.JsRender loadFileOnly (string path)
659 return JsRender.JsRender.factory(xt, this, path);
660 } catch (JsRender.Error e) {
661 GLib.error("failed to create file %s", e.message);
667 public JsRender.JsRender create(string filename)
669 var ret = this.loadFileOnly(filename);
676 private void loadSubDirectories(string subdir, int dp)
679 //print("Project.Base: Running scandir on " + dir +"\n");
680 if (dp > 5) { // no more than 5 deep?
683 if (subdir == "build") { // cmake!
687 if (subdir == "autom4te.cache") { // automake?
690 if (subdir == "debian") { // debian!?
695 var dir = this.path + (subdir.length > 0 ? "/" : "") + subdir;
698 GLib.debug("Project %s Scan Dir: %s", this.name, dir);
699 var jsDir = new JsRender.Dir(this, dir);
700 this.sub_paths.add(jsDir); // might be ''...
703 // this should be done async -- but since we are getting the proto up ...
704 var other_files = new Gee.ArrayList<string>();
705 var bjs_files = new Gee.ArrayList<string>();
706 var vala_files = new Gee.ArrayList<string>();
707 var subs = new Gee.ArrayList<string>();
710 var f = File.new_for_path(dir);
712 var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
716 while ((next_file = file_enum.next_file(null)) != null) {
717 var fn = next_file.get_display_name();
720 //print("trying" + dir + "/" + fn +"\n");
722 if (fn[0] == '.') { // skip hidden
726 if (FileUtils.test(dir + "/" + fn, GLib.FileTest.IS_DIR)) {
727 subs.add(dir + "/" + fn);
730 if (Regex.match_simple("\\.(o|cache|gif|jpg|png|gif|out|stamp|~)$", fn)) { // object..
733 if (Regex.match_simple("^(config1.builder|a.out|stamp-h1|depcomp|config.log|config.status)$", fn)) { // object..
738 if (Regex.match_simple("\\.vala$", fn)) {
741 //print("no a bjs\n");
744 if (!Regex.match_simple("\\.bjs$", fn)) {
746 //print("no a bjs\n");
749 bjs_files.add(fn.substring(0, fn.length-4));
752 var el = JsRender.JsRender.factory(xt,this, dir + "/" + fn);
753 this.files.set( dir + "/" + fn, el);
754 jsDir.childfiles.append(el);
761 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
762 } catch (GLib.Error e) {
763 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
766 foreach(var fn in other_files) {
767 var dpos = fn.last_index_of(".");
768 var without_ext = fn.substring(0, dpos);
769 if (bjs_files.contains(without_ext)) { // will remove vala and c.
772 // c with a vala - skip
773 if (Regex.match_simple("\\.c$", fn) && vala_files.contains(without_ext + ".vala")) {
776 // Makefile (only allow am files at present.
777 if (without_ext == "Makefile") {
778 if (!Regex.match_simple("\\.am$", fn)) {
782 if (without_ext == "configure") {
783 if (!Regex.match_simple("\\.ac$", fn)) {
792 //GLib.debug("Could have added %s/%s", dir, fn);
794 var el = JsRender.JsRender.factory("PlainFile",this, dir + "/" + fn);
795 this.files.set( dir + "/" + fn, el);
796 jsDir.childfiles.append(el);
797 } catch (JsRender.Error e) {
798 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
802 foreach (var sd in subs) {
803 this.loadSubDirectories(sd.substring(this.path.length+1), dp+1);
809 // calle dfrom new file dialog
810 // add files to dires (and main file list)
814 public void addFile(JsRender.JsRender pfile)
815 { // add a single file, and trigger changed.
817 if (pfile.xtype == "Gtk" || pfile.xtype == "Roo" ) {
818 this.files.set(pfile.path, pfile); // duplicate check
820 if (pfile.xtype == "Gtk" && pfile.build_module != "") {
822 var gfile = (JsRender.Gtk) pfile;
823 gfile.updateCompileGroup("", pfile.build_module);
827 var sp = this.findDir(pfile.dir);
828 sp.childfiles.append(pfile);
829 this.files.set(pfile.path, pfile);
836 public void deleteFile(JsRender.JsRender file)
838 if (file.xtype =="Dir") {
841 var sp = this.findDir(file.dir);
842 for(var i =0;i < sp.childfiles.n_items; i++) {
843 var jf = (JsRender.JsRender) sp.childfiles.get_item(i);
844 if (jf.path == file.path) {
845 sp.childfiles.remove(i);
849 if (this.files.has_key(file.path)) {
850 this.files.unset(file.path);
860 // but do not add it to our list.!!!
861 public void makeProjectSubdir(string name)
863 var dir = File.new_for_path(this.path + "/" + name);
864 if (FileUtils.test(this.path + "/" + name, FileTest.EXISTS)) {
869 dir.make_directory();
870 } catch (GLib.Error e) {
871 GLib.error("Failed to make directory %s", this.path + "/" + name);
875 public void createDir(string subdir) // add a single dir, and trigger changed.
877 if (subdir.strip() == "" || this.subpathsContains(subdir)) {
880 var dir= File.new_for_path(this.path + "/" + subdir);
882 if (!dir.query_exists()) {
886 dir.make_directory();
887 } catch (GLib.Error e) {
888 GLib.error("Failed to make directory %s", this.path + "/" + name);
892 this.sub_paths.add(new JsRender.Dir(this,this.path + "/" + subdir));
893 this.on_changed(); // not sure if it's needed - adding a dir doesnt really change much.
896 // this store is used in the icon view ?? do we need to store and update it?
897 public void loadFilesIntoStore(GLib.ListStore ls)
900 //GLib.debug("Load files (into grid) %s", this.name);
901 foreach(var f in this.files.values) {
902 // GLib.debug("Add file %s", f.name);
903 if (f.xtype == "PlainFile") {
910 public void loadDirsIntoStore(GLib.ListStore ls)
913 foreach(var f in this.sub_paths) {
914 //GLib.debug("Add %s", f.name);
920 public bool subpathsContains(string subpath)
922 foreach(var sp in this.sub_paths) {
924 if (sp.path == this.path + "/" + subpath) {
932 public void loadDirsToStringList( global::Gtk.StringList sl, string prefix)
935 while (sl.get_n_items() > 0) {
939 foreach(var sp in this.sub_paths) {
940 var add = sp.path == this.path ? "/" : sp.path.substring(this.path.length);
941 if (prefix.length > 0 && !add.has_prefix(prefix)) {
949 public JsRender.Dir? findDir(string path) {
951 foreach(var jdir in this.sub_paths) {
952 if (path == jdir.path) {
953 return (JsRender.Dir)jdir;
959 public string[] pathsMatching(string name, bool full_path)
963 foreach(var jdir in this.sub_paths) {
967 if (Path.get_basename (jdir.path) == name) {
968 GLib.debug("pathsMatching %s\n", jdir.path);
969 ret += full_path ? jdir.path : jdir.relpath;
976 public Gee.ArrayList<string> readArray(Json.Array ar)
978 var ret = new Gee.ArrayList<string>();
979 for(var i =0; i< ar.get_length(); i++) {
980 var add = ar.get_string_element(i);
981 if (ret.contains(add)) {
990 public abstract void onSave(); // write meson?
991 public abstract void initDatabase();
992 public abstract void initialize(); // for new projects (make dirs?);
993 public abstract void loadJson(Json.Object obj);
994 public abstract void saveJson(Json.Object obj);