From ad83104d76d372874c583e4716b81a1c7f94e0bb Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 7 Aug 2024 21:14:24 +0700 Subject: [PATCH] refactor: separate configure and build context --- dist/index.mjs | 34 +++++++------- src/cmake.test.ts | 47 ++++++++++++++------ src/cmake.ts | 10 ++--- src/context.test.ts | 105 ++++++++++++++++++++++++++++++++------------ src/context.ts | 36 +++++++++------ src/index.ts | 2 +- 6 files changed, 159 insertions(+), 75 deletions(-) diff --git a/dist/index.mjs b/dist/index.mjs index 39bb779..9d69773 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -16,11 +16,11 @@ function configureProject(context) { configureArgs.push(context.sourceDir); } configureArgs.push("-B", context.buildDir); - if (context.generator) { - configureArgs.push(...["-G", context.generator]); + if (context.configure.generator) { + configureArgs.push(...["-G", context.configure.generator]); } - configureArgs.push(...context.options.map((opt) => "-D" + opt)); - configureArgs.push(...context.args); + configureArgs.push(...context.configure.options.map((opt) => "-D" + opt)); + configureArgs.push(...context.configure.args); execFileSync("cmake", configureArgs, { stdio: "inherit" }); } /** @@ -29,7 +29,7 @@ function configureProject(context) { * @param context - The action context. */ function buildProject(context) { - execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], { + execFileSync("cmake", ["--build", context.buildDir, ...context.build.args], { stdio: "inherit", }); } @@ -72,15 +72,19 @@ function getContext() { return { sourceDir, buildDir: getInput("build-dir") || path.join(sourceDir, "build"), - generator: getInput("generator"), - options, - args: getInput("args") - .split(/\s+/) - .filter((arg) => arg != ""), - runBuild: getInput("run-build") == "true", - buildArgs: getInput("build-args") - .split(/\s+/) - .filter((arg) => arg != ""), + 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 != ""), + }, }; } @@ -88,7 +92,7 @@ try { const context = getContext(); configureProject(context); fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`); - if (context.runBuild) { + if (context.build.enabled) { buildProject(context); } } diff --git a/src/cmake.test.ts b/src/cmake.test.ts index c18ef96..0b676e8 100644 --- a/src/cmake.test.ts +++ b/src/cmake.test.ts @@ -10,11 +10,15 @@ interface TestCase { const defaultContext: Context = { sourceDir: "", buildDir: "build", - generator: "", - options: [], - args: [], - runBuild: true, - buildArgs: [], + configure: { + generator: "", + options: [], + args: [], + }, + build: { + enabled: true, + args: [], + }, }; jest.unstable_mockModule("node:child_process", () => ({ @@ -39,12 +43,18 @@ describe("configure a CMake project", () => { }, { name: "with generator specified", - context: { generator: "Ninja" }, + context: { configure: { generator: "Ninja", options: [], args: [] } }, expectedArgs: ["-B", "build", "-G", "Ninja"], }, { name: "with additional options specified", - context: { options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"] }, + context: { + configure: { + generator: "", + options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], + args: [], + }, + }, expectedArgs: [ "-B", "build", @@ -54,7 +64,13 @@ describe("configure a CMake project", () => { }, { name: "with additional arguments specified", - context: { args: ["-Wdev", "-Wdeprecated"] }, + context: { + configure: { + generator: "", + options: [], + args: ["-Wdev", "-Wdeprecated"], + }, + }, expectedArgs: ["-B", "build", "-Wdev", "-Wdeprecated"], }, { @@ -62,9 +78,11 @@ describe("configure a CMake project", () => { context: { sourceDir: "project", buildDir: "output", - generator: "Ninja", - options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], - args: ["-Wdev", "-Wdeprecated"], + configure: { + generator: "Ninja", + options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], + args: ["-Wdev", "-Wdeprecated"], + }, }, expectedArgs: [ "project", @@ -112,14 +130,17 @@ describe("build a CMake project", () => { }, { name: "with additional arguments specified", - context: { buildArgs: ["--target", "foo"] }, + context: { build: { enabled: true, args: ["--target", "foo"] } }, expectedArgs: ["--build", "build", "--target", "foo"], }, { name: "with all specified", context: { buildDir: "output", - buildArgs: ["--target", "foo"], + build: { + enabled: true, + args: ["--target", "foo"], + }, }, expectedArgs: ["--build", "output", "--target", "foo"], }, diff --git a/src/cmake.ts b/src/cmake.ts index f9a27da..512aa6b 100644 --- a/src/cmake.ts +++ b/src/cmake.ts @@ -15,12 +15,12 @@ export function configureProject(context: Context): void { configureArgs.push("-B", context.buildDir); - if (context.generator) { - configureArgs.push(...["-G", context.generator]); + if (context.configure.generator) { + configureArgs.push(...["-G", context.configure.generator]); } - configureArgs.push(...context.options.map((opt) => "-D" + opt)); - configureArgs.push(...context.args); + configureArgs.push(...context.configure.options.map((opt) => "-D" + opt)); + configureArgs.push(...context.configure.args); execFileSync("cmake", configureArgs, { stdio: "inherit" }); } @@ -31,7 +31,7 @@ export function configureProject(context: Context): void { * @param context - The action context. */ export function buildProject(context: Context): void { - execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], { + execFileSync("cmake", ["--build", context.buildDir, ...context.build.args], { stdio: "inherit", }); } diff --git a/src/context.test.ts b/src/context.test.ts index 43e9386..ee0ba3e 100644 --- a/src/context.test.ts +++ b/src/context.test.ts @@ -39,28 +39,56 @@ describe("get action context", () => { { name: "with generator specified", env: { INPUT_GENERATOR: "Ninja" }, - expectedContext: { generator: "Ninja" }, + expectedContext: { + configure: { + generator: "Ninja", + options: [], + args: [], + }, + }, }, { name: "with C compiler specified", env: { "INPUT_C-COMPILER": "clang" }, - expectedContext: { options: ["CMAKE_C_COMPILER=clang"] }, + expectedContext: { + configure: { + generator: "", + options: ["CMAKE_C_COMPILER=clang"], + args: [], + }, + }, }, { name: "with C++ compiler specified", env: { "INPUT_CXX-COMPILER": "clang++" }, - expectedContext: { options: ["CMAKE_CXX_COMPILER=clang++"] }, + expectedContext: { + configure: { + generator: "", + options: ["CMAKE_CXX_COMPILER=clang++"], + args: [], + }, + }, }, { name: "with C flags specified", env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" }, - expectedContext: { options: ["CMAKE_C_FLAGS=-Werror -Wall -Wextra"] }, + expectedContext: { + configure: { + generator: "", + options: ["CMAKE_C_FLAGS=-Werror -Wall -Wextra"], + args: [], + }, + }, }, { name: "with C++ flags specified", env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" }, expectedContext: { - options: ["CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic"], + configure: { + generator: "", + options: ["CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic"], + args: [], + }, }, }, { @@ -69,23 +97,38 @@ describe("get action context", () => { INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON", }, 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", env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" }, - expectedContext: { args: ["-Wdev", "-Wdeprecated", "--fresh"] }, + expectedContext: { + configure: { + generator: "", + options: [], + args: ["-Wdev", "-Wdeprecated", "--fresh"], + }, + }, }, { name: "with run build specified", env: { "INPUT_RUN-BUILD": "true" }, - expectedContext: { runBuild: true }, + expectedContext: { build: { enabled: true, args: [] } }, }, { name: "with additional build arguments specified", env: { "INPUT_BUILD-ARGS": "--target foo\n--parallel 8" }, - expectedContext: { buildArgs: ["--target", "foo", "--parallel", "8"] }, + expectedContext: { + build: { + enabled: false, + args: ["--target", "foo", "--parallel", "8"], + }, + }, }, { name: "with all specified", @@ -105,19 +148,23 @@ describe("get action context", () => { expectedContext: { sourceDir: "project", buildDir: "output", - generator: "Ninja", - options: [ - "CMAKE_C_COMPILER=clang", - "CMAKE_CXX_COMPILER=clang++", - "CMAKE_C_FLAGS=-Werror -Wall -Wextra", - "CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic", - "BUILD_TESTING=ON", - "BUILD_EXAMPLES=ON", - "BUILD_DOCS=ON", - ], - args: ["-Wdev", "-Wdeprecated", "--fresh"], - runBuild: true, - buildArgs: ["--target", "foo", "--parallel", "8"], + configure: { + generator: "Ninja", + options: [ + "CMAKE_C_COMPILER=clang", + "CMAKE_CXX_COMPILER=clang++", + "CMAKE_C_FLAGS=-Werror -Wall -Wextra", + "CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic", + "BUILD_TESTING=ON", + "BUILD_EXAMPLES=ON", + "BUILD_DOCS=ON", + ], + args: ["-Wdev", "-Wdeprecated", "--fresh"], + }, + build: { + enabled: true, + args: ["--target", "foo", "--parallel", "8"], + }, }, }, ]; @@ -133,11 +180,15 @@ describe("get action context", () => { expect(getContext()).toStrictEqual({ sourceDir: "", buildDir: "build", - generator: "", - options: [], - args: [], - runBuild: false, - buildArgs: [], + configure: { + generator: "", + options: [], + args: [], + }, + build: { + enabled: false, + args: [], + }, ...testCase.expectedContext, }); diff --git a/src/context.ts b/src/context.ts index 058f11f..8f6ac2a 100644 --- a/src/context.ts +++ b/src/context.ts @@ -3,11 +3,15 @@ import path from "node:path"; export interface Context { sourceDir: string; buildDir: string; - generator: string; - options: string[]; - args: string[]; - runBuild: boolean; - buildArgs: string[]; + configure: { + generator: string; + options: string[]; + args: string[]; + }; + build: { + enabled: boolean; + args: string[]; + }; } /** @@ -53,14 +57,18 @@ export function getContext(): Context { return { sourceDir, buildDir: getInput("build-dir") || path.join(sourceDir, "build"), - generator: getInput("generator"), - options, - args: getInput("args") - .split(/\s+/) - .filter((arg) => arg != ""), - runBuild: getInput("run-build") == "true", - buildArgs: getInput("build-args") - .split(/\s+/) - .filter((arg) => arg != ""), + 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 != ""), + }, }; } diff --git a/src/index.ts b/src/index.ts index dadfd72..5238f92 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ try { `build-dir=${context.buildDir}${os.EOL}`, ); - if (context.runBuild) { + if (context.build.enabled) { buildProject(context); } } catch (err) {