|
1 | 1 | import * as crypto from "node:crypto"; |
| 2 | +import * as os from "node:os"; |
2 | 3 | import * as path from "node:path"; |
3 | 4 | import * as cache from "@actions/cache"; |
4 | 5 | import * as core from "@actions/core"; |
@@ -65,6 +66,21 @@ async function composeOpamCacheKeys() { |
65 | 66 | return { key, restoreKeys }; |
66 | 67 | } |
67 | 68 |
|
| 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 | + |
68 | 84 | function composeCygwinCachePaths() { |
69 | 85 | const cygwinRootSymlinkPath = path.posix.join("/cygdrive", "d", "cygwin"); |
70 | 86 | const cygwinLocalPackageDirectory = path.join( |
@@ -104,6 +120,16 @@ function composeOpamCachePaths() { |
104 | 120 | return paths; |
105 | 121 | } |
106 | 122 |
|
| 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 | + |
107 | 133 | async function restoreCache( |
108 | 134 | key: string, |
109 | 135 | restoreKeys: string[], |
@@ -185,6 +211,15 @@ export async function restoreOpamCaches() { |
185 | 211 | }); |
186 | 212 | } |
187 | 213 |
|
| 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 | + |
188 | 223 | export async function saveCygwinCache() { |
189 | 224 | await core.group("Save the Cygwin cache", async () => { |
190 | 225 | const { key } = await composeCygwinCacheKeys(); |
@@ -217,3 +252,11 @@ export async function saveOpamCache() { |
217 | 252 | await saveCache(key, paths); |
218 | 253 | }); |
219 | 254 | } |
| 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 | +} |
0 commit comments