2020-06-18 07:06:33 +00:00
|
|
|
import {
|
|
|
|
genValueRegExp,
|
|
|
|
logger,
|
|
|
|
read,
|
2021-03-19 17:17:44 +00:00
|
|
|
run,
|
2020-06-18 07:06:33 +00:00
|
|
|
sha256RegExp,
|
|
|
|
versionRegExp,
|
|
|
|
write,
|
|
|
|
} from "./common.ts";
|
|
|
|
|
|
|
|
interface Replacer {
|
|
|
|
regex: RegExp;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const log = logger("src");
|
|
|
|
|
2023-04-29 16:46:19 +00:00
|
|
|
const prefetchHash = (nixpkgs: string, version: string) =>
|
2021-03-19 17:17:44 +00:00
|
|
|
run("nix-prefetch", ["-f", nixpkgs, "deno.src", "--rev", version]);
|
2023-04-29 16:46:19 +00:00
|
|
|
const prefetchCargoHash = (nixpkgs: string) =>
|
2021-03-19 17:17:44 +00:00
|
|
|
run(
|
|
|
|
"nix-prefetch",
|
2023-04-29 16:46:19 +00:00
|
|
|
[`{ sha256 }: (import ${nixpkgs} {}).deno.cargoDeps.overrideAttrs (_: { hash = sha256; })`],
|
2020-06-18 07:06:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const replace = (str: string, replacers: Replacer[]) =>
|
|
|
|
replacers.reduce(
|
|
|
|
(str, r) => str.replace(r.regex, r.value),
|
|
|
|
str,
|
|
|
|
);
|
|
|
|
|
|
|
|
const updateNix = (filePath: string, replacers: Replacer[]) =>
|
|
|
|
read(filePath).then((str) => write(filePath, replace(str, replacers)));
|
|
|
|
|
|
|
|
const genVerReplacer = (k: string, value: string): Replacer => (
|
|
|
|
{ regex: genValueRegExp(k, versionRegExp), value }
|
|
|
|
);
|
|
|
|
const genShaReplacer = (k: string, value: string): Replacer => (
|
|
|
|
{ regex: genValueRegExp(k, sha256RegExp), value }
|
|
|
|
);
|
|
|
|
|
|
|
|
export async function updateSrc(
|
|
|
|
filePath: string,
|
|
|
|
nixpkgs: string,
|
|
|
|
denoVersion: string,
|
|
|
|
) {
|
|
|
|
log("Starting src update");
|
2023-04-29 16:46:19 +00:00
|
|
|
const trimVersion = denoVersion.substring(1);
|
|
|
|
log("Fetching hash for:", trimVersion);
|
|
|
|
const sha256 = await prefetchHash(nixpkgs, denoVersion);
|
2020-06-18 07:06:33 +00:00
|
|
|
log("sha256 to update:", sha256);
|
|
|
|
await updateNix(
|
|
|
|
filePath,
|
|
|
|
[
|
|
|
|
genVerReplacer("version", trimVersion),
|
2023-04-29 16:46:19 +00:00
|
|
|
genShaReplacer("hash", sha256),
|
2020-06-18 07:06:33 +00:00
|
|
|
],
|
|
|
|
);
|
2023-04-29 16:46:19 +00:00
|
|
|
log("Fetching cargoHash for:", sha256);
|
|
|
|
const cargoHash = await prefetchCargoHash(nixpkgs);
|
|
|
|
log("cargoHash to update:", cargoHash);
|
2020-06-18 07:06:33 +00:00
|
|
|
await updateNix(
|
|
|
|
filePath,
|
2023-04-29 16:46:19 +00:00
|
|
|
[genShaReplacer("cargoHash", cargoHash)],
|
2020-06-18 07:06:33 +00:00
|
|
|
);
|
|
|
|
log("Finished src update");
|
|
|
|
}
|