refactor: refactor setupCmake and setupNinja

This commit is contained in:
Amin Yahyaabadi 2021-09-14 03:38:47 -05:00
parent 0a3aa6cc9c
commit 7f512aeee3
2 changed files with 30 additions and 25 deletions

View File

@ -10,7 +10,7 @@ import { URL } from "url"
interface PackageInfo { interface PackageInfo {
url: string url: string
binPath: string binRelativePath: string
extractFunction: { extractFunction: {
(url: string, outputPath: string): Promise<string> (url: string, outputPath: string): Promise<string>
} }
@ -32,7 +32,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 {
binPath: "bin/", binRelativePath: "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 +42,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 {
binPath: "CMake.app/Contents/bin/", binRelativePath: "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 +57,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 {
binPath: "bin/", binRelativePath: "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`,
@ -70,38 +70,43 @@ function getCmakePlatformData(version: string, platform?: NodeJS.Platform): Pack
/** Setup cmake */ /** Setup cmake */
export async function setupCmake(version: string): Promise<string> { export async function setupCmake(version: string): Promise<string> {
// Build artifact name
const cmakeBin = process.platform === "win32" ? "cmake.exe" : "cmake"
// Restore from cache (if found). // Restore from cache (if found).
const cmakeDir = find("cmake", version) const cmakeDir = find("cmake", version)
if (cmakeDir) { if (cmakeDir) {
addPath(cmakeDir) addPath(cmakeDir)
return join(cmakeDir, process.platform === "win32" ? "cmake.exe" : "cmake") return join(cmakeDir, cmakeBin)
} }
const data = getCmakePlatformData(version, process.platform) const { url, dropSuffix, binRelativePath: binPath, extractFunction } = getCmakePlatformData(version, process.platform)
// Get an unique output directory name from the URL. // Get an unique output directory name from the URL.
const key: string = await hasha.async(data.url) const key: string = await hasha.async(url)
const cmakePath = join(process.env.RUNNER_TEMP ?? tmpdir(), key) const finalDir = join(process.env.RUNNER_TEMP ?? tmpdir(), key)
const { pathname } = new URL(data.url) const { pathname } = new URL(url)
const dirName = basename(pathname) const dirName = basename(pathname)
const outputPath = join(cmakePath, dirName.replace(data.dropSuffix, ""), data.binPath) const finalPath = join(finalDir, dirName.replace(dropSuffix, ""), binPath)
if (!existsSync(cmakePath)) { const finalBinPath = join(finalPath, cmakeBin)
if (!existsSync(finalDir)) {
await group("Download and extract CMake", async () => { await group("Download and extract CMake", async () => {
const downloaded = await downloadTool(data.url) const downloaded = await downloadTool(url)
await data.extractFunction(downloaded, cmakePath) await extractFunction(downloaded, finalDir)
}) })
} }
try { try {
startGroup(`Add CMake to PATH`) startGroup(`Add CMake to PATH`)
addPath(outputPath) addPath(finalPath)
} finally { } finally {
endGroup() endGroup()
} }
await cacheDir(cmakePath, "cmake", version) await cacheDir(finalDir, "cmake", version)
return join(outputPath, process.platform === "win32" ? "cmake.exe" : "cmake") return finalBinPath
} }

View File

@ -36,25 +36,25 @@ export async function setupNinja(version: string): Promise<string> {
// 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 outputDir = join(process.env.RUNNER_TEMP ?? tmpdir(), key) const finalDir = join(process.env.RUNNER_TEMP ?? tmpdir(), key)
const ninjaPath = join(outputDir, ninjaBin) const finalBinPath = join(finalDir, ninjaBin)
if (!existsSync(outputDir)) { if (!existsSync(finalDir)) {
await group("Download and extract ninja-build", async () => { await group("Download and extract ninja", async () => {
const downloaded = await downloadTool(url) const downloaded = await downloadTool(url)
await extractZip(downloaded, outputDir) await extractZip(downloaded, finalDir)
}) })
} }
try { try {
startGroup("Add ninja-build to PATH") startGroup("Add ninja to PATH")
addPath(outputDir) addPath(finalDir)
} finally { } finally {
endGroup() endGroup()
} }
await cacheFile(ninjaPath, ninjaBin, "ninja", version) await cacheFile(finalBinPath, ninjaBin, "ninja", version)
return ninjaPath return finalBinPath
} }