refactor: rename inputs to context

This commit is contained in:
Alfi Maulana 2024-08-07 20:30:22 +07:00
parent 9708492b8e
commit 98f46f3e06
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4
6 changed files with 92 additions and 92 deletions

52
dist/index.mjs generated vendored
View File

@ -8,40 +8,40 @@ function r(r){return function(r){if("object"==typeof(e=r)&&null!==e&&"message"in
/** /**
* Configures the build system of a CMake project. * Configures the build system of a CMake project.
* *
* @param inputs - The action inputs. * @param context - The action context.
*/ */
function configureProject(inputs) { function configureProject(context) {
const configureArgs = []; const configureArgs = [];
if (inputs.sourceDir) { if (context.sourceDir) {
configureArgs.push(inputs.sourceDir); configureArgs.push(context.sourceDir);
} }
configureArgs.push("-B", inputs.buildDir); configureArgs.push("-B", context.buildDir);
if (inputs.generator) { if (context.generator) {
configureArgs.push(...["-G", inputs.generator]); configureArgs.push(...["-G", context.generator]);
} }
if (inputs.cCompiler) { if (context.cCompiler) {
configureArgs.push("-DCMAKE_C_COMPILER=" + inputs.cCompiler); configureArgs.push("-DCMAKE_C_COMPILER=" + context.cCompiler);
} }
if (inputs.cxxCompiler) { if (context.cxxCompiler) {
configureArgs.push("-DCMAKE_CXX_COMPILER=" + inputs.cxxCompiler); configureArgs.push("-DCMAKE_CXX_COMPILER=" + context.cxxCompiler);
} }
if (inputs.cFlags) { if (context.cFlags) {
configureArgs.push("-DCMAKE_C_FLAGS=" + inputs.cFlags); configureArgs.push("-DCMAKE_C_FLAGS=" + context.cFlags);
} }
if (inputs.cxxFlags) { if (context.cxxFlags) {
configureArgs.push("-DCMAKE_CXX_FLAGS=" + inputs.cxxFlags); configureArgs.push("-DCMAKE_CXX_FLAGS=" + context.cxxFlags);
} }
configureArgs.push(...inputs.options.map((opt) => "-D" + opt)); configureArgs.push(...context.options.map((opt) => "-D" + opt));
configureArgs.push(...inputs.args); configureArgs.push(...context.args);
execFileSync("cmake", configureArgs, { stdio: "inherit" }); execFileSync("cmake", configureArgs, { stdio: "inherit" });
} }
/** /**
* Build a CMake project. * Build a CMake project.
* *
* @param inputs - The action inputs. * @param context - The action context.
*/ */
function buildProject(inputs) { function buildProject(context) {
execFileSync("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs], { execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], {
stdio: "inherit", stdio: "inherit",
}); });
} }
@ -55,7 +55,7 @@ function getInput(key) {
const value = process.env[`INPUT_${key.toUpperCase()}`] || ""; const value = process.env[`INPUT_${key.toUpperCase()}`] || "";
return value.trim(); return value.trim();
} }
function getInputs() { function getContext() {
const sourceDir = getInput("source-dir"); const sourceDir = getInput("source-dir");
return { return {
sourceDir, sourceDir,
@ -79,11 +79,11 @@ function getInputs() {
} }
try { try {
const inputs = getInputs(); const context = getContext();
configureProject(inputs); configureProject(context);
fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${inputs.buildDir}${os.EOL}`); fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`);
if (inputs.runBuild) { if (context.runBuild) {
buildProject(inputs); buildProject(context);
} }
} }
catch (err) { catch (err) {

View File

@ -1,13 +1,13 @@
import { jest } from "@jest/globals"; import { jest } from "@jest/globals";
import type { Inputs } from "./inputs.js"; import type { Context } from "./context.js";
interface TestCase { interface TestCase {
name: string; name: string;
inputs?: Partial<Inputs>; context?: Partial<Context>;
expectedArgs: string[]; expectedArgs: string[];
} }
const defaultInputs: Inputs = { const defaultContext: Context = {
sourceDir: "", sourceDir: "",
buildDir: "build", buildDir: "build",
generator: "", generator: "",
@ -33,42 +33,42 @@ describe("configure a CMake project", () => {
}, },
{ {
name: "with source directory specified", name: "with source directory specified",
inputs: { sourceDir: "project" }, context: { sourceDir: "project" },
expectedArgs: ["project", "-B", "build"], expectedArgs: ["project", "-B", "build"],
}, },
{ {
name: "with build directory specified", name: "with build directory specified",
inputs: { buildDir: "output" }, context: { buildDir: "output" },
expectedArgs: ["-B", "output"], expectedArgs: ["-B", "output"],
}, },
{ {
name: "with generator specified", name: "with generator specified",
inputs: { generator: "Ninja" }, context: { generator: "Ninja" },
expectedArgs: ["-B", "build", "-G", "Ninja"], expectedArgs: ["-B", "build", "-G", "Ninja"],
}, },
{ {
name: "with C compiler specified", name: "with C compiler specified",
inputs: { cCompiler: "clang" }, context: { cCompiler: "clang" },
expectedArgs: ["-B", "build", "-DCMAKE_C_COMPILER=clang"], expectedArgs: ["-B", "build", "-DCMAKE_C_COMPILER=clang"],
}, },
{ {
name: "with C++ compiler specified", name: "with C++ compiler specified",
inputs: { cxxCompiler: "clang++" }, context: { cxxCompiler: "clang++" },
expectedArgs: ["-B", "build", "-DCMAKE_CXX_COMPILER=clang++"], expectedArgs: ["-B", "build", "-DCMAKE_CXX_COMPILER=clang++"],
}, },
{ {
name: "with C flags specified", name: "with C flags specified",
inputs: { cFlags: "-Werror -Wall" }, context: { cFlags: "-Werror -Wall" },
expectedArgs: ["-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"], expectedArgs: ["-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"],
}, },
{ {
name: "with C++ flags specified", name: "with C++ flags specified",
inputs: { cxxFlags: "-Werror -Wall -Wextra" }, context: { cxxFlags: "-Werror -Wall -Wextra" },
expectedArgs: ["-B", "build", "-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra"], expectedArgs: ["-B", "build", "-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra"],
}, },
{ {
name: "with additional options specified", name: "with additional options specified",
inputs: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] }, context: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] },
expectedArgs: [ expectedArgs: [
"-B", "-B",
"build", "build",
@ -78,12 +78,12 @@ describe("configure a CMake project", () => {
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
inputs: { args: ["-Wdev", "-Wdeprecated"] }, context: { args: ["-Wdev", "-Wdeprecated"] },
expectedArgs: ["-B", "build", "-Wdev", "-Wdeprecated"], expectedArgs: ["-B", "build", "-Wdev", "-Wdeprecated"],
}, },
{ {
name: "with all specified", name: "with all specified",
inputs: { context: {
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
generator: "Ninja", generator: "Ninja",
@ -119,7 +119,7 @@ describe("configure a CMake project", () => {
jest.mocked(execFileSync).mockReset(); jest.mocked(execFileSync).mockReset();
configureProject({ ...defaultInputs, ...testCase.inputs }); configureProject({ ...defaultContext, ...testCase.context });
expect(execFileSync).toHaveBeenCalledTimes(1); expect(execFileSync).toHaveBeenCalledTimes(1);
expect(execFileSync).toHaveBeenLastCalledWith( expect(execFileSync).toHaveBeenLastCalledWith(
@ -139,17 +139,17 @@ describe("build a CMake project", () => {
}, },
{ {
name: "with build directory specified", name: "with build directory specified",
inputs: { buildDir: "output" }, context: { buildDir: "output" },
expectedArgs: ["--build", "output"], expectedArgs: ["--build", "output"],
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
inputs: { buildArgs: ["--target", "foo"] }, context: { buildArgs: ["--target", "foo"] },
expectedArgs: ["--build", "build", "--target", "foo"], expectedArgs: ["--build", "build", "--target", "foo"],
}, },
{ {
name: "with all specified", name: "with all specified",
inputs: { context: {
buildDir: "output", buildDir: "output",
buildArgs: ["--target", "foo"], buildArgs: ["--target", "foo"],
}, },
@ -164,7 +164,7 @@ describe("build a CMake project", () => {
jest.mocked(execFileSync).mockReset(); jest.mocked(execFileSync).mockReset();
buildProject({ ...defaultInputs, ...testCase.inputs }); buildProject({ ...defaultContext, ...testCase.context });
expect(execFileSync).toHaveBeenCalledTimes(1); expect(execFileSync).toHaveBeenCalledTimes(1);
expect(execFileSync).toHaveBeenLastCalledWith( expect(execFileSync).toHaveBeenLastCalledWith(

View File

@ -1,42 +1,42 @@
import { execFileSync } from "node:child_process"; import { execFileSync } from "node:child_process";
import type { Inputs } from "./inputs.js"; import type { Context } from "./context.js";
/** /**
* Configures the build system of a CMake project. * Configures the build system of a CMake project.
* *
* @param inputs - The action inputs. * @param context - The action context.
*/ */
export function configureProject(inputs: Inputs): void { export function configureProject(context: Context): void {
const configureArgs = []; const configureArgs = [];
if (inputs.sourceDir) { if (context.sourceDir) {
configureArgs.push(inputs.sourceDir); configureArgs.push(context.sourceDir);
} }
configureArgs.push("-B", inputs.buildDir); configureArgs.push("-B", context.buildDir);
if (inputs.generator) { if (context.generator) {
configureArgs.push(...["-G", inputs.generator]); configureArgs.push(...["-G", context.generator]);
} }
if (inputs.cCompiler) { if (context.cCompiler) {
configureArgs.push("-DCMAKE_C_COMPILER=" + inputs.cCompiler); configureArgs.push("-DCMAKE_C_COMPILER=" + context.cCompiler);
} }
if (inputs.cxxCompiler) { if (context.cxxCompiler) {
configureArgs.push("-DCMAKE_CXX_COMPILER=" + inputs.cxxCompiler); configureArgs.push("-DCMAKE_CXX_COMPILER=" + context.cxxCompiler);
} }
if (inputs.cFlags) { if (context.cFlags) {
configureArgs.push("-DCMAKE_C_FLAGS=" + inputs.cFlags); configureArgs.push("-DCMAKE_C_FLAGS=" + context.cFlags);
} }
if (inputs.cxxFlags) { if (context.cxxFlags) {
configureArgs.push("-DCMAKE_CXX_FLAGS=" + inputs.cxxFlags); configureArgs.push("-DCMAKE_CXX_FLAGS=" + context.cxxFlags);
} }
configureArgs.push(...inputs.options.map((opt) => "-D" + opt)); configureArgs.push(...context.options.map((opt) => "-D" + opt));
configureArgs.push(...inputs.args); configureArgs.push(...context.args);
execFileSync("cmake", configureArgs, { stdio: "inherit" }); execFileSync("cmake", configureArgs, { stdio: "inherit" });
} }
@ -44,10 +44,10 @@ export function configureProject(inputs: Inputs): void {
/** /**
* Build a CMake project. * Build a CMake project.
* *
* @param inputs - The action inputs. * @param context - The action context.
*/ */
export function buildProject(inputs: Inputs): void { export function buildProject(context: Context): void {
execFileSync("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs], { execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], {
stdio: "inherit", stdio: "inherit",
}); });
} }

View File

@ -1,11 +1,11 @@
import path from "node:path"; import path from "node:path";
import { Inputs, getInputs } from "./inputs.js"; import { Context, getContext } from "./context.js";
describe("get action inputs", () => { describe("get action context", () => {
interface TestCase { interface TestCase {
name: string; name: string;
env?: Record<string, string>; env?: Record<string, string>;
expectedInputs?: Partial<Inputs>; expectedContext?: Partial<Context>;
} }
const testCases: TestCase[] = [ const testCases: TestCase[] = [
@ -15,7 +15,7 @@ describe("get action inputs", () => {
{ {
name: "with source directory specified", name: "with source directory specified",
env: { "INPUT_SOURCE-DIR": "project" }, env: { "INPUT_SOURCE-DIR": "project" },
expectedInputs: { expectedContext: {
sourceDir: "project", sourceDir: "project",
buildDir: path.join("project", "build"), buildDir: path.join("project", "build"),
}, },
@ -23,7 +23,7 @@ describe("get action inputs", () => {
{ {
name: "with build directory specified", name: "with build directory specified",
env: { "INPUT_BUILD-DIR": "output" }, env: { "INPUT_BUILD-DIR": "output" },
expectedInputs: { buildDir: "output" }, expectedContext: { buildDir: "output" },
}, },
{ {
name: "with source and build directories specified", name: "with source and build directories specified",
@ -31,7 +31,7 @@ describe("get action inputs", () => {
"INPUT_SOURCE-DIR": "project", "INPUT_SOURCE-DIR": "project",
"INPUT_BUILD-DIR": "output", "INPUT_BUILD-DIR": "output",
}, },
expectedInputs: { expectedContext: {
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
}, },
@ -39,51 +39,51 @@ describe("get action inputs", () => {
{ {
name: "with generator specified", name: "with generator specified",
env: { INPUT_GENERATOR: "Ninja" }, env: { INPUT_GENERATOR: "Ninja" },
expectedInputs: { generator: "Ninja" }, expectedContext: { generator: "Ninja" },
}, },
{ {
name: "with C compiler specified", name: "with C compiler specified",
env: { "INPUT_C-COMPILER": "clang" }, env: { "INPUT_C-COMPILER": "clang" },
expectedInputs: { cCompiler: "clang" }, expectedContext: { cCompiler: "clang" },
}, },
{ {
name: "with C++ compiler specified", name: "with C++ compiler specified",
env: { "INPUT_CXX-COMPILER": "clang++" }, env: { "INPUT_CXX-COMPILER": "clang++" },
expectedInputs: { cxxCompiler: "clang++" }, expectedContext: { cxxCompiler: "clang++" },
}, },
{ {
name: "with C flags specified", name: "with C flags specified",
env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" }, env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" },
expectedInputs: { cFlags: "-Werror -Wall -Wextra" }, expectedContext: { cFlags: "-Werror -Wall -Wextra" },
}, },
{ {
name: "with C++ flags specified", name: "with C++ flags specified",
env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" }, env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" },
expectedInputs: { cxxFlags: "-Werror -Wall -Wextra -Wpedantic" }, expectedContext: { cxxFlags: "-Werror -Wall -Wextra -Wpedantic" },
}, },
{ {
name: "with additional options specified", name: "with additional options specified",
env: { env: {
INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
}, },
expectedInputs: { expectedContext: {
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"], options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
}, },
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" }, env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" },
expectedInputs: { args: ["-Wdev", "-Wdeprecated", "--fresh"] }, expectedContext: { args: ["-Wdev", "-Wdeprecated", "--fresh"] },
}, },
{ {
name: "with run build specified", name: "with run build specified",
env: { "INPUT_RUN-BUILD": "true" }, env: { "INPUT_RUN-BUILD": "true" },
expectedInputs: { runBuild: true }, expectedContext: { runBuild: true },
}, },
{ {
name: "with additional build arguments specified", name: "with additional build arguments specified",
env: { "INPUT_BUILD-ARGS": "--target foo\n--parallel 8" }, env: { "INPUT_BUILD-ARGS": "--target foo\n--parallel 8" },
expectedInputs: { buildArgs: ["--target", "foo", "--parallel", "8"] }, expectedContext: { buildArgs: ["--target", "foo", "--parallel", "8"] },
}, },
{ {
name: "with all specified", name: "with all specified",
@ -100,7 +100,7 @@ describe("get action inputs", () => {
"INPUT_RUN-BUILD": "true", "INPUT_RUN-BUILD": "true",
"INPUT_BUILD-ARGS": "--target foo\n--parallel 8", "INPUT_BUILD-ARGS": "--target foo\n--parallel 8",
}, },
expectedInputs: { expectedContext: {
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
generator: "Ninja", generator: "Ninja",
@ -117,14 +117,14 @@ 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 context ${testCase.name}`, async () => {
const prevEnv = process.env; const prevEnv = process.env;
process.env = { process.env = {
...process.env, ...process.env,
...testCase.env, ...testCase.env,
}; };
expect(getInputs()).toStrictEqual({ expect(getContext()).toStrictEqual({
sourceDir: "", sourceDir: "",
buildDir: "build", buildDir: "build",
generator: "", generator: "",
@ -136,7 +136,7 @@ describe("get action inputs", () => {
args: [], args: [],
runBuild: false, runBuild: false,
buildArgs: [], buildArgs: [],
...testCase.expectedInputs, ...testCase.expectedContext,
}); });
process.env = prevEnv; process.env = prevEnv;

View File

@ -1,6 +1,6 @@
import path from "node:path"; import path from "node:path";
export interface Inputs { export interface Context {
sourceDir: string; sourceDir: string;
buildDir: string; buildDir: string;
generator: string; generator: string;
@ -24,7 +24,7 @@ function getInput(key: string): string {
return value.trim(); return value.trim();
} }
export function getInputs(): Inputs { export function getContext(): Context {
const sourceDir = getInput("source-dir"); const sourceDir = getInput("source-dir");
return { return {
sourceDir, sourceDir,

View File

@ -2,20 +2,20 @@ import { getErrorMessage } from "catched-error-message";
import fs from "node:fs"; import fs from "node:fs";
import os from "node:os"; import os from "node:os";
import { buildProject, configureProject } from "./cmake.js"; import { buildProject, configureProject } from "./cmake.js";
import { getInputs } from "./inputs.js"; import { getContext } from "./context.js";
try { try {
const inputs = getInputs(); const context = getContext();
configureProject(inputs); configureProject(context);
fs.appendFileSync( fs.appendFileSync(
process.env["GITHUB_OUTPUT"] as string, process.env["GITHUB_OUTPUT"] as string,
`build-dir=${inputs.buildDir}${os.EOL}`, `build-dir=${context.buildDir}${os.EOL}`,
); );
if (inputs.runBuild) { if (context.runBuild) {
buildProject(inputs); buildProject(context);
} }
} catch (err) { } catch (err) {
process.exitCode = 1; process.exitCode = 1;