Skip to content

Commit 28c519f

Browse files
authored
Merge pull request #904 from ocaml/opam-download-cache
Restore opam download cache feature
2 parents 1a390ea + df2da05 commit 28c519f

File tree

5 files changed

+129
-3
lines changed

5 files changed

+129
-3
lines changed

dist/index.js

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post/index.js

Lines changed: 42 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/setup-ocaml/src/cache.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as crypto from "node:crypto";
2+
import * as os from "node:os";
23
import * as path from "node:path";
34
import * as cache from "@actions/cache";
45
import * as core from "@actions/core";
@@ -65,6 +66,21 @@ async function composeOpamCacheKeys() {
6566
return { key, restoreKeys };
6667
}
6768

69+
async function composeOpamDownloadCacheKeys() {
70+
const ocamlCompiler = await RESOLVED_COMPILER;
71+
const repositoryUrls = OPAM_REPOSITORIES.map(([_, value]) => value).join(",");
72+
const plainKey = [ocamlCompiler, repositoryUrls].join(",");
73+
const hash = crypto.createHash("sha256").update(plainKey).digest("hex");
74+
const { runId } = github.context;
75+
const key = `${CACHE_PREFIX}-setup-ocaml-opam-download-${hash}-${runId}`;
76+
const restoreKeys = [
77+
key,
78+
`${CACHE_PREFIX}-setup-ocaml-opam-download-${hash}-`,
79+
`${CACHE_PREFIX}-setup-ocaml-opam-download-`,
80+
];
81+
return { key, restoreKeys };
82+
}
83+
6884
function composeCygwinCachePaths() {
6985
const cygwinRootSymlinkPath = path.posix.join("/cygdrive", "d", "cygwin");
7086
const cygwinLocalPackageDirectory = path.join(
@@ -104,6 +120,16 @@ function composeOpamCachePaths() {
104120
return paths;
105121
}
106122

123+
function composeOpamDownloadCachePaths() {
124+
if (PLATFORM === "windows") {
125+
const opamDownloadCachePath = path.join("D:", ".opam", "download-cache");
126+
return [opamDownloadCachePath];
127+
}
128+
const homeDir = os.homedir();
129+
const opamDownloadCachePath = path.join(homeDir, ".opam", "download-cache");
130+
return [opamDownloadCachePath];
131+
}
132+
107133
async function restoreCache(
108134
key: string,
109135
restoreKeys: string[],
@@ -185,6 +211,15 @@ export async function restoreOpamCaches() {
185211
});
186212
}
187213

214+
export async function restoreOpamDownloadCache() {
215+
return await core.group("Retrieve the opam download cache", async () => {
216+
const { key, restoreKeys } = await composeOpamDownloadCacheKeys();
217+
const paths = composeOpamDownloadCachePaths();
218+
const cacheKey = await restoreCache(key, restoreKeys, paths);
219+
return cacheKey;
220+
});
221+
}
222+
188223
export async function saveCygwinCache() {
189224
await core.group("Save the Cygwin cache", async () => {
190225
const { key } = await composeCygwinCacheKeys();
@@ -217,3 +252,11 @@ export async function saveOpamCache() {
217252
await saveCache(key, paths);
218253
});
219254
}
255+
256+
export async function saveOpamDownloadCache() {
257+
await core.group("Save the opam download cache", async () => {
258+
const { key } = await composeOpamDownloadCacheKeys();
259+
const paths = composeOpamDownloadCachePaths();
260+
await saveCache(key, paths);
261+
});
262+
}

packages/setup-ocaml/src/installer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { exec } from "@actions/exec";
55
import {
66
restoreDuneCache,
77
restoreOpamCaches,
8+
restoreOpamDownloadCache,
89
saveCygwinCache,
910
saveOpamCache,
1011
} from "./cache.js";
@@ -79,6 +80,7 @@ export async function installer() {
7980
} else {
8081
await update();
8182
}
83+
await restoreOpamDownloadCache();
8284
if (DUNE_CACHE) {
8385
await restoreDuneCache();
8486
await installDune();

packages/setup-ocaml/src/post.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as process from "node:process";
22
import * as core from "@actions/core";
3-
import { saveDuneCache } from "./cache.js";
3+
import { saveDuneCache, saveOpamDownloadCache } from "./cache.js";
44
import { DUNE_CACHE } from "./constants.js";
55
import { trimDuneCache } from "./dune.js";
66

@@ -10,6 +10,7 @@ async function run() {
1010
await trimDuneCache();
1111
await saveDuneCache();
1212
}
13+
await saveOpamDownloadCache();
1314
process.exit(0);
1415
} catch (error) {
1516
if (error instanceof Error) {

0 commit comments

Comments
 (0)