From 0e7542179a54d2da012e7609daeb2b3a63ffe08c Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 14 Sep 2021 02:42:17 -0500 Subject: [PATCH] feat: add setupNinja Used some code from https://github.com/aminya/install-cmake/blob/new-versions-and-arch/src/ninja.ts --- action.yml | 5 +++-- src/main.ts | 19 +++++++++++++++++-- src/ninja/ninja.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/ninja/ninja.ts diff --git a/action.yml b/action.yml index 3ae0826f..150a8661 100644 --- a/action.yml +++ b/action.yml @@ -11,11 +11,12 @@ inputs: required: false cmake: description: "The cmake version to install." - default: '3.20.2' + default: "3.20.2" required: false ninja: description: "The ninja version to install." - required: false + default: "1.10.2" + required: true runs: using: "node12" diff --git a/src/main.ts b/src/main.ts index 43098d54..151adfb4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,28 @@ import * as core from "@actions/core" import { setupCmake } from "./cmake/cmake" +import { setupNinja } from "./ninja/ninja" + +function maybeGetInput(key: string) { + const value = core.getInput(key) + if (value !== "false" && value !== "") { + return value + } + return undefined +} export async function main(): Promise { try { // setup cmake - const cmakeVersion = core.getInput("cmake") - if (cmakeVersion !== "false" && cmakeVersion !== "") { + const cmakeVersion = maybeGetInput("cmake") + if (cmakeVersion !== undefined) { await setupCmake(cmakeVersion) } + + // setup ninja + const ninjaVersion = maybeGetInput("ninja") + if (ninjaVersion !== undefined) { + await setupNinja(ninjaVersion) + } } catch (err) { core.error(err as string | Error) core.setFailed("install-cpp failed") diff --git a/src/ninja/ninja.ts b/src/ninja/ninja.ts new file mode 100644 index 00000000..118aa699 --- /dev/null +++ b/src/ninja/ninja.ts @@ -0,0 +1,46 @@ +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" + +export async function setupNinja(version: string): Promise { + const platform = process.platform + + // Build artifact name + const ninjaBin = platform === "win32" ? "ninja.exe" : "ninja" + + // Restore from cache (if found). + const ninjaDir = find("ninja", version) + if (ninjaDir) { + addPath(ninjaDir) + return join(ninjaDir, ninjaBin) + } + + 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 outputDir = join(process.env.RUNNER_TEMP ?? tmpdir(), key) + + const ninjaPath = join(outputDir, ninjaBin) + + if (!existsSync(outputDir)) { + await group("Download and extract ninja-build", async () => { + const downloaded = await downloadTool(url) + await extractZip(downloaded, outputDir) + }) + } + + try { + startGroup("Add ninja-build to PATH") + addPath(outputDir) + } finally { + endGroup() + } + + await cacheFile(ninjaPath, ninjaBin, "ninja", version) + + return ninjaPath +}