Something went wrong. Try again.
Zig language support for VS Code; Zig and ZLS are managed by zvm
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397/** * A version manager for Zig and ZLS. */
import vscode from "vscode";
import childProcess from "child_process";import fs from "fs";import util from "util";import which from "which";
import semver from "semver";
import * as minisign from "./minisign";import * as zigUtil from "./zigUtil";
const execFile = util.promisify(childProcess.execFile);const chmod = util.promisify(fs.chmod);
/** The maxmimum number of installation that can be store until they will be removed */const maxInstallCount = 5;/** Maps concurrent requests to install a version of an exe to a single promise */const inProgressInstalls = new Map<string, Promise<string>>();
export interface Config { context: vscode.ExtensionContext; /** The name of the application. */ title: string; /** The name of the executable file. */ exeName: string; minisignKey: minisign.Key; /** The command-line argument that should passed to `tar` to exact the tarball. */ extraTarArgs: string[]; /** * The command-line argument that should passed to the executable to query the version. * `"version"` for Zig, `"--version"` for ZLS */ versionArg: string; getMirrorUrls: () => Promise<vscode.Uri[]>; canonicalUrl: { release: vscode.Uri; nightly: vscode.Uri; }; /** * Get the artifact file name for a specific version. * * Example: * - `zig-x86_64-windows-0.14.1.zip` * - `zls-linux-x86_64-0.14.0.tar.xz` */ getArtifactName: (version: semver.SemVer) => string;}
/** Returns the path to the executable */export async function install(config: Config, version: semver.SemVer): Promise<string> { const key = config.exeName + version.raw; const entry = inProgressInstalls.get(key); if (entry) { return await entry; }
const promise = installGuarded(config, version); inProgressInstalls.set(key, promise);
return await promise.finally(() => { inProgressInstalls.delete(key); });}
async function installGuarded(config: Config, version: semver.SemVer): Promise<string> { const exeName = config.exeName + (process.platform === "win32" ? ".exe" : ""); const subDirName = `${getTargetName()}-${version.raw}`; const exeUri = vscode.Uri.joinPath(config.context.globalStorageUri, config.exeName, subDirName, exeName);
await setLastAccessTime(config, version);
try { await vscode.workspace.fs.stat(exeUri); return exeUri.fsPath; } catch (e) { if (e instanceof vscode.FileSystemError) { if (e.code !== "FileNotFound") { throw e; } // go ahead an install } else { throw e; } }
const tarPath = await getTarExePath(); if (!tarPath) { throw new Error(`Can't install ${config.title} because 'tar' could not be found`); }
const mirrors = [...(await config.getMirrorUrls())] .map((mirror) => ({ mirror, sort: Math.random() })) .sort((a, b) => a.sort - b.sort) .map(({ mirror }) => mirror);
return await vscode.window.withProgress<string>( { title: `Installing ${config.title} ${version.toString()}`, location: vscode.ProgressLocation.Notification, cancellable: true, }, async (progress, cancelToken) => { for (const mirrorUrl of mirrors) { const mirrorName = new URL(mirrorUrl.toString()).host; try { return await installFromMirror( config, version, mirrorUrl, mirrorName, tarPath, progress, cancelToken, ); } catch {} }
const canonicalUrl = version.prerelease.length === 0 ? config.canonicalUrl.release : config.canonicalUrl.nightly; const mirrorName = new URL(canonicalUrl.toString()).host; return await installFromMirror(config, version, canonicalUrl, mirrorName, tarPath, progress, cancelToken); }, );}
/** Returns the path to the executable */async function installFromMirror( config: Config, version: semver.SemVer, mirrorUrl: vscode.Uri, mirrorName: string, tarPath: string, progress: vscode.Progress<{ message?: string; increment?: number; }>, cancelToken: vscode.CancellationToken,): Promise<string> { progress.report({ message: `trying ${mirrorName}` });
const isWindows = process.platform === "win32"; const exeName = config.exeName + (isWindows ? ".exe" : ""); const subDirName = `${getTargetName()}-${version.raw}`; const fileName = config.getArtifactName(version);
const installDir = vscode.Uri.joinPath(config.context.globalStorageUri, config.exeName, subDirName); const exeUri = vscode.Uri.joinPath(installDir, exeName); const tarballUri = vscode.Uri.joinPath(installDir, fileName);
const abortController = new AbortController(); cancelToken.onCancellationRequested(() => { abortController.abort(); });
const artifactUrl = new URL(fileName, mirrorUrl.toString() + (mirrorUrl.path.endsWith("/") ? "" : "/")); const artifactMinisignUrl = new URL(`${artifactUrl.toString()}.minisig`);
/** * https://ziglang.org/download/community-mirrors/ encouraged the addition * of a `source` query parameter to specify what is making this request. * This extension is published as `ziglang.vscode-zig` so we use base it off that. */ artifactUrl.searchParams.set("source", "ziglang-vscode-zig"); artifactMinisignUrl.searchParams.set("source", "ziglang-vscode-zig");
const signatureResponse = await fetch(artifactMinisignUrl, { signal: abortController.signal, });
if (signatureResponse.status !== 200) { throw new Error(`${signatureResponse.statusText} (${signatureResponse.status.toString()})`); }
let artifactResponse = await fetch(artifactUrl, { signal: abortController.signal, });
if (artifactResponse.status !== 200) { throw new Error(`${artifactResponse.statusText} (${artifactResponse.status.toString()})`); }
progress.report({ message: `downloading from ${mirrorName}` });
const signatureData = Buffer.from(await signatureResponse.arrayBuffer());
let contentLength = artifactResponse.headers.has("content-length") ? Number(artifactResponse.headers.get("content-length")) : null; if (!Number.isFinite(contentLength)) contentLength = null;
if (contentLength) { let receivedLength = 0; const progressStream = new TransformStream<{ length: number }>({ transform(chunk, controller) { receivedLength += chunk.length; const increment = (chunk.length / contentLength) * 100; const currentProgress = (receivedLength / contentLength) * 100; progress.report({ message: `downloading tarball ${currentProgress.toFixed()}%`, increment: increment, }); controller.enqueue(chunk); }, }); artifactResponse = new Response(artifactResponse.body?.pipeThrough(progressStream)); } const artifactData = Buffer.from(await artifactResponse.arrayBuffer());
progress.report({ message: "Verifying Signature..." });
await minisign.ready;
const signature = minisign.parseSignature(signatureData); if (!minisign.verifySignature(config.minisignKey, signature, artifactData)) { throw new Error(`signature verification failed for '${artifactUrl.toString()}'`); }
const match = /^timestamp:\d+\s+file:([^\s]+)\s+hashed$/.test(signature.trustedComment.toString()); if (!match) { throw new Error(`filename verification failed for '${artifactUrl.toString()}'`); }
progress.report({ message: "Extracting..." });
try { await vscode.workspace.fs.delete(installDir, { recursive: true, useTrash: false }); } catch {}
try { await vscode.workspace.fs.createDirectory(installDir); await vscode.workspace.fs.writeFile(tarballUri, artifactData); await execFile(tarPath, ["-xf", tarballUri.fsPath, "-C", installDir.fsPath].concat(config.extraTarArgs), { signal: abortController.signal, timeout: 60000, // 60 seconds }); } catch (err) { try { await vscode.workspace.fs.delete(installDir, { recursive: true, useTrash: false }); } catch {} if (err instanceof Error) { throw new Error(`Failed to extract ${config.title} tarball: ${err.message}`); } else { throw err; } } finally { try { await vscode.workspace.fs.delete(tarballUri, { useTrash: false }); } catch {} }
const exeVersion = zigUtil.getVersion(exeUri.fsPath, config.versionArg); if (!exeVersion || exeVersion.compare(version) !== 0) { try { await vscode.workspace.fs.delete(installDir, { recursive: true, useTrash: false }); } catch {} // a mirror may provide the wrong version throw new Error(`Failed to validate version of ${config.title} installation!`); }
await chmod(exeUri.fsPath, 0o755);
try { await removeUnusedInstallations(config); } catch (err) { if (err instanceof Error) { void vscode.window.showWarningMessage( `Failed to uninstall unused ${config.title} versions: ${err.message}`, ); } else { void vscode.window.showWarningMessage(`Failed to uninstall unused ${config.title} versions`); } }
return exeUri.fsPath;}
/** Returns all locally installed versions */export async function query(config: Config): Promise<semver.SemVer[]> { const available: semver.SemVer[] = []; const prefix = getTargetName();
const storageDir = vscode.Uri.joinPath(config.context.globalStorageUri, config.exeName); try { for (const [name] of await vscode.workspace.fs.readDirectory(storageDir)) { if (name.startsWith(prefix)) { available.push(new semver.SemVer(name.substring(prefix.length + 1))); } } } catch (e) { if (e instanceof vscode.FileSystemError && e.code === "FileNotFound") { return []; } throw e; }
return available;}
async function getTarExePath(): Promise<string | null> { if (process.platform === "win32" && process.env["SYSTEMROOT"]) { // We may be running from within Git Bash which adds GNU tar to // the $PATH but we need bsdtar to extract zip files so we look // in the system directory before falling back to the $PATH. // See https://github.com/ziglang/vscode-zig/issues/382 const tarPath = `${process.env["SYSTEMROOT"]}\\system32\\tar.exe`; try { await vscode.workspace.fs.stat(vscode.Uri.file(tarPath)); return tarPath; } catch {} } return await which("tar", { nothrow: true });}
/** Set the last access time of the (installed) version. */async function setLastAccessTime(config: Config, version: semver.SemVer): Promise<void> { await config.context.globalState.update( `${config.exeName}-last-access-time-${getTargetName()}-${version.raw}`, Date.now(), );}
/** Remove installations with the oldest last access time until at most `VersionManager.maxInstallCount` versions remain. */async function removeUnusedInstallations(config: Config) { const storageDir = vscode.Uri.joinPath(config.context.globalStorageUri, config.exeName);
const keys: { key: string; installDir: vscode.Uri; lastAccessTime: number }[] = [];
try { for (const [name, fileType] of await vscode.workspace.fs.readDirectory(storageDir)) { const key = `${config.exeName}-last-access-time-${name}`; const uri = vscode.Uri.joinPath(storageDir, name); const lastAccessTime = config.context.globalState.get<number>(key);
if (!lastAccessTime || fileType !== vscode.FileType.Directory) { await vscode.workspace.fs.delete(uri, { recursive: true, useTrash: false }); } else { keys.push({ key: key, installDir: uri, lastAccessTime: lastAccessTime, }); } } } catch (e) { if (e instanceof vscode.FileSystemError && e.code === "FileNotFound") return; throw e; }
keys.sort((lhs, rhs) => rhs.lastAccessTime - lhs.lastAccessTime);
for (const item of keys.slice(maxInstallCount)) { await vscode.workspace.fs.delete(item.installDir, { recursive: true, useTrash: false }); await config.context.globalState.update(item.key, undefined); }}
/** Remove after some time has passed from the prefix change. */export async function convertOldInstallPrefixes(config: Config): Promise<void> { const oldPrefix = `${zigUtil.getZigOSName()}-${zigUtil.getZigArchName("armv7a")}`; const newPrefix = getTargetName();
const storageDir = vscode.Uri.joinPath(config.context.globalStorageUri, config.exeName); try { for (const [name] of await vscode.workspace.fs.readDirectory(storageDir)) { if (!name.startsWith(oldPrefix)) continue;
const version = name.substring(oldPrefix.length + 1); const oldInstallDir = vscode.Uri.joinPath(storageDir, name); const newInstallDir = vscode.Uri.joinPath(storageDir, `${newPrefix}-${version}`); try { await vscode.workspace.fs.rename(oldInstallDir, newInstallDir); } catch { // A possible cause could be that the user downgraded the extension // version to install it to the old install prefix while it was // already present with the new prefix. }
const oldKey = `${config.exeName}-last-access-time-${oldPrefix}-${version}`; const newKey = `${config.exeName}-last-access-time-${newPrefix}-${version}`; const lastAccessTime = config.context.globalState.get<number>(oldKey); if (lastAccessTime !== undefined) { config.context.globalState.update(newKey, lastAccessTime); config.context.globalState.update(oldKey, undefined); } } } catch {}}
function getTargetName(): string { return `${zigUtil.getZigArchName("armv7a")}-${zigUtil.getZigOSName()}`;}