-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-zig.ts
355 lines (324 loc) · 8.5 KB
/
update-zig.ts
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
import { encode as encodeToHex } from "https://deno.land/std/encoding/hex.ts";
import * as pathLib from "https://deno.land/std/path/mod.ts";
import { parse } from "https://deno.land/std/flags/mod.ts";
import dir from "https://deno.land/x/dir/mod.ts";
interface RawArchive {
tarball: string;
shasum: string;
size: string;
}
interface Archive {
version: string;
platform: string;
size: number;
shasum: string;
url: URL;
filePath: string;
dirPath: string;
}
function archiveDir(path: string): string {
const i = path.lastIndexOf(".tar");
if (i !== -1) {
return path.slice(0, i);
}
const parts = pathLib.parse(path);
if (parts.ext === ".zip") {
return parts.name;
}
throw new Error(`unknown archive extension for ${path}`);
}
function archiveFromRaw(
raw: RawArchive,
version: string,
platform: string,
): Archive {
const { tarball, shasum, size } = raw;
const url = new URL(tarball);
const filePath = pathLib.basename(url.pathname);
const dirPath = archiveDir(filePath);
return {
version,
platform,
size: Number(size),
shasum,
url,
filePath,
dirPath,
};
}
type PlatformName = string;
interface RawRelease extends Record<string, string | undefined | RawArchive> {
date: string;
docs: string;
stdDocs?: string;
notes?: string;
}
interface ReleaseMeta {
version: string;
date: Date;
docs: URL;
stdDocs?: URL;
notes?: URL;
}
interface Release {
meta: ReleaseMeta;
platforms: Record<PlatformName, Archive>;
}
type Releases = Record<PlatformName, Release>;
function releaseFromRaw(raw: RawRelease, version: string): Release {
const { date, docs, stdDocs, notes } = raw;
const meta = {
version,
date: new Date(date),
docs: new URL(docs),
stdDocs: stdDocs ? new URL(stdDocs) : undefined,
notes: notes ? new URL(notes) : undefined,
};
const platforms = Object.fromEntries(
Object.entries(raw)
.filter((kv): kv is [PlatformName, RawArchive] => {
const [key, value] = kv;
return !Object.hasOwn(meta, key) && typeof value === "object";
})
.map((
[platform, rawArchive],
) => [platform, archiveFromRaw(rawArchive, version, platform)]),
);
return {
meta,
platforms,
};
}
async function downloadReleaseInfo(): Promise<Releases> {
const response = await fetch("https://ziglang.org/download/index.json");
const raw = await response.json() as Record<PlatformName, RawRelease>;
return Object.fromEntries(
Object.entries(raw)
.map((
[version, rawRelease],
) => [version, releaseFromRaw(rawRelease, version)]),
);
}
type Json = unknown[] | Record<string, unknown>;
async function runZigToJson<T extends object = Json>(
args: string[],
): Promise<T | undefined> {
let process;
try {
process = Deno.run({
cmd: ["zig", ...args],
stdout: "piped",
});
} catch (e) {
if (e.name === "NotFound") {
return;
} else {
throw e;
}
}
const stdoutBuffer = await process.output();
const stdoutString = new TextDecoder().decode(stdoutBuffer);
const json = JSON.parse(stdoutString);
return json as T;
}
interface Platform {
arch: string;
os: string;
}
async function getCurrentPlatform(): Promise<Platform> {
const targets = await runZigToJson<{
native: {
triple: string;
cpu: {
arch: string;
name: string;
features: string[];
};
os: string;
abi: string;
};
}>(["targets"]);
if (!targets) {
return Deno.build;
}
const { native: { cpu: { arch }, os } } = targets;
return { arch, os };
}
interface ZigEnv {
zig_exe: string;
lib_dir: string;
std_dir: string;
global_cache_dir: string;
version: string;
}
async function getZigEnv(): Promise<ZigEnv | undefined> {
return await runZigToJson<ZigEnv>(["env"]);
}
function getReleaseWithVersion(
releases: Releases,
version: string,
): Release {
return version === "latest"
? Object.values(releases).filter((e) => e.meta.version !== "master")[0]
: releases[version];
}
async function computeHash(
buffer: BufferSource,
algorithm: AlgorithmIdentifier,
): Promise<string> {
const digest = await crypto.subtle.digest(algorithm, buffer);
const hex = encodeToHex(new Uint8Array(digest));
return new TextDecoder().decode(hex);
}
function checkIf<T>(
log: boolean,
what: string,
expected: T,
actual: T,
eq: (a: T, b: T) => boolean = (a, b) => a === b,
) {
const isEq = eq(expected, actual);
if (!isEq && log) {
console.log(`${what} does not match: expecting ${expected}, got ${actual}`);
}
return isEq;
}
async function checkArchiveBuffer(
download: Archive,
buffer: BufferSource,
{ log }: { log: boolean } = { log: false },
): Promise<boolean> {
return checkIf(log, "archive size", download.size, buffer.byteLength) &&
checkIf(
log,
"archive shasum",
download.shasum,
await computeHash(buffer, "sha-256"),
);
}
async function fetchArchive(
archive: Archive,
): Promise<ArrayBuffer> {
console.log(`downloading archive: ${archive.url.href}`);
const response = await fetch(archive.url.href);
const buffer = await response.arrayBuffer();
assert(await checkArchiveBuffer(archive, buffer));
return buffer;
}
async function saveArchive(archive: Archive) {
let buffer;
try {
buffer = await Deno.readFile(archive.filePath);
} catch {
buffer = undefined;
}
if (buffer && checkArchiveBuffer(archive, buffer, { log: true })) {
console.log(`archive already downloaded: ${archive.filePath}`);
return;
}
buffer = await fetchArchive(archive);
await Deno.writeFile(archive.filePath, new Uint8Array(buffer));
}
function quote(s: string): string {
return s.includes(" ") ? `'${s}'` : s;
}
async function unpackArchive(archive: Archive) {
let stats;
try {
stats = await Deno.stat(archive.dirPath);
} catch {
stats = undefined;
}
if (stats?.isDirectory) {
console.log(`archive already unpacked: ${archive.dirPath}`);
return;
}
const cmd = ["tar", "xf", archive.filePath];
const cmdString = cmd.map(quote).join(" ");
console.log(`unpacking archive: ${archive.filePath}`);
console.log(`running: ${cmdString}`);
const status = await Deno.run({
cmd,
}).status();
if (!status.success) {
throw new Error(`error running: ${cmdString}`);
}
}
const currentLinkName = "current";
async function setArchiveToCurrent(archive: Archive) {
let currentLink;
try {
currentLink = await Deno.readLink(currentLinkName);
} catch {
currentLink = undefined;
}
if (archive.dirPath === currentLink) {
console.log(`version already set to ${archive.version}`);
} else {
console.log(`setting version to ${archive.version}`);
const tempPath = `${archive.dirPath}.${currentLinkName}`;
await Deno.symlink(archive.dirPath, tempPath);
await Deno.rename(tempPath, currentLinkName);
}
const zigEnv = await getZigEnv();
if (
zigEnv?.zig_exe ===
await Deno.realPath(pathLib.join(currentLinkName, "zig"))
) {
// on path
} else {
const currentZigDir = pathLib.join(Deno.cwd(), currentLinkName);
console.log(
`add zig dir to $PATH: ${currentZigDir}\n` +
`or zig binary to a dir on $PATH: ${
pathLib.join(currentZigDir, "zig")
}`,
);
}
}
async function updateArchive(archive: Archive) {
await saveArchive(archive);
await unpackArchive(archive);
}
async function removeArchive(archive: Archive) {
await Deno.remove(archive.dirPath, { recursive: true });
await Deno.remove(archive.filePath);
}
async function main() {
const args = parse(Deno.args, {
alias: {
"rm": "remove",
"delete": "remove",
},
default: {
"version": "latest",
"set": true,
},
boolean: ["remove", "set"],
});
console.log(args);
const zigDir = args.dir ?? (() => {
const homeDir = dir("home");
if (!homeDir) {
throw new Error(`can't find home directory`);
}
return pathLib.join(homeDir, ".zig");
})();
Deno.mkdir(zigDir, { recursive: true });
console.log(`cd ${quote(zigDir)}`);
Deno.chdir(zigDir);
const releases = await downloadReleaseInfo();
const platform = await getCurrentPlatform();
const release = getReleaseWithVersion(releases, args.version);
const archive = release.platforms[`${platform.arch}-${platform.os}`];
if (args.remove) {
await removeArchive(archive);
} else {
await updateArchive(archive);
if (args.set) {
await setArchiveToCurrent(archive);
}
}
}
await main();