-
Notifications
You must be signed in to change notification settings - Fork 42
/
toolchainManager.d
executable file
·323 lines (268 loc) · 8.32 KB
/
toolchainManager.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env rdmd
/**
* PowerNex's toolchain manager
*
* Copyright: © 2017, Dan Printzell
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, Mozilla Public License Version 2.0)
* (See accompanying file LICENSE)
* Authors: Dan Printzell
*/
pragma(lib, "curl");
import core.sys.posix.stdio : fgets;
import core.sys.posix.unistd : isatty;
import core.stdc.string : strlen;
import std.stdio : write, writeln, stdout, stderr, stdin, File;
import std.file : exists, readText, fwrite = write, rmdirRecurse, mkdirRecurse;
import std.ascii : toLower, toUpper;
import std.json : JSONValue, parseJSON, JSON_TYPE;
import std.net.curl : get, HTTP;
import std.range : repeat;
import std.math : isNaN;
import std.format : format;
import std.process : executeShell, pipeShell, wait, ProcessPipes, Redirect;
import std.conv : to;
import std.typecons : Yes, No, Flag;
import std.getopt : getopt, defaultGetoptPrinter;
//
enum size_t major = 1;
enum size_t minor = 0;
enum size_t patch = 0;
//Flags
bool showVersion;
bool clean;
bool noconfirm;
void normal(Args...)(Args args) {
write("\x1b[39;1m", args, "\x1b[0m");
}
void good(Args...)(Args args) {
write("\x1b[33;1m", args, "\x1b[0m");
}
void warning(Args...)(Args args) {
stderr.write("\x1b[31;1m", args, "\x1b[0m");
}
void error(Args...)(Args args) {
stderr.write("\x1b[37;41;1m", args, "\x1b[0m");
}
//Version = Successfull ci build
struct VersionInfo {
size_t dmdVersion;
size_t binutilsVersion;
}
const string toolchainFolder = "build/cc";
const string versionInfoFile = toolchainFolder ~ "/versionInfo";
VersionInfo getOldInfo() {
VersionInfo oldVI;
if (!exists(versionInfoFile))
return oldVI;
JSONValue data = parseJSON(readText(versionInfoFile));
if (auto _ = "dmdVersion" in data) {
if (_.type == JSON_TYPE.UINTEGER)
oldVI.dmdVersion = _.uinteger;
else
oldVI.dmdVersion = cast(ulong)_.integer;
}
if (auto _ = "binutilsVersion" in data) {
if (_.type == JSON_TYPE.UINTEGER)
oldVI.binutilsVersion = _.uinteger;
else
oldVI.binutilsVersion = cast(ulong)_.integer;
}
return oldVI;
}
VersionInfo getNewInfo() {
size_t getLatestVersion(string url) {
JSONValue data = parseJSON(get(url));
auto _ = data["lastCompletedBuild"]["number"];
if (_.type == JSON_TYPE.UINTEGER)
return _.uinteger;
else
return cast(ulong)_.integer;
}
VersionInfo newVI;
newVI.dmdVersion = getLatestVersion("https://ci.vild.io/job/PowerNex/job/powernex-dmd/job/master/api/json");
newVI.binutilsVersion = getLatestVersion("https://ci.vild.io/job/PowerNex/job/powernex-binutils/job/master/api/json");
return newVI;
}
char question(char defaultAlt, char[] alternative, Args...)(Args args) {
char[64] data;
warning(args, " [");
foreach (idx, alt; alternative) {
if (idx)
warning("/");
if (alt == defaultAlt)
alt = alt.toUpper;
warning(alt);
}
warning("]: ");
stdout.flush();
stderr.flush();
if (noconfirm) {
warning("\n");
return defaultAlt;
}
if (!fgets(data.ptr, data.length, stdin.getFP)) {
error("[fgets] Is stdin valid?\n");
return char.init;
}
char[] input = data[0 .. data.ptr.strlen];
char[2] altStr;
altStr[1] = '\0';
foreach (alt; alternative) {
altStr[0] = alt.toLower;
if (!strcasecmp(altStr, input))
return alt;
}
if (!strcasecmp("\n", input))
return defaultAlt;
error("Invalid choice!\n");
return char.init;
}
struct SaveFile {
File f;
@disable this();
this(string path) {
f = File(path, "wb");
}
~this() {
f.close();
}
size_t opCall(ubyte[] data) {
f.rawWrite(data);
return data.length;
}
}
struct ProcessPipe {
ProcessPipes p;
@disable this();
this(string command, Flag!"IgnoreStdOut" ignoreStdOut = No.IgnoreStdOut, Flag!"IgnoreStdErr" ignoreStdErr = No.IgnoreStdErr) {
Redirect flags = Redirect.stdin;
if (ignoreStdOut)
flags |= Redirect.stdout;
if (ignoreStdErr)
flags |= Redirect.stderr;
p = pipeShell(command, flags);
}
~this() {
p.stdin.flush();
p.stdin.close();
wait(p.pid);
}
size_t opCall(ubyte[] data) {
p.stdin.rawWrite(data);
return data.length;
}
}
void downloadProgress(T = SaveFile, Args...)(string name, const(char)[] url, Args args) {
T receiver = T(args);
HTTP http = HTTP(url); // Because opCall
http.onReceive = (ubyte[] data) => receiver(data);
normal("\x1b[?25l");
static float lastDiff = -1;
http.onProgress = (size_t total, size_t current, size_t _, size_t __) {
import std.string : leftJustifier;
enum width = 64;
float fDiff = cast(float)current / cast(float)total;
if (fDiff.isNaN)
fDiff = 0;
if (cast(size_t)(100 * lastDiff) == cast(size_t)(100 * fDiff))
return 0;
size_t procent = cast(size_t)(100 * fDiff);
size_t filled = cast(size_t)(width * fDiff * 8);
dchar[] step = [' ', '▏', '▎', '▍', '▋', '▊', '▉', '█'];
long fullFilled = cast(long)(filled) / 8;
if (fullFilled < 0)
fullFilled = 0;
long empty = width - fullFilled - 1;
if (empty < 0)
empty = 0;
normal("\r", name, ":", leftJustifier("", 8 - name.length + 1, ' '), format("%3d", procent), "% \x1b[36;46;1m",
repeat(step[$ - 1], fullFilled), repeat(step[filled % 8], (procent != 100) * 1), repeat(step[0], empty));
return 0;
};
http.perform();
normal("\x1b[?25h\n");
}
int main(string[] args) {
static string versionMsg = "PowerNex's toolchain manager v" ~ major.to!string ~ "." ~ minor.to!string ~ "."
~ patch.to!string ~ "\n" ~ "Copyright © 2017, Dan Printzell - https://github.com/Vild/PowerNex";
// dfmt off
auto helpInformation = getopt(args,
"v|version", "Show the updaters version", &showVersion,
"c|clean", "Clean out the toolchain folder before starting", &clean,
"noconfirm", "Always choose the default answer to questions", &noconfirm
);
// dfmt on
if (helpInformation.helpWanted) {
defaultGetoptPrinter(versionMsg, helpInformation.options);
return 0;
}
if (showVersion) {
writeln(versionMsg);
return 0;
}
normal("PowerNex's toolchain manager - https://github.com/Vild/PowerNex\n");
VersionInfo oldVI = clean ? VersionInfo.init : getOldInfo();
VersionInfo newVI = getNewInfo();
bool newDMD = newVI.dmdVersion > oldVI.dmdVersion;
bool newBinutils = newVI.binutilsVersion > oldVI.binutilsVersion;
if (!newDMD && !newBinutils) {
good("You already have the latest toolchain!\n");
return 0;
}
if (newDMD) {
if (oldVI.dmdVersion)
good("There is a new DMD version! (from: v.", oldVI.dmdVersion, " to: v.", newVI.dmdVersion, ")\n");
else
good("DMD is missing! Will download version ", newVI.dmdVersion, ".\n");
}
if (newBinutils) {
if (oldVI.binutilsVersion)
good("There is a new BINUTILS version! (from: v.", oldVI.binutilsVersion, " to: v.", newVI.binutilsVersion, ")\n");
else
good("BINUTILS is missing! Will download version ", newVI.binutilsVersion, ".\n");
}
char answer = question!('y', ['y', 'n'])("Do you want to continue with the download?");
if (answer == char.init)
return -1;
if (answer == 'n')
return 0;
if (exists(toolchainFolder)) {
if (!clean) {
answer = question!('n', ['y', 'n'])("Erase the toolchains folder content before starting? (Will force download everything)");
if (answer == char.init)
return -1;
clean |= answer == 'y';
}
if (clean)
rmdirRecurse(toolchainFolder);
}
mkdirRecurse(toolchainFolder ~ "/bin");
if (newDMD || clean) {
downloadProgress!ProcessPipe("DMD",
"https://ci.vild.io/job/PowerNex/job/powernex-dmd/job/master/" ~ newVI.dmdVersion.to!string ~ "/artifact/powernex-dmd.tar.xz",
"tar xkJ --no-same-owner -C " ~ toolchainFolder, No.IgnoreStdOut, Yes.IgnoreStdErr);
}
if (newBinutils || clean) {
downloadProgress!ProcessPipe("BINUTILS",
"https://ci.vild.io/job/PowerNex/job/powernex-binutils/job/master/" ~ newVI.binutilsVersion.to!string ~ "/artifact/powernex-binutils.tar.xz",
"tar xkJ --no-same-owner -C " ~ toolchainFolder, No.IgnoreStdOut, Yes.IgnoreStdErr);
}
normal("Saving new version file...\n");
{
JSONValue data = ["dmdVersion" : newVI.dmdVersion, "binutilsVersion" : newVI.binutilsVersion];
fwrite(versionInfoFile, data.toString);
}
normal("Everything is now up to date :)\n");
return 0;
}
// Not defined in phobos, or has a wrapper in core.sys.posix.string
int strcasecmp(scope const char[] s1, scope const char[] s2) @trusted pure @nogc {
size_t len = s1.length < s2.length ? s1.length : s2.length;
size_t idx;
while (idx < len && s1[idx]) {
if (s1[idx].toLower != s2[idx].toLower)
return s1[idx] - s2[idx];
idx++;
}
return 0;
}