fix: fix setupBin

This commit is contained in:
Amin Yahyaabadi 2021-11-21 13:54:12 -06:00
parent c6eb5ca89b
commit ec22e0200a
9 changed files with 47 additions and 21 deletions

View File

@ -6,7 +6,7 @@ jest.setTimeout(300000)
describe("setup-cmake", () => {
let directory: string
beforeAll(async () => {
directory = await setupTmpDir("setup-cmake")
directory = await setupTmpDir("cmake")
})
it("should setup CMake", async () => {
@ -21,6 +21,6 @@ describe("setup-cmake", () => {
})
afterAll(async () => {
await cleanupTmpDir("setup-cmake")
await cleanupTmpDir("cmake")
}, 100000)
})

View File

@ -3,6 +3,7 @@ import { getInput } from "@actions/core"
import semverLte from "semver/functions/lte"
import semverCoerce from "semver/functions/coerce"
import { setupBin, PackageInfo, InstallationInfo } from "../utils/setup/setupBin"
import { addBinExtension } from "../utils/extension/extension"
/** Get the platform data for cmake */
function getCmakePackageInfo(version: string, platform?: NodeJS.Platform): PackageInfo {
@ -21,6 +22,7 @@ function getCmakePackageInfo(version: string, platform?: NodeJS.Platform): Packa
const folderName = `cmake-${version}-${osArchStr}`
return {
binRelativeDir: "bin/",
binFileName: addBinExtension("cmake"),
extractedFolderName: folderName,
extractFunction: extractZip,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.zip`,
@ -32,6 +34,7 @@ function getCmakePackageInfo(version: string, platform?: NodeJS.Platform): Packa
const folderName = `cmake-${version}-${osArchStr}`
return {
binRelativeDir: "CMake.app/Contents/bin/",
binFileName: addBinExtension("cmake"),
extractedFolderName: folderName,
extractFunction: extractTar,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.tar.gz`,
@ -48,6 +51,7 @@ function getCmakePackageInfo(version: string, platform?: NodeJS.Platform): Packa
const folderName = `cmake-${version}-${osArchStr}`
return {
binRelativeDir: "bin/",
binFileName: addBinExtension("cmake"),
extractedFolderName: folderName,
extractFunction: extractTar,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.tar.gz`,

View File

@ -15,7 +15,7 @@ async function testUrl(version: string) {
describe("setup-llvm", () => {
let directory: string
beforeAll(async () => {
directory = await setupTmpDir("setup-llvm")
directory = await setupTmpDir("llvm")
})
it("Finds valid LLVM URLs", async () => {

View File

@ -7,6 +7,7 @@ import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin
import { extractExe, extractTarByExe } from "../utils/setup/extract"
import { getSpecificVersionAndUrl, getVersions } from "../utils/setup/version"
import { setupMacOSSDK } from "../macos-sdk/macos-sdk"
import { addBinExtension } from "../utils/extension/extension"
//================================================
// Version
@ -227,6 +228,7 @@ async function getLLVMPackageInfo(version: string, platform: NodeJS.Platform): P
url,
extractedFolderName: "",
binRelativeDir: "bin",
binFileName: addBinExtension("clang"),
extractFunction: platform === "win32" ? extractExe : extractTarByExe,
}
}

View File

@ -11,7 +11,7 @@ async function testNinja(directory: string) {
describe("setup-ninja", () => {
let directory: string
beforeEach(async () => {
directory = await setupTmpDir("setup-ninja")
directory = await setupTmpDir("ninja")
})
it("should setup Ninja", async () => {

View File

@ -1,4 +1,5 @@
import { extractZip } from "@actions/tool-cache"
import { addBinExtension } from "../utils/extension/extension"
import { setupBin, PackageInfo, InstallationInfo } from "../utils/setup/setupBin"
/** Get the platform name Ninja uses in their download links */
@ -20,6 +21,7 @@ function getNinjaPackageInfo(version: string, platform: NodeJS.Platform): Packag
const ninjaPlatform = getNinjaPlatform(platform)
return {
binRelativeDir: "",
binFileName: addBinExtension("ninja"),
extractedFolderName: "",
extractFunction: extractZip,
url: `https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${ninjaPlatform}.zip`,

View File

@ -14,6 +14,8 @@ export type PackageInfo = {
extractedFolderName: string
/** The relative directory in which the binary is located. It can be `""` if the exe is in the top folder */
binRelativeDir: string
/** The main binary file. */
binFileName: string
/** The function to extract the downloaded archive. It can be `undefined`, if the binary itself is downloaded directly. */
extractFunction?: {
(file: string, dest: string): Promise<string> | Promise<void>
@ -42,36 +44,42 @@ export async function setupBin(
setupDir: string
): Promise<InstallationInfo> {
process.env.RUNNER_TEMP = process.env.RUNNER_TEMP ?? tmpdir()
process.env.RUNNER_TOOL_CACHE = process.env.RUNNER_TOOL_CACH ?? join(tmpdir(), "setup_cpp", "ToolCache")
process.env.RUNNER_TOOL_CACHE = process.env.RUNNER_TOOL_CACH ?? join(tmpdir(), "setup-cpp", "ToolCache")
const { url, binRelativeDir, extractedFolderName, extractFunction } = await getPackageInfo(version, process.platform)
const { url, binRelativeDir, binFileName, extractedFolderName, extractFunction } = await getPackageInfo(
version,
process.platform
)
// Restore from cache (if found).
if (isCI()) {
try {
const dir = find(name, version)
if (dir) {
info(`${name} ${version} was found in the cache.`)
const installDir = join(dir, extractedFolderName)
const binDir = join(installDir, binRelativeDir)
if (existsSync(binDir) && existsSync(join(binRelativeDir, binFileName))) {
info(`${name} ${version} was found in the cache.`)
addPath(binDir)
return { installDir, binDir }
}
}
} catch {
// fails on a local machine?
}
}
const installDir = join(setupDir, extractedFolderName)
const binDir = join(installDir, binRelativeDir)
const binFile = join(binDir, binFileName)
// download ane extract the package into the installation directory.
if (!existsSync(setupDir)) {
if (!existsSync(binDir) || !existsSync(binFile)) {
info(`Download and extract ${name} ${version}`)
const downloaded = await downloadTool(url)
await extractFunction?.(downloaded, setupDir)
}
const installDir = join(setupDir, extractedFolderName)
const binDir = join(installDir, binRelativeDir)
// Adding the bin dir to the path
/** The directory which the tool is installed to */
info(`Add ${binDir} to PATH`)

View File

@ -1,15 +1,20 @@
import { setupVcpkg } from "../vcpkg"
import { testBin } from "../../utils/tests/test-helpers"
import { testBin, setupTmpDir } from "../../utils/tests/test-helpers"
jest.setTimeout(300000)
async function testvcpkg() {
const { binDir } = await setupVcpkg("", "", "")
async function testvcpkg(directory: string) {
const { binDir } = await setupVcpkg("", directory, "")
await testBin("vcpkg", ["--version"], binDir)
return binDir
}
describe("setup-vcpkg", () => {
let directory: string
beforeAll(async () => {
directory = await setupTmpDir("vcpkg")
})
it("should setup vcpkg", async () => {
await testvcpkg()
await testvcpkg(directory)
})
})

View File

@ -1,6 +1,7 @@
import { addPath } from "@actions/core"
import { addPath, warning } from "@actions/core"
import execa from "execa"
import path, { dirname } from "path"
import { existsSync } from "fs"
import { dirname, join } from "path"
import which from "which"
import { addShellExtension, addShellHere } from "../utils/extension/extension"
import { InstallationInfo } from "../utils/setup/setupBin"
@ -10,12 +11,16 @@ let hasVCPKG = false
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function setupVcpkg(_version: string, setupDir: string, _arch: string): InstallationInfo {
if (!hasVCPKG || which.sync("vcpkg", { nothrow: true }) === null) {
if (!existsSync(join(setupDir, addShellExtension("bootstrap-vcpkg")))) {
execa.sync("git", ["clone", "https://github.com/microsoft/vcpkg"], { cwd: dirname(setupDir) })
} else {
warning(`Vcpkg folder already exists at ${setupDir}`)
}
execa.sync(addShellExtension(addShellHere("bootstrap-vcpkg")), { cwd: setupDir, shell: true })
addPath(setupDir)
hasVCPKG = true
return { binDir: setupDir }
}
return { binDir: path.dirname(which.sync("vcpkg")) }
return { binDir: dirname(which.sync("vcpkg")) }
}