feat: add setupBrew

This commit is contained in:
Amin Yahyaabadi 2021-09-16 03:26:53 -05:00
parent 1cad859704
commit 7957d7f52a
4 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import { setupBrew } from "../brew"
import { spawnSync as spawn } from "child_process"
jest.setTimeout(200000)
describe("setup-brew", () => {
it("should setup brew", async () => {
if (process.platform !== "darwin") {
return
}
await setupBrew()
const { status } = spawn("brew", ["--version"], {
encoding: "utf8",
})
expect(status).toBe(0)
})
})

20
src/brew/brew.ts Normal file
View File

@ -0,0 +1,20 @@
import { exec } from "@actions/exec"
import which from "which"
export async function setupBrew() {
if (!["darwin", "linux"].includes(process.platform)) {
return
}
if (which.sync("brew", { nothrow: true }) !== null) {
return
}
const exit = await exec(
`/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"`
)
if (exit !== 0) {
throw new Error(`Failed to install brew`)
}
}

View File

@ -1,4 +1,5 @@
import * as core from "@actions/core"
import { setupBrew } from "./brew/brew"
import { setupChocolatey } from "./chocolatey/chocolatey"
import { setupCmake } from "./cmake/cmake"
import { setupConan } from "./conan/conan"
@ -69,6 +70,12 @@ export async function main(): Promise<number> {
await setupChocolatey()
}
// setup brew
const brewVersion = maybeGetInput("brew")
if (brewVersion !== undefined) {
await setupBrew()
}
// setup msvc
const msvcVersion = maybeGetInput("msvc")
if (msvcVersion !== undefined) {

View File

@ -1,8 +1,17 @@
/* eslint-disable require-atomic-updates */
import { exec } from "@actions/exec"
import which from "which"
import { setupBrew } from "../../brew/brew"
let hasBrew = false
/** A function that installs a package using brew */
export async function setupBrewPack(name: string, version?: string) {
if (!hasBrew || which.sync("brew", { nothrow: true }) === null) {
await setupBrew()
hasBrew = true
}
const exit = await exec("brew", ["install", version !== undefined ? `${name}@${version}` : name])
if (exit !== 0) {