mirror of
https://github.com/threeal/cmake-action.git
synced 2025-04-20 10:41:21 +00:00
refactor: rename inputs
to context
This commit is contained in:
parent
9708492b8e
commit
98f46f3e06
52
dist/index.mjs
generated
vendored
52
dist/index.mjs
generated
vendored
@ -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.
|
||||
*
|
||||
* @param inputs - The action inputs.
|
||||
* @param context - The action context.
|
||||
*/
|
||||
function configureProject(inputs) {
|
||||
function configureProject(context) {
|
||||
const configureArgs = [];
|
||||
if (inputs.sourceDir) {
|
||||
configureArgs.push(inputs.sourceDir);
|
||||
if (context.sourceDir) {
|
||||
configureArgs.push(context.sourceDir);
|
||||
}
|
||||
configureArgs.push("-B", inputs.buildDir);
|
||||
if (inputs.generator) {
|
||||
configureArgs.push(...["-G", inputs.generator]);
|
||||
configureArgs.push("-B", context.buildDir);
|
||||
if (context.generator) {
|
||||
configureArgs.push(...["-G", context.generator]);
|
||||
}
|
||||
if (inputs.cCompiler) {
|
||||
configureArgs.push("-DCMAKE_C_COMPILER=" + inputs.cCompiler);
|
||||
if (context.cCompiler) {
|
||||
configureArgs.push("-DCMAKE_C_COMPILER=" + context.cCompiler);
|
||||
}
|
||||
if (inputs.cxxCompiler) {
|
||||
configureArgs.push("-DCMAKE_CXX_COMPILER=" + inputs.cxxCompiler);
|
||||
if (context.cxxCompiler) {
|
||||
configureArgs.push("-DCMAKE_CXX_COMPILER=" + context.cxxCompiler);
|
||||
}
|
||||
if (inputs.cFlags) {
|
||||
configureArgs.push("-DCMAKE_C_FLAGS=" + inputs.cFlags);
|
||||
if (context.cFlags) {
|
||||
configureArgs.push("-DCMAKE_C_FLAGS=" + context.cFlags);
|
||||
}
|
||||
if (inputs.cxxFlags) {
|
||||
configureArgs.push("-DCMAKE_CXX_FLAGS=" + inputs.cxxFlags);
|
||||
if (context.cxxFlags) {
|
||||
configureArgs.push("-DCMAKE_CXX_FLAGS=" + context.cxxFlags);
|
||||
}
|
||||
configureArgs.push(...inputs.options.map((opt) => "-D" + opt));
|
||||
configureArgs.push(...inputs.args);
|
||||
configureArgs.push(...context.options.map((opt) => "-D" + opt));
|
||||
configureArgs.push(...context.args);
|
||||
execFileSync("cmake", configureArgs, { stdio: "inherit" });
|
||||
}
|
||||
/**
|
||||
* Build a CMake project.
|
||||
*
|
||||
* @param inputs - The action inputs.
|
||||
* @param context - The action context.
|
||||
*/
|
||||
function buildProject(inputs) {
|
||||
execFileSync("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs], {
|
||||
function buildProject(context) {
|
||||
execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], {
|
||||
stdio: "inherit",
|
||||
});
|
||||
}
|
||||
@ -55,7 +55,7 @@ function getInput(key) {
|
||||
const value = process.env[`INPUT_${key.toUpperCase()}`] || "";
|
||||
return value.trim();
|
||||
}
|
||||
function getInputs() {
|
||||
function getContext() {
|
||||
const sourceDir = getInput("source-dir");
|
||||
return {
|
||||
sourceDir,
|
||||
@ -79,11 +79,11 @@ function getInputs() {
|
||||
}
|
||||
|
||||
try {
|
||||
const inputs = getInputs();
|
||||
configureProject(inputs);
|
||||
fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${inputs.buildDir}${os.EOL}`);
|
||||
if (inputs.runBuild) {
|
||||
buildProject(inputs);
|
||||
const context = getContext();
|
||||
configureProject(context);
|
||||
fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`);
|
||||
if (context.runBuild) {
|
||||
buildProject(context);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
|
@ -1,13 +1,13 @@
|
||||
import { jest } from "@jest/globals";
|
||||
import type { Inputs } from "./inputs.js";
|
||||
import type { Context } from "./context.js";
|
||||
|
||||
interface TestCase {
|
||||
name: string;
|
||||
inputs?: Partial<Inputs>;
|
||||
context?: Partial<Context>;
|
||||
expectedArgs: string[];
|
||||
}
|
||||
|
||||
const defaultInputs: Inputs = {
|
||||
const defaultContext: Context = {
|
||||
sourceDir: "",
|
||||
buildDir: "build",
|
||||
generator: "",
|
||||
@ -33,42 +33,42 @@ describe("configure a CMake project", () => {
|
||||
},
|
||||
{
|
||||
name: "with source directory specified",
|
||||
inputs: { sourceDir: "project" },
|
||||
context: { sourceDir: "project" },
|
||||
expectedArgs: ["project", "-B", "build"],
|
||||
},
|
||||
{
|
||||
name: "with build directory specified",
|
||||
inputs: { buildDir: "output" },
|
||||
context: { buildDir: "output" },
|
||||
expectedArgs: ["-B", "output"],
|
||||
},
|
||||
{
|
||||
name: "with generator specified",
|
||||
inputs: { generator: "Ninja" },
|
||||
context: { generator: "Ninja" },
|
||||
expectedArgs: ["-B", "build", "-G", "Ninja"],
|
||||
},
|
||||
{
|
||||
name: "with C compiler specified",
|
||||
inputs: { cCompiler: "clang" },
|
||||
context: { cCompiler: "clang" },
|
||||
expectedArgs: ["-B", "build", "-DCMAKE_C_COMPILER=clang"],
|
||||
},
|
||||
{
|
||||
name: "with C++ compiler specified",
|
||||
inputs: { cxxCompiler: "clang++" },
|
||||
context: { cxxCompiler: "clang++" },
|
||||
expectedArgs: ["-B", "build", "-DCMAKE_CXX_COMPILER=clang++"],
|
||||
},
|
||||
{
|
||||
name: "with C flags specified",
|
||||
inputs: { cFlags: "-Werror -Wall" },
|
||||
context: { cFlags: "-Werror -Wall" },
|
||||
expectedArgs: ["-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"],
|
||||
},
|
||||
{
|
||||
name: "with C++ flags specified",
|
||||
inputs: { cxxFlags: "-Werror -Wall -Wextra" },
|
||||
context: { cxxFlags: "-Werror -Wall -Wextra" },
|
||||
expectedArgs: ["-B", "build", "-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra"],
|
||||
},
|
||||
{
|
||||
name: "with additional options specified",
|
||||
inputs: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] },
|
||||
context: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] },
|
||||
expectedArgs: [
|
||||
"-B",
|
||||
"build",
|
||||
@ -78,12 +78,12 @@ describe("configure a CMake project", () => {
|
||||
},
|
||||
{
|
||||
name: "with additional arguments specified",
|
||||
inputs: { args: ["-Wdev", "-Wdeprecated"] },
|
||||
context: { args: ["-Wdev", "-Wdeprecated"] },
|
||||
expectedArgs: ["-B", "build", "-Wdev", "-Wdeprecated"],
|
||||
},
|
||||
{
|
||||
name: "with all specified",
|
||||
inputs: {
|
||||
context: {
|
||||
sourceDir: "project",
|
||||
buildDir: "output",
|
||||
generator: "Ninja",
|
||||
@ -119,7 +119,7 @@ describe("configure a CMake project", () => {
|
||||
|
||||
jest.mocked(execFileSync).mockReset();
|
||||
|
||||
configureProject({ ...defaultInputs, ...testCase.inputs });
|
||||
configureProject({ ...defaultContext, ...testCase.context });
|
||||
|
||||
expect(execFileSync).toHaveBeenCalledTimes(1);
|
||||
expect(execFileSync).toHaveBeenLastCalledWith(
|
||||
@ -139,17 +139,17 @@ describe("build a CMake project", () => {
|
||||
},
|
||||
{
|
||||
name: "with build directory specified",
|
||||
inputs: { buildDir: "output" },
|
||||
context: { buildDir: "output" },
|
||||
expectedArgs: ["--build", "output"],
|
||||
},
|
||||
{
|
||||
name: "with additional arguments specified",
|
||||
inputs: { buildArgs: ["--target", "foo"] },
|
||||
context: { buildArgs: ["--target", "foo"] },
|
||||
expectedArgs: ["--build", "build", "--target", "foo"],
|
||||
},
|
||||
{
|
||||
name: "with all specified",
|
||||
inputs: {
|
||||
context: {
|
||||
buildDir: "output",
|
||||
buildArgs: ["--target", "foo"],
|
||||
},
|
||||
@ -164,7 +164,7 @@ describe("build a CMake project", () => {
|
||||
|
||||
jest.mocked(execFileSync).mockReset();
|
||||
|
||||
buildProject({ ...defaultInputs, ...testCase.inputs });
|
||||
buildProject({ ...defaultContext, ...testCase.context });
|
||||
|
||||
expect(execFileSync).toHaveBeenCalledTimes(1);
|
||||
expect(execFileSync).toHaveBeenLastCalledWith(
|
||||
|
42
src/cmake.ts
42
src/cmake.ts
@ -1,42 +1,42 @@
|
||||
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.
|
||||
*
|
||||
* @param inputs - The action inputs.
|
||||
* @param context - The action context.
|
||||
*/
|
||||
export function configureProject(inputs: Inputs): void {
|
||||
export function configureProject(context: Context): void {
|
||||
const configureArgs = [];
|
||||
|
||||
if (inputs.sourceDir) {
|
||||
configureArgs.push(inputs.sourceDir);
|
||||
if (context.sourceDir) {
|
||||
configureArgs.push(context.sourceDir);
|
||||
}
|
||||
|
||||
configureArgs.push("-B", inputs.buildDir);
|
||||
configureArgs.push("-B", context.buildDir);
|
||||
|
||||
if (inputs.generator) {
|
||||
configureArgs.push(...["-G", inputs.generator]);
|
||||
if (context.generator) {
|
||||
configureArgs.push(...["-G", context.generator]);
|
||||
}
|
||||
|
||||
if (inputs.cCompiler) {
|
||||
configureArgs.push("-DCMAKE_C_COMPILER=" + inputs.cCompiler);
|
||||
if (context.cCompiler) {
|
||||
configureArgs.push("-DCMAKE_C_COMPILER=" + context.cCompiler);
|
||||
}
|
||||
|
||||
if (inputs.cxxCompiler) {
|
||||
configureArgs.push("-DCMAKE_CXX_COMPILER=" + inputs.cxxCompiler);
|
||||
if (context.cxxCompiler) {
|
||||
configureArgs.push("-DCMAKE_CXX_COMPILER=" + context.cxxCompiler);
|
||||
}
|
||||
|
||||
if (inputs.cFlags) {
|
||||
configureArgs.push("-DCMAKE_C_FLAGS=" + inputs.cFlags);
|
||||
if (context.cFlags) {
|
||||
configureArgs.push("-DCMAKE_C_FLAGS=" + context.cFlags);
|
||||
}
|
||||
|
||||
if (inputs.cxxFlags) {
|
||||
configureArgs.push("-DCMAKE_CXX_FLAGS=" + inputs.cxxFlags);
|
||||
if (context.cxxFlags) {
|
||||
configureArgs.push("-DCMAKE_CXX_FLAGS=" + context.cxxFlags);
|
||||
}
|
||||
|
||||
configureArgs.push(...inputs.options.map((opt) => "-D" + opt));
|
||||
configureArgs.push(...inputs.args);
|
||||
configureArgs.push(...context.options.map((opt) => "-D" + opt));
|
||||
configureArgs.push(...context.args);
|
||||
|
||||
execFileSync("cmake", configureArgs, { stdio: "inherit" });
|
||||
}
|
||||
@ -44,10 +44,10 @@ export function configureProject(inputs: Inputs): void {
|
||||
/**
|
||||
* Build a CMake project.
|
||||
*
|
||||
* @param inputs - The action inputs.
|
||||
* @param context - The action context.
|
||||
*/
|
||||
export function buildProject(inputs: Inputs): void {
|
||||
execFileSync("cmake", ["--build", inputs.buildDir, ...inputs.buildArgs], {
|
||||
export function buildProject(context: Context): void {
|
||||
execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], {
|
||||
stdio: "inherit",
|
||||
});
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
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 {
|
||||
name: string;
|
||||
env?: Record<string, string>;
|
||||
expectedInputs?: Partial<Inputs>;
|
||||
expectedContext?: Partial<Context>;
|
||||
}
|
||||
|
||||
const testCases: TestCase[] = [
|
||||
@ -15,7 +15,7 @@ describe("get action inputs", () => {
|
||||
{
|
||||
name: "with source directory specified",
|
||||
env: { "INPUT_SOURCE-DIR": "project" },
|
||||
expectedInputs: {
|
||||
expectedContext: {
|
||||
sourceDir: "project",
|
||||
buildDir: path.join("project", "build"),
|
||||
},
|
||||
@ -23,7 +23,7 @@ describe("get action inputs", () => {
|
||||
{
|
||||
name: "with build directory specified",
|
||||
env: { "INPUT_BUILD-DIR": "output" },
|
||||
expectedInputs: { buildDir: "output" },
|
||||
expectedContext: { buildDir: "output" },
|
||||
},
|
||||
{
|
||||
name: "with source and build directories specified",
|
||||
@ -31,7 +31,7 @@ describe("get action inputs", () => {
|
||||
"INPUT_SOURCE-DIR": "project",
|
||||
"INPUT_BUILD-DIR": "output",
|
||||
},
|
||||
expectedInputs: {
|
||||
expectedContext: {
|
||||
sourceDir: "project",
|
||||
buildDir: "output",
|
||||
},
|
||||
@ -39,51 +39,51 @@ describe("get action inputs", () => {
|
||||
{
|
||||
name: "with generator specified",
|
||||
env: { INPUT_GENERATOR: "Ninja" },
|
||||
expectedInputs: { generator: "Ninja" },
|
||||
expectedContext: { generator: "Ninja" },
|
||||
},
|
||||
{
|
||||
name: "with C compiler specified",
|
||||
env: { "INPUT_C-COMPILER": "clang" },
|
||||
expectedInputs: { cCompiler: "clang" },
|
||||
expectedContext: { cCompiler: "clang" },
|
||||
},
|
||||
{
|
||||
name: "with C++ compiler specified",
|
||||
env: { "INPUT_CXX-COMPILER": "clang++" },
|
||||
expectedInputs: { cxxCompiler: "clang++" },
|
||||
expectedContext: { cxxCompiler: "clang++" },
|
||||
},
|
||||
{
|
||||
name: "with C flags specified",
|
||||
env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" },
|
||||
expectedInputs: { cFlags: "-Werror -Wall -Wextra" },
|
||||
expectedContext: { cFlags: "-Werror -Wall -Wextra" },
|
||||
},
|
||||
{
|
||||
name: "with C++ flags specified",
|
||||
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",
|
||||
env: {
|
||||
INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
|
||||
},
|
||||
expectedInputs: {
|
||||
expectedContext: {
|
||||
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with additional arguments specified",
|
||||
env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" },
|
||||
expectedInputs: { args: ["-Wdev", "-Wdeprecated", "--fresh"] },
|
||||
expectedContext: { args: ["-Wdev", "-Wdeprecated", "--fresh"] },
|
||||
},
|
||||
{
|
||||
name: "with run build specified",
|
||||
env: { "INPUT_RUN-BUILD": "true" },
|
||||
expectedInputs: { runBuild: true },
|
||||
expectedContext: { runBuild: true },
|
||||
},
|
||||
{
|
||||
name: "with additional build arguments specified",
|
||||
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",
|
||||
@ -100,7 +100,7 @@ describe("get action inputs", () => {
|
||||
"INPUT_RUN-BUILD": "true",
|
||||
"INPUT_BUILD-ARGS": "--target foo\n--parallel 8",
|
||||
},
|
||||
expectedInputs: {
|
||||
expectedContext: {
|
||||
sourceDir: "project",
|
||||
buildDir: "output",
|
||||
generator: "Ninja",
|
||||
@ -117,14 +117,14 @@ describe("get action inputs", () => {
|
||||
];
|
||||
|
||||
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;
|
||||
process.env = {
|
||||
...process.env,
|
||||
...testCase.env,
|
||||
};
|
||||
|
||||
expect(getInputs()).toStrictEqual({
|
||||
expect(getContext()).toStrictEqual({
|
||||
sourceDir: "",
|
||||
buildDir: "build",
|
||||
generator: "",
|
||||
@ -136,7 +136,7 @@ describe("get action inputs", () => {
|
||||
args: [],
|
||||
runBuild: false,
|
||||
buildArgs: [],
|
||||
...testCase.expectedInputs,
|
||||
...testCase.expectedContext,
|
||||
});
|
||||
|
||||
process.env = prevEnv;
|
@ -1,6 +1,6 @@
|
||||
import path from "node:path";
|
||||
|
||||
export interface Inputs {
|
||||
export interface Context {
|
||||
sourceDir: string;
|
||||
buildDir: string;
|
||||
generator: string;
|
||||
@ -24,7 +24,7 @@ function getInput(key: string): string {
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
export function getInputs(): Inputs {
|
||||
export function getContext(): Context {
|
||||
const sourceDir = getInput("source-dir");
|
||||
return {
|
||||
sourceDir,
|
12
src/index.ts
12
src/index.ts
@ -2,20 +2,20 @@ import { getErrorMessage } from "catched-error-message";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import { buildProject, configureProject } from "./cmake.js";
|
||||
import { getInputs } from "./inputs.js";
|
||||
import { getContext } from "./context.js";
|
||||
|
||||
try {
|
||||
const inputs = getInputs();
|
||||
const context = getContext();
|
||||
|
||||
configureProject(inputs);
|
||||
configureProject(context);
|
||||
|
||||
fs.appendFileSync(
|
||||
process.env["GITHUB_OUTPUT"] as string,
|
||||
`build-dir=${inputs.buildDir}${os.EOL}`,
|
||||
`build-dir=${context.buildDir}${os.EOL}`,
|
||||
);
|
||||
|
||||
if (inputs.runBuild) {
|
||||
buildProject(inputs);
|
||||
if (context.runBuild) {
|
||||
buildProject(context);
|
||||
}
|
||||
} catch (err) {
|
||||
process.exitCode = 1;
|
||||
|
Loading…
Reference in New Issue
Block a user