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; // depricated?
87 public Gee.HashMap<string,GLib.ListStore>? errorsByType = null;
91 protected Gee.HashMap<string,Palete.LanguageClient> language_servers;
93 protected Project (string path) {
96 //this.json_project_data = new Json.Object();
98 this.is_scanned = false;
99 this.sub_paths = new Gee.ArrayList<JsRender.JsRender>();
100 this.files = new Gee.HashMap<string,JsRender.JsRender>();
101 //XObject.extend(this, cfg);
104 this.language_servers = new Gee.HashMap<string,Palete.LanguageClient>();
105 this.language_servers.set("dummy", new Palete.LanguageClientDummy(this));
106 this.errorsByType = new Gee.HashMap<string,GLib.ListStore>();
113 public static void loadAll(bool force = false)
115 if (projects_loaded && !force) {
119 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
120 var dir = File.new_for_path(dirname);
121 if (!dir.query_exists()) {
123 dir.make_directory();
124 } catch(GLib.Error e) {
125 GLib.error("could not make builder directory %s", e.message);
129 projects = new Gee.ArrayList<Project>();
132 if (FileUtils.test(dirname + "/Projects.list", GLib.FileTest.IS_REGULAR)) {
134 projects_loaded = true;
137 convertOldProjects(); // this saves..
138 foreach(var p in projects) {
141 projects_loaded = true;
145 public static void remove(Project p) {
151 public static void saveProjectList()
153 var f = new Json.Object();
154 foreach(var p in projects) {
155 f.set_string_member(p.path, p.xtype);
158 var generator = new Json.Generator ();
159 var root = new Json.Node(Json.NodeType.OBJECT);
161 generator.set_root (root);
162 generator.pretty = true;
163 generator.indent = 4;
165 var data = generator.to_data (null);
166 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
167 GLib.debug("Write new Project list\n %s", data);
171 //FileUtils.set_contents(dirname + "/" + this.fn + ".json", s, s.length);
172 FileUtils.set_contents(dirname + "/Projects.list", data, data.length);
173 } catch (GLib.Error e) {
174 GLib.error("failed to save file %s", e.message);
181 public static void convertOldProjects()
184 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
185 var dir = File.new_for_path(dirname);
187 var file_enum = dir.enumerate_children(
188 GLib.FileAttribute.STANDARD_DISPLAY_NAME,
189 GLib.FileQueryInfoFlags.NONE,
194 while ((next_file = file_enum.next_file(null)) != null) {
195 var fn = next_file.get_display_name();
196 if (!Regex.match_simple("\\.json$", fn)) {
199 Project.factoryFromFileOld(dirname + "/" + fn);
201 } catch(GLib.Error e) {
202 GLib.warning("oops - something went wrong scanning the projects\n");
204 GLib.debug("Loaded all old Projects - saving");
205 Project.saveProjectList();
209 public static Gee.ArrayList<Project> allProjectsByName()
216 public static Project? getProjectByName(string name)
219 foreach (var p in projects) {
220 if (p.name == name) {
228 public static Project? getProjectByPath(string path)
231 foreach (var p in projects) {
232 if (p.path == path) {
240 public static string listAllToString()
246 all.sort((fa,fb) => {
247 return ((Project)fa).name.collate(((Project)fb).name);
251 var iter = all.list_iterator();
252 var ret = "ID\tName\tDirectory\n";
253 while (iter.next()) {
254 ret += "%s\t%s\n".printf(
268 public static void loadIntoStore(GLib.ListStore st)
271 foreach (var p in projects) {
280 static void loadProjectList()
283 var dirname = GLib.Environment.get_home_dir() + "/.Builder";
285 projects = new Gee.ArrayList<Project>();
287 var pa = new Json.Parser();
289 pa.load_from_file(dirname + "/Projects.list");
290 } catch (GLib.Error e) {
291 GLib.error("could not load json file %s", e.message);
293 var node = pa.get_root();
294 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
295 GLib.error( dirname + "/Projects.list - invalid format?");
300 var obj = node.get_object ();
301 obj.foreach_member((sobj, key, val) => {
302 GLib.debug("read ProjectList %s: %s", key, val.get_string());
303 // facotry adds project!
305 Project.factory(val.get_string(), key );
306 } catch (GLib.Error e ) {
307 GLib.debug("error createing project %s", e.message);
315 // load project data from project file.
316 public static void factoryFromFileOld(string jsonfile)
319 GLib.debug("parse %s", jsonfile);
321 var pa = new Json.Parser();
323 pa.load_from_file(jsonfile);
324 } catch (GLib.Error e) {
325 GLib.error("could not load json file %s", e.message);
327 var node = pa.get_root();
330 if (node == null || node.get_node_type () != Json.NodeType.OBJECT) {
331 GLib.debug("SKIP " + jsonfile + " - invalid format?");
335 var obj = node.get_object ();
336 var xtype = obj.get_string_member("xtype");
339 var paths = obj.get_object_member("paths");
342 paths.foreach_member((sobj, key, val) => {
349 if (fpath.length < 0 || !FileUtils.test(fpath,FileTest.IS_DIR)) {
355 proj = factory(xtype, fpath);
357 GLib.debug("Skip file - invalid file type");
361 //proj.json_project_data = obj; // store the original object...
363 //proj.fn = Path.get_basename(jsonfile).split(".")[0];
368 proj.name = obj.get_string_member("name");
370 // used to load paths..
371 //proj.initSubDirectories();
374 //proj.initDatabase();
376 GLib.debug("Add Project %s", proj.name);
384 public static Project factory(string xtype, string path) throws Error
387 // check to see if it's already loaded..
389 foreach(var p in projects) {
390 if (p.path == path) {
398 var ret = new Gtk(path);
403 var ret = new Roo(path);
408 // return new Flutter(path);
410 throw new Error.INVALID_TYPE("invalid project type");
420 var s = this.toJSON();
421 GLib.debug("Save Project %s\n%s", this.name, s);
423 //FileUtils.set_contents(dirname + "/" + this.fn + ".json", s, s.length);
424 FileUtils.set_contents(this.path + "/.roobuilder.jcfg", s, s.length);
425 } catch (GLib.Error e) {
426 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");
529 public string firstPath()
531 var iter = this.paths.map_iterator();
532 while (iter.next()) {
533 return iter.get_key();
539 public bool hasPath(string path)
541 var iter = this.paths.map_iterator();
542 while (iter.next()) {
543 if (iter.get_key() == path) {
552 // returns the first path
553 public string getName()
555 var iter = this.paths.map_iterator();
556 while (iter.next()) {
557 return GLib.Path.get_basename(iter.get_key());
564 public Gee.ArrayList<JsRender.JsRender> sortedFiles()
566 var files = new Gee.ArrayList<JsRender.JsRender>();
568 var fiter = this.files.map_iterator();
569 while(fiter.next()) {
570 files.add(fiter.get_value());
572 files.sort((fa,fb) => {
573 return ((JsRender.JsRender)fa).name.collate(((JsRender.JsRender)fb).name);
582 public string listAllFilesToString()
585 var iter = this.sortedFiles().list_iterator();
586 var ret = "ID\tName\tDirectory\n";
587 while (iter.next()) {
588 ret += "%s\n".printf(
604 public JsRender.JsRender? getByRelPath(string relpath)
606 foreach(var f in this.files.values) {
607 if (f.relpath == relpath || f.relTargetName() == relpath) {
613 // this get's a file using the full path ( replaces vala->bjs if they exist);
615 public JsRender.JsRender? getByPath(string path)
618 // keys are not paths...
619 foreach(var f in this.files.values) {
620 //GLib.debug("check %s = %s ? %s", path, f.path, f.targetName());
621 if (f.path == path || f.targetName() == path) {
628 public JsRender.JsRender? getById(string id)
630 foreach(var f in this.files.values) {
638 // name should include extension.
640 public JsRender.JsRender? newFile (string xtype, string sub_dir, string name)
643 var fp = this.path + (sub_dir.length > 0 ? "/" : "") + sub_dir;
644 if (this.files.has_key(fp + "/" + name)) {
649 var ret = JsRender.JsRender.factory(xtype,
653 this.files.set(fp + "/" + name , ret);
655 } catch (JsRender.Error e) {
656 GLib.error("failed to create file %s", e.message);
661 public JsRender.JsRender loadFileOnly (string path)
665 return JsRender.JsRender.factory(xt, this, path);
666 } catch (JsRender.Error e) {
667 GLib.error("failed to create file %s", e.message);
673 public JsRender.JsRender create(string filename)
675 var ret = this.loadFileOnly(filename);
682 private void loadSubDirectories(string subdir, int dp)
685 //print("Project.Base: Running scandir on " + dir +"\n");
686 if (dp > 5) { // no more than 5 deep?
689 if (subdir == "build") { // cmake!
693 if (subdir == "autom4te.cache") { // automake?
696 if (subdir == "debian") { // debian!?
701 var dir = this.path + (subdir.length > 0 ? "/" : "") + subdir;
704 GLib.debug("Project %s Scan Dir: %s", this.name, dir);
705 var jsDir = new JsRender.Dir(this, dir);
706 this.sub_paths.add(jsDir); // might be ''...
709 // this should be done async -- but since we are getting the proto up ...
710 var other_files = new Gee.ArrayList<string>();
711 var bjs_files = new Gee.ArrayList<string>();
712 var vala_files = new Gee.ArrayList<string>();
713 var subs = new Gee.ArrayList<string>();
716 var f = File.new_for_path(dir);
718 var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
722 while ((next_file = file_enum.next_file(null)) != null) {
723 var fn = next_file.get_display_name();
726 //print("trying" + dir + "/" + fn +"\n");
728 if (fn[0] == '.') { // skip hidden
732 if (FileUtils.test(dir + "/" + fn, GLib.FileTest.IS_DIR)) {
733 subs.add(dir + "/" + fn);
736 if (Regex.match_simple("\\.(o|cache|gif|jpg|png|gif|out|stamp|~)$", fn)) { // object..
739 if (Regex.match_simple("^(config1.builder|a.out|stamp-h1|depcomp|config.log|config.status)$", fn)) { // object..
744 if (Regex.match_simple("\\.vala$", fn)) {
747 //print("no a bjs\n");
750 if (!Regex.match_simple("\\.bjs$", fn)) {
752 //print("no a bjs\n");
755 bjs_files.add(fn.substring(0, fn.length-4));
758 var el = JsRender.JsRender.factory(xt,this, dir + "/" + fn);
759 this.files.set( dir + "/" + fn, el);
760 jsDir.childfiles.append(el);
767 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
768 } catch (GLib.Error e) {
769 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
772 foreach(var fn in other_files) {
773 var dpos = fn.last_index_of(".");
774 var without_ext = fn.substring(0, dpos);
775 if (bjs_files.contains(without_ext)) { // will remove vala and c.
778 // c with a vala - skip
779 if (Regex.match_simple("\\.c$", fn) && vala_files.contains(without_ext + ".vala")) {
782 // Makefile (only allow am files at present.
783 if (without_ext == "Makefile") {
784 if (!Regex.match_simple("\\.am$", fn)) {
788 if (without_ext == "configure") {
789 if (!Regex.match_simple("\\.ac$", fn)) {
798 //GLib.debug("Could have added %s/%s", dir, fn);
800 var el = JsRender.JsRender.factory("PlainFile",this, dir + "/" + fn);
801 this.files.set( dir + "/" + fn, el);
802 jsDir.childfiles.append(el);
803 } catch (JsRender.Error e) {
804 GLib.warning("Project::scanDirs failed : " + e.message + "\n");
808 foreach (var sd in subs) {
809 this.loadSubDirectories(sd.substring(this.path.length+1), dp+1);
815 // calle dfrom new file dialog
816 // add files to dires (and main file list)
820 public void addFile(JsRender.JsRender pfile)
821 { // add a single file, and trigger changed.
823 if (pfile.xtype == "Gtk" || pfile.xtype == "Roo" ) {
824 this.files.set(pfile.path, pfile); // duplicate check
826 if (pfile.xtype == "Gtk" && pfile.build_module != "") {
828 var gfile = (JsRender.Gtk) pfile;
829 gfile.updateCompileGroup("", pfile.build_module);
833 var sp = this.findDir(pfile.dir);
834 sp.childfiles.append(pfile);
835 this.files.set(pfile.path, pfile);
842 public void deleteFile(JsRender.JsRender file)
844 if (file.xtype =="Dir") {
847 var sp = this.findDir(file.dir);
848 for(var i =0;i < sp.childfiles.n_items; i++) {
849 var jf = (JsRender.JsRender) sp.childfiles.get_item(i);
850 if (jf.path == file.path) {
851 sp.childfiles.remove(i);
855 if (this.files.has_key(file.path)) {
856 this.files.unset(file.path);
866 // but do not add it to our list.!!!
867 public void makeProjectSubdir(string name)
869 var dir = File.new_for_path(this.path + "/" + name);
870 if (FileUtils.test(this.path + "/" + name, FileTest.EXISTS)) {
875 dir.make_directory();
876 } catch (GLib.Error e) {
877 GLib.error("Failed to make directory %s", this.path + "/" + name);
881 public void createDir(string subdir) // add a single dir, and trigger changed.
883 if (subdir.strip() == "" || this.subpathsContains(subdir)) {
886 var dir= File.new_for_path(this.path + "/" + subdir);
888 if (!dir.query_exists()) {
892 dir.make_directory();
893 } catch (GLib.Error e) {
894 GLib.error("Failed to make directory %s", this.path + "/" + name);
898 this.sub_paths.add(new JsRender.Dir(this,this.path + "/" + subdir));
899 this.on_changed(); // not sure if it's needed - adding a dir doesnt really change much.
902 // this store is used in the icon view ?? do we need to store and update it?
903 public void loadFilesIntoStore(GLib.ListStore ls)
906 //GLib.debug("Load files (into grid) %s", this.name);
907 foreach(var f in this.files.values) {
908 // GLib.debug("Add file %s", f.name);
909 if (f.xtype == "PlainFile") {
916 public void loadDirsIntoStore(GLib.ListStore ls)
919 foreach(var f in this.sub_paths) {
920 //GLib.debug("Add %s", f.name);
926 public bool subpathsContains(string subpath)
928 foreach(var sp in this.sub_paths) {
930 if (sp.path == this.path + "/" + subpath) {
938 public void loadDirsToStringList( global::Gtk.StringList sl, string prefix)
941 while (sl.get_n_items() > 0) {
945 foreach(var sp in this.sub_paths) {
946 var add = sp.path == this.path ? "/" : sp.path.substring(this.path.length);
947 if (prefix.length > 0 && !add.has_prefix(prefix)) {
955 public JsRender.Dir? findDir(string path) {
957 foreach(var jdir in this.sub_paths) {
958 if (path == jdir.path) {
959 return (JsRender.Dir)jdir;
965 public string[] pathsMatching(string name, bool full_path)
969 foreach(var jdir in this.sub_paths) {
973 if (Path.get_basename (jdir.path) == name) {
974 GLib.debug("pathsMatching %s\n", jdir.path);
975 ret += full_path ? jdir.path : jdir.relpath;
982 public Gee.ArrayList<string> readArray(Json.Array ar)
984 var ret = new Gee.ArrayList<string>();
985 for(var i =0; i< ar.get_length(); i++) {
986 var add = ar.get_string_element(i);
987 if (ret.contains(add)) {
996 public void updateErrorsforFile(JsRender.JsRender f)
998 var n = this.updateErrorsByType(f, "WARN");
999 n += this.updateErrorsByType(f, "ERR");
1000 n += this.updateErrorsByType(f, "DEPR");
1003 BuilderApplication.updateCompileResults();
1007 public int updateErrorsByType(JsRender.JsRender f, string n)
1009 var ls = this.getErrors(n);
1011 // remove thie file from the list.
1012 for(var i =0; i < ls.get_n_items(); i++) {
1013 var ce = ls.get_item(i) as Palete.CompileError;
1014 if (ce.file.path == f.path) {
1019 var add = new Palete.CompileError.new_from_file(f, n);
1020 if (add.hasErrors()) {
1026 public GLib.ListStore getErrors(string n)
1028 var ls = this.errorsByType.get(n);
1030 ls = new GLib.ListStore(typeof(Palete.CompileError));
1031 this.errorsByType.set(n, ls );
1037 public abstract Palete.LanguageClient getLanguageServer(string lang);
1040 public abstract void onSave(); // write meson?
1041 public abstract void initDatabase();
1042 public abstract void initialize(); // for new projects (make dirs?);
1043 public abstract void loadJson(Json.Object obj);
1044 public abstract void saveJson(Json.Object obj);