diff --git a/src/ninja/__tests__/ninja.test.ts b/src/ninja/__tests__/ninja.test.ts index 5d7bfeb5..c3b234a2 100644 --- a/src/ninja/__tests__/ninja.test.ts +++ b/src/ninja/__tests__/ninja.test.ts @@ -6,11 +6,11 @@ jest.setTimeout(30 * 1000) describe("setup-ninja", () => { beforeEach(async () => { - await setupTmpDir("setup-cmake") + await setupTmpDir("setup-ninja") }) afterAll(async () => { - await cleanupTmpDir("setup-cmake") + await cleanupTmpDir("setup-ninja") }, 100000) it("should setup Ninja", async () => { diff --git a/src/ninja/ninja.ts b/src/ninja/ninja.ts index 579636c7..20fbe635 100644 --- a/src/ninja/ninja.ts +++ b/src/ninja/ninja.ts @@ -1,9 +1,5 @@ -import { extractZip, find, downloadTool, cacheFile } from "@actions/tool-cache" -import { addPath, group, startGroup, endGroup } from "@actions/core" -import { join } from "path" -import { existsSync } from "fs" -import * as hasha from "hasha" -import { tmpdir } from "os" +import { extractZip } from "@actions/tool-cache" +import { setupBin, PackageInfo } from "../utils/setup/setupBin" /** Get the platform name Ninja uses in their download links */ function getNinjaPlatform(platform: NodeJS.Platform) { @@ -19,42 +15,17 @@ function getNinjaPlatform(platform: NodeJS.Platform) { } } -export async function setupNinja(version: string): Promise { - const platform = getNinjaPlatform(process.platform) - - // Build artifact name - const ninjaBin = platform === "win" ? "ninja.exe" : "ninja" - - // Restore from cache (if found). - const ninjaDir = find("ninja", version) - if (ninjaDir) { - addPath(ninjaDir) - return join(ninjaDir, ninjaBin) +/** Get the platform data for ninja */ +function getNinjaPackageInfo(version: string, platform: NodeJS.Platform): PackageInfo { + const ninjaPlatform = getNinjaPlatform(platform) + return { + binRelativeDir: "", + extractedFolderName: "", + extractFunction: extractZip, + url: `https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${ninjaPlatform}.zip`, } - - const url = `https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${platform}.zip` - - // 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 finalBinPath = join(finalDir, ninjaBin) - - if (!existsSync(finalDir)) { - await group("Download and extract ninja", async () => { - const downloaded = await downloadTool(url) - await extractZip(downloaded, finalDir) - }) - } - - try { - startGroup("Add ninja to PATH") - addPath(finalDir) - } finally { - endGroup() - } - - await cacheFile(finalBinPath, ninjaBin, "ninja", version) - - return finalBinPath +} + +export function setupNinja(version: string): Promise { + return setupBin("ninja", version, getNinjaPackageInfo) } diff --git a/src/utils/setup/setupBin.ts b/src/utils/setup/setupBin.ts index 27dc451e..98711c29 100644 --- a/src/utils/setup/setupBin.ts +++ b/src/utils/setup/setupBin.ts @@ -5,11 +5,15 @@ import { existsSync } from "fs" import * as hasha from "hasha" import { tmpdir } from "os" +/** A type that describes a package */ export type PackageInfo = { + /** url to download the package */ url: string - binRelativeDir: string - /** The top folder name once it is extracted */ + /** The top folder name once it is extracted. It can be `""` if there is no top folder */ 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 function to extract the downloaded archive */ extractFunction: { (url: string, outputPath: string): Promise }