Skip to content

Commit

Permalink
Refactor constants
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <[email protected]>
  • Loading branch information
smorimoto committed Nov 26, 2023
1 parent 1a897aa commit b4b08ec
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 371 deletions.
229 changes: 98 additions & 131 deletions dist/index.js

Large diffs are not rendered by default.

210 changes: 90 additions & 120 deletions dist/post/index.js

Large diffs are not rendered by default.

40 changes: 16 additions & 24 deletions packages/setup-ocaml/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ import * as github from "@actions/github";
import * as datefns from "date-fns";

import {
ARCHITECTURE,
CACHE_PREFIX,
OCAML_COMPILER,
OPAM_DISABLE_SANDBOXING,
OPAM_REPOSITORIES,
Platform,
PLATFORM,
} from "./constants.js";
import { getLatestOpamRelease } from "./opam.js";
import {
getArchitecture,
getPlatform,
getSystemIdentificationInfo,
} from "./system.js";
import { getSystemIdentificationInfo } from "./system.js";
import { resolveCompiler } from "./version.js";
import { getCygwinVersion } from "./win32.js";

Expand Down Expand Up @@ -48,8 +45,8 @@ async function composeCygwinCacheKeys() {
}

function composeDuneCacheKeys() {
const platform = getPlatform().replaceAll(/\W/g, "_");
const architecture = getArchitecture().replaceAll(/\W/g, "_");
const platform = PLATFORM.replaceAll(/\W/g, "_");
const architecture = ARCHITECTURE.replaceAll(/\W/g, "_");
const { workflow: _workflow, job: _job, runId, runNumber } = github.context;
const workflow = _workflow.toLowerCase().replaceAll(/\W/g, "_");
const job = _job.replaceAll(/\W/g, "_");
Expand All @@ -64,27 +61,25 @@ function composeDuneCacheKeys() {
}

async function composeOpamCacheKeys() {
const platform = getPlatform();
const fullPlatform =
platform === Platform.Win32
? platform
PLATFORM === "win32"
? PLATFORM
: // eslint-disable-next-line unicorn/no-await-expression-member
`${platform}-${(await getSystemIdentificationInfo()).version}`;
const architecture = getArchitecture();
`${PLATFORM}-${(await getSystemIdentificationInfo()).version}`;
const opamVersion =
platform === Platform.Win32
PLATFORM === "win32"
? "0.0.0.2"
: // eslint-disable-next-line unicorn/no-await-expression-member
(await getLatestOpamRelease()).version;
const ocamlCompiler = await resolveCompiler(OCAML_COMPILER);
const ocamlVersion = ocamlCompiler.toLowerCase().replaceAll(/\W/g, "_");
const sandboxed = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox";
const { year, week } = composeDate();
const key = `${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-${year}-${week}`;
const key = `${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-${year}-${week}`;
const restoreKeys = [
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-${year}-${week}`,
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-${year}-`,
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${architecture}-${ocamlVersion}-`,
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-${year}-${week}`,
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-${year}-`,
`${CACHE_PREFIX}-setup-ocaml-opam-${opamVersion}-${sandboxed}-${fullPlatform}-${ARCHITECTURE}-${ocamlVersion}-`,
];
return { key, restoreKeys };
}
Expand Down Expand Up @@ -130,8 +125,7 @@ function composeCygwinCachePaths() {
function composeDuneCachePaths() {
const paths = [];
const homeDir = os.homedir();
const platform = getPlatform();
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
const duneCacheDir = path.join(homeDir, "Local Settings", "Cache", "dune");
paths.push(duneCacheDir);
} else {
Expand All @@ -146,8 +140,7 @@ function composeDuneCachePaths() {

function composeOpamCachePaths() {
const paths = [];
const platform = getPlatform();
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
const opamRootCachePath = path.join("D:", ".opam");
paths.push(opamRootCachePath);
const {
Expand Down Expand Up @@ -175,8 +168,7 @@ function composeOpamCachePaths() {

function composeOpamDownloadCachePaths() {
const paths = [];
const platform = getPlatform();
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
const opamDownloadCachePath = path.join("D:", ".opam", "download-cache");
paths.push(opamDownloadCachePath);
} else {
Expand Down
49 changes: 33 additions & 16 deletions packages/setup-ocaml/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import * as os from "node:os";
import * as path from "node:path";

import * as core from "@actions/core";
import * as yaml from "yaml";

import { getPlatform } from "./system.js";

export enum Architecture {
X86_64 = "x86_64",
ARM = "arm64",
}

export enum Platform {
Linux = "linux",
MacOS = "macos",
Win32 = "win32",
}

const platform = getPlatform();
export const ARCHITECTURE = (() => {
switch (os.arch()) {
case "x64": {
return "x86_64";
}
case "arm64": {
return "arm64";
}
default: {
throw new Error("The architecture is not supported.");
}
}
})();

export const PLATFORM = (() => {
switch (os.platform()) {
case "linux": {
return "linux";
}
case "darwin": {
return "macos";
}
case "win32": {
return "win32";
}
default: {
throw new Error("The platform is not supported.");
}
}
})();

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

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

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

const defaultRepository =
platform === Platform.Win32
PLATFORM === "win32"
? "https://github.com/ocaml-opam/opam-repository-mingw.git#sunset"
: "https://github.com/ocaml/opam-repository.git";

Expand Down
8 changes: 3 additions & 5 deletions packages/setup-ocaml/src/depext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import * as path from "node:path";
import * as core from "@actions/core";
import { exec } from "@actions/exec";

import { OPAM_DEPEXT_FLAGS, Platform } from "./constants.js";
import { getPlatform } from "./system.js";
import { OPAM_DEPEXT_FLAGS, PLATFORM } from "./constants.js";

export async function installDepext(ocamlVersion: string) {
await core.group("Install depext", async () => {
const platform = getPlatform();
const depextCygwinports =
platform === Platform.Win32 ? ["depext-cygwinports"] : [];
PLATFORM === "win32" ? ["depext-cygwinports"] : [];
await exec("opam", ["install", "opam-depext", ...depextCygwinports]);
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
let base = "";
if (ocamlVersion.includes("mingw64")) {
base = "x86_64-w64-mingw32";
Expand Down
17 changes: 8 additions & 9 deletions packages/setup-ocaml/src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
OPAM_DEPEXT,
OPAM_PIN,
OPAM_REPOSITORIES,
Platform,
PLATFORM,
} from "./constants.js";
import { installDepext, installDepextPackages } from "./depext.js";
import { installDune } from "./dune.js";
Expand All @@ -31,11 +31,10 @@ import {
setupOpam,
} from "./opam.js";
import { getOpamLocalPackages } from "./packages.js";
import { getPlatform, updateUnixPackageIndexFiles } from "./system.js";
import { updateUnixPackageIndexFiles } from "./system.js";
import { resolveCompiler } from "./version.js";

export async function installer() {
const platform = getPlatform();
if (!ALLOW_PRELEASE_OPAM) {
// [todo] remove this once opam 2.2 is released as stable.
// https://github.com/ocaml/setup-ocaml/issues/299
Expand All @@ -49,11 +48,11 @@ export async function installer() {
// https://github.com/ocaml/opam/issues/3447
core.exportVariable("OPAMSOLVERTIMEOUT", 1000);
core.exportVariable("OPAMYES", 1);
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
const opamRoot = path.join("D:", ".opam");
core.exportVariable("OPAMROOT", opamRoot);
}
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
await core.group("Change the file system behavior parameters", async () => {
await exec("fsutil", ["behavior", "query", "SymlinkEvaluation"]);
// https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/fsutil-behavior
Expand All @@ -67,25 +66,25 @@ export async function installer() {
await exec("fsutil", ["behavior", "query", "SymlinkEvaluation"]);
});
}
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
core.exportVariable("HOME", process.env["USERPROFILE"]);
core.exportVariable("MSYS", "winsymlinks:native");
}
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
await restoreCygwinCache();
}
const opamCacheHit = await restoreOpamCache();
await setupOpam();
await repositoryRemoveAll();
await repositoryAddAll(OPAM_REPOSITORIES);
const ocamlCompiler = await resolveCompiler(OCAML_COMPILER);
if (!opamCacheHit) {
const ocamlCompiler = await resolveCompiler(OCAML_COMPILER);
await installOcaml(ocamlCompiler);
await saveOpamCache();
}
await restoreOpamDownloadCache();
if (OPAM_DEPEXT) {
await installDepext(platform);
await installDepext(ocamlCompiler);
}
if (DUNE_CACHE) {
await restoreDuneCache();
Expand Down
31 changes: 11 additions & 20 deletions packages/setup-ocaml/src/opam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import * as semver from "semver";
import { saveCygwinCache } from "./cache.js";
import {
ALLOW_PRELEASE_OPAM,
ARCHITECTURE,
CYGWIN_ROOT,
CYGWIN_ROOT_BIN,
CYGWIN_ROOT_WRAPPERBIN,
GITHUB_TOKEN,
OPAM_DISABLE_SANDBOXING,
Platform,
PLATFORM,
} from "./constants.js";
import {
getArchitecture,
getPlatform,
getSystemIdentificationInfo,
updateUnixPackageIndexFiles,
} from "./system.js";
Expand Down Expand Up @@ -51,10 +50,8 @@ export async function getLatestOpamRelease() {
"Could not retrieve the opam release matching the version constraint",
);
}
const architecture = getArchitecture();
const platform = getPlatform();
const matchedAssets = latestRelease.assets.find((asset) =>
asset.browser_download_url.includes(`${architecture}-${platform}`),
asset.browser_download_url.includes(`${ARCHITECTURE}-${PLATFORM}`),
);
if (!matchedAssets) {
throw new Error(
Expand All @@ -68,8 +65,7 @@ export async function getLatestOpamRelease() {
}

async function findOpam() {
const platform = getPlatform();
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
const opamPath = path.join(CYGWIN_ROOT, "bin", "opam.exe");
return opamPath;
} else {
Expand All @@ -80,8 +76,7 @@ async function findOpam() {

async function acquireOpamUnix() {
const { version, browserDownloadUrl } = await getLatestOpamRelease();
const architecture = getArchitecture();
const cachedPath = tc.find("opam", version, architecture);
const cachedPath = tc.find("opam", version, ARCHITECTURE);
if (cachedPath === "") {
const downloadedPath = await tc.downloadTool(browserDownloadUrl);
core.info(`Acquired ${version} from ${browserDownloadUrl}`);
Expand All @@ -90,7 +85,7 @@ async function acquireOpamUnix() {
"opam",
"opam",
version,
architecture,
ARCHITECTURE,
);
core.info(`Successfully cached opam to ${cachedPath}`);
await fs.chmod(`${cachedPath}/opam`, 0o755);
Expand All @@ -104,9 +99,8 @@ async function acquireOpamUnix() {

async function installUnixSystemPackages() {
const isGitHubRunner = process.env["ImageOS"] !== undefined;
const platform = getPlatform();
if (isGitHubRunner) {
if (platform === Platform.Linux) {
if (PLATFORM === "linux") {
const { version: systemVersion } = await getSystemIdentificationInfo();
if (systemVersion === "18.04") {
// [info]: musl-tools bug in ubuntu 18.04;
Expand All @@ -125,7 +119,7 @@ async function installUnixSystemPackages() {
"musl-tools",
"rsync",
]);
} else if (platform === Platform.MacOS) {
} else if (PLATFORM === "macos") {
await exec("brew", ["install", "darcs", "gpatch", "mercurial"]);
}
}
Expand Down Expand Up @@ -275,8 +269,7 @@ async function setupOpamWindows() {
}

export async function setupOpam() {
const platform = getPlatform();
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
await setupOpamWindows();
} else {
await setupOpamUnix();
Expand All @@ -285,8 +278,7 @@ export async function setupOpam() {

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

export async function repositoryAddAll(repositories: [string, string][]) {
await core.group("Initialise the opam repositories", async () => {
const platform = getPlatform();
let restore_autocrlf;
// Works around the lack of https://github.com/ocaml/opam/pull/3882 when
// adding ocaml/opam-repository on Windows. Can be removed when the action
// switches to opam 2.2
if (platform === Platform.Win32) {
if (PLATFORM === "win32") {
const autocrlf = await getExecOutput(
"git",
["config", "--global", "core.autocrlf"],
Expand Down
Loading

0 comments on commit b4b08ec

Please sign in to comment.