Files
setup-uv/src/download/checksum/update-known-checksums.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.8 KiB
TypeScript
Raw Normal View History

import { promises as fs } from "node:fs";
2024-09-05 08:06:45 -04:00
import * as tc from "@actions/tool-cache";
2024-11-25 09:18:03 +01:00
import { KNOWN_CHECKSUMS } from "./known-checksums";
2024-08-23 23:58:26 +02:00
export async function updateChecksums(
filePath: string,
2024-09-05 08:06:45 -04:00
downloadUrls: string[],
2024-08-23 23:58:26 +02:00
): Promise<void> {
2024-09-05 08:06:45 -04:00
await fs.rm(filePath);
2024-08-23 23:58:26 +02:00
await fs.appendFile(
filePath,
2024-11-25 09:18:03 +01:00
"// AUTOGENERATED_DO_NOT_EDIT\nexport const KNOWN_CHECKSUMS: { [key: string]: string } = {\n",
2024-09-05 08:06:45 -04:00
);
let firstLine = true;
2024-08-23 23:58:26 +02:00
for (const downloadUrl of downloadUrls) {
2024-09-05 08:06:45 -04:00
const key = getKey(downloadUrl);
2024-11-25 09:18:03 +01:00
if (key === undefined) {
continue;
}
const checksum = await getOrDownloadChecksum(key, downloadUrl);
2024-08-23 23:58:26 +02:00
if (!firstLine) {
2024-09-05 08:06:45 -04:00
await fs.appendFile(filePath, ",\n");
2024-08-23 23:58:26 +02:00
}
2024-11-25 09:18:03 +01:00
await fs.appendFile(filePath, ` "${key}":\n "${checksum}"`);
2024-09-05 08:06:45 -04:00
firstLine = false;
2024-08-23 23:58:26 +02:00
}
2024-11-25 09:18:03 +01:00
await fs.appendFile(filePath, ",\n};\n");
2024-08-23 23:58:26 +02:00
}
2024-11-25 09:18:03 +01:00
function getKey(downloadUrl: string): string | undefined {
2024-08-23 23:58:26 +02:00
// https://github.com/astral-sh/uv/releases/download/0.3.2/uv-aarch64-apple-darwin.tar.gz.sha256
2024-09-05 08:06:45 -04:00
const parts = downloadUrl.split("/");
const fileName = parts[parts.length - 1];
2024-11-25 09:18:03 +01:00
if (fileName.startsWith("source")) {
return undefined;
}
2024-09-05 08:06:45 -04:00
const name = fileName.split(".")[0].split("uv-")[1];
const version = parts[parts.length - 2];
return `${name}-${version}`;
2024-08-23 23:58:26 +02:00
}
2024-11-25 09:18:03 +01:00
async function getOrDownloadChecksum(
key: string,
downloadUrl: string,
): Promise<string> {
let checksum = "";
if (key in KNOWN_CHECKSUMS) {
checksum = KNOWN_CHECKSUMS[key];
} else {
const content = await downloadAssetContent(downloadUrl);
checksum = content.split(" ")[0].trim();
}
return checksum;
}
2024-08-23 23:58:26 +02:00
async function downloadAssetContent(downloadUrl: string): Promise<string> {
2024-09-05 08:06:45 -04:00
const downloadPath = await tc.downloadTool(downloadUrl);
const content = await fs.readFile(downloadPath, "utf8");
return content;
2024-08-23 23:58:26 +02:00
}