1import {
2 genValueRegExp,
3 logger,
4 read,
5 run,
6 sha256RegExp,
7 versionRegExp,
8 write,
9} from "./common.ts";
10
11interface Replacer {
12 regex: RegExp;
13 value: string;
14}
15
16const log = logger("src");
17
18const prefetchHash = (nixpkgs: string, version: string) =>
19 run("nix-prefetch", ["-f", nixpkgs, "deno.src", "--rev", version]);
20const prefetchCargoHash = (nixpkgs: string) =>
21 run(
22 "nix-prefetch",
23 [`{ sha256 }: (import ${nixpkgs} {}).deno.cargoDeps.overrideAttrs (_: { hash = sha256; })`],
24 );
25
26const replace = (str: string, replacers: Replacer[]) =>
27 replacers.reduce(
28 (str, r) => str.replace(r.regex, r.value),
29 str,
30 );
31
32const updateNix = (filePath: string, replacers: Replacer[]) =>
33 read(filePath).then((str) => write(filePath, replace(str, replacers)));
34
35const genVerReplacer = (k: string, value: string): Replacer => (
36 { regex: genValueRegExp(k, versionRegExp), value }
37);
38const genShaReplacer = (k: string, value: string): Replacer => (
39 { regex: genValueRegExp(k, sha256RegExp), value }
40);
41
42export async function updateSrc(
43 filePath: string,
44 nixpkgs: string,
45 denoVersion: string,
46) {
47 log("Starting src update");
48 const trimVersion = denoVersion.substring(1);
49 log("Fetching hash for:", trimVersion);
50 const sha256 = await prefetchHash(nixpkgs, denoVersion);
51 log("sha256 to update:", sha256);
52 await updateNix(
53 filePath,
54 [
55 genVerReplacer("version", trimVersion),
56 genShaReplacer("hash", sha256),
57 ],
58 );
59 log("Fetching cargoHash for:", sha256);
60 const cargoHash = await prefetchCargoHash(nixpkgs);
61 log("cargoHash to update:", cargoHash);
62 await updateNix(
63 filePath,
64 [genShaReplacer("cargoHash", cargoHash)],
65 );
66 log("Finished src update");
67}