Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use Node http client for downloading instead of curl + fix brew ARM #262

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions dist/actions/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/actions/setup-cpp.js.map

Large diffs are not rendered by default.

72 changes: 36 additions & 36 deletions dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

72 changes: 36 additions & 36 deletions dist/modern/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@
"untildify-user": "workspace:*",
"util.types": "^0.0.2",
"web-streams-polyfill": "^4.0.0",
"which": "^4.0.0"
"which": "^4.0.0",
"node-downloader-helper": "2.1.9"
},
"productionDependencies": [
"@actions/core",
Expand All @@ -148,6 +149,7 @@
"micro-memoize",
"mri",
"msvc-dev-cmd",
"node-downloader-helper",
"numerous",
"envosman",
"path-exists",
Expand Down
3 changes: 2 additions & 1 deletion packages/setup-apt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"envosman": "workspace:*",
"which": "4.0.0",
"execa": "^7.2.0",
"escape-string-regexp": "^5.0.0"
"escape-string-regexp": "^5.0.0",
"node-downloader-helper": "2.1.9"
},
"engines": {
"node": ">=12"
Expand Down
13 changes: 10 additions & 3 deletions packages/setup-apt/src/apt-key.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { tmpdir } from "os"
import { execRoot, execRootSync } from "admina"
import { warning } from "ci-log"
import { execa } from "execa"
import { DownloaderHelper } from "node-downloader-helper"
import { pathExists } from "path-exists"
import { installAptPack } from "./install.js"

Expand Down Expand Up @@ -53,8 +54,14 @@ export async function addAptKeyViaDownload(name: string, url: string) {
const fileName = `/etc/apt/trusted.gpg.d/${name}`
if (!(await pathExists(fileName))) {
initGpg()
await installAptPack([{ name: "curl" }, { name: "ca-certificates" }], undefined)
await execa("curl", ["-s", url, "-o", `/tmp/${name}`])

await installAptPack([{ name: "ca-certificates" }])
const dl = new DownloaderHelper(url, tmpdir(), { fileName: name })
dl.on("error", (err) => {
throw new Error(`Failed to download ${url}: ${err}`)
})
await dl.start()

execRootSync("gpg", ["--no-default-keyring", "--keyring", `gnupg-ring:${fileName}`, "--import", `/tmp/${name}`])
execRootSync("chmod", ["644", fileName])
}
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 34 additions & 19 deletions src/brew/brew.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { tmpdir } from "os"
import path, { join } from "path"
import { mkdirP } from "@actions/io"
import { addPath } from "envosman"
import { execaSync } from "execa"
import { readFile } from "fs/promises"
import { DownloaderHelper } from "node-downloader-helper"
import { dirname } from "patha"
import { installAptPack } from "setup-apt"
import which from "which"
import { rcOptions } from "../cli-options.js"

Expand All @@ -13,50 +12,66 @@ 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)

execaSync("curl", ["-LJO", "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"], {
cwd: brewTempDirectory,
// download the installation script
await installAptPack([{ name: "ca-certificates" }])
const dl = new DownloaderHelper("https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh", tmpdir(), {
fileName: "install-brew.sh",
})
const installSh = join(brewTempDirectory, "install.sh")

if (process.platform === "linux") {
const installShContent = await readFile(installSh, "utf-8")
installShContent.replace("#!/bin/bash", "")
}
dl.on("error", (err) => {
throw new Error(`Failed to download the brew installer script: ${err}`)
})
await dl.start()

execaSync("/bin/bash", [installSh], {
// brew installation is not thread-safe
execaSync("/bin/bash", [dl.getDownloadPath()], {
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 === "darwin") {
if (process.arch === "arm64") {
return "/opt/homebrew/bin/"
} else {
return "/usr/local/bin/"
}
}

if (process.platform === "linux") {
return "/home/linuxbrew/.linuxbrew/bin/"
} else {
return "/usr/local/bin/"
}

throw new Error("Unsupported platform for brew")
}
38 changes: 28 additions & 10 deletions src/llvm/llvm_installer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { info } from "console"
import { tmpdir } from "os"
import { join } from "path"
import { execRoot } from "admina"
import { addPath } from "envosman"
import { execa } from "execa"
import { chmod, readFile, writeFile } from "fs/promises"
import { DownloaderHelper } from "node-downloader-helper"
import { aptTimeout, hasNala, installAptPack, isAptPackRegexInstalled } from "setup-apt"
import { rcOptions } from "../cli-options.js"
import { DEFAULT_TIMEOUT } from "../installTool.js"
Expand All @@ -21,14 +23,27 @@ export async function setupLLVMApt(
// TODO for older versions, this also includes the minor version
const installationFolder = `/usr/lib/llvm-${majorVersion}`

await installAptPack([{ name: "curl" }])
await execa("curl", ["-LJO", "https://apt.llvm.org/llvm.sh"], { cwd: "/tmp" })
const neededPackages = await patchAptLLVMScript("/tmp/llvm.sh", "/tmp/llvm-setup-cpp.sh", majorVersion, packages)
// download the installation script
await installAptPack([{ name: "ca-certificates" }])
const dl = new DownloaderHelper("https://apt.llvm.org/llvm.sh", tmpdir(), { fileName: "llvm.sh" })
dl.on("error", (err) => {
throw new Error(`Failed to download the LLVM installer script: ${err}`)
})
await dl.start()
const installerScript = await readFile(dl.getDownloadPath(), "utf-8")

const installerPath = join(tmpdir(), "llvm-setup-cpp.sh")
const neededPackages = await patchAptLLVMScript(
installerScript,
installerPath,
majorVersion,
packages,
)
await installAptPack(neededPackages)
await chmod("/tmp/llvm-setup-cpp.sh", "755")
await chmod(installerPath, "755")
await execRoot(
"bash",
["/tmp/llvm-setup-cpp.sh", `${majorVersion}`, ...(packages === LLVMPackages.All ? ["all"] : [])],
[installerPath, `${majorVersion}`, ...(packages === LLVMPackages.All ? ["all"] : [])],
{
stdio: "inherit",
shell: true,
Expand All @@ -45,10 +60,13 @@ export async function setupLLVMApt(
}
}

async function patchAptLLVMScript(path: string, target_path: string, majorVersion: number, packages: LLVMPackages) {
let script = await readFile(path, "utf-8")

script = debugScript(script)
async function patchAptLLVMScript(
givenScript: string,
target_path: string,
majorVersion: number,
packages: LLVMPackages,
) {
let script = debugScript(givenScript)
script = nonInteractiveScript(script)
script = choosePackages(packages, script, majorVersion)
script = await removeConflictingPackages(script)
Expand Down
Loading