Skip to content

Commit 5141167

Browse files
authored
Merge pull request #727 from ocaml/refactor-constants
Refactor constants
2 parents c6d5af0 + b4b08ec commit 5141167

File tree

10 files changed

+271
-372
lines changed

10 files changed

+271
-372
lines changed

dist/index.js

Lines changed: 98 additions & 131 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: 90 additions & 120 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: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ import * as github from "@actions/github";
99
import * as datefns from "date-fns";
1010

1111
import {
12+
ARCHITECTURE,
1213
CACHE_PREFIX,
1314
OCAML_COMPILER,
1415
OPAM_DISABLE_SANDBOXING,
1516
OPAM_REPOSITORIES,
16-
Platform,
17+
PLATFORM,
1718
} from "./constants.js";
1819
import { getLatestOpamRelease } from "./opam.js";
19-
import {
20-
getArchitecture,
21-
getPlatform,
22-
getSystemIdentificationInfo,
23-
} from "./system.js";
20+
import { getSystemIdentificationInfo } from "./system.js";
2421
import { resolveCompiler } from "./version.js";
2522
import { getCygwinVersion } from "./win32.js";
2623

@@ -48,8 +45,8 @@ async function composeCygwinCacheKeys() {
4845
}
4946

5047
function composeDuneCacheKeys() {
51-
const platform = getPlatform().replaceAll(/\W/g, "_");
52-
const architecture = getArchitecture().replaceAll(/\W/g, "_");
48+
const platform = PLATFORM.replaceAll(/\W/g, "_");
49+
const architecture = ARCHITECTURE.replaceAll(/\W/g, "_");
5350
const { workflow: _workflow, job: _job, runId, runNumber } = github.context;
5451
const workflow = _workflow.toLowerCase().replaceAll(/\W/g, "_");
5552
const job = _job.replaceAll(/\W/g, "_");
@@ -64,27 +61,25 @@ function composeDuneCacheKeys() {
6461
}
6562

6663
async function composeOpamCacheKeys() {
67-
const platform = getPlatform();
6864
const fullPlatform =
69-
platform === Platform.Win32
70-
? platform
65+
PLATFORM === "win32"
66+
? PLATFORM
7167
: // eslint-disable-next-line unicorn/no-await-expression-member
72-
`${platform}-${(await getSystemIdentificationInfo()).version}`;
73-
const architecture = getArchitecture();
68+
`${PLATFORM}-${(await getSystemIdentificationInfo()).version}`;
7469
const opamVersion =
75-
platform === Platform.Win32
70+
PLATFORM === "win32"
7671
? "0.0.0.2"
7772
: // eslint-disable-next-line unicorn/no-await-expression-member
7873
(await getLatestOpamRelease()).version;
7974
const ocamlCompiler = await resolveCompiler(OCAML_COMPILER);
8075
const ocamlVersion = ocamlCompiler.toLowerCase().replaceAll(/\W/g, "_");
8176
const sandboxed = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox";
8277
const { year, week } = composeDate();
83-
const key = `${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-${year}-${week}`;
78+
const key = `${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-${year}-${week}`;
8479
const restoreKeys = [
85-
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-${year}-${week}`,
86-
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-${year}-`,
87-
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-`,
80+
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-${year}-${week}`,
81+
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-${year}-`,
82+
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-`,
8883
];
8984
return { key, restoreKeys };
9085
}
@@ -130,8 +125,7 @@ function composeCygwinCachePaths() {
130125
function composeDuneCachePaths() {
131126
const paths = [];
132127
const homeDir = os.homedir();
133-
const platform = getPlatform();
134-
if (platform === Platform.Win32) {
128+
if (PLATFORM === "win32") {
135129
const duneCacheDir = path.join(homeDir, "Local Settings", "Cache", "dune");
136130
paths.push(duneCacheDir);
137131
} else {
@@ -146,8 +140,7 @@ function composeDuneCachePaths() {
146140

147141
function composeOpamCachePaths() {
148142
const paths = [];
149-
const platform = getPlatform();
150-
if (platform === Platform.Win32) {
143+
if (PLATFORM === "win32") {
151144
const opamRootCachePath = path.join("D:", ".opam");
152145
paths.push(opamRootCachePath);
153146
const {
@@ -175,8 +168,7 @@ function composeOpamCachePaths() {
175168

176169
function composeOpamDownloadCachePaths() {
177170
const paths = [];
178-
const platform = getPlatform();
179-
if (platform === Platform.Win32) {
171+
if (PLATFORM === "win32") {
180172
const opamDownloadCachePath = path.join("D:", ".opam", "download-cache");
181173
paths.push(opamDownloadCachePath);
182174
} else {

packages/setup-ocaml/src/constants.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
1+
import * as os from "node:os";
12
import * as path from "node:path";
23

34
import * as core from "@actions/core";
45
import * as yaml from "yaml";
56

6-
import { getPlatform } from "./system.js";
7-
8-
export enum Architecture {
9-
X86_64 = "x86_64",
10-
ARM = "arm64",
11-
}
12-
13-
export enum Platform {
14-
Linux = "linux",
15-
MacOS = "macos",
16-
Win32 = "win32",
17-
}
18-
19-
const platform = getPlatform();
7+
export const ARCHITECTURE = (() => {
8+
switch (os.arch()) {
9+
case "x64": {
10+
return "x86_64";
11+
}
12+
case "arm64": {
13+
return "arm64";
14+
}
15+
default: {
16+
throw new Error("The architecture is not supported.");
17+
}
18+
}
19+
})();
20+
21+
export const PLATFORM = (() => {
22+
switch (os.platform()) {
23+
case "linux": {
24+
return "linux";
25+
}
26+
case "darwin": {
27+
return "macos";
28+
}
29+
case "win32": {
30+
return "win32";
31+
}
32+
default: {
33+
throw new Error("The platform is not supported.");
34+
}
35+
}
36+
})();
2037

2138
export const CYGWIN_ROOT = path.join("D:", "cygwin");
2239

@@ -26,7 +43,7 @@ export const CYGWIN_ROOT_WRAPPERBIN = path.join(CYGWIN_ROOT, "wrapperbin");
2643

2744
// [todo] remove the branch for Windows once opam 2.2 is released as stable.
2845
export const ALLOW_PRELEASE_OPAM =
29-
platform !== Platform.Win32 &&
46+
PLATFORM !== "win32" &&
3047
core.getBooleanInput("allow-prelease-opam", {
3148
required: false,
3249
trimWhitespace: true,
@@ -86,7 +103,7 @@ const repositories_yaml = yaml.parse(
86103
) as Record<string, string> | null;
87104

88105
const defaultRepository =
89-
platform === Platform.Win32
106+
PLATFORM === "win32"
90107
? "https://github.com/ocaml-opam/opam-repository-mingw.git#sunset"
91108
: "https://github.com/ocaml/opam-repository.git";
92109

packages/setup-ocaml/src/depext.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import * as path from "node:path";
33
import * as core from "@actions/core";
44
import { exec } from "@actions/exec";
55

6-
import { OPAM_DEPEXT_FLAGS, Platform } from "./constants.js";
7-
import { getPlatform } from "./system.js";
6+
import { OPAM_DEPEXT_FLAGS, PLATFORM } from "./constants.js";
87

98
export async function installDepext(ocamlVersion: string) {
109
await core.group("Install depext", async () => {
11-
const platform = getPlatform();
1210
const depextCygwinports =
13-
platform === Platform.Win32 ? ["depext-cygwinports"] : [];
11+
PLATFORM === "win32" ? ["depext-cygwinports"] : [];
1412
await exec("opam", ["install", "opam-depext", ...depextCygwinports]);
15-
if (platform === Platform.Win32) {
13+
if (PLATFORM === "win32") {
1614
let base = "";
1715
if (ocamlVersion.includes("mingw64")) {
1816
base = "x86_64-w64-mingw32";

packages/setup-ocaml/src/installer.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
OPAM_DEPEXT,
2020
OPAM_PIN,
2121
OPAM_REPOSITORIES,
22-
Platform,
22+
PLATFORM,
2323
} from "./constants.js";
2424
import { installDepext, installDepextPackages } from "./depext.js";
2525
import { installDune } from "./dune.js";
@@ -31,11 +31,10 @@ import {
3131
setupOpam,
3232
} from "./opam.js";
3333
import { getOpamLocalPackages } from "./packages.js";
34-
import { getPlatform, updateUnixPackageIndexFiles } from "./system.js";
34+
import { updateUnixPackageIndexFiles } from "./system.js";
3535
import { resolveCompiler } from "./version.js";
3636

3737
export async function installer() {
38-
const platform = getPlatform();
3938
if (!ALLOW_PRELEASE_OPAM) {
4039
// [todo] remove this once opam 2.2 is released as stable.
4140
// https://github.com/ocaml/setup-ocaml/issues/299
@@ -49,11 +48,11 @@ export async function installer() {
4948
// https://github.com/ocaml/opam/issues/3447
5049
core.exportVariable("OPAMSOLVERTIMEOUT", 1000);
5150
core.exportVariable("OPAMYES", 1);
52-
if (platform === Platform.Win32) {
51+
if (PLATFORM === "win32") {
5352
const opamRoot = path.join("D:", ".opam");
5453
core.exportVariable("OPAMROOT", opamRoot);
5554
}
56-
if (platform === Platform.Win32) {
55+
if (PLATFORM === "win32") {
5756
await core.group("Change the file system behavior parameters", async () => {
5857
await exec("fsutil", ["behavior", "query", "SymlinkEvaluation"]);
5958
// https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/fsutil-behavior
@@ -67,25 +66,25 @@ export async function installer() {
6766
await exec("fsutil", ["behavior", "query", "SymlinkEvaluation"]);
6867
});
6968
}
70-
if (platform === Platform.Win32) {
69+
if (PLATFORM === "win32") {
7170
core.exportVariable("HOME", process.env["USERPROFILE"]);
7271
core.exportVariable("MSYS", "winsymlinks:native");
7372
}
74-
if (platform === Platform.Win32) {
73+
if (PLATFORM === "win32") {
7574
await restoreCygwinCache();
7675
}
7776
const opamCacheHit = await restoreOpamCache();
7877
await setupOpam();
7978
await repositoryRemoveAll();
8079
await repositoryAddAll(OPAM_REPOSITORIES);
80+
const ocamlCompiler = await resolveCompiler(OCAML_COMPILER);
8181
if (!opamCacheHit) {
82-
const ocamlCompiler = await resolveCompiler(OCAML_COMPILER);
8382
await installOcaml(ocamlCompiler);
8483
await saveOpamCache();
8584
}
8685
await restoreOpamDownloadCache();
8786
if (OPAM_DEPEXT) {
88-
await installDepext(platform);
87+
await installDepext(ocamlCompiler);
8988
}
9089
if (DUNE_CACHE) {
9190
await restoreDuneCache();

packages/setup-ocaml/src/opam.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ import * as semver from "semver";
1313
import { saveCygwinCache } from "./cache.js";
1414
import {
1515
ALLOW_PRELEASE_OPAM,
16+
ARCHITECTURE,
1617
CYGWIN_ROOT,
1718
CYGWIN_ROOT_BIN,
1819
CYGWIN_ROOT_WRAPPERBIN,
1920
GITHUB_TOKEN,
2021
OPAM_DISABLE_SANDBOXING,
21-
Platform,
22+
PLATFORM,
2223
} from "./constants.js";
2324
import {
24-
getArchitecture,
25-
getPlatform,
2625
getSystemIdentificationInfo,
2726
updateUnixPackageIndexFiles,
2827
} from "./system.js";
@@ -51,10 +50,8 @@ export async function getLatestOpamRelease() {
5150
"Could not retrieve the opam release matching the version constraint",
5251
);
5352
}
54-
const architecture = getArchitecture();
55-
const platform = getPlatform();
5653
const matchedAssets = latestRelease.assets.find((asset) =>
57-
asset.browser_download_url.includes(`${architecture}-${platform}`),
54+
asset.browser_download_url.includes(`${ARCHITECTURE}-${PLATFORM}`),
5855
);
5956
if (!matchedAssets) {
6057
throw new Error(
@@ -68,8 +65,7 @@ export async function getLatestOpamRelease() {
6865
}
6966

7067
async function findOpam() {
71-
const platform = getPlatform();
72-
if (platform === Platform.Win32) {
68+
if (PLATFORM === "win32") {
7369
const opamPath = path.join(CYGWIN_ROOT, "bin", "opam.exe");
7470
return opamPath;
7571
} else {
@@ -80,8 +76,7 @@ async function findOpam() {
8076

8177
async function acquireOpamUnix() {
8278
const { version, browserDownloadUrl } = await getLatestOpamRelease();
83-
const architecture = getArchitecture();
84-
const cachedPath = tc.find("opam", version, architecture);
79+
const cachedPath = tc.find("opam", version, ARCHITECTURE);
8580
if (cachedPath === "") {
8681
const downloadedPath = await tc.downloadTool(browserDownloadUrl);
8782
core.info(`Acquired ${version} from ${browserDownloadUrl}`);
@@ -90,7 +85,7 @@ async function acquireOpamUnix() {
9085
"opam",
9186
"opam",
9287
version,
93-
architecture,
88+
ARCHITECTURE,
9489
);
9590
core.info(`Successfully cached opam to ${cachedPath}`);
9691
await fs.chmod(`${cachedPath}/opam`, 0o755);
@@ -104,9 +99,8 @@ async function acquireOpamUnix() {
10499

105100
async function installUnixSystemPackages() {
106101
const isGitHubRunner = process.env["ImageOS"] !== undefined;
107-
const platform = getPlatform();
108102
if (isGitHubRunner) {
109-
if (platform === Platform.Linux) {
103+
if (PLATFORM === "linux") {
110104
const { version: systemVersion } = await getSystemIdentificationInfo();
111105
if (systemVersion === "18.04") {
112106
// [info]: musl-tools bug in ubuntu 18.04;
@@ -125,7 +119,7 @@ async function installUnixSystemPackages() {
125119
"musl-tools",
126120
"rsync",
127121
]);
128-
} else if (platform === Platform.MacOS) {
122+
} else if (PLATFORM === "macos") {
129123
await exec("brew", ["install", "darcs", "gpatch", "mercurial"]);
130124
}
131125
}
@@ -275,8 +269,7 @@ async function setupOpamWindows() {
275269
}
276270

277271
export async function setupOpam() {
278-
const platform = getPlatform();
279-
if (platform === Platform.Win32) {
272+
if (PLATFORM === "win32") {
280273
await setupOpamWindows();
281274
} else {
282275
await setupOpamUnix();
@@ -285,8 +278,7 @@ export async function setupOpam() {
285278

286279
export async function installOcaml(ocamlCompiler: string) {
287280
await core.group("Install OCaml", async () => {
288-
const platform = getPlatform();
289-
if (platform === Platform.Win32) {
281+
if (PLATFORM === "win32") {
290282
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
291283
const originalPath = process.env["PATH"]!.split(path.delimiter);
292284
const patchedPath = [CYGWIN_ROOT_BIN, ...originalPath];
@@ -340,12 +332,11 @@ async function repositoryAdd(name: string, address: string) {
340332

341333
export async function repositoryAddAll(repositories: [string, string][]) {
342334
await core.group("Initialise the opam repositories", async () => {
343-
const platform = getPlatform();
344335
let restore_autocrlf;
345336
// Works around the lack of https://github.com/ocaml/opam/pull/3882 when
346337
// adding ocaml/opam-repository on Windows. Can be removed when the action
347338
// switches to opam 2.2
348-
if (platform === Platform.Win32) {
339+
if (PLATFORM === "win32") {
349340
const autocrlf = await getExecOutput(
350341
"git",
351342
["config", "--global", "core.autocrlf"],

0 commit comments

Comments
 (0)