|
| 1 | +// Downloads Dub. For use in the auto-tester CI environment. |
| 2 | +module download_dub; |
| 3 | + |
| 4 | +version (FreeBSD) |
| 5 | +{ |
| 6 | + version (X86_64) |
| 7 | + enum platform = "freebsd-64"; |
| 8 | + else version (X86) |
| 9 | + enum platform = "freebsd-32"; |
| 10 | + |
| 11 | + enum binDir = "bin"; |
| 12 | +} |
| 13 | +else version (linux) |
| 14 | +{ |
| 15 | + enum platform = "linux"; |
| 16 | + |
| 17 | + version (X86_64) |
| 18 | + enum binDir = "bin64"; |
| 19 | + else version (X86) |
| 20 | + enum bin64 = "bin32"; |
| 21 | +} |
| 22 | +else version (OSX) |
| 23 | +{ |
| 24 | + enum platform = "osx"; |
| 25 | + enum binDir = "bin"; |
| 26 | +} |
| 27 | +else version (Windows) |
| 28 | +{ |
| 29 | + enum platform = "windows"; |
| 30 | + enum binDir = "bin"; |
| 31 | +} |
| 32 | + |
| 33 | +version (Posix) |
| 34 | +{ |
| 35 | + enum archiveExtension = "tar.xz"; |
| 36 | + enum exeExtension = ""; |
| 37 | +} |
| 38 | +else version (Windows) |
| 39 | +{ |
| 40 | + enum archiveExtension = "7z"; |
| 41 | + enum exeExtension = ".exe"; |
| 42 | +} |
| 43 | + |
| 44 | +string resolveLatest() |
| 45 | +{ |
| 46 | + import std.net.curl : get; |
| 47 | + import std.exception : assumeUnique; |
| 48 | + |
| 49 | + return get("http://downloads.dlang.org/releases/LATEST").assumeUnique; |
| 50 | +} |
| 51 | + |
| 52 | +string dmdUrl() |
| 53 | +{ |
| 54 | + import std.format : format; |
| 55 | + |
| 56 | + enum fmt = "http://downloads.dlang.org/releases/2.x/%s/dmd.%s.%s.%s"; |
| 57 | + const version_ = resolveLatest; |
| 58 | + return format!fmt(version_, version_, platform, archiveExtension); |
| 59 | +} |
| 60 | + |
| 61 | +void unpack(string dmdArchivePath) |
| 62 | +{ |
| 63 | + import std.process : spawnProcess, wait; |
| 64 | + |
| 65 | + version (Posix) |
| 66 | + const arguments = ["tar", "xf", dmdArchivePath]; |
| 67 | + else version (Windows) |
| 68 | + const arguments = ["7z", "x", dmdArchivePath]; |
| 69 | + |
| 70 | + spawnProcess(arguments).wait(); |
| 71 | +} |
| 72 | + |
| 73 | +void extractDub(string path, string dest) |
| 74 | +{ |
| 75 | + import std.path : buildPath; |
| 76 | + import std.file : rename; |
| 77 | + |
| 78 | + enum filename = "dub" ~ exeExtension; |
| 79 | + const dubPath = buildPath(path, platform, binDir, filename); |
| 80 | + |
| 81 | + rename(dubPath, buildPath(dest, filename)); |
| 82 | +} |
| 83 | + |
| 84 | +void cleanup(string dmdArchivePath, string dmdPath) |
| 85 | +{ |
| 86 | + import std.file : rmdirRecurse; |
| 87 | + import std.file : remove; |
| 88 | + |
| 89 | + rmdirRecurse(dmdPath); |
| 90 | + remove(dmdArchivePath); |
| 91 | +} |
| 92 | + |
| 93 | +void main() |
| 94 | +{ |
| 95 | + import std.file : getcwd; |
| 96 | + import std.net.curl : download; |
| 97 | + |
| 98 | + enum dmdArchivePath = "dmd." ~ archiveExtension; |
| 99 | + enum dmdPath = "dmd2"; |
| 100 | + |
| 101 | + download(dmdUrl, dmdArchivePath); |
| 102 | + unpack(dmdArchivePath); |
| 103 | + extractDub(dmdPath, getcwd); |
| 104 | + cleanup(dmdArchivePath, dmdPath); |
| 105 | +} |
0 commit comments