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