fix: add setupBin function and use it inside setupCmake

This commit is contained in:
Amin Yahyaabadi 2021-09-14 04:23:12 -05:00
parent 7f512aeee3
commit ae8703687a
2 changed files with 71 additions and 59 deletions

View File

@ -1,21 +1,8 @@
import { extractZip, extractTar, find, downloadTool, cacheDir } from "@actions/tool-cache" import { extractZip, extractTar } from "@actions/tool-cache"
import { getInput, addPath, group, startGroup, endGroup } from "@actions/core" import { getInput } from "@actions/core"
import { join, basename } from "path"
import { existsSync } from "fs"
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 * as hasha from "hasha" import { setupBin, PackageInfo } from "../utils/setup/setup"
import { tmpdir } from "os"
import { URL } from "url"
interface PackageInfo {
url: string
binRelativePath: string
extractFunction: {
(url: string, outputPath: string): Promise<string>
}
dropSuffix: string
}
/** Get the platform data for cmake */ /** Get the platform data for cmake */
function getCmakePlatformData(version: string, platform?: NodeJS.Platform): PackageInfo { function getCmakePlatformData(version: string, platform?: NodeJS.Platform): PackageInfo {
@ -32,7 +19,7 @@ function getCmakePlatformData(version: string, platform?: NodeJS.Platform): Pack
osArchStr = isOld ? "win64-x64" : "windows-x86_64" osArchStr = isOld ? "win64-x64" : "windows-x86_64"
} }
return { return {
binRelativePath: "bin/", binRelativeDir: "bin/",
dropSuffix: ".zip", dropSuffix: ".zip",
extractFunction: extractZip, extractFunction: extractZip,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-${osArchStr}.zip`, url: `https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-${osArchStr}.zip`,
@ -42,7 +29,7 @@ function getCmakePlatformData(version: string, platform?: NodeJS.Platform): Pack
const isOld = semverLte(semVersion, "v3.19.1") const isOld = semverLte(semVersion, "v3.19.1")
const osArchStr = isOld ? "Darwin-x86_64" : "macos-universal" const osArchStr = isOld ? "Darwin-x86_64" : "macos-universal"
return { return {
binRelativePath: "CMake.app/Contents/bin/", binRelativeDir: "CMake.app/Contents/bin/",
dropSuffix: ".tar.gz", dropSuffix: ".tar.gz",
extractFunction: extractTar, extractFunction: extractTar,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-${osArchStr}.tar.gz`, url: `https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-${osArchStr}.tar.gz`,
@ -57,7 +44,7 @@ function getCmakePlatformData(version: string, platform?: NodeJS.Platform): Pack
osArchStr = isOld ? "Linux-x86_64" : "linux-x86_64" osArchStr = isOld ? "Linux-x86_64" : "linux-x86_64"
} }
return { return {
binRelativePath: "bin/", binRelativeDir: "bin/",
dropSuffix: ".tar.gz", dropSuffix: ".tar.gz",
extractFunction: extractTar, extractFunction: extractTar,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-${osArchStr}.tar.gz`, url: `https://github.com/Kitware/CMake/releases/download/v${version}/cmake-${version}-${osArchStr}.tar.gz`,
@ -69,44 +56,6 @@ function getCmakePlatformData(version: string, platform?: NodeJS.Platform): Pack
} }
/** Setup cmake */ /** Setup cmake */
export async function setupCmake(version: string): Promise<string> { export function setupCmake(version: string): Promise<string> {
// Build artifact name return setupBin("cmake", version, getCmakePlatformData)
const cmakeBin = process.platform === "win32" ? "cmake.exe" : "cmake"
// Restore from cache (if found).
const cmakeDir = find("cmake", version)
if (cmakeDir) {
addPath(cmakeDir)
return join(cmakeDir, cmakeBin)
}
const { url, dropSuffix, binRelativePath: binPath, extractFunction } = getCmakePlatformData(version, process.platform)
// Get an unique output directory name from the URL.
const key: string = await hasha.async(url)
const finalDir = join(process.env.RUNNER_TEMP ?? tmpdir(), key)
const { pathname } = new URL(url)
const dirName = basename(pathname)
const finalPath = join(finalDir, dirName.replace(dropSuffix, ""), binPath)
const finalBinPath = join(finalPath, cmakeBin)
if (!existsSync(finalDir)) {
await group("Download and extract CMake", async () => {
const downloaded = await downloadTool(url)
await extractFunction(downloaded, finalDir)
})
}
try {
startGroup(`Add CMake to PATH`)
addPath(finalPath)
} finally {
endGroup()
}
await cacheDir(finalDir, "cmake", version)
return finalBinPath
} }

63
src/utils/setup/setup.ts Normal file
View File

@ -0,0 +1,63 @@
import { find, downloadTool, cacheDir } from "@actions/tool-cache"
import { addPath, group, startGroup, endGroup } from "@actions/core"
import { join, basename } from "path"
import { existsSync } from "fs"
import * as hasha from "hasha"
import { tmpdir } from "os"
import { URL } from "url"
export type PackageInfo = {
url: string
binRelativeDir: string
extractFunction: {
(url: string, outputPath: string): Promise<string>
}
dropSuffix: string
}
/** A function that downloads and installs a tool. Then it caches it in the tool-cache. */
export async function setupBin(
name: string,
version: string,
getPlatformData: (version: string, platform: NodeJS.Platform) => PackageInfo
): Promise<string> {
// Build artifact name
const binName = process.platform === "win32" ? `${name}.exe` : name
// Restore from cache (if found).
const dir = find(name, version)
if (dir) {
addPath(dir)
return join(dir, binName)
}
const { url, dropSuffix, binRelativeDir: binRelativePath, extractFunction } = getPlatformData(version, process.platform)
// Get an unique output directory name from the URL.
const key: string = await hasha.async(url)
const workDir = join(process.env.RUNNER_TEMP ?? tmpdir(), key)
const { pathname } = new URL(url)
const dirName = basename(pathname)
/** The directory which the tool is installed to */
const binDir = join(workDir, dirName.replace(dropSuffix, ""), binRelativePath)
if (!existsSync(workDir)) {
await group(`Download and extract ${name}`, async () => {
const downloaded = await downloadTool(url)
await extractFunction(downloaded, workDir)
})
}
try {
startGroup(`Add ${name} to PATH`)
addPath(binDir)
} finally {
endGroup()
}
await cacheDir(workDir, name, version)
return join(binDir, binName)
}