1 module sbylib.engine.tools.dub;
2 
3 import std;
4 import sbylib.engine.project.metainfo : MetaInfo;
5 
6 class Dub {
7 static:
8 
9     string[] getImportPath() 
10         out (r; r.all!(p => p.exists))
11     {
12 
13         auto result = describe("--import-paths");
14         result ~= absolutePath(MetaInfo().projectDirectory);
15 
16         return result;
17     }
18 
19     string[] getVersions() {
20         return getData("versions");
21     }
22 
23     auto describe() {
24         return DescribeResult(parseJSON(describe("").join("\n")));
25     }
26 
27     private string[] getData(string name) {
28         return describe("--data=" ~ name);
29     }
30 
31     private string[] describe(string option) {
32         return execute("describe " ~ option);
33     }
34 
35     private string[] execute(string cmd) {
36         const result = executeShell("dub " ~ cmd);
37         if (result.status != 0) {
38             throw new Exception(result.output);
39         }
40         return result.output
41             .split("\n")
42             .filter!(s => s.length > 0)
43             .array;
44     }
45 }
46 
47 struct DescribeResult {
48     private JSONValue content;
49 
50     auto root() const {
51         return content.object;
52     }
53 
54     auto packages() const {
55         return root["packages"].array
56             .map!(p => Package(p));
57     }
58 
59     auto rootPackageName() const {
60         return root["rootPackage"].str;
61     }
62 
63     auto rootPackage() const {
64         return findPackage(rootPackageName);
65     }
66 
67     auto findPackage(string name) const {
68         return packages
69             .filter!(p => p.name == name)
70             .front;
71     }
72 }
73 
74 struct Package {
75 
76     private JSONValue content;
77 
78     auto root() const { 
79         return content.object; 
80     }
81 
82     string name() const { 
83         return root["name"].str; 
84     }
85 
86     string path() const {
87         return root["path"].str;
88     }
89 
90     string targetFileName() const {
91         return root["targetFileName"].str; 
92     }
93 
94     string targetPath() const {
95         return root["targetPath"].str; 
96     }
97 
98     auto dependencies() const { 
99         return root["dependencies"].array
100             .map!(s => s.str);
101     }
102 
103 }