Skip to content

Commit 1a9cdb3

Browse files
committed
fix: use http client for downloading brew + fix brew ARM path
1 parent d3b2f35 commit 1a9cdb3

File tree

7 files changed

+119
-102
lines changed

7 files changed

+119
-102
lines changed

dist/actions/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/brew/brew.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,79 @@
11
import { tmpdir } from "os"
2-
import path, { join } from "path"
3-
import { mkdirP } from "@actions/io"
2+
import { join } from "path"
43
import { addPath } from "envosman"
54
import { execaSync } from "execa"
6-
import { readFile } from "fs/promises"
75
import { dirname } from "patha"
86
import which from "which"
97
import { rcOptions } from "../cli-options.js"
8+
import { HttpClient } from "@actions/http-client"
9+
import { writeFile } from "fs/promises"
1010

1111
/* eslint-disable require-atomic-updates */
1212
let binDir: string | undefined
1313

1414
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1515
export async function setupBrew(_version: string, _setupDir: string, _arch: string) {
16+
// brew is only available on darwin and linux
1617
if (!["darwin", "linux"].includes(process.platform)) {
1718
return undefined
1819
}
20+
21+
// check if the function has already been called
1922
if (typeof binDir === "string") {
2023
return { binDir }
2124
}
2225

23-
const maybeBinDir = which.sync("brew", { nothrow: true })
26+
// check if brew is already installed
27+
const maybeBinDir = await which("brew", { nothrow: true })
2428
if (maybeBinDir !== null) {
2529
binDir = dirname(maybeBinDir)
2630
return { binDir }
2731
}
2832

29-
// brew is not thread-safe
30-
const brewTempDirectory = path.join(tmpdir(), "setup-cpp", "brew")
31-
await mkdirP(brewTempDirectory)
32-
33-
execaSync("curl", ["-LJO", "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"], {
34-
cwd: brewTempDirectory,
35-
})
36-
const installSh = join(brewTempDirectory, "install.sh")
33+
// download the installation script
34+
const installerPath = join(tmpdir(), "install-brew.sh")
3735

38-
if (process.platform === "linux") {
39-
const installShContent = await readFile(installSh, "utf-8")
40-
installShContent.replace("#!/bin/bash", "")
36+
const http = new HttpClient("setup-brew")
37+
const response = await http.get("https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh")
38+
if (response.message.statusCode !== 200) {
39+
throw new Error(`Failed to download brew installation script: ${response.message.statusCode}`)
4140
}
4241

43-
execaSync("/bin/bash", [installSh], {
42+
await writeFile(installerPath, await response.readBody())
43+
44+
// brew installation is not thread-safe
45+
execaSync("/bin/bash", [installerPath], {
4446
stdio: "inherit",
4547
env: {
4648
NONINTERACTIVE: "1",
4749
},
4850
})
4951

52+
// add the bin directory to the PATH
5053
binDir = getBrewPath()
5154
await addPath(binDir, rcOptions)
5255

5356
return { binDir }
5457
}
5558

59+
/**
60+
* Get the path where brew is installed
61+
* @returns {string} The path where brew is installed
62+
*
63+
* Based on the installation script from https://brew.sh
64+
*/
5665
export function getBrewPath() {
66+
if (process.platform === "darwin") {
67+
if (process.arch === "arm64") {
68+
return "/opt/homebrew/bin/"
69+
} else {
70+
return "/usr/local/bin/"
71+
}
72+
}
73+
5774
if (process.platform === "linux") {
5875
return "/home/linuxbrew/.linuxbrew/bin/"
59-
} else {
60-
return "/usr/local/bin/"
6176
}
77+
78+
throw new Error("Unsupported platform for brew")
6279
}

0 commit comments

Comments
 (0)