|
1 | 1 | import { tmpdir } from "os" |
2 | | -import path, { join } from "path" |
3 | | -import { mkdirP } from "@actions/io" |
| 2 | +import { join } from "path" |
4 | 3 | import { addPath } from "envosman" |
5 | 4 | import { execaSync } from "execa" |
6 | | -import { readFile } from "fs/promises" |
7 | 5 | import { dirname } from "patha" |
8 | 6 | import which from "which" |
9 | 7 | import { rcOptions } from "../cli-options.js" |
| 8 | +import { HttpClient } from "@actions/http-client" |
| 9 | +import { writeFile } from "fs/promises" |
10 | 10 |
|
11 | 11 | /* eslint-disable require-atomic-updates */ |
12 | 12 | let binDir: string | undefined |
13 | 13 |
|
14 | 14 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
15 | 15 | export async function setupBrew(_version: string, _setupDir: string, _arch: string) { |
| 16 | + // brew is only available on darwin and linux |
16 | 17 | if (!["darwin", "linux"].includes(process.platform)) { |
17 | 18 | return undefined |
18 | 19 | } |
| 20 | + |
| 21 | + // check if the function has already been called |
19 | 22 | if (typeof binDir === "string") { |
20 | 23 | return { binDir } |
21 | 24 | } |
22 | 25 |
|
23 | | - const maybeBinDir = which.sync("brew", { nothrow: true }) |
| 26 | + // check if brew is already installed |
| 27 | + const maybeBinDir = await which("brew", { nothrow: true }) |
24 | 28 | if (maybeBinDir !== null) { |
25 | 29 | binDir = dirname(maybeBinDir) |
26 | 30 | return { binDir } |
27 | 31 | } |
28 | 32 |
|
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") |
37 | 35 |
|
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}`) |
41 | 40 | } |
42 | 41 |
|
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], { |
44 | 46 | stdio: "inherit", |
45 | 47 | env: { |
46 | 48 | NONINTERACTIVE: "1", |
47 | 49 | }, |
48 | 50 | }) |
49 | 51 |
|
| 52 | + // add the bin directory to the PATH |
50 | 53 | binDir = getBrewPath() |
51 | 54 | await addPath(binDir, rcOptions) |
52 | 55 |
|
53 | 56 | return { binDir } |
54 | 57 | } |
55 | 58 |
|
| 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 | + */ |
56 | 65 | 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 | + |
57 | 74 | if (process.platform === "linux") { |
58 | 75 | return "/home/linuxbrew/.linuxbrew/bin/" |
59 | | - } else { |
60 | | - return "/usr/local/bin/" |
61 | 76 | } |
| 77 | + |
| 78 | + throw new Error("Unsupported platform for brew") |
62 | 79 | } |
0 commit comments