1 /*
2 Copyright (c) 2019 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module main;
30 
31 import std.stdio;
32 import std.array : split;
33 import std.file : readText, exists, mkdir, getcwd, write;
34 import std.process : execute, Config;
35 import std.json;
36 import std.path : absolutePath;
37 import std.experimental.logger;
38 
39 struct Git
40 {
41     string workDir;
42 
43     this(string dir)
44     {
45         workDir = dir;
46     }
47 
48     auto cmd(string[] commands)
49     {
50         tracef("%s %(%s %)", workDir, commands);
51         return execute(["git"] ~ commands, null, Config.none, size_t.max, workDir);
52     }
53 
54     auto cmd(string command)
55     {
56         return cmd([command]);
57     }
58 
59     auto clone(string url)
60     {
61         return cmd(["clone"] ~ url);
62     }
63 
64     auto checkout(string branchName)
65     {
66         return cmd(["checkout"] ~ branchName);
67     }
68 
69     auto pull()
70     {
71         return cmd("pull");
72     }
73 
74     auto pull(string url, string b)
75     {
76         return cmd(["pull"] ~ url ~ b);
77     }
78 }
79 
80 void run()
81 {
82     string depJ = readText("dependencies.json");
83     JSONValue dubConfig = parseJSON(depJ);
84     JSONValue deps = dubConfig["git"];
85 
86     JSONValue[string] versions;
87 
88     foreach (string depName, ref JSONValue _dep; deps)
89     {
90         string[] s = depName.split(":");
91         string packageName = s[0];
92         string subpackageName = "";
93         if (s.length > 1)
94         {
95             subpackageName = s[1];
96         }
97         tracef("pack:%s sub:%s", packageName, subpackageName);
98 
99         JSONValue dep = deps[depName];
100         string repoUrl = dep.array[0].str;
101         string branchName = dep.array[1].str;
102         string dir = ".resolve/" ~ packageName;
103 
104         if (!exists(".resolve"))
105             mkdir(".resolve");
106 
107         Git repo;
108         string dirAbs = absolutePath(dir);
109 
110         writeln("Resolving ", depName, "@", branchName, " to ", "\"", dir, "\"...");
111 
112         string gitConfig = dir ~ "/.git/config";
113 
114         if (exists(gitConfig))
115         {
116             repo = Git(dirAbs);
117             tracef("pull return: %s", repo.pull(repoUrl, branchName));
118         }
119         else
120         {
121             repo = Git(absolutePath(".resolve"));
122             repo.clone(repoUrl);
123             repo = Git(dirAbs);
124             repo.checkout(branchName);
125         }
126 
127         if (subpackageName != "")
128             versions[depName] = JSONValue(["path" : JSONValue(dir)]);
129         else
130             versions[packageName] = JSONValue(["path" : JSONValue(dir)]);
131     }
132 
133     JSONValue dubSelections;
134 
135     if (exists("dub.selections.json"))
136     {
137         dubSelections = parseJSON(readText("dub.selections.json"));
138         if ("versions" in dubSelections)
139         {
140             foreach (name, value; versions)
141                 dubSelections["versions"][name] = value;
142         }
143         else
144         {
145             dubSelections["versions"] = JSONValue(versions);
146         }
147     }
148     else
149     {
150         dubSelections = JSONValue(["fileVersion" : JSONValue(1), "versions" : JSONValue(versions)]);
151     }
152 
153     writeln("Updating dub.selections.json...");
154     write("dub.selections.json", dubSelections.toPrettyString(JSONOptions.doNotEscapeSlashes));
155 
156     writeln("Done. Run \"dub build\".");
157 }
158 
159 void main(string[] args)
160 {
161     import std.getopt;
162 
163     bool verbose;
164 
165     auto opt = getopt(args, "verbose|v", "Verbose", &verbose);
166     if (verbose)
167     {
168         globalLogLevel(LogLevel.trace);
169     }
170     else
171     {
172         globalLogLevel(LogLevel.error);
173     }
174     if (opt.helpWanted)
175     {
176         defaultGetoptPrinter("resolve", opt.options);
177         help;
178     }
179     else
180     {
181         run();
182     }
183 }
184 
185 void help()
186 {
187 }