fix: return the installDir and binDir

This commit is contained in:
Amin Yahyaabadi 2021-09-14 09:13:39 -05:00
parent 4824306aab
commit 63126aeffa
5 changed files with 32 additions and 22 deletions

View File

@ -17,11 +17,11 @@ describe("setup-cmake", () => {
}, 100000)
it("should setup CMake", async () => {
const cmakePath = await setupCmake("3.20.2", directory)
expect(cmakePath).toBeDefined()
expect(cmakePath).not.toHaveLength(0)
const { binDir } = await setupCmake("3.20.2", directory)
expect(binDir).toBeDefined()
expect(binDir).not.toHaveLength(0)
const cmakeBin = join(cmakePath, addBinExtension("cmake"))
const cmakeBin = join(binDir, addBinExtension("cmake"))
const { status, error } = spawn(cmakeBin, ["--version"], {
encoding: "utf8",

View File

@ -2,7 +2,7 @@ import { extractZip, extractTar } from "@actions/tool-cache"
import { getInput } from "@actions/core"
import semverLte from "semver/functions/lte"
import semverCoerce from "semver/functions/coerce"
import { setupPackage, PackageInfo } from "../utils/setup/setupBin"
import { setupPackage, PackageInfo, InstallationInfo } from "../utils/setup/setupBin"
/** Get the platform data for cmake */
function getCmakePackageInfo(version: string, platform?: NodeJS.Platform): PackageInfo {
@ -59,6 +59,6 @@ function getCmakePackageInfo(version: string, platform?: NodeJS.Platform): Packa
}
/** Setup cmake */
export function setupCmake(version: string, setupCppDir: string): Promise<string> {
export function setupCmake(version: string, setupCppDir: string): Promise<InstallationInfo> {
return setupPackage("cmake", version, getCmakePackageInfo, setupCppDir)
}

View File

@ -17,11 +17,11 @@ describe("setup-ninja", () => {
}, 100000)
it("should setup Ninja", async () => {
const ninjaPath = await setupNinja("1.10.2", directory)
expect(ninjaPath).toBeDefined()
expect(ninjaPath).not.toHaveLength(0)
const { binDir } = await setupNinja("1.10.2", directory)
expect(binDir).toBeDefined()
expect(binDir).not.toHaveLength(0)
const ninjaBin = join(ninjaPath, addBinExtension("ninja"))
const ninjaBin = join(binDir, addBinExtension("ninja"))
const { status } = spawn(ninjaBin, ["--version"], {
encoding: "utf8",

View File

@ -1,5 +1,5 @@
import { extractZip } from "@actions/tool-cache"
import { setupPackage, PackageInfo } from "../utils/setup/setupBin"
import { setupPackage, PackageInfo, InstallationInfo } from "../utils/setup/setupBin"
/** Get the platform name Ninja uses in their download links */
function getNinjaPlatform(platform: NodeJS.Platform) {
@ -26,6 +26,6 @@ function getNinjaPackageInfo(version: string, platform: NodeJS.Platform): Packag
}
}
export function setupNinja(version: string, setupCppDir: string): Promise<string> {
export function setupNinja(version: string, setupCppDir: string): Promise<InstallationInfo> {
return setupPackage("ninja", version, getNinjaPackageInfo, setupCppDir)
}

View File

@ -19,6 +19,12 @@ export type PackageInfo = {
}
}
export type InstallationInfo = {
/** The top install dir */
installDir: string
binDir: string
}
/**
* A function that:
*
@ -33,35 +39,39 @@ export async function setupPackage(
version: string,
getPackageInfo: (version: string, platform: NodeJS.Platform) => PackageInfo | Promise<PackageInfo>,
setupCppDir: string
): Promise<string> {
): Promise<InstallationInfo> {
process.env.RUNNER_TEMP = process.env.RUNNER_TEMP ?? tmpdir()
const { url, binRelativeDir, extractedFolderName, extractFunction } = await getPackageInfo(version, process.platform)
// Restore from cache (if found).
const dir = find(name, version)
if (dir) {
info(`${name} ${version} was found in the cache.`)
addPath(dir)
return dir
}
const { url, binRelativeDir, extractedFolderName, extractFunction } = await getPackageInfo(version, process.platform)
const installDir = join(dir, extractedFolderName)
return { installDir, binDir: join(installDir, binRelativeDir) }
}
// Get an unique output directory name from the URL.
const key: string = await hasha.async(url)
const installDir = join(setupCppDir, key)
const rootDir = join(setupCppDir, key)
// download ane extract the package into the installation directory.
if (!existsSync(installDir)) {
if (!existsSync(rootDir)) {
await group(`Download and extract ${name} ${version}`, async () => {
const downloaded = await downloadTool(url)
await extractFunction?.(downloaded, installDir)
await extractFunction?.(downloaded, rootDir)
})
}
const installDir = join(rootDir, extractedFolderName)
const binDir = join(installDir, binRelativeDir)
// Adding the bin dir to the path
try {
/** The directory which the tool is installed to */
const binDir = join(installDir, extractedFolderName, binRelativeDir)
startGroup(`Add ${binDir} to PATH`)
addPath(binDir)
} finally {
@ -70,10 +80,10 @@ export async function setupPackage(
// check if inside Github Actions. If so, cache the installation
if (typeof process.env.RUNNER_TOOL_CACHE === "string") {
await cacheDir(installDir, name, version)
await cacheDir(rootDir, name, version)
}
return installDir
return { installDir, binDir }
}
/** Add bin extension to a binary. This will be `.exe` on Windows. */
export function addBinExtension(name: string) {