refactor: remove core actions toolkit dependency (#397)

* refactor: add a `getInput` function

* refactor: replace `core.getBooleanInput` with `getInput`

* refactor: replace `core.getMultilineInput` with `getInput`

* refactor: replace `core.setOutput` with dirrect append to `GITHUB_OUTPUT`

* refactor: replace `core.setFailed` with direct stdout write
This commit is contained in:
Alfi Maulana 2024-08-06 22:42:23 +07:00 committed by GitHub
parent fe729787d8
commit 44a0a4c6c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 106 additions and 26807 deletions

26715
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,6 @@
"test": "jest" "test": "jest"
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1",
"catched-error-message": "^0.0.1" "catched-error-message": "^0.0.1"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,5 +1,6 @@
import * as core from "@actions/core";
import { getErrorMessage } from "catched-error-message"; import { getErrorMessage } from "catched-error-message";
import fs from "node:fs";
import os from "node:os";
import { buildProject, configureProject } from "./cmake.js"; import { buildProject, configureProject } from "./cmake.js";
import { getInputs } from "./inputs.js"; import { getInputs } from "./inputs.js";
@ -8,11 +9,15 @@ try {
configureProject(inputs); configureProject(inputs);
core.setOutput("build-dir", inputs.buildDir); fs.appendFileSync(
process.env["GITHUB_OUTPUT"] as string,
`build-dir=${inputs.buildDir}${os.EOL}`,
);
if (inputs.runBuild) { if (inputs.runBuild) {
buildProject(inputs); buildProject(inputs);
} }
} catch (err) { } catch (err) {
core.setFailed(getErrorMessage(err)); process.exitCode = 1;
process.stdout.write(`::error::${getErrorMessage(err)}${os.EOL}`);
} }

View File

@ -1,19 +1,10 @@
import { jest } from "@jest/globals";
import path from "node:path"; import path from "node:path";
import type { Inputs } from "./inputs.js"; import { Inputs, getInputs } from "./inputs.js";
jest.unstable_mockModule("@actions/core", () => ({
getBooleanInput: jest.fn(),
getInput: jest.fn(),
getMultilineInput: jest.fn(),
}));
describe("get action inputs", () => { describe("get action inputs", () => {
interface TestCase { interface TestCase {
name: string; name: string;
booleanInputs?: Record<string, boolean>; env?: Record<string, string>;
stringInputs?: Record<string, string>;
multilineInputs?: Record<string, string[]>;
expectedInputs?: Partial<Inputs>; expectedInputs?: Partial<Inputs>;
} }
@ -23,7 +14,7 @@ describe("get action inputs", () => {
}, },
{ {
name: "with source directory specified", name: "with source directory specified",
stringInputs: { "source-dir": "project" }, env: { "INPUT_SOURCE-DIR": "project" },
expectedInputs: { expectedInputs: {
sourceDir: "project", sourceDir: "project",
buildDir: path.join("project", "build"), buildDir: path.join("project", "build"),
@ -31,14 +22,14 @@ describe("get action inputs", () => {
}, },
{ {
name: "with build directory specified", name: "with build directory specified",
stringInputs: { "build-dir": "output" }, env: { "INPUT_BUILD-DIR": "output" },
expectedInputs: { buildDir: "output" }, expectedInputs: { buildDir: "output" },
}, },
{ {
name: "with source and build directories specified", name: "with source and build directories specified",
stringInputs: { env: {
"source-dir": "project", "INPUT_SOURCE-DIR": "project",
"build-dir": "output", "INPUT_BUILD-DIR": "output",
}, },
expectedInputs: { expectedInputs: {
sourceDir: "project", sourceDir: "project",
@ -47,33 +38,33 @@ describe("get action inputs", () => {
}, },
{ {
name: "with generator specified", name: "with generator specified",
stringInputs: { generator: "Ninja" }, env: { INPUT_GENERATOR: "Ninja" },
expectedInputs: { generator: "Ninja" }, expectedInputs: { generator: "Ninja" },
}, },
{ {
name: "with C compiler specified", name: "with C compiler specified",
stringInputs: { "c-compiler": "clang" }, env: { "INPUT_C-COMPILER": "clang" },
expectedInputs: { cCompiler: "clang" }, expectedInputs: { cCompiler: "clang" },
}, },
{ {
name: "with C++ compiler specified", name: "with C++ compiler specified",
stringInputs: { "cxx-compiler": "clang++" }, env: { "INPUT_CXX-COMPILER": "clang++" },
expectedInputs: { cxxCompiler: "clang++" }, expectedInputs: { cxxCompiler: "clang++" },
}, },
{ {
name: "with C flags specified", name: "with C flags specified",
multilineInputs: { "c-flags": ["-Werror -Wall", "-Wextra"] }, env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" },
expectedInputs: { cFlags: "-Werror -Wall -Wextra" }, expectedInputs: { cFlags: "-Werror -Wall -Wextra" },
}, },
{ {
name: "with C++ flags specified", name: "with C++ flags specified",
multilineInputs: { "cxx-flags": ["-Werror -Wall", "-Wextra -Wpedantic"] }, env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" },
expectedInputs: { cxxFlags: "-Werror -Wall -Wextra -Wpedantic" }, expectedInputs: { cxxFlags: "-Werror -Wall -Wextra -Wpedantic" },
}, },
{ {
name: "with additional options specified", name: "with additional options specified",
multilineInputs: { env: {
options: ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"], INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
}, },
expectedInputs: { expectedInputs: {
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"], options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
@ -81,37 +72,33 @@ describe("get action inputs", () => {
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
multilineInputs: { args: ["-Wdev -Wdeprecated", "--fresh"] }, env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" },
expectedInputs: { args: ["-Wdev", "-Wdeprecated", "--fresh"] }, expectedInputs: { args: ["-Wdev", "-Wdeprecated", "--fresh"] },
}, },
{ {
name: "with run build specified", name: "with run build specified",
booleanInputs: { "run-build": false }, env: { "INPUT_RUN-BUILD": "true" },
expectedInputs: { runBuild: false }, expectedInputs: { runBuild: true },
}, },
{ {
name: "with additional build arguments specified", name: "with additional build arguments specified",
multilineInputs: { "build-args": ["--target foo", "--parallel 8"] }, env: { "INPUT_BUILD-ARGS": "--target foo\n--parallel 8" },
expectedInputs: { buildArgs: ["--target", "foo", "--parallel", "8"] }, expectedInputs: { buildArgs: ["--target", "foo", "--parallel", "8"] },
}, },
{ {
name: "with all specified", name: "with all specified",
booleanInputs: { env: {
"run-build": false, "INPUT_SOURCE-DIR": "project",
}, "INPUT_BUILD-DIR": "output",
stringInputs: { INPUT_GENERATOR: "Ninja",
"source-dir": "project", "INPUT_C-COMPILER": "clang",
"build-dir": "output", "INPUT_CXX-COMPILER": "clang++",
generator: "Ninja", "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra",
"c-compiler": "clang", "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic",
"cxx-compiler": "clang++", INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
}, INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh",
multilineInputs: { "INPUT_RUN-BUILD": "true",
"c-flags": ["-Werror -Wall", "-Wextra"], "INPUT_BUILD-ARGS": "--target foo\n--parallel 8",
"cxx-flags": ["-Werror -Wall", "-Wextra -Wpedantic"],
options: ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev -Wdeprecated", "--fresh"],
"build-args": ["--target foo", "--parallel 8"],
}, },
expectedInputs: { expectedInputs: {
sourceDir: "project", sourceDir: "project",
@ -123,7 +110,7 @@ describe("get action inputs", () => {
cxxFlags: "-Werror -Wall -Wextra -Wpedantic", cxxFlags: "-Werror -Wall -Wextra -Wpedantic",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"], options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev", "-Wdeprecated", "--fresh"], args: ["-Wdev", "-Wdeprecated", "--fresh"],
runBuild: false, runBuild: true,
buildArgs: ["--target", "foo", "--parallel", "8"], buildArgs: ["--target", "foo", "--parallel", "8"],
}, },
}, },
@ -131,26 +118,11 @@ describe("get action inputs", () => {
for (const testCase of testCases) { for (const testCase of testCases) {
it(`should get the action inputs ${testCase.name}`, async () => { it(`should get the action inputs ${testCase.name}`, async () => {
const { getInputs } = await import("./inputs.js"); const prevEnv = process.env;
const core = await import("@actions/core"); process.env = {
...process.env,
const booleanInputs: Record<string, boolean> = { ...testCase.env,
"run-build": true,
...testCase.booleanInputs,
}; };
jest.mocked(core.getBooleanInput).mockImplementation((name) => {
return booleanInputs[name] ?? false;
});
const stringInputs = { ...testCase.stringInputs };
jest.mocked(core.getInput).mockImplementation((name) => {
return stringInputs[name] ?? "";
});
const multilineInputs = { ...testCase.multilineInputs };
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
return multilineInputs[name] ?? [];
});
expect(getInputs()).toStrictEqual({ expect(getInputs()).toStrictEqual({
sourceDir: "", sourceDir: "",
@ -162,10 +134,12 @@ describe("get action inputs", () => {
cxxFlags: "", cxxFlags: "",
options: [], options: [],
args: [], args: [],
runBuild: true, runBuild: false,
buildArgs: [], buildArgs: [],
...testCase.expectedInputs, ...testCase.expectedInputs,
}); });
process.env = prevEnv;
}); });
} }
}); });

View File

@ -1,4 +1,3 @@
import { getBooleanInput, getInput, getMultilineInput } from "@actions/core";
import path from "node:path"; import path from "node:path";
export interface Inputs { export interface Inputs {
@ -15,6 +14,16 @@ export interface Inputs {
buildArgs: string[]; buildArgs: string[];
} }
/**
* Retrieves an action input.
* @param key - The key of the action input.
* @returns The action input value as a string.
*/
function getInput(key: string): string {
const value = process.env[`INPUT_${key.toUpperCase()}`] || "";
return value.trim();
}
export function getInputs(): Inputs { export function getInputs(): Inputs {
const sourceDir = getInput("source-dir"); const sourceDir = getInput("source-dir");
return { return {
@ -23,13 +32,17 @@ export function getInputs(): Inputs {
generator: getInput("generator"), generator: getInput("generator"),
cCompiler: getInput("c-compiler"), cCompiler: getInput("c-compiler"),
cxxCompiler: getInput("cxx-compiler"), cxxCompiler: getInput("cxx-compiler"),
cFlags: getMultilineInput("c-flags").join(" "), cFlags: getInput("c-flags").replaceAll(/\s+/g, " "),
cxxFlags: getMultilineInput("cxx-flags").join(" "), cxxFlags: getInput("cxx-flags").replaceAll(/\s+/g, " "),
options: getMultilineInput("options").flatMap((opts) => opts.split(" ")), options: getInput("options")
args: getMultilineInput("args").flatMap((args) => args.split(" ")), .split(/\s+/)
runBuild: getBooleanInput("run-build"), .filter((arg) => arg != ""),
buildArgs: getMultilineInput("build-args").flatMap((args) => args: getInput("args")
args.split(" "), .split(/\s+/)
), .filter((arg) => arg != ""),
runBuild: getInput("run-build") == "true",
buildArgs: getInput("build-args")
.split(/\s+/)
.filter((arg) => arg != ""),
}; };
} }

53
yarn.lock generated
View File

@ -12,26 +12,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@actions/core@npm:^1.10.1":
version: 1.10.1
resolution: "@actions/core@npm:1.10.1"
dependencies:
"@actions/http-client": "npm:^2.0.1"
uuid: "npm:^8.3.2"
checksum: 10c0/7a61446697a23dcad3545cf0634dedbdedf20ae9a0ee6ee977554589a15deb4a93593ee48a41258933d58ce0778f446b0d2c162b60750956fb75e0b9560fb832
languageName: node
linkType: hard
"@actions/http-client@npm:^2.0.1":
version: 2.2.0
resolution: "@actions/http-client@npm:2.2.0"
dependencies:
tunnel: "npm:^0.0.6"
undici: "npm:^5.25.4"
checksum: 10c0/868fe8529d78beb72f84ea2486e232fa6f66abe00d6ec4591b98c37e762c3d812868a3548638d75b49917961fd10ba1556916b47b1e9e4b55c266e2013c3ae8e
languageName: node
linkType: hard
"@ampproject/remapping@npm:^2.2.0": "@ampproject/remapping@npm:^2.2.0":
version: 2.3.0 version: 2.3.0
resolution: "@ampproject/remapping@npm:2.3.0" resolution: "@ampproject/remapping@npm:2.3.0"
@ -502,13 +482,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@fastify/busboy@npm:^2.0.0":
version: 2.1.0
resolution: "@fastify/busboy@npm:2.1.0"
checksum: 10c0/7bb641080aac7cf01d88749ad331af10ba9ec3713ec07cabbe833908c75df21bd56249bb6173bdec07f5a41896b21e3689316f86684c06635da45f91ff4565a2
languageName: node
linkType: hard
"@humanwhocodes/module-importer@npm:^1.0.1": "@humanwhocodes/module-importer@npm:^1.0.1":
version: 1.0.1 version: 1.0.1
resolution: "@humanwhocodes/module-importer@npm:1.0.1" resolution: "@humanwhocodes/module-importer@npm:1.0.1"
@ -3891,7 +3864,6 @@ __metadata:
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "root@workspace:." resolution: "root@workspace:."
dependencies: dependencies:
"@actions/core": "npm:^1.10.1"
"@eslint/js": "npm:^9.8.0" "@eslint/js": "npm:^9.8.0"
"@jest/globals": "npm:^29.7.0" "@jest/globals": "npm:^29.7.0"
"@types/jest": "npm:^29.5.12" "@types/jest": "npm:^29.5.12"
@ -4270,13 +4242,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"tunnel@npm:^0.0.6":
version: 0.0.6
resolution: "tunnel@npm:0.0.6"
checksum: 10c0/e27e7e896f2426c1c747325b5f54efebc1a004647d853fad892b46d64e37591ccd0b97439470795e5262b5c0748d22beb4489a04a0a448029636670bfd801b75
languageName: node
linkType: hard
"type-check@npm:^0.4.0, type-check@npm:~0.4.0": "type-check@npm:^0.4.0, type-check@npm:~0.4.0":
version: 0.4.0 version: 0.4.0
resolution: "type-check@npm:0.4.0" resolution: "type-check@npm:0.4.0"
@ -4348,15 +4313,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"undici@npm:^5.25.4":
version: 5.28.4
resolution: "undici@npm:5.28.4"
dependencies:
"@fastify/busboy": "npm:^2.0.0"
checksum: 10c0/08d0f2596553aa0a54ca6e8e9c7f45aef7d042c60918564e3a142d449eda165a80196f6ef19ea2ef2e6446959e293095d8e40af1236f0d67223b06afac5ecad7
languageName: node
linkType: hard
"unique-filename@npm:^3.0.0": "unique-filename@npm:^3.0.0":
version: 3.0.0 version: 3.0.0
resolution: "unique-filename@npm:3.0.0" resolution: "unique-filename@npm:3.0.0"
@ -4398,15 +4354,6 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
uuid: dist/bin/uuid
checksum: 10c0/bcbb807a917d374a49f475fae2e87fdca7da5e5530820ef53f65ba1d12131bd81a92ecf259cc7ce317cbe0f289e7d79fdfebcef9bfa3087c8c8a2fa304c9be54
languageName: node
linkType: hard
"v8-to-istanbul@npm:^9.0.1": "v8-to-istanbul@npm:^9.0.1":
version: 9.2.0 version: 9.2.0
resolution: "v8-to-istanbul@npm:9.2.0" resolution: "v8-to-istanbul@npm:9.2.0"