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) }, 100000)
it("should setup CMake", async () => { it("should setup CMake", async () => {
const cmakePath = await setupCmake("3.20.2", directory) const { binDir } = await setupCmake("3.20.2", directory)
expect(cmakePath).toBeDefined() expect(binDir).toBeDefined()
expect(cmakePath).not.toHaveLength(0) expect(binDir).not.toHaveLength(0)
const cmakeBin = join(cmakePath, addBinExtension("cmake")) const cmakeBin = join(binDir, addBinExtension("cmake"))
const { status, error } = spawn(cmakeBin, ["--version"], { const { status, error } = spawn(cmakeBin, ["--version"], {
encoding: "utf8", encoding: "utf8",

View File

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

View File

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

View File

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