Skip to content

Commit 2baffc2

Browse files
hhugokit-ty-kate
andcommitted
Add initial support for opam 2.2 on Windows
Co-authored-by: Kate <[email protected]>
1 parent 364fb19 commit 2baffc2

File tree

4 files changed

+28
-62
lines changed

4 files changed

+28
-62
lines changed

packages/setup-ocaml/src/constants.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ export const CYGWIN_ROOT_BIN = path.join(CYGWIN_ROOT, "bin");
4141

4242
export const CYGWIN_ROOT_WRAPPERBIN = path.join(CYGWIN_ROOT, "wrapperbin");
4343

44-
// [todo] remove the branch for Windows once opam 2.2 is released as stable.
4544
export const ALLOW_PRERELEASE_OPAM =
46-
PLATFORM !== "win32" &&
4745
core.getBooleanInput("allow-prerelease-opam", {
4846
required: false,
4947
trimWhitespace: true,
@@ -102,10 +100,7 @@ const repositories_yaml = yaml.parse(
102100
core.getInput("opam-repositories", { required: false, trimWhitespace: true }),
103101
) as Record<string, string> | null;
104102

105-
const defaultRepository =
106-
PLATFORM === "win32"
107-
? "https://github.com/ocaml-opam/opam-repository-mingw.git#sunset"
108-
: "https://github.com/ocaml/opam-repository.git";
103+
const defaultRepository = "https://github.com/ocaml/opam-repository.git";
109104

110105
export const OPAM_REPOSITORIES: [string, string][] = repositories_yaml
111106
? Object.entries(repositories_yaml).reverse()

packages/setup-ocaml/src/depext.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { OPAM_DEPEXT_FLAGS, PLATFORM } from "./constants.js";
77

88
export async function installDepext(ocamlVersion: string) {
99
await core.group("Install depext", async () => {
10-
const depextCygwinports =
11-
PLATFORM === "win32" ? ["depext-cygwinports"] : [];
12-
await exec("opam", ["install", "opam-depext", ...depextCygwinports]);
10+
await exec("opam", ["install", "opam-depext"]);
1311
if (PLATFORM === "win32") {
1412
let base = "";
1513
if (ocamlVersion.includes("mingw64")) {

packages/setup-ocaml/src/opam.ts

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { promises as fs } from "node:fs";
2-
import * as os from "node:os";
32
import * as path from "node:path";
43
import * as process from "node:process";
54

@@ -30,6 +29,7 @@ import { getCygwinVersion } from "./win32.js";
3029
export async function getLatestOpamRelease() {
3130
const semverRange = ALLOW_PRERELEASE_OPAM ? "*" : "<2.2.0";
3231
const octokit = github.getOctokit(GITHUB_TOKEN);
32+
const opam_platform = PLATFORM === "win32" ? "windows" : PLATFORM;
3333
const { data: releases } = await octokit.rest.repos.listReleases({
3434
owner: "ocaml",
3535
repo: "opam",
@@ -51,7 +51,7 @@ export async function getLatestOpamRelease() {
5151
);
5252
}
5353
const matchedAssets = latestRelease.assets.find((asset) =>
54-
asset.browser_download_url.includes(`${ARCHITECTURE}-${PLATFORM}`),
54+
asset.browser_download_url.includes(`${ARCHITECTURE}-${opam_platform}`),
5555
);
5656
if (!matchedAssets) {
5757
throw new Error(
@@ -64,15 +64,6 @@ export async function getLatestOpamRelease() {
6464
};
6565
}
6666

67-
async function findOpam() {
68-
if (PLATFORM === "win32") {
69-
const opamPath = path.join(CYGWIN_ROOT, "bin", "opam.exe");
70-
return opamPath;
71-
}
72-
const opamPath = await io.which("opam");
73-
return opamPath;
74-
}
75-
7667
async function acquireOpamUnix() {
7768
const { version, browserDownloadUrl } = await getLatestOpamRelease();
7869
const cachedPath = toolCache.find("opam", version, ARCHITECTURE);
@@ -206,25 +197,25 @@ async function setupCygwin() {
206197
}
207198

208199
async function acquireOpamWindows() {
209-
const opamVersion = "0.0.0.2";
210-
const cachedPath = toolCache.find("opam", opamVersion);
200+
const { version, browserDownloadUrl } = await getLatestOpamRelease();
201+
const cachedPath = toolCache.find("opam", version, ARCHITECTURE);
211202
if (cachedPath === "") {
212-
const downloadedPath = await toolCache.downloadTool(
213-
`https://github.com/fdopen/opam-repository-mingw/releases/download/${opamVersion}/opam64.zip`,
214-
);
215-
const extractedPath = await toolCache.extractZip(downloadedPath);
216-
const cachedPath = await toolCache.cacheDir(
217-
extractedPath,
203+
const downloadedPath = await toolCache.downloadTool(browserDownloadUrl);
204+
core.info(`Acquired ${version} from ${browserDownloadUrl}`);
205+
const cachedPath = await toolCache.cacheFile(
206+
downloadedPath,
207+
"opam.exe",
218208
"opam",
219-
opamVersion,
209+
version,
210+
ARCHITECTURE,
220211
);
221-
const installSh = path.join(cachedPath, "opam64", "install.sh");
222-
await fs.chmod(installSh, 0o755);
223-
await exec("bash", [installSh, "--prefix", "/usr"]);
212+
core.info(`Successfully cached opam to ${cachedPath}`);
213+
await fs.chmod(`${cachedPath}/opam.exe`, 0o755);
214+
core.addPath(cachedPath);
215+
core.info("Added opam to the path");
224216
} else {
225-
const installSh = path.join(cachedPath, "opam64", "install.sh");
226-
await fs.chmod(installSh, 0o755);
227-
await exec("bash", [installSh, "--prefix", "/usr"]);
217+
core.addPath(cachedPath);
218+
core.info("Added cached opam to the path");
228219
}
229220
}
230221

@@ -237,15 +228,6 @@ async function initializeOpamWindows() {
237228
"--disable-sandboxing",
238229
"--enable-shell-hook",
239230
]);
240-
await io.mkdirP(CYGWIN_ROOT_WRAPPERBIN);
241-
const opamCmd = path.join(CYGWIN_ROOT_WRAPPERBIN, "opam.cmd");
242-
const data = [
243-
"@setlocal",
244-
"@echo off",
245-
"set PATH=%CYGWIN_ROOT_BIN%;%PATH%",
246-
"ocaml-env exec -- opam.exe %*",
247-
].join(os.EOL);
248-
await fs.writeFile(opamCmd, data, { mode: 0o755 });
249231
}
250232

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

273254
export async function setupOpam() {
@@ -308,20 +289,18 @@ export async function installOcaml(ocamlCompiler: string) {
308289

309290
export async function pin(fpaths: string[]) {
310291
await core.group("Pin local packages", async () => {
311-
const opam = await findOpam();
312292
for (const fpath of fpaths) {
313293
const fname = path.basename(fpath, ".opam");
314294
const dname = path.dirname(fpath);
315-
await exec(opam, ["pin", "add", `${fname}.dev`, ".", "--no-action"], {
295+
await exec("opam", ["pin", "add", `${fname}.dev`, ".", "--no-action"], {
316296
cwd: dname,
317297
});
318298
}
319299
});
320300
}
321301

322302
async function repositoryAdd(name: string, address: string) {
323-
const opam = await findOpam();
324-
await exec(opam, [
303+
await exec("opam", [
325304
"repository",
326305
"add",
327306
name,
@@ -369,14 +348,12 @@ export async function repositoryAddAll(repositories: [string, string][]) {
369348
}
370349

371350
async function repositoryRemove(name: string) {
372-
const opam = await findOpam();
373-
await exec(opam, ["repository", "remove", name, "--all-switches"]);
351+
await exec("opam", ["repository", "remove", name, "--all-switches"]);
374352
}
375353

376354
async function repositoryList() {
377-
const opam = await findOpam();
378355
const repositoryList = await getExecOutput(
379-
opam,
356+
"opam",
380357
["repository", "list", "--all-switches", "--short"],
381358
{ ignoreReturnCode: true },
382359
);

packages/setup-ocaml/src/version.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "node:path";
33
import * as github from "@actions/github";
44
import * as semver from "semver";
55

6-
import { GITHUB_TOKEN, PLATFORM } from "./constants.js";
6+
import { GITHUB_TOKEN } from "./constants.js";
77

88
function isSemverValidRange(semverVersion: string) {
99
const isValidSemver =
@@ -17,11 +17,9 @@ function isSemverValidRange(semverVersion: string) {
1717
async function getAllCompilerVersions() {
1818
const octokit = github.getOctokit(GITHUB_TOKEN);
1919

20-
const owner = PLATFORM === "win32" ? "ocaml-opam" : "ocaml";
21-
const repo =
22-
PLATFORM === "win32" ? "opam-repository-mingw" : "opam-repository";
23-
const prefix =
24-
PLATFORM === "win32" ? "ocaml-variants" : "ocaml-base-compiler";
20+
const owner = "ocaml";
21+
const repo = "opam-repository";
22+
const prefix = "ocaml-base-compiler";
2523
const { data: packages } = await octokit.rest.repos.getContent({
2624
owner,
2725
repo,
@@ -61,9 +59,7 @@ async function resolveVersion(semverVersion: string) {
6159

6260
export async function resolveCompiler(compiler: string) {
6361
const resolvedCompiler = isSemverValidRange(compiler)
64-
? PLATFORM === "win32"
65-
? `ocaml-variants.${await resolveVersion(compiler)}+mingw64c`
66-
: `ocaml-base-compiler.${await resolveVersion(compiler)}`
62+
? `ocaml-base-compiler.${await resolveVersion(compiler)}`
6763
: compiler;
6864
return resolvedCompiler;
6965
}

0 commit comments

Comments
 (0)