fix: use http client for downloading brew + fix brew ARM path

This commit is contained in:
Amin Yahyaabadi 2024-08-16 23:21:05 -07:00
parent d3b2f3531c
commit 1a9cdb35d8
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
7 changed files with 119 additions and 102 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,62 +1,79 @@
import { tmpdir } from "os" import { tmpdir } from "os"
import path, { join } from "path" import { join } from "path"
import { mkdirP } from "@actions/io"
import { addPath } from "envosman" import { addPath } from "envosman"
import { execaSync } from "execa" import { execaSync } from "execa"
import { readFile } from "fs/promises"
import { dirname } from "patha" import { dirname } from "patha"
import which from "which" import which from "which"
import { rcOptions } from "../cli-options.js" import { rcOptions } from "../cli-options.js"
import { HttpClient } from "@actions/http-client"
import { writeFile } from "fs/promises"
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
let binDir: string | undefined let binDir: string | undefined
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupBrew(_version: string, _setupDir: string, _arch: string) { export async function setupBrew(_version: string, _setupDir: string, _arch: string) {
// brew is only available on darwin and linux
if (!["darwin", "linux"].includes(process.platform)) { if (!["darwin", "linux"].includes(process.platform)) {
return undefined return undefined
} }
// check if the function has already been called
if (typeof binDir === "string") { if (typeof binDir === "string") {
return { binDir } return { binDir }
} }
const maybeBinDir = which.sync("brew", { nothrow: true }) // check if brew is already installed
const maybeBinDir = await which("brew", { nothrow: true })
if (maybeBinDir !== null) { if (maybeBinDir !== null) {
binDir = dirname(maybeBinDir) binDir = dirname(maybeBinDir)
return { binDir } return { binDir }
} }
// brew is not thread-safe // download the installation script
const brewTempDirectory = path.join(tmpdir(), "setup-cpp", "brew") const installerPath = join(tmpdir(), "install-brew.sh")
await mkdirP(brewTempDirectory)
execaSync("curl", ["-LJO", "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"], { const http = new HttpClient("setup-brew")
cwd: brewTempDirectory, const response = await http.get("https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh")
}) if (response.message.statusCode !== 200) {
const installSh = join(brewTempDirectory, "install.sh") throw new Error(`Failed to download brew installation script: ${response.message.statusCode}`)
if (process.platform === "linux") {
const installShContent = await readFile(installSh, "utf-8")
installShContent.replace("#!/bin/bash", "")
} }
execaSync("/bin/bash", [installSh], { await writeFile(installerPath, await response.readBody())
// brew installation is not thread-safe
execaSync("/bin/bash", [installerPath], {
stdio: "inherit", stdio: "inherit",
env: { env: {
NONINTERACTIVE: "1", NONINTERACTIVE: "1",
}, },
}) })
// add the bin directory to the PATH
binDir = getBrewPath() binDir = getBrewPath()
await addPath(binDir, rcOptions) await addPath(binDir, rcOptions)
return { binDir } return { binDir }
} }
/**
* Get the path where brew is installed
* @returns {string} The path where brew is installed
*
* Based on the installation script from https://brew.sh
*/
export function getBrewPath() { export function getBrewPath() {
if (process.platform === "linux") { if (process.platform === "darwin") {
return "/home/linuxbrew/.linuxbrew/bin/" if (process.arch === "arm64") {
return "/opt/homebrew/bin/"
} else { } else {
return "/usr/local/bin/" return "/usr/local/bin/"
} }
}
if (process.platform === "linux") {
return "/home/linuxbrew/.linuxbrew/bin/"
}
throw new Error("Unsupported platform for brew")
} }