/
common.d
193 lines (170 loc) · 5.54 KB
/
common.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
import std.file, std.path, std.stdio;
//------------------------------------------------------------------------------
// File/Folder tools
///
enum allProjects = ["dmd", "druntime", "phobos", "tools", "dlang.org", "installer"];
/// Copy files, creating destination directories as needed
void copyFiles(string[] files, string srcDir, string dstDir, bool delegate(string) filter = null)
{
writefln("Copying the following files from '%s' to '%s':", srcDir, dstDir);
writefln("%(\t%s\n%)", files);
foreach(file; files)
{
if (filter && !filter(file)) continue;
auto srcPath = buildPath(srcDir, file);
auto dstPath = buildPath(dstDir, file);
mkdirRecurse(dirName(dstPath));
copy(srcPath, dstPath);
setAttributes(dstPath, getAttributes(srcPath));
}
}
void copyFile(string src, string dst)
{
writefln("Copying file '%s' to '%s'.", src, dst);
mkdirRecurse(dirName(dst));
copy(src, dst);
setAttributes(dst, getAttributes(src));
}
void copyFileIfExists(string src, string dst)
{
if(exists(src))
copyFile(src, dst);
}
//------------------------------------------------------------------------------
// tmpfile et. al.
// should be in core.stdc.stdlib
version (Posix) extern(C) char* mkdtemp(char* template_);
string mkdtemp()
{
version (Posix)
{
import core.stdc.string : strlen;
auto tmp = buildPath(tempDir(), "tmp.XXXXXX\0").dup;
auto dir = mkdtemp(tmp.ptr);
return dir[0 .. strlen(dir)].idup;
}
else
{
import std.format, std.random;
return buildPath(tempDir(), format("tmp.%06X\0", uniform(0, 0xFFFFFF)));
}
}
//------------------------------------------------------------------------------
// Download helpers
// templated so that we don't drag in libcurl unnecessarily
template fetchFile()
{
pragma(lib, "curl");
void fetchFile(string url, string path)
{
import std.array, std.datetime, std.exception, std.net.curl,
std.path, std.stdio, std.string;
auto client = HTTP(url);
size_t cnt;
auto app = appender!(ubyte[])();
ushort statusCode;
string etag;
bool abort;
client.onReceiveStatusLine = (status)
{
statusCode = status.code;
};
client.onReceiveHeader = (key, value)
{
if (key != "etag") return;
auto petag = path~".etag";
if (path.exists && petag.exists && cast(string)std.file.read(petag) == value)
{
writefln("Using cached download '%s'.", path);
abort = true;
return;
}
etag = value.idup;
writefln("Downloading file '%s' to '%s'.", url, path);
};
client.onReceive = (data)
{
if (!abort) app.put(data);
return data.length;
};
client.onProgress = (dlt, dln, _, _2)
{
if (!abort && dlt && cnt++ % 32 == 0)
writef("Progress: %.1f%% of %s kB\r", 100.0 * dln / dlt, dlt / 1024);
return abort ? 1 : 0;
};
try
client.perform();
catch (CurlException ce)
{
// stupid std.net.curl throws an exception when aborting, ignore it
if (abort) return;
throw ce;
}
enforce(statusCode / 100 == 2,
format("Download of '%s' failed with HTTP status code '%s'.",
url, statusCode));
mkdirRecurse(path.dirName);
std.file.write(path, app.data);
app.clear();
if (etag.length)
std.file.write(path~".etag", etag);
writeln(); // CR
stdout.flush();
}
}
//------------------------------------------------------------------------------
// Zip tools
import std.zip;
void extractZip(string archive, string outputDir)
{
import std.array : replace;
scope zip = new ZipArchive(std.file.read(archive));
foreach(name, am; zip.directory)
{
if(!am.expandedSize) continue;
string path = buildPath(outputDir, name.replace("\\", "/"));
auto dir = dirName(path);
if (dir != "" && !dir.exists)
mkdirRecurse(dir);
zip.expand(am);
std.file.write(path, am.expandedData);
import std.datetime : DosFileTimeToSysTime;
auto mtime = DosFileTimeToSysTime(am.time);
setTimes(path, mtime, mtime);
if (auto attrs = am.fileAttributes)
std.file.setAttributes(path, attrs);
}
}
void archiveZip(string inputDir, string archive)
{
import std.algorithm : startsWith;
import std.string : chomp, chompPrefix;
archive = absolutePath(archive);
scope zip = new ZipArchive();
auto parentDir = chomp(inputDir, baseName(inputDir));
foreach (de; dirEntries(inputDir, SpanMode.depth))
{
if(!de.isFile || de.baseName.startsWith(".git", ".DS_Store")) continue;
auto path = chompPrefix(de.name, parentDir);
zip.addMember(toArchiveMember(de, path));
}
if(exists(archive))
remove(archive);
std.file.write(archive, zip.build());
}
private ArchiveMember toArchiveMember(ref DirEntry de, string path)
{
auto am = new ArchiveMember();
am.compressionMethod = CompressionMethod.deflate;
am.time = de.timeLastModified;
am.name = path;
am.fileAttributes = de.linkAttributes;
version (Posix) if (de.isSymlink)
{
am.expandedData = cast(ubyte[])readLink(de.name);
return am;
}
am.expandedData = cast(ubyte[])std.file.read(de.name);
return am;
}