From 7957d7f52adc4699ab5a2e8942ecc5d55f6beecb Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Thu, 16 Sep 2021 03:26:53 -0500 Subject: [PATCH] feat: add setupBrew --- src/brew/__tests__/brew.test.ts | 17 +++++++++++++++++ src/brew/brew.ts | 20 ++++++++++++++++++++ src/main.ts | 7 +++++++ src/utils/setup/setupBrewPack.ts | 9 +++++++++ 4 files changed, 53 insertions(+) create mode 100644 src/brew/__tests__/brew.test.ts create mode 100644 src/brew/brew.ts diff --git a/src/brew/__tests__/brew.test.ts b/src/brew/__tests__/brew.test.ts new file mode 100644 index 00000000..079127be --- /dev/null +++ b/src/brew/__tests__/brew.test.ts @@ -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) + }) +}) diff --git a/src/brew/brew.ts b/src/brew/brew.ts new file mode 100644 index 00000000..1b38caed --- /dev/null +++ b/src/brew/brew.ts @@ -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`) + } +} diff --git a/src/main.ts b/src/main.ts index f482848b..a354270b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { await setupChocolatey() } + // setup brew + const brewVersion = maybeGetInput("brew") + if (brewVersion !== undefined) { + await setupBrew() + } + // setup msvc const msvcVersion = maybeGetInput("msvc") if (msvcVersion !== undefined) { diff --git a/src/utils/setup/setupBrewPack.ts b/src/utils/setup/setupBrewPack.ts index f2f9675d..2b29db13 100644 --- a/src/utils/setup/setupBrewPack.ts +++ b/src/utils/setup/setupBrewPack.ts @@ -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) {