Skip to content

Commit 8ca4c46

Browse files
authored
Merge pull request #945 from ocaml/reduce-api-calls
Don't make heavy API requests multiple times
2 parents 4f0c8be + ecb2fc4 commit 8ca4c46

File tree

8 files changed

+288
-292
lines changed

8 files changed

+288
-292
lines changed

dist/index.js

Lines changed: 161 additions & 161 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: 102 additions & 103 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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ import {
1717
OPAM_REPOSITORIES,
1818
OPAM_ROOT,
1919
PLATFORM,
20-
RESOLVED_COMPILER,
2120
} from "./constants.js";
22-
import { retrieveLatestOpamRelease } from "./opam.js";
23-
import { retrieveCygwinVersion } from "./windows.js";
21+
import { latestOpamRelease } from "./opam.js";
22+
import { resolvedCompiler } from "./version.js";
23+
import { cygwinVersion } from "./windows.js";
2424

2525
async function composeCygwinCacheKeys() {
26-
const cygwinVersion = await retrieveCygwinVersion();
27-
const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${CYGWIN_MIRROR_ENCODED_URI}-${cygwinVersion}`;
26+
const version = await cygwinVersion;
27+
const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${CYGWIN_MIRROR_ENCODED_URI}-${version}`;
2828
const restoreKeys = [key];
2929
return { key, restoreKeys };
3030
}
3131

3232
async function composeDuneCacheKeys() {
3333
const { workflow, job, runId } = github.context;
34-
const ocamlCompiler = await RESOLVED_COMPILER;
34+
const ocamlCompiler = await resolvedCompiler;
3535
const plainKey = [ocamlCompiler, workflow, job].join();
3636
const hash = crypto.createHash("sha256").update(plainKey).digest("hex");
3737
const key = `${CACHE_PREFIX}-setup-ocaml-dune-${PLATFORM}-${ARCHITECTURE}-${hash}-${runId}`;
@@ -45,9 +45,9 @@ async function composeDuneCacheKeys() {
4545
}
4646

4747
async function composeOpamCacheKeys() {
48-
const { version: opamVersion } = await retrieveLatestOpamRelease();
48+
const { version: opamVersion } = await latestOpamRelease;
4949
const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox";
50-
const ocamlCompiler = await RESOLVED_COMPILER;
50+
const ocamlCompiler = await resolvedCompiler;
5151
const repositoryUrls = OPAM_REPOSITORIES.map(([_, value]) => value).join();
5252
const osInfo = await system.osInfo();
5353
const plainKey = [

packages/setup-ocaml/src/constants.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as path from "node:path";
33
import * as process from "node:process";
44
import * as core from "@actions/core";
55
import * as yaml from "yaml";
6-
import { resolveCompiler } from "./version.js";
76

87
export const ARCHITECTURE = (() => {
98
switch (process.arch) {
@@ -99,7 +98,9 @@ export const GITHUB_TOKEN = core.getInput("github-token");
9998

10099
export const DUNE_CACHE = core.getBooleanInput("dune-cache");
101100

102-
const OCAML_COMPILER = core.getInput("ocaml-compiler", { required: true });
101+
export const OCAML_COMPILER = core.getInput("ocaml-compiler", {
102+
required: true,
103+
});
103104

104105
export const OPAM_DISABLE_SANDBOXING =
105106
// [TODO] unlock this once sandboxing is supported on Windows
@@ -115,7 +116,3 @@ export const OPAM_REPOSITORIES: [string, string][] = (() => {
115116
) as Record<string, string>;
116117
return Object.entries(repositoriesYaml).reverse();
117118
})();
118-
119-
export const RESOLVED_COMPILER = (async () => {
120-
return await resolveCompiler(OCAML_COMPILER);
121-
})();

packages/setup-ocaml/src/installer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
OPAM_REPOSITORIES,
1919
OPAM_ROOT,
2020
PLATFORM,
21-
RESOLVED_COMPILER,
2221
} from "./constants.js";
2322
import { installDune } from "./dune.js";
2423
import {
@@ -30,6 +29,7 @@ import {
3029
update,
3130
} from "./opam.js";
3231
import { retrieveOpamLocalPackages } from "./packages.js";
32+
import { resolvedCompiler } from "./version.js";
3333
import { setupCygwin } from "./windows.js";
3434

3535
export async function installer() {
@@ -75,7 +75,7 @@ export async function installer() {
7575
if (!opamCacheHit) {
7676
await repositoryRemoveAll();
7777
await repositoryAddAll(OPAM_REPOSITORIES);
78-
const ocamlCompiler = await RESOLVED_COMPILER;
78+
const ocamlCompiler = await resolvedCompiler;
7979
await installOcaml(ocamlCompiler);
8080
await saveOpamCache();
8181
} else {

packages/setup-ocaml/src/opam.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
updateUnixPackageIndexFiles,
2020
} from "./unix.js";
2121

22-
export async function retrieveLatestOpamRelease() {
22+
export const latestOpamRelease = (async () => {
2323
const semverRange = ALLOW_PRERELEASE_OPAM ? "*" : "<2.4.0";
2424
const octokit = github.getOctokit(GITHUB_TOKEN, undefined, retry);
2525
const { data: releases } = await octokit.rest.repos.listReleases({
@@ -59,11 +59,11 @@ export async function retrieveLatestOpamRelease() {
5959
version: latestRelease.tag_name,
6060
browserDownloadUrl: matchedAssets.browser_download_url,
6161
};
62-
}
62+
})();
6363

6464
async function acquireOpam() {
6565
await core.group("Installing opam", async () => {
66-
const { version, browserDownloadUrl } = await retrieveLatestOpamRelease();
66+
const { version, browserDownloadUrl } = await latestOpamRelease;
6767
const cachedPath = toolCache.find("opam", version, ARCHITECTURE);
6868
const opam = PLATFORM !== "windows" ? "opam" : "opam.exe";
6969
if (cachedPath === "") {

packages/setup-ocaml/src/version.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from "node:path";
22
import * as github from "@actions/github";
33
import { retry } from "@octokit/plugin-retry";
44
import * as semver from "semver";
5-
import { GITHUB_TOKEN } from "./constants.js";
5+
import { GITHUB_TOKEN, OCAML_COMPILER } from "./constants.js";
66

77
function isSemverValidRange(semverVersion: string) {
88
return semver.validRange(semverVersion, { loose: true }) !== null;
@@ -52,9 +52,9 @@ async function resolveVersion(semverVersion: string) {
5252
return matchedFullCompilerVersion;
5353
}
5454

55-
export async function resolveCompiler(compiler: string) {
56-
const resolvedCompiler = isSemverValidRange(compiler)
57-
? `ocaml-base-compiler.${await resolveVersion(compiler)}`
58-
: compiler;
55+
export const resolvedCompiler = (async () => {
56+
const resolvedCompiler = isSemverValidRange(OCAML_COMPILER)
57+
? `ocaml-base-compiler.${await resolveVersion(OCAML_COMPILER)}`
58+
: OCAML_COMPILER;
5959
return resolvedCompiler;
60-
}
60+
})();

0 commit comments

Comments
 (0)