Skip to content

Commit 42d0df7

Browse files
authored
Merge pull request #262 from aminya/downloader [skip ci]
2 parents d3b2f35 + 8a5b753 commit 42d0df7

File tree

12 files changed

+201
-145
lines changed

12 files changed

+201
-145
lines changed

dist/actions/setup-cpp.js

+36-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

+36-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

+36-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126
"untildify-user": "workspace:*",
127127
"util.types": "^0.0.2",
128128
"web-streams-polyfill": "^4.0.0",
129-
"which": "^4.0.0"
129+
"which": "^4.0.0",
130+
"node-downloader-helper": "2.1.9"
130131
},
131132
"productionDependencies": [
132133
"@actions/core",
@@ -148,6 +149,7 @@
148149
"micro-memoize",
149150
"mri",
150151
"msvc-dev-cmd",
152+
"node-downloader-helper",
151153
"numerous",
152154
"envosman",
153155
"path-exists",

packages/setup-apt/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"envosman": "workspace:*",
2424
"which": "4.0.0",
2525
"execa": "^7.2.0",
26-
"escape-string-regexp": "^5.0.0"
26+
"escape-string-regexp": "^5.0.0",
27+
"node-downloader-helper": "2.1.9"
2728
},
2829
"engines": {
2930
"node": ">=12"

packages/setup-apt/src/apt-key.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { tmpdir } from "os"
12
import { execRoot, execRootSync } from "admina"
23
import { warning } from "ci-log"
3-
import { execa } from "execa"
4+
import { DownloaderHelper } from "node-downloader-helper"
45
import { pathExists } from "path-exists"
56
import { installAptPack } from "./install.js"
67

@@ -53,8 +54,14 @@ export async function addAptKeyViaDownload(name: string, url: string) {
5354
const fileName = `/etc/apt/trusted.gpg.d/${name}`
5455
if (!(await pathExists(fileName))) {
5556
initGpg()
56-
await installAptPack([{ name: "curl" }, { name: "ca-certificates" }], undefined)
57-
await execa("curl", ["-s", url, "-o", `/tmp/${name}`])
57+
58+
await installAptPack([{ name: "ca-certificates" }])
59+
const dl = new DownloaderHelper(url, tmpdir(), { fileName: name })
60+
dl.on("error", (err) => {
61+
throw new Error(`Failed to download ${url}: ${err}`)
62+
})
63+
await dl.start()
64+
5865
execRootSync("gpg", ["--no-default-keyring", "--keyring", `gnupg-ring:${fileName}`, "--import", `/tmp/${name}`])
5966
execRootSync("chmod", ["644", fileName])
6067
}

pnpm-lock.yaml

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/brew/brew.ts

+34-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { tmpdir } from "os"
2-
import path, { join } from "path"
3-
import { mkdirP } from "@actions/io"
42
import { addPath } from "envosman"
53
import { execaSync } from "execa"
6-
import { readFile } from "fs/promises"
4+
import { DownloaderHelper } from "node-downloader-helper"
75
import { dirname } from "patha"
6+
import { installAptPack } from "setup-apt"
87
import which from "which"
98
import { rcOptions } from "../cli-options.js"
109

@@ -13,50 +12,66 @@ let binDir: string | undefined
1312

1413
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1514
export async function setupBrew(_version: string, _setupDir: string, _arch: string) {
15+
// brew is only available on darwin and linux
1616
if (!["darwin", "linux"].includes(process.platform)) {
1717
return undefined
1818
}
19+
20+
// check if the function has already been called
1921
if (typeof binDir === "string") {
2022
return { binDir }
2123
}
2224

23-
const maybeBinDir = which.sync("brew", { nothrow: true })
25+
// check if brew is already installed
26+
const maybeBinDir = await which("brew", { nothrow: true })
2427
if (maybeBinDir !== null) {
2528
binDir = dirname(maybeBinDir)
2629
return { binDir }
2730
}
2831

29-
// brew is not thread-safe
30-
const brewTempDirectory = path.join(tmpdir(), "setup-cpp", "brew")
31-
await mkdirP(brewTempDirectory)
32-
33-
execaSync("curl", ["-LJO", "https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh"], {
34-
cwd: brewTempDirectory,
32+
// download the installation script
33+
await installAptPack([{ name: "ca-certificates" }])
34+
const dl = new DownloaderHelper("https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh", tmpdir(), {
35+
fileName: "install-brew.sh",
3536
})
36-
const installSh = join(brewTempDirectory, "install.sh")
37-
38-
if (process.platform === "linux") {
39-
const installShContent = await readFile(installSh, "utf-8")
40-
installShContent.replace("#!/bin/bash", "")
41-
}
37+
dl.on("error", (err) => {
38+
throw new Error(`Failed to download the brew installer script: ${err}`)
39+
})
40+
await dl.start()
4241

43-
execaSync("/bin/bash", [installSh], {
42+
// brew installation is not thread-safe
43+
execaSync("/bin/bash", [dl.getDownloadPath()], {
4444
stdio: "inherit",
4545
env: {
4646
NONINTERACTIVE: "1",
4747
},
4848
})
4949

50+
// add the bin directory to the PATH
5051
binDir = getBrewPath()
5152
await addPath(binDir, rcOptions)
5253

5354
return { binDir }
5455
}
5556

57+
/**
58+
* Get the path where brew is installed
59+
* @returns {string} The path where brew is installed
60+
*
61+
* Based on the installation script from https://brew.sh
62+
*/
5663
export function getBrewPath() {
64+
if (process.platform === "darwin") {
65+
if (process.arch === "arm64") {
66+
return "/opt/homebrew/bin/"
67+
} else {
68+
return "/usr/local/bin/"
69+
}
70+
}
71+
5772
if (process.platform === "linux") {
5873
return "/home/linuxbrew/.linuxbrew/bin/"
59-
} else {
60-
return "/usr/local/bin/"
6174
}
75+
76+
throw new Error("Unsupported platform for brew")
6277
}

src/llvm/llvm_installer.ts

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { info } from "console"
2+
import { tmpdir } from "os"
3+
import { join } from "path"
24
import { execRoot } from "admina"
35
import { addPath } from "envosman"
4-
import { execa } from "execa"
56
import { chmod, readFile, writeFile } from "fs/promises"
7+
import { DownloaderHelper } from "node-downloader-helper"
68
import { aptTimeout, hasNala, installAptPack, isAptPackRegexInstalled } from "setup-apt"
79
import { rcOptions } from "../cli-options.js"
810
import { DEFAULT_TIMEOUT } from "../installTool.js"
@@ -21,14 +23,27 @@ export async function setupLLVMApt(
2123
// TODO for older versions, this also includes the minor version
2224
const installationFolder = `/usr/lib/llvm-${majorVersion}`
2325

24-
await installAptPack([{ name: "curl" }])
25-
await execa("curl", ["-LJO", "https://apt.llvm.org/llvm.sh"], { cwd: "/tmp" })
26-
const neededPackages = await patchAptLLVMScript("/tmp/llvm.sh", "/tmp/llvm-setup-cpp.sh", majorVersion, packages)
26+
// download the installation script
27+
await installAptPack([{ name: "ca-certificates" }])
28+
const dl = new DownloaderHelper("https://apt.llvm.org/llvm.sh", tmpdir(), { fileName: "llvm.sh" })
29+
dl.on("error", (err) => {
30+
throw new Error(`Failed to download the LLVM installer script: ${err}`)
31+
})
32+
await dl.start()
33+
const installerScript = await readFile(dl.getDownloadPath(), "utf-8")
34+
35+
const installerPath = join(tmpdir(), "llvm-setup-cpp.sh")
36+
const neededPackages = await patchAptLLVMScript(
37+
installerScript,
38+
installerPath,
39+
majorVersion,
40+
packages,
41+
)
2742
await installAptPack(neededPackages)
28-
await chmod("/tmp/llvm-setup-cpp.sh", "755")
43+
await chmod(installerPath, "755")
2944
await execRoot(
3045
"bash",
31-
["/tmp/llvm-setup-cpp.sh", `${majorVersion}`, ...(packages === LLVMPackages.All ? ["all"] : [])],
46+
[installerPath, `${majorVersion}`, ...(packages === LLVMPackages.All ? ["all"] : [])],
3247
{
3348
stdio: "inherit",
3449
shell: true,
@@ -45,10 +60,13 @@ export async function setupLLVMApt(
4560
}
4661
}
4762

48-
async function patchAptLLVMScript(path: string, target_path: string, majorVersion: number, packages: LLVMPackages) {
49-
let script = await readFile(path, "utf-8")
50-
51-
script = debugScript(script)
63+
async function patchAptLLVMScript(
64+
givenScript: string,
65+
target_path: string,
66+
majorVersion: number,
67+
packages: LLVMPackages,
68+
) {
69+
let script = debugScript(givenScript)
5270
script = nonInteractiveScript(script)
5371
script = choosePackages(packages, script, majorVersion)
5472
script = await removeConflictingPackages(script)

0 commit comments

Comments
 (0)