mirror of
https://github.com/threeal/cmake-action.git
synced 2025-04-21 19:11:21 +00:00
refactor: refactor inputs to context (#400)
* refactor: rename `inputs` to `context` * refactor: merge compiler and flags inputs to options input * refactor: separate configure and build context
This commit is contained in:
parent
9708492b8e
commit
d1c539d005
100
dist/index.mjs
generated
vendored
100
dist/index.mjs
generated
vendored
@ -8,40 +8,28 @@ 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.configure.generator) {
|
||||||
configureArgs.push(...["-G", inputs.generator]);
|
configureArgs.push(...["-G", context.configure.generator]);
|
||||||
}
|
}
|
||||||
if (inputs.cCompiler) {
|
configureArgs.push(...context.configure.options.map((opt) => "-D" + opt));
|
||||||
configureArgs.push("-DCMAKE_C_COMPILER=" + inputs.cCompiler);
|
configureArgs.push(...context.configure.args);
|
||||||
}
|
|
||||||
if (inputs.cxxCompiler) {
|
|
||||||
configureArgs.push("-DCMAKE_CXX_COMPILER=" + inputs.cxxCompiler);
|
|
||||||
}
|
|
||||||
if (inputs.cFlags) {
|
|
||||||
configureArgs.push("-DCMAKE_C_FLAGS=" + inputs.cFlags);
|
|
||||||
}
|
|
||||||
if (inputs.cxxFlags) {
|
|
||||||
configureArgs.push("-DCMAKE_CXX_FLAGS=" + inputs.cxxFlags);
|
|
||||||
}
|
|
||||||
configureArgs.push(...inputs.options.map((opt) => "-D" + opt));
|
|
||||||
configureArgs.push(...inputs.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.build.args], {
|
||||||
stdio: "inherit",
|
stdio: "inherit",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -55,35 +43,57 @@ 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");
|
||||||
|
const options = [];
|
||||||
|
let input = getInput("c-compiler");
|
||||||
|
if (input)
|
||||||
|
options.push(`CMAKE_C_COMPILER=${input}`);
|
||||||
|
input = getInput("cxx-compiler");
|
||||||
|
if (input)
|
||||||
|
options.push(`CMAKE_CXX_COMPILER=${input}`);
|
||||||
|
input = getInput("c-flags");
|
||||||
|
if (input) {
|
||||||
|
const flags = input.replaceAll(/\s+/g, " ");
|
||||||
|
options.push(`CMAKE_C_FLAGS=${flags}`);
|
||||||
|
}
|
||||||
|
input = getInput("cxx-flags");
|
||||||
|
if (input) {
|
||||||
|
const flags = input.replaceAll(/\s+/g, " ");
|
||||||
|
options.push(`CMAKE_CXX_FLAGS=${flags}`);
|
||||||
|
}
|
||||||
|
input = getInput("options");
|
||||||
|
if (input) {
|
||||||
|
const opts = input.split(/\s+/).filter((arg) => arg != "");
|
||||||
|
for (const opt of opts) {
|
||||||
|
options.push(opt);
|
||||||
|
}
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
sourceDir,
|
sourceDir,
|
||||||
buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
|
buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
|
||||||
generator: getInput("generator"),
|
configure: {
|
||||||
cCompiler: getInput("c-compiler"),
|
generator: getInput("generator"),
|
||||||
cxxCompiler: getInput("cxx-compiler"),
|
options,
|
||||||
cFlags: getInput("c-flags").replaceAll(/\s+/g, " "),
|
args: getInput("args")
|
||||||
cxxFlags: getInput("cxx-flags").replaceAll(/\s+/g, " "),
|
.split(/\s+/)
|
||||||
options: getInput("options")
|
.filter((arg) => arg != ""),
|
||||||
.split(/\s+/)
|
},
|
||||||
.filter((arg) => arg != ""),
|
build: {
|
||||||
args: getInput("args")
|
enabled: getInput("run-build") == "true",
|
||||||
.split(/\s+/)
|
args: getInput("build-args")
|
||||||
.filter((arg) => arg != ""),
|
.split(/\s+/)
|
||||||
runBuild: getInput("run-build") == "true",
|
.filter((arg) => arg != ""),
|
||||||
buildArgs: getInput("build-args")
|
},
|
||||||
.split(/\s+/)
|
|
||||||
.filter((arg) => arg != ""),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
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.build.enabled) {
|
||||||
buildProject(inputs);
|
buildProject(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
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: "",
|
configure: {
|
||||||
cCompiler: "",
|
generator: "",
|
||||||
cxxCompiler: "",
|
options: [],
|
||||||
cFlags: "",
|
args: [],
|
||||||
cxxFlags: "",
|
},
|
||||||
options: [],
|
build: {
|
||||||
args: [],
|
enabled: true,
|
||||||
runBuild: true,
|
args: [],
|
||||||
buildArgs: [],
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
jest.unstable_mockModule("node:child_process", () => ({
|
jest.unstable_mockModule("node:child_process", () => ({
|
||||||
@ -33,42 +33,28 @@ 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: { configure: { generator: "Ninja", options: [], args: [] } },
|
||||||
expectedArgs: ["-B", "build", "-G", "Ninja"],
|
expectedArgs: ["-B", "build", "-G", "Ninja"],
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "with C compiler specified",
|
|
||||||
inputs: { cCompiler: "clang" },
|
|
||||||
expectedArgs: ["-B", "build", "-DCMAKE_C_COMPILER=clang"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "with C++ compiler specified",
|
|
||||||
inputs: { cxxCompiler: "clang++" },
|
|
||||||
expectedArgs: ["-B", "build", "-DCMAKE_CXX_COMPILER=clang++"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "with C flags specified",
|
|
||||||
inputs: { cFlags: "-Werror -Wall" },
|
|
||||||
expectedArgs: ["-B", "build", "-DCMAKE_C_FLAGS=-Werror -Wall"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "with C++ flags specified",
|
|
||||||
inputs: { cxxFlags: "-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: {
|
||||||
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
expectedArgs: [
|
expectedArgs: [
|
||||||
"-B",
|
"-B",
|
||||||
"build",
|
"build",
|
||||||
@ -78,21 +64,25 @@ describe("configure a CMake project", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with additional arguments specified",
|
name: "with additional arguments specified",
|
||||||
inputs: { args: ["-Wdev", "-Wdeprecated"] },
|
context: {
|
||||||
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: [],
|
||||||
|
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",
|
configure: {
|
||||||
cCompiler: "clang",
|
generator: "Ninja",
|
||||||
cxxCompiler: "clang++",
|
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
|
||||||
cFlags: "-Werror -Wall",
|
args: ["-Wdev", "-Wdeprecated"],
|
||||||
cxxFlags: "-Werror -Wall -Wextra",
|
},
|
||||||
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
|
|
||||||
args: ["-Wdev", "-Wdeprecated"],
|
|
||||||
},
|
},
|
||||||
expectedArgs: [
|
expectedArgs: [
|
||||||
"project",
|
"project",
|
||||||
@ -100,10 +90,6 @@ describe("configure a CMake project", () => {
|
|||||||
"output",
|
"output",
|
||||||
"-G",
|
"-G",
|
||||||
"Ninja",
|
"Ninja",
|
||||||
"-DCMAKE_C_COMPILER=clang",
|
|
||||||
"-DCMAKE_CXX_COMPILER=clang++",
|
|
||||||
"-DCMAKE_C_FLAGS=-Werror -Wall",
|
|
||||||
"-DCMAKE_CXX_FLAGS=-Werror -Wall -Wextra",
|
|
||||||
"-DBUILD_TESTING=ON",
|
"-DBUILD_TESTING=ON",
|
||||||
"-DBUILD_EXAMPLES=ON",
|
"-DBUILD_EXAMPLES=ON",
|
||||||
"-Wdev",
|
"-Wdev",
|
||||||
@ -119,7 +105,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,19 +125,22 @@ 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: { build: { enabled: true, args: ["--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"],
|
build: {
|
||||||
|
enabled: true,
|
||||||
|
args: ["--target", "foo"],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
expectedArgs: ["--build", "output", "--target", "foo"],
|
expectedArgs: ["--build", "output", "--target", "foo"],
|
||||||
},
|
},
|
||||||
@ -164,7 +153,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(
|
||||||
|
42
src/cmake.ts
42
src/cmake.ts
@ -1,42 +1,26 @@
|
|||||||
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.configure.generator) {
|
||||||
configureArgs.push(...["-G", inputs.generator]);
|
configureArgs.push(...["-G", context.configure.generator]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inputs.cCompiler) {
|
configureArgs.push(...context.configure.options.map((opt) => "-D" + opt));
|
||||||
configureArgs.push("-DCMAKE_C_COMPILER=" + inputs.cCompiler);
|
configureArgs.push(...context.configure.args);
|
||||||
}
|
|
||||||
|
|
||||||
if (inputs.cxxCompiler) {
|
|
||||||
configureArgs.push("-DCMAKE_CXX_COMPILER=" + inputs.cxxCompiler);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inputs.cFlags) {
|
|
||||||
configureArgs.push("-DCMAKE_C_FLAGS=" + inputs.cFlags);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inputs.cxxFlags) {
|
|
||||||
configureArgs.push("-DCMAKE_CXX_FLAGS=" + inputs.cxxFlags);
|
|
||||||
}
|
|
||||||
|
|
||||||
configureArgs.push(...inputs.options.map((opt) => "-D" + opt));
|
|
||||||
configureArgs.push(...inputs.args);
|
|
||||||
|
|
||||||
execFileSync("cmake", configureArgs, { stdio: "inherit" });
|
execFileSync("cmake", configureArgs, { stdio: "inherit" });
|
||||||
}
|
}
|
||||||
@ -44,10 +28,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.build.args], {
|
||||||
stdio: "inherit",
|
stdio: "inherit",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -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,96 @@ describe("get action inputs", () => {
|
|||||||
{
|
{
|
||||||
name: "with generator specified",
|
name: "with generator specified",
|
||||||
env: { INPUT_GENERATOR: "Ninja" },
|
env: { INPUT_GENERATOR: "Ninja" },
|
||||||
expectedInputs: { generator: "Ninja" },
|
expectedContext: {
|
||||||
|
configure: {
|
||||||
|
generator: "Ninja",
|
||||||
|
options: [],
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C compiler specified",
|
name: "with C compiler specified",
|
||||||
env: { "INPUT_C-COMPILER": "clang" },
|
env: { "INPUT_C-COMPILER": "clang" },
|
||||||
expectedInputs: { cCompiler: "clang" },
|
expectedContext: {
|
||||||
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: ["CMAKE_C_COMPILER=clang"],
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C++ compiler specified",
|
name: "with C++ compiler specified",
|
||||||
env: { "INPUT_CXX-COMPILER": "clang++" },
|
env: { "INPUT_CXX-COMPILER": "clang++" },
|
||||||
expectedInputs: { cxxCompiler: "clang++" },
|
expectedContext: {
|
||||||
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: ["CMAKE_CXX_COMPILER=clang++"],
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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: {
|
||||||
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: ["CMAKE_C_FLAGS=-Werror -Wall -Wextra"],
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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: {
|
||||||
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: ["CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic"],
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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"],
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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: {
|
||||||
|
configure: {
|
||||||
|
generator: "",
|
||||||
|
options: [],
|
||||||
|
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: { build: { enabled: true, args: [] } },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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: {
|
||||||
|
build: {
|
||||||
|
enabled: false,
|
||||||
|
args: ["--target", "foo", "--parallel", "8"],
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with all specified",
|
name: "with all specified",
|
||||||
@ -100,43 +145,51 @@ 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",
|
configure: {
|
||||||
cCompiler: "clang",
|
generator: "Ninja",
|
||||||
cxxCompiler: "clang++",
|
options: [
|
||||||
cFlags: "-Werror -Wall -Wextra",
|
"CMAKE_C_COMPILER=clang",
|
||||||
cxxFlags: "-Werror -Wall -Wextra -Wpedantic",
|
"CMAKE_CXX_COMPILER=clang++",
|
||||||
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
|
"CMAKE_C_FLAGS=-Werror -Wall -Wextra",
|
||||||
args: ["-Wdev", "-Wdeprecated", "--fresh"],
|
"CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic",
|
||||||
runBuild: true,
|
"BUILD_TESTING=ON",
|
||||||
buildArgs: ["--target", "foo", "--parallel", "8"],
|
"BUILD_EXAMPLES=ON",
|
||||||
|
"BUILD_DOCS=ON",
|
||||||
|
],
|
||||||
|
args: ["-Wdev", "-Wdeprecated", "--fresh"],
|
||||||
|
},
|
||||||
|
build: {
|
||||||
|
enabled: true,
|
||||||
|
args: ["--target", "foo", "--parallel", "8"],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
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: "",
|
configure: {
|
||||||
cCompiler: "",
|
generator: "",
|
||||||
cxxCompiler: "",
|
options: [],
|
||||||
cFlags: "",
|
args: [],
|
||||||
cxxFlags: "",
|
},
|
||||||
options: [],
|
build: {
|
||||||
args: [],
|
enabled: false,
|
||||||
runBuild: false,
|
args: [],
|
||||||
buildArgs: [],
|
},
|
||||||
...testCase.expectedInputs,
|
...testCase.expectedContext,
|
||||||
});
|
});
|
||||||
|
|
||||||
process.env = prevEnv;
|
process.env = prevEnv;
|
74
src/context.ts
Normal file
74
src/context.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
export interface Context {
|
||||||
|
sourceDir: string;
|
||||||
|
buildDir: string;
|
||||||
|
configure: {
|
||||||
|
generator: string;
|
||||||
|
options: string[];
|
||||||
|
args: string[];
|
||||||
|
};
|
||||||
|
build: {
|
||||||
|
enabled: boolean;
|
||||||
|
args: 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 getContext(): Context {
|
||||||
|
const sourceDir = getInput("source-dir");
|
||||||
|
const options: string[] = [];
|
||||||
|
|
||||||
|
let input = getInput("c-compiler");
|
||||||
|
if (input) options.push(`CMAKE_C_COMPILER=${input}`);
|
||||||
|
|
||||||
|
input = getInput("cxx-compiler");
|
||||||
|
if (input) options.push(`CMAKE_CXX_COMPILER=${input}`);
|
||||||
|
|
||||||
|
input = getInput("c-flags");
|
||||||
|
if (input) {
|
||||||
|
const flags = input.replaceAll(/\s+/g, " ");
|
||||||
|
options.push(`CMAKE_C_FLAGS=${flags}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
input = getInput("cxx-flags");
|
||||||
|
if (input) {
|
||||||
|
const flags = input.replaceAll(/\s+/g, " ");
|
||||||
|
options.push(`CMAKE_CXX_FLAGS=${flags}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
input = getInput("options");
|
||||||
|
if (input) {
|
||||||
|
const opts = input.split(/\s+/).filter((arg) => arg != "");
|
||||||
|
for (const opt of opts) {
|
||||||
|
options.push(opt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
sourceDir,
|
||||||
|
buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
|
||||||
|
configure: {
|
||||||
|
generator: getInput("generator"),
|
||||||
|
options,
|
||||||
|
args: getInput("args")
|
||||||
|
.split(/\s+/)
|
||||||
|
.filter((arg) => arg != ""),
|
||||||
|
},
|
||||||
|
build: {
|
||||||
|
enabled: getInput("run-build") == "true",
|
||||||
|
args: getInput("build-args")
|
||||||
|
.split(/\s+/)
|
||||||
|
.filter((arg) => arg != ""),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
12
src/index.ts
12
src/index.ts
@ -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.build.enabled) {
|
||||||
buildProject(inputs);
|
buildProject(context);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
process.exitCode = 1;
|
process.exitCode = 1;
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
import path from "node:path";
|
|
||||||
|
|
||||||
export interface Inputs {
|
|
||||||
sourceDir: string;
|
|
||||||
buildDir: string;
|
|
||||||
generator: string;
|
|
||||||
cCompiler: string;
|
|
||||||
cxxCompiler: string;
|
|
||||||
cFlags: string;
|
|
||||||
cxxFlags: string;
|
|
||||||
options: string[];
|
|
||||||
args: string[];
|
|
||||||
runBuild: boolean;
|
|
||||||
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 {
|
|
||||||
const sourceDir = getInput("source-dir");
|
|
||||||
return {
|
|
||||||
sourceDir,
|
|
||||||
buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
|
|
||||||
generator: getInput("generator"),
|
|
||||||
cCompiler: getInput("c-compiler"),
|
|
||||||
cxxCompiler: getInput("cxx-compiler"),
|
|
||||||
cFlags: getInput("c-flags").replaceAll(/\s+/g, " "),
|
|
||||||
cxxFlags: getInput("cxx-flags").replaceAll(/\s+/g, " "),
|
|
||||||
options: getInput("options")
|
|
||||||
.split(/\s+/)
|
|
||||||
.filter((arg) => arg != ""),
|
|
||||||
args: getInput("args")
|
|
||||||
.split(/\s+/)
|
|
||||||
.filter((arg) => arg != ""),
|
|
||||||
runBuild: getInput("run-build") == "true",
|
|
||||||
buildArgs: getInput("build-args")
|
|
||||||
.split(/\s+/)
|
|
||||||
.filter((arg) => arg != ""),
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user