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     override bool load(string fname = null) {
35         if (fname.length > 0)
36             filename = fname;
37         if (!exists(_filename) || !isFile(_filename))  {
38             return false;
39         }
40         Log.d("Reading workspace from file ", _filename);
41 
42         try {
43             string jsonSource = readText!string(_filename);
44             JSONValue json = parseJSON(jsonSource);
45             _name = toUTF32(json["name"].str);
46             _description = toUTF32(json["description"].str);
47             Log.d("workspace name: ", _name);
48             Log.d("workspace description: ", _description);
49             JSONValue projects = json["projects"];
50             foreach(string key, ref JSONValue value; projects) {
51                 string path = value.str;
52                 Log.d("project: ", key, " path:", path);
53                 if (!isAbsolute(path))
54                     path = buildNormalizedPath(_dir, path, "dub.json");
55                 Project project = new Project(path);
56                 _projects ~= project;
57                 project.load();
58 
59             }
60         } catch (JSONException e) {
61             Log.e("Cannot parse json", e);
62             return false;
63         } catch (Exception e) {
64             Log.e("Cannot read workspace file", e);
65             return false;
66         }
67         return true;
68     }
69     void close() {
70     }
71 }
72 
73 /// global workspace
74 __gshared Workspace currentWorkspace;