Skip to content

Commit

Permalink
Add initial support for opam 2.2 on Windows
Browse files Browse the repository at this point in the history
Co-authored-by: Kate <[email protected]>
  • Loading branch information
hhugo and kit-ty-kate committed May 17, 2024
1 parent 364fb19 commit 2baffc2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 62 deletions.
7 changes: 1 addition & 6 deletions packages/setup-ocaml/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export const CYGWIN_ROOT_BIN = path.join(CYGWIN_ROOT, "bin");

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_PRERELEASE_OPAM =
PLATFORM !== "win32" &&
core.getBooleanInput("allow-prerelease-opam", {
required: false,
trimWhitespace: true,
Expand Down Expand Up @@ -102,10 +100,7 @@ const repositories_yaml = yaml.parse(
core.getInput("opam-repositories", { required: false, trimWhitespace: true }),
) as Record<string, string> | null;

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

export const OPAM_REPOSITORIES: [string, string][] = repositories_yaml
? Object.entries(repositories_yaml).reverse()
Expand Down
4 changes: 1 addition & 3 deletions packages/setup-ocaml/src/depext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { OPAM_DEPEXT_FLAGS, PLATFORM } from "./constants.js";

export async function installDepext(ocamlVersion: string) {
await core.group("Install depext", async () => {
const depextCygwinports =
PLATFORM === "win32" ? ["depext-cygwinports"] : [];
await exec("opam", ["install", "opam-depext", ...depextCygwinports]);
await exec("opam", ["install", "opam-depext"]);
if (PLATFORM === "win32") {
let base = "";
if (ocamlVersion.includes("mingw64")) {
Expand Down
65 changes: 21 additions & 44 deletions packages/setup-ocaml/src/opam.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { promises as fs } from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import * as process from "node:process";

Expand Down Expand Up @@ -30,6 +29,7 @@ import { getCygwinVersion } from "./win32.js";
export async function getLatestOpamRelease() {
const semverRange = ALLOW_PRERELEASE_OPAM ? "*" : "<2.2.0";
const octokit = github.getOctokit(GITHUB_TOKEN);
const opam_platform = PLATFORM === "win32" ? "windows" : PLATFORM;
const { data: releases } = await octokit.rest.repos.listReleases({
owner: "ocaml",
repo: "opam",
Expand All @@ -51,7 +51,7 @@ export async function getLatestOpamRelease() {
);
}
const matchedAssets = latestRelease.assets.find((asset) =>
asset.browser_download_url.includes(`${ARCHITECTURE}-${PLATFORM}`),
asset.browser_download_url.includes(`${ARCHITECTURE}-${opam_platform}`),
);
if (!matchedAssets) {
throw new Error(
Expand All @@ -64,15 +64,6 @@ export async function getLatestOpamRelease() {
};
}

async function findOpam() {
if (PLATFORM === "win32") {
const opamPath = path.join(CYGWIN_ROOT, "bin", "opam.exe");
return opamPath;
}
const opamPath = await io.which("opam");
return opamPath;
}

async function acquireOpamUnix() {
const { version, browserDownloadUrl } = await getLatestOpamRelease();
const cachedPath = toolCache.find("opam", version, ARCHITECTURE);
Expand Down Expand Up @@ -206,25 +197,25 @@ async function setupCygwin() {
}

async function acquireOpamWindows() {
const opamVersion = "0.0.0.2";
const cachedPath = toolCache.find("opam", opamVersion);
const { version, browserDownloadUrl } = await getLatestOpamRelease();
const cachedPath = toolCache.find("opam", version, ARCHITECTURE);
if (cachedPath === "") {
const downloadedPath = await toolCache.downloadTool(
`https://github.com/fdopen/opam-repository-mingw/releases/download/${opamVersion}/opam64.zip`,
);
const extractedPath = await toolCache.extractZip(downloadedPath);
const cachedPath = await toolCache.cacheDir(
extractedPath,
const downloadedPath = await toolCache.downloadTool(browserDownloadUrl);
core.info(`Acquired ${version} from ${browserDownloadUrl}`);
const cachedPath = await toolCache.cacheFile(
downloadedPath,
"opam.exe",
"opam",
opamVersion,
version,
ARCHITECTURE,
);
const installSh = path.join(cachedPath, "opam64", "install.sh");
await fs.chmod(installSh, 0o755);
await exec("bash", [installSh, "--prefix", "/usr"]);
core.info(`Successfully cached opam to ${cachedPath}`);
await fs.chmod(`${cachedPath}/opam.exe`, 0o755);
core.addPath(cachedPath);
core.info("Added opam to the path");
} else {
const installSh = path.join(cachedPath, "opam64", "install.sh");
await fs.chmod(installSh, 0o755);
await exec("bash", [installSh, "--prefix", "/usr"]);
core.addPath(cachedPath);
core.info("Added cached opam to the path");
}
}

Expand All @@ -237,15 +228,6 @@ async function initializeOpamWindows() {
"--disable-sandboxing",
"--enable-shell-hook",
]);
await io.mkdirP(CYGWIN_ROOT_WRAPPERBIN);
const opamCmd = path.join(CYGWIN_ROOT_WRAPPERBIN, "opam.cmd");
const data = [
"@setlocal",
"@echo off",
"set PATH=%CYGWIN_ROOT_BIN%;%PATH%",
"ocaml-env exec -- opam.exe %*",
].join(os.EOL);
await fs.writeFile(opamCmd, data, { mode: 0o755 });
}

async function setupOpamWindows() {
Expand All @@ -267,7 +249,6 @@ async function setupOpamWindows() {
await core.group("Initialise the opam state", async () => {
await initializeOpamWindows();
});
process.env["PATH"] = originalPath.join(path.delimiter);
}

export async function setupOpam() {
Expand Down Expand Up @@ -308,20 +289,18 @@ export async function installOcaml(ocamlCompiler: string) {

export async function pin(fpaths: string[]) {
await core.group("Pin local packages", async () => {
const opam = await findOpam();
for (const fpath of fpaths) {
const fname = path.basename(fpath, ".opam");
const dname = path.dirname(fpath);
await exec(opam, ["pin", "add", `${fname}.dev`, ".", "--no-action"], {
await exec("opam", ["pin", "add", `${fname}.dev`, ".", "--no-action"], {
cwd: dname,
});
}
});
}

async function repositoryAdd(name: string, address: string) {
const opam = await findOpam();
await exec(opam, [
await exec("opam", [
"repository",
"add",
name,
Expand Down Expand Up @@ -369,14 +348,12 @@ export async function repositoryAddAll(repositories: [string, string][]) {
}

async function repositoryRemove(name: string) {
const opam = await findOpam();
await exec(opam, ["repository", "remove", name, "--all-switches"]);
await exec("opam", ["repository", "remove", name, "--all-switches"]);
}

async function repositoryList() {
const opam = await findOpam();
const repositoryList = await getExecOutput(
opam,
"opam",
["repository", "list", "--all-switches", "--short"],
{ ignoreReturnCode: true },
);
Expand Down
14 changes: 5 additions & 9 deletions packages/setup-ocaml/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "node:path";
import * as github from "@actions/github";
import * as semver from "semver";

import { GITHUB_TOKEN, PLATFORM } from "./constants.js";
import { GITHUB_TOKEN } from "./constants.js";

function isSemverValidRange(semverVersion: string) {
const isValidSemver =
Expand All @@ -17,11 +17,9 @@ function isSemverValidRange(semverVersion: string) {
async function getAllCompilerVersions() {
const octokit = github.getOctokit(GITHUB_TOKEN);

const owner = PLATFORM === "win32" ? "ocaml-opam" : "ocaml";
const repo =
PLATFORM === "win32" ? "opam-repository-mingw" : "opam-repository";
const prefix =
PLATFORM === "win32" ? "ocaml-variants" : "ocaml-base-compiler";
const owner = "ocaml";
const repo = "opam-repository";
const prefix = "ocaml-base-compiler";
const { data: packages } = await octokit.rest.repos.getContent({
owner,
repo,
Expand Down Expand Up @@ -61,9 +59,7 @@ async function resolveVersion(semverVersion: string) {

export async function resolveCompiler(compiler: string) {
const resolvedCompiler = isSemverValidRange(compiler)
? PLATFORM === "win32"
? `ocaml-variants.${await resolveVersion(compiler)}+mingw64c`
: `ocaml-base-compiler.${await resolveVersion(compiler)}`
? `ocaml-base-compiler.${await resolveVersion(compiler)}`
: compiler;
return resolvedCompiler;
}

0 comments on commit 2baffc2

Please sign in to comment.