2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-08-20 15:14:37 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file no-console
|
|
|
|
|
2024-07-25 01:30:28 -04:00
|
|
|
import { dirname, fromFileUrl, join, resolve, toFileUrl } from "@std/path";
|
2023-08-19 00:26:12 -04:00
|
|
|
import { wait } from "https://deno.land/x/wait@0.1.13/mod.ts";
|
2021-06-23 11:46:15 -04:00
|
|
|
export { dirname, fromFileUrl, join, resolve, toFileUrl };
|
2024-07-25 01:30:28 -04:00
|
|
|
export { existsSync, walk } from "@std/fs";
|
|
|
|
export { TextLineStream } from "@std/streams/text-line-stream";
|
|
|
|
export { delay } from "@std/async/delay";
|
2020-11-05 09:53:21 -05:00
|
|
|
|
2023-08-19 00:26:12 -04:00
|
|
|
// [toolName] --version output
|
|
|
|
const versions = {
|
2024-06-04 19:09:29 -04:00
|
|
|
"dlint": "dlint 0.60.0",
|
2023-08-19 00:26:12 -04:00
|
|
|
};
|
|
|
|
|
2023-11-23 17:30:26 -05:00
|
|
|
const compressed = new Set(["ld64.lld", "rcodesign"]);
|
2023-11-19 18:11:20 -05:00
|
|
|
|
2020-11-05 09:53:21 -05:00
|
|
|
export const ROOT_PATH = dirname(dirname(fromFileUrl(import.meta.url)));
|
|
|
|
|
2022-05-18 16:00:11 -04:00
|
|
|
async function getFilesFromGit(baseDir, args) {
|
2022-12-02 08:43:17 -05:00
|
|
|
const { success, stdout } = await new Deno.Command("git", {
|
2022-05-18 16:00:11 -04:00
|
|
|
stderr: "inherit",
|
|
|
|
args,
|
2022-12-02 08:43:17 -05:00
|
|
|
}).output();
|
2022-05-18 16:00:11 -04:00
|
|
|
const output = new TextDecoder().decode(stdout);
|
2022-07-21 04:45:15 -04:00
|
|
|
if (!success) {
|
2020-11-05 09:53:21 -05:00
|
|
|
throw new Error("gitLsFiles failed");
|
|
|
|
}
|
|
|
|
|
2024-06-10 12:20:44 -04:00
|
|
|
const files = output
|
|
|
|
.split("\0")
|
|
|
|
.filter((line) => line.length > 0)
|
|
|
|
.map((filePath) => {
|
|
|
|
try {
|
|
|
|
return Deno.realPathSync(join(baseDir, filePath));
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter((filePath) => filePath !== null);
|
2020-11-05 09:53:21 -05:00
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2020-11-14 14:27:37 -05:00
|
|
|
function gitLsFiles(baseDir, patterns) {
|
2020-11-05 09:53:21 -05:00
|
|
|
baseDir = Deno.realPathSync(baseDir);
|
|
|
|
const cmd = [
|
|
|
|
"-C",
|
|
|
|
baseDir,
|
|
|
|
"ls-files",
|
|
|
|
"-z",
|
|
|
|
"--exclude-standard",
|
|
|
|
"--cached",
|
|
|
|
"--modified",
|
|
|
|
"--others",
|
|
|
|
"--",
|
|
|
|
...patterns,
|
|
|
|
];
|
|
|
|
return getFilesFromGit(baseDir, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** List all files staged for commit */
|
2020-11-14 14:27:37 -05:00
|
|
|
function gitStaged(baseDir, patterns) {
|
2020-11-05 09:53:21 -05:00
|
|
|
baseDir = Deno.realPathSync(baseDir);
|
|
|
|
const cmd = [
|
|
|
|
"-C",
|
|
|
|
baseDir,
|
|
|
|
"diff",
|
|
|
|
"--staged",
|
|
|
|
"--diff-filter=ACMR",
|
|
|
|
"--name-only",
|
|
|
|
"-z",
|
|
|
|
"--",
|
|
|
|
...patterns,
|
|
|
|
];
|
|
|
|
return getFilesFromGit(baseDir, cmd);
|
|
|
|
}
|
|
|
|
|
2021-04-28 10:08:51 -04:00
|
|
|
/**
|
2020-11-05 09:53:21 -05:00
|
|
|
* Recursively list all files in (a subdirectory of) a git worktree.
|
|
|
|
* * Optionally, glob patterns may be specified to e.g. only list files with a
|
|
|
|
* certain extension.
|
|
|
|
* * Untracked files are included, unless they're listed in .gitignore.
|
|
|
|
* * Directory names themselves are not listed (but the files inside are).
|
|
|
|
* * Submodules and their contents are ignored entirely.
|
|
|
|
* * This function fails if the query matches no files.
|
2021-04-28 10:08:51 -04:00
|
|
|
*
|
2020-11-05 09:53:21 -05:00
|
|
|
* If --staged argument was provided when program is run
|
|
|
|
* only staged sources will be returned.
|
|
|
|
*/
|
|
|
|
export async function getSources(baseDir, patterns) {
|
|
|
|
const stagedOnly = Deno.args.includes("--staged");
|
|
|
|
|
|
|
|
if (stagedOnly) {
|
|
|
|
return await gitStaged(baseDir, patterns);
|
|
|
|
} else {
|
|
|
|
return await gitLsFiles(baseDir, patterns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildMode() {
|
|
|
|
if (Deno.args.includes("--release")) {
|
|
|
|
return "release";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "debug";
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildPath() {
|
|
|
|
return join(ROOT_PATH, "target", buildMode());
|
|
|
|
}
|
|
|
|
|
2023-08-19 00:26:12 -04:00
|
|
|
const platformDirName = {
|
|
|
|
"windows": "win",
|
|
|
|
"darwin": "mac",
|
|
|
|
"linux": "linux64",
|
|
|
|
}[Deno.build.os];
|
|
|
|
|
|
|
|
const executableSuffix = Deno.build.os === "windows" ? ".exe" : "";
|
|
|
|
|
2023-09-07 13:13:11 -04:00
|
|
|
async function sanityCheckPrebuiltFile(toolPath) {
|
|
|
|
const stat = await Deno.stat(toolPath);
|
|
|
|
if (stat.size < PREBUILT_MINIMUM_SIZE) {
|
|
|
|
throw new Error(
|
|
|
|
`File size ${stat.size} is less than expected minimum file size ${PREBUILT_MINIMUM_SIZE}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const file = await Deno.open(toolPath, { read: true });
|
|
|
|
const buffer = new Uint8Array(1024);
|
|
|
|
let n = 0;
|
|
|
|
while (n < 1024) {
|
|
|
|
n += await file.read(buffer.subarray(n));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mac: OK
|
|
|
|
if (buffer[0] == 0xcf && buffer[1] == 0xfa) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Windows OK
|
|
|
|
if (buffer[0] == "M".charCodeAt(0) && buffer[1] == "Z".charCodeAt(0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Linux OK
|
|
|
|
if (
|
|
|
|
buffer[0] == 0x7f && buffer[1] == "E".charCodeAt(0) &&
|
|
|
|
buffer[2] == "L".charCodeAt(0) && buffer[3] == "F".charCodeAt(0)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Invalid executable (header was ${buffer.subarray(0, 16)}`);
|
|
|
|
}
|
|
|
|
|
2023-08-19 00:26:12 -04:00
|
|
|
export async function getPrebuilt(toolName) {
|
|
|
|
const toolPath = getPrebuiltToolPath(toolName);
|
|
|
|
try {
|
2023-09-07 13:13:11 -04:00
|
|
|
await sanityCheckPrebuiltFile(toolPath);
|
2023-11-02 17:37:10 -04:00
|
|
|
const versionOk = await verifyVersion(toolName, toolPath);
|
2023-08-19 00:26:12 -04:00
|
|
|
if (!versionOk) {
|
|
|
|
throw new Error("Version mismatch");
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
await downloadPrebuilt(toolName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return toolPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PREBUILT_PATH = join(ROOT_PATH, "third_party", "prebuilt");
|
|
|
|
const PREBUILT_TOOL_DIR = join(PREBUILT_PATH, platformDirName);
|
2023-09-07 13:13:11 -04:00
|
|
|
const PREBUILT_MINIMUM_SIZE = 16 * 1024;
|
|
|
|
const DOWNLOAD_TASKS = {};
|
2023-08-19 00:26:12 -04:00
|
|
|
|
2020-11-05 09:53:21 -05:00
|
|
|
export function getPrebuiltToolPath(toolName) {
|
2023-08-19 00:26:12 -04:00
|
|
|
return join(PREBUILT_TOOL_DIR, toolName + executableSuffix);
|
|
|
|
}
|
|
|
|
|
2024-06-04 19:09:29 -04:00
|
|
|
const commitId = "b8aac22e0cd7c1c6557a56a813fe0c25486fafee";
|
2023-08-19 00:26:12 -04:00
|
|
|
const downloadUrl =
|
2023-11-23 17:30:26 -05:00
|
|
|
`https://raw.githubusercontent.com/denoland/deno_third_party/${commitId}/prebuilt/${platformDirName}`;
|
2023-08-19 00:26:12 -04:00
|
|
|
|
|
|
|
export async function downloadPrebuilt(toolName) {
|
2023-09-07 13:13:11 -04:00
|
|
|
// Ensure only one download per tool happens at a time
|
|
|
|
if (DOWNLOAD_TASKS[toolName]) {
|
2023-11-22 06:11:20 -05:00
|
|
|
return await DOWNLOAD_TASKS[toolName].promise;
|
2023-09-07 13:13:11 -04:00
|
|
|
}
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const downloadDeferred = DOWNLOAD_TASKS[toolName] = Promise.withResolvers();
|
2023-11-02 17:37:10 -04:00
|
|
|
const spinner = wait({
|
|
|
|
text: "Downloading prebuilt tool: " + toolName,
|
|
|
|
interval: 1000,
|
|
|
|
}).start();
|
2023-08-19 00:26:12 -04:00
|
|
|
const toolPath = getPrebuiltToolPath(toolName);
|
2023-09-07 13:13:11 -04:00
|
|
|
const tempFile = `${toolPath}.temp`;
|
2023-08-19 00:26:12 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
await Deno.mkdir(PREBUILT_TOOL_DIR, { recursive: true });
|
|
|
|
|
2023-11-19 18:11:20 -05:00
|
|
|
let url = `${downloadUrl}/${toolName}${executableSuffix}`;
|
|
|
|
if (compressed.has(toolName)) {
|
|
|
|
url += ".gz";
|
|
|
|
}
|
2023-08-19 00:26:12 -04:00
|
|
|
|
|
|
|
const resp = await fetch(url);
|
2023-09-07 13:13:11 -04:00
|
|
|
if (!resp.ok) {
|
|
|
|
throw new Error(`Non-successful response from ${url}: ${resp.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = await Deno.open(tempFile, {
|
2023-08-19 00:26:12 -04:00
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
mode: 0o755,
|
|
|
|
});
|
|
|
|
|
2023-11-19 18:11:20 -05:00
|
|
|
if (compressed.has(toolName)) {
|
|
|
|
await resp.body.pipeThrough(new DecompressionStream("gzip")).pipeTo(
|
|
|
|
file.writable,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await resp.body.pipeTo(file.writable);
|
|
|
|
}
|
2023-09-07 13:13:11 -04:00
|
|
|
spinner.text = `Checking prebuilt tool: ${toolName}`;
|
|
|
|
await sanityCheckPrebuiltFile(tempFile);
|
2023-11-02 17:37:10 -04:00
|
|
|
if (!await verifyVersion(toolName, tempFile)) {
|
|
|
|
throw new Error(
|
|
|
|
"Didn't get the correct version of the tool after downloading.",
|
|
|
|
);
|
|
|
|
}
|
2023-09-07 13:13:11 -04:00
|
|
|
spinner.text = `Successfully downloaded: ${toolName}`;
|
2024-06-05 11:04:16 -04:00
|
|
|
try {
|
|
|
|
// necessary on Windows it seems
|
|
|
|
await Deno.remove(toolPath);
|
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
2023-09-07 13:13:11 -04:00
|
|
|
await Deno.rename(tempFile, toolPath);
|
2023-08-19 00:26:12 -04:00
|
|
|
} catch (e) {
|
|
|
|
spinner.fail();
|
2023-11-22 06:11:20 -05:00
|
|
|
downloadDeferred.reject(e);
|
2023-08-19 00:26:12 -04:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
spinner.succeed();
|
2023-11-22 06:11:20 -05:00
|
|
|
downloadDeferred.resolve(null);
|
2023-08-19 00:26:12 -04:00
|
|
|
}
|
|
|
|
|
2023-11-02 17:37:10 -04:00
|
|
|
export async function verifyVersion(toolName, toolPath) {
|
2023-08-19 00:26:12 -04:00
|
|
|
const requiredVersion = versions[toolName];
|
|
|
|
if (!requiredVersion) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const cmd = new Deno.Command(toolPath, {
|
|
|
|
args: ["--version"],
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "inherit",
|
|
|
|
});
|
|
|
|
const output = await cmd.output();
|
|
|
|
const version = new TextDecoder().decode(output.stdout).trim();
|
|
|
|
return version == requiredVersion;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-05 09:53:21 -05:00
|
|
|
}
|