Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore opam download cache feature #904

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 42 additions & 2 deletions dist/post/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions packages/setup-ocaml/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as crypto from "node:crypto";
import * as os from "node:os";
import * as path from "node:path";
import * as cache from "@actions/cache";
import * as core from "@actions/core";
Expand Down Expand Up @@ -65,6 +66,21 @@ async function composeOpamCacheKeys() {
return { key, restoreKeys };
}

async function composeOpamDownloadCacheKeys() {
const ocamlCompiler = await RESOLVED_COMPILER;
const repositoryUrls = OPAM_REPOSITORIES.map(([_, value]) => value).join(",");
const plainKey = [ocamlCompiler, repositoryUrls].join(",");
const hash = crypto.createHash("sha256").update(plainKey).digest("hex");
const { runId } = github.context;
const key = `${CACHE_PREFIX}-setup-ocaml-opam-download-${hash}-${runId}`;
const restoreKeys = [
key,
`${CACHE_PREFIX}-setup-ocaml-opam-download-${hash}-`,
`${CACHE_PREFIX}-setup-ocaml-opam-download-`,
];
return { key, restoreKeys };
}

function composeCygwinCachePaths() {
const cygwinRootSymlinkPath = path.posix.join("/cygdrive", "d", "cygwin");
const cygwinLocalPackageDirectory = path.join(
Expand Down Expand Up @@ -104,6 +120,16 @@ function composeOpamCachePaths() {
return paths;
}

function composeOpamDownloadCachePaths() {
if (PLATFORM === "windows") {
const opamDownloadCachePath = path.join("D:", ".opam", "download-cache");
return [opamDownloadCachePath];
}
const homeDir = os.homedir();
const opamDownloadCachePath = path.join(homeDir, ".opam", "download-cache");
return [opamDownloadCachePath];
}

async function restoreCache(
key: string,
restoreKeys: string[],
Expand Down Expand Up @@ -185,6 +211,15 @@ export async function restoreOpamCaches() {
});
}

export async function restoreOpamDownloadCache() {
return await core.group("Retrieve the opam download cache", async () => {
const { key, restoreKeys } = await composeOpamDownloadCacheKeys();
const paths = composeOpamDownloadCachePaths();
const cacheKey = await restoreCache(key, restoreKeys, paths);
return cacheKey;
});
}

export async function saveCygwinCache() {
await core.group("Save the Cygwin cache", async () => {
const { key } = await composeCygwinCacheKeys();
Expand Down Expand Up @@ -217,3 +252,11 @@ export async function saveOpamCache() {
await saveCache(key, paths);
});
}

export async function saveOpamDownloadCache() {
await core.group("Save the opam download cache", async () => {
const { key } = await composeOpamDownloadCacheKeys();
const paths = composeOpamDownloadCachePaths();
await saveCache(key, paths);
});
}
2 changes: 2 additions & 0 deletions packages/setup-ocaml/src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { exec } from "@actions/exec";
import {
restoreDuneCache,
restoreOpamCaches,
restoreOpamDownloadCache,
saveCygwinCache,
saveOpamCache,
} from "./cache.js";
Expand Down Expand Up @@ -79,6 +80,7 @@ export async function installer() {
} else {
await update();
}
await restoreOpamDownloadCache();
if (DUNE_CACHE) {
await restoreDuneCache();
await installDune();
Expand Down
3 changes: 2 additions & 1 deletion packages/setup-ocaml/src/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as process from "node:process";
import * as core from "@actions/core";
import { saveDuneCache } from "./cache.js";
import { saveDuneCache, saveOpamDownloadCache } from "./cache.js";
import { DUNE_CACHE } from "./constants.js";
import { trimDuneCache } from "./dune.js";

Expand All @@ -10,6 +10,7 @@ async function run() {
await trimDuneCache();
await saveDuneCache();
}
await saveOpamDownloadCache();
process.exit(0);
} catch (error) {
if (error instanceof Error) {
Expand Down
Loading