From ffd7014e286361276a022b2589eb9e8b62a12316 Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Thu, 17 Oct 2024 05:21:34 +0900 Subject: [PATCH] Better optional dependencies management Signed-off-by: Sora Morimoto --- analysis/dist/index.js | 4 +-- dist/index.js | 46 +++++++++++++++------------ dist/post/index.js | 8 ++--- lint-fmt/dist/index.js | 4 +-- packages/analysis/src/analysis.ts | 4 +-- packages/lint-fmt/src/index.ts | 4 +-- packages/lint-fmt/src/ocamlformat.ts | 2 +- packages/setup-ocaml/src/cache.ts | 8 ++--- packages/setup-ocaml/src/installer.ts | 4 +-- packages/setup-ocaml/src/opam.ts | 4 +-- packages/setup-ocaml/src/packages.ts | 2 +- packages/setup-ocaml/src/unix.ts | 31 ++++++++++++------ packages/setup-ocaml/src/version.ts | 4 +-- packages/setup-ocaml/src/windows.ts | 4 +-- 14 files changed, 73 insertions(+), 56 deletions(-) diff --git a/analysis/dist/index.js b/analysis/dist/index.js index 6f818443..f780f064 100644 --- a/analysis/dist/index.js +++ b/analysis/dist/index.js @@ -35972,7 +35972,7 @@ function createBuildTarget(output, filePath) { -async function getOpamLocalPackages() { +async function retrieveOpamLocalPackages() { const globber = await glob.create("*.opam"); const fpaths = await globber.glob(); return fpaths; @@ -35983,7 +35983,7 @@ async function analysis() { url: "https://github.com/ocaml/setup-ocaml/tree/master/analysis", version: "0.0.0", }); - const fpaths = await getOpamLocalPackages(); + const fpaths = await retrieveOpamLocalPackages(); for (const fpath of fpaths) { const temp = await promises_namespaceObject.mkdtemp(external_node_path_namespaceObject.join(external_node_os_namespaceObject.tmpdir(), "setup-ocaml-opam-tree-")); const tempJson = external_node_path_namespaceObject.join(temp, "tmp.json"); diff --git a/dist/index.js b/dist/index.js index d2b6c251..8bca2ebe 100644 --- a/dist/index.js +++ b/dist/index.js @@ -137872,7 +137872,7 @@ var semver = __nccwpck_require__(90084); function isSemverValidRange(semverVersion) { return semver.validRange(semverVersion, { loose: true }) !== null; } -async function getAllCompilerVersions() { +async function retrieveAllCompilerVersions() { const octokit = lib_github.getOctokit(constants_GITHUB_TOKEN); const { data: packages } = await octokit.rest.repos.getContent({ owner: "ocaml", @@ -137900,7 +137900,7 @@ async function getAllCompilerVersions() { return [...versions]; } async function resolveVersion(semverVersion) { - const compilerVersions = await getAllCompilerVersions(); + const compilerVersions = await retrieveAllCompilerVersions(); const matchedFullCompilerVersion = semver.maxSatisfying(compilerVersions, semverVersion, { loose: true }); if (matchedFullCompilerVersion === null) { throw new Error(`No OCaml base compiler packages matched the version ${semverVersion} in the opam-repository.`); @@ -138041,7 +138041,7 @@ var tool_cache = __nccwpck_require__(60532); -async function checkInstallability(packageName) { +async function checkAptInstallability(packageName) { const output = await (0,lib_exec.getExecOutput)("sudo", [ "apt-cache", "search", @@ -138050,17 +138050,24 @@ async function checkInstallability(packageName) { ]); return output.stdout.length !== 0; } +async function retrieveInstallableOptionalDependencies(optionalDependencies) { + switch (PLATFORM) { + case "linux": { + return optionalDependencies.filter(async (dep) => await checkAptInstallability(dep)); + } + default: { + return []; + } + } +} async function installUnixSystemPackages() { const isGitHubRunner = external_node_process_.env.GITHUB_ACTIONS === "true"; + const optionalDependencies = await retrieveInstallableOptionalDependencies([ + "darcs", + "mercurial", + ]); if (isGitHubRunner) { if (PLATFORM === "linux") { - const darcs = await (async () => { - const installability = await checkInstallability("darcs"); - if (installability) { - return ["darcs"]; - } - return []; - })(); await (0,lib_exec.exec)("sudo", [ "apt-get", "--yes", @@ -138068,10 +138075,9 @@ async function installUnixSystemPackages() { "bubblewrap", "g++-multilib", "gcc-multilib", - "mercurial", "musl-tools", "rsync", - ...darcs, + ...optionalDependencies, ]); } else if (PLATFORM === "macos") { @@ -138101,7 +138107,7 @@ async function updateUnixPackageIndexFiles() { -async function getLatestOpamRelease() { +async function retrieveLatestOpamRelease() { const semverRange = ALLOW_PRERELEASE_OPAM ? "*" : "<2.3.0"; const octokit = lib_github.getOctokit(constants_GITHUB_TOKEN); const { data: releases } = await octokit.rest.repos.listReleases({ @@ -138134,7 +138140,7 @@ async function getLatestOpamRelease() { } async function acquireOpam() { await lib_core.group("Install opam", async () => { - const { version, browserDownloadUrl } = await getLatestOpamRelease(); + const { version, browserDownloadUrl } = await retrieveLatestOpamRelease(); const cachedPath = tool_cache.find("opam", version, ARCHITECTURE); const opam = PLATFORM !== "windows" ? "opam" : "opam.exe"; if (cachedPath === "") { @@ -156704,7 +156710,7 @@ async function fromURL(url, options = {}) { function createHttpClient() { return new http_client_lib.HttpClient("OCamlBot (+https://github.com/ocaml/setup-ocaml)", [], { allowRetries: true, maxRetries: 5 }); } -async function getCygwinVersion() { +async function retrieveCygwinVersion() { const httpClient = createHttpClient(); const response = await httpClient.get("https://www.cygwin.com"); const body = await response.readBody(); @@ -156749,7 +156755,7 @@ async function setGitToIgnoreCygwinLocalPackageDirectory() { async function setupCygwin() { await lib_core.group("Prepare the Cygwin environment", async () => { await setGitToIgnoreCygwinLocalPackageDirectory(); - const version = await getCygwinVersion(); + const version = await retrieveCygwinVersion(); const cachedPath = tool_cache.find("cygwin", version, "x86_64"); if (cachedPath === "") { const downloadedPath = await tool_cache.downloadTool("https://cygwin.com/setup-x86_64.exe"); @@ -156797,7 +156803,7 @@ async function setupCygwin() { async function composeCygwinCacheKeys() { - const cygwinVersion = await getCygwinVersion(); + const cygwinVersion = await retrieveCygwinVersion(); const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${cygwinVersion}`; const restoreKeys = [key]; return { key, restoreKeys }; @@ -156817,7 +156823,7 @@ async function composeDuneCacheKeys() { return { key, restoreKeys }; } async function composeOpamCacheKeys() { - const { version: opamVersion } = await getLatestOpamRelease(); + const { version: opamVersion } = await retrieveLatestOpamRelease(); const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox"; const ocamlCompiler = await RESOLVED_COMPILER; const repositoryUrls = OPAM_REPOSITORIES.map(([_, value]) => value).join(","); @@ -156993,7 +156999,7 @@ var glob = __nccwpck_require__(72922); ;// CONCATENATED MODULE: ./src/packages.ts -async function getOpamLocalPackages() { +async function retrieveOpamLocalPackages() { const globber = await glob.create(OPAM_LOCAL_PACKAGES); const fpaths = await globber.glob(); return fpaths; @@ -157063,7 +157069,7 @@ async function installer() { lib_core.exportVariable("DUNE_CACHE_TRANSPORT", "direct"); } lib_core.exportVariable("CLICOLOR_FORCE", "1"); - const fnames = await getOpamLocalPackages(); + const fnames = await retrieveOpamLocalPackages(); if (fnames.length > 0) { if (OPAM_PIN) { await pin(fnames); diff --git a/dist/post/index.js b/dist/post/index.js index 7caf9cd5..4634e2b2 100644 --- a/dist/post/index.js +++ b/dist/post/index.js @@ -102957,7 +102957,7 @@ var semver = __nccwpck_require__(84); function isSemverValidRange(semverVersion) { return semver.validRange(semverVersion, { loose: true }) !== null; } -async function getAllCompilerVersions() { +async function retrieveAllCompilerVersions() { const octokit = lib_github.getOctokit(GITHUB_TOKEN); const { data: packages } = await octokit.rest.repos.getContent({ owner: "ocaml", @@ -102985,7 +102985,7 @@ async function getAllCompilerVersions() { return [...versions]; } async function resolveVersion(semverVersion) { - const compilerVersions = await getAllCompilerVersions(); + const compilerVersions = await retrieveAllCompilerVersions(); const matchedFullCompilerVersion = semver.maxSatisfying(compilerVersions, semverVersion, { loose: true }); if (matchedFullCompilerVersion === null) { throw new Error(`No OCaml base compiler packages matched the version ${semverVersion} in the opam-repository.`); @@ -103130,7 +103130,7 @@ const constants_RESOLVED_COMPILER = (async () => { async function composeCygwinCacheKeys() { - const cygwinVersion = await getCygwinVersion(); + const cygwinVersion = await retrieveCygwinVersion(); const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${cygwinVersion}`; const restoreKeys = [key]; return { key, restoreKeys }; @@ -103150,7 +103150,7 @@ async function composeDuneCacheKeys() { return { key, restoreKeys }; } async function composeOpamCacheKeys() { - const { version: opamVersion } = await getLatestOpamRelease(); + const { version: opamVersion } = await retrieveLatestOpamRelease(); const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox"; const ocamlCompiler = await RESOLVED_COMPILER; const repositoryUrls = OPAM_REPOSITORIES.map(([_, value]) => value).join(","); diff --git a/lint-fmt/dist/index.js b/lint-fmt/dist/index.js index ee39879a..630843b5 100644 --- a/lint-fmt/dist/index.js +++ b/lint-fmt/dist/index.js @@ -27457,7 +27457,7 @@ async function parse() { return; } } -async function getOcamlformatVersion() { +async function retrieveOcamlformatVersion() { const config = await parse(); if (config === undefined) { core.warning(".ocamlformat file is not found"); @@ -27492,7 +27492,7 @@ async function installDune() { async function run() { try { - const version = await getOcamlformatVersion(); + const version = await retrieveOcamlformatVersion(); if (version) { await installOcamlformat(version); } diff --git a/packages/analysis/src/analysis.ts b/packages/analysis/src/analysis.ts index 0bed6235..27bdae6a 100644 --- a/packages/analysis/src/analysis.ts +++ b/packages/analysis/src/analysis.ts @@ -11,7 +11,7 @@ import { import type { Output } from "./opam-detector.js"; import { createBuildTarget } from "./opam-detector.js"; -async function getOpamLocalPackages() { +async function retrieveOpamLocalPackages() { const globber = await glob.create("*.opam"); const fpaths = await globber.glob(); return fpaths; @@ -23,7 +23,7 @@ export async function analysis() { url: "https://github.com/ocaml/setup-ocaml/tree/master/analysis", version: "0.0.0", }); - const fpaths = await getOpamLocalPackages(); + const fpaths = await retrieveOpamLocalPackages(); for (const fpath of fpaths) { const temp = await fs.mkdtemp( path.join(os.tmpdir(), "setup-ocaml-opam-tree-"), diff --git a/packages/lint-fmt/src/index.ts b/packages/lint-fmt/src/index.ts index aa84f51b..656f22b6 100644 --- a/packages/lint-fmt/src/index.ts +++ b/packages/lint-fmt/src/index.ts @@ -1,12 +1,12 @@ import * as process from "node:process"; import * as core from "@actions/core"; import { checkFmt } from "./lint.js"; -import { getOcamlformatVersion } from "./ocamlformat.js"; +import { retrieveOcamlformatVersion } from "./ocamlformat.js"; import { installDune, installOcamlformat } from "./opam.js"; async function run() { try { - const version = await getOcamlformatVersion(); + const version = await retrieveOcamlformatVersion(); if (version) { await installOcamlformat(version); } diff --git a/packages/lint-fmt/src/ocamlformat.ts b/packages/lint-fmt/src/ocamlformat.ts index 2d6e98d6..019aec90 100644 --- a/packages/lint-fmt/src/ocamlformat.ts +++ b/packages/lint-fmt/src/ocamlformat.ts @@ -22,7 +22,7 @@ async function parse() { } } -export async function getOcamlformatVersion() { +export async function retrieveOcamlformatVersion() { const config = await parse(); if (config === undefined) { core.warning(".ocamlformat file is not found"); diff --git a/packages/setup-ocaml/src/cache.ts b/packages/setup-ocaml/src/cache.ts index 7904c582..c5d10b20 100644 --- a/packages/setup-ocaml/src/cache.ts +++ b/packages/setup-ocaml/src/cache.ts @@ -18,11 +18,11 @@ import { PLATFORM, RESOLVED_COMPILER, } from "./constants.js"; -import { getLatestOpamRelease } from "./opam.js"; -import { getCygwinVersion } from "./windows.js"; +import { retrieveLatestOpamRelease } from "./opam.js"; +import { retrieveCygwinVersion } from "./windows.js"; async function composeCygwinCacheKeys() { - const cygwinVersion = await getCygwinVersion(); + const cygwinVersion = await retrieveCygwinVersion(); const key = `${CACHE_PREFIX}-setup-ocaml-cygwin-${cygwinVersion}`; const restoreKeys = [key]; return { key, restoreKeys }; @@ -44,7 +44,7 @@ async function composeDuneCacheKeys() { } async function composeOpamCacheKeys() { - const { version: opamVersion } = await getLatestOpamRelease(); + const { version: opamVersion } = await retrieveLatestOpamRelease(); const sandbox = OPAM_DISABLE_SANDBOXING ? "nosandbox" : "sandbox"; const ocamlCompiler = await RESOLVED_COMPILER; const repositoryUrls = OPAM_REPOSITORIES.map(([_, value]) => value).join(","); diff --git a/packages/setup-ocaml/src/installer.ts b/packages/setup-ocaml/src/installer.ts index 9f38d94a..26f65490 100644 --- a/packages/setup-ocaml/src/installer.ts +++ b/packages/setup-ocaml/src/installer.ts @@ -26,7 +26,7 @@ import { repositoryRemoveAll, setupOpam, } from "./opam.js"; -import { getOpamLocalPackages } from "./packages.js"; +import { retrieveOpamLocalPackages } from "./packages.js"; import { setupCygwin } from "./windows.js"; export async function installer() { @@ -85,7 +85,7 @@ export async function installer() { core.exportVariable("DUNE_CACHE_TRANSPORT", "direct"); } core.exportVariable("CLICOLOR_FORCE", "1"); - const fnames = await getOpamLocalPackages(); + const fnames = await retrieveOpamLocalPackages(); if (fnames.length > 0) { if (OPAM_PIN) { await pin(fnames); diff --git a/packages/setup-ocaml/src/opam.ts b/packages/setup-ocaml/src/opam.ts index d620d272..6f1bfc6d 100644 --- a/packages/setup-ocaml/src/opam.ts +++ b/packages/setup-ocaml/src/opam.ts @@ -18,7 +18,7 @@ import { updateUnixPackageIndexFiles, } from "./unix.js"; -export async function getLatestOpamRelease() { +export async function retrieveLatestOpamRelease() { const semverRange = ALLOW_PRERELEASE_OPAM ? "*" : "<2.3.0"; const octokit = github.getOctokit(GITHUB_TOKEN); const { data: releases } = await octokit.rest.repos.listReleases({ @@ -62,7 +62,7 @@ export async function getLatestOpamRelease() { async function acquireOpam() { await core.group("Install opam", async () => { - const { version, browserDownloadUrl } = await getLatestOpamRelease(); + const { version, browserDownloadUrl } = await retrieveLatestOpamRelease(); const cachedPath = toolCache.find("opam", version, ARCHITECTURE); const opam = PLATFORM !== "windows" ? "opam" : "opam.exe"; if (cachedPath === "") { diff --git a/packages/setup-ocaml/src/packages.ts b/packages/setup-ocaml/src/packages.ts index a3e1ba1e..5070d147 100644 --- a/packages/setup-ocaml/src/packages.ts +++ b/packages/setup-ocaml/src/packages.ts @@ -1,7 +1,7 @@ import * as glob from "@actions/glob"; import { OPAM_LOCAL_PACKAGES } from "./constants.js"; -export async function getOpamLocalPackages() { +export async function retrieveOpamLocalPackages() { const globber = await glob.create(OPAM_LOCAL_PACKAGES); const fpaths = await globber.glob(); return fpaths; diff --git a/packages/setup-ocaml/src/unix.ts b/packages/setup-ocaml/src/unix.ts index 8b45c0e8..8a7a1d9f 100644 --- a/packages/setup-ocaml/src/unix.ts +++ b/packages/setup-ocaml/src/unix.ts @@ -2,7 +2,7 @@ import * as process from "node:process"; import { exec, getExecOutput } from "@actions/exec"; import { PLATFORM } from "./constants.js"; -async function checkInstallability(packageName: string) { +async function checkAptInstallability(packageName: string) { const output = await getExecOutput("sudo", [ "apt-cache", "search", @@ -12,17 +12,29 @@ async function checkInstallability(packageName: string) { return output.stdout.length !== 0; } +async function retrieveInstallableOptionalDependencies( + optionalDependencies: string[], +) { + switch (PLATFORM) { + case "linux": { + return optionalDependencies.filter( + async (dep) => await checkAptInstallability(dep), + ); + } + default: { + return []; + } + } +} + export async function installUnixSystemPackages() { const isGitHubRunner = process.env.GITHUB_ACTIONS === "true"; + const optionalDependencies = await retrieveInstallableOptionalDependencies([ + "darcs", + "mercurial", + ]); if (isGitHubRunner) { if (PLATFORM === "linux") { - const darcs = await (async () => { - const installability = await checkInstallability("darcs"); - if (installability) { - return ["darcs"]; - } - return []; - })(); await exec("sudo", [ "apt-get", "--yes", @@ -30,10 +42,9 @@ export async function installUnixSystemPackages() { "bubblewrap", "g++-multilib", "gcc-multilib", - "mercurial", "musl-tools", "rsync", - ...darcs, + ...optionalDependencies, ]); } else if (PLATFORM === "macos") { await exec("brew", ["install", "darcs", "gpatch", "mercurial"]); diff --git a/packages/setup-ocaml/src/version.ts b/packages/setup-ocaml/src/version.ts index 39b42e46..dc03875d 100644 --- a/packages/setup-ocaml/src/version.ts +++ b/packages/setup-ocaml/src/version.ts @@ -7,7 +7,7 @@ function isSemverValidRange(semverVersion: string) { return semver.validRange(semverVersion, { loose: true }) !== null; } -async function getAllCompilerVersions() { +async function retrieveAllCompilerVersions() { const octokit = github.getOctokit(GITHUB_TOKEN); const { data: packages } = await octokit.rest.repos.getContent({ owner: "ocaml", @@ -37,7 +37,7 @@ async function getAllCompilerVersions() { } async function resolveVersion(semverVersion: string) { - const compilerVersions = await getAllCompilerVersions(); + const compilerVersions = await retrieveAllCompilerVersions(); const matchedFullCompilerVersion = semver.maxSatisfying( compilerVersions, semverVersion, diff --git a/packages/setup-ocaml/src/windows.ts b/packages/setup-ocaml/src/windows.ts index 42444668..0f046594 100644 --- a/packages/setup-ocaml/src/windows.ts +++ b/packages/setup-ocaml/src/windows.ts @@ -22,7 +22,7 @@ function createHttpClient() { ); } -export async function getCygwinVersion() { +export async function retrieveCygwinVersion() { const httpClient = createHttpClient(); const response = await httpClient.get("https://www.cygwin.com"); const body = await response.readBody(); @@ -71,7 +71,7 @@ async function setGitToIgnoreCygwinLocalPackageDirectory() { export async function setupCygwin() { await core.group("Prepare the Cygwin environment", async () => { await setGitToIgnoreCygwinLocalPackageDirectory(); - const version = await getCygwinVersion(); + const version = await retrieveCygwinVersion(); const cachedPath = toolCache.find("cygwin", version, "x86_64"); if (cachedPath === "") { const downloadedPath = await toolCache.downloadTool(