1 module dlangide.workspace.workspace;
2 
3 import dlangide.workspace.project;
4 import dlangui.core.logger;
5 import std.path;
6 import std.file;
7 import std.json;
8 import std.utf;
9 
10 /**
11     Exception thrown on Workspace errors
12 */
13 class WorkspaceException : Exception
14 {
15     this(string msg, string file = __FILE__, size_t line = __LINE__)
16     {
17         super(msg, file, line);
18     }
19 }
20 
21 
22 /// DlangIDE workspace
23 class Workspace : WorkspaceItem {
24     protected Project[] _projects;
25 
26     this(string fname = null) {
27         super(fname);
28     }
29 
30     @property Project[] projects() {
31         return _projects;
32     }
33 
34     /// tries to find source file in one of projects, returns found project source file item, or null if not found
35     ProjectSourceFile findSourceFileItem(string filename) {
36         foreach (Project p; _projects) {
37             ProjectSourceFile res = p.findSourceFileItem(filename);
38             if (res)
39                 return res;
40         }
41         return null;
42     }
43 
44     override bool load(string fname = null) {
45         if (fname.length > 0)
46             filename = fname;
47         if (!exists(_filename) || !isFile(_filename))  {
48             return false;
49         }
50         Log.d("Reading workspace from file ", _filename);
51 
52         try {
53             string jsonSource = readText!string(_filename);
54             JSONValue json = parseJSON(jsonSource);
55             _name = toUTF32(json["name"].str);
56             _description = toUTF32(json["description"].str);
57             Log.d("workspace name: ", _name);
58             Log.d("workspace description: ", _description);
59             JSONValue projects = json["projects"];
60             foreach(string key, ref JSONValue value; projects) {
61                 string path = value.str;
62                 Log.d("project: ", key, " path:", path);
63                 if (!isAbsolute(path))
64                     path = buildNormalizedPath(_dir, path, "dub.json");
65                 Project project = new Project(path);
66                 _projects ~= project;
67                 project.load();
68 
69             }
70         } catch (JSONException e) {
71             Log.e("Cannot parse json", e);
72             return false;
73         } catch (Exception e) {
74             Log.e("Cannot read workspace file", e);
75             return false;
76         }
77         return true;
78     }
79     void close() {
80     }
81 }
82 
83 /// global workspace
84 __gshared Workspace currentWorkspace;