refactor: separate configure and build context

This commit is contained in:
Alfi Maulana 2024-08-07 21:14:24 +07:00
parent c67eb4dd73
commit ad83104d76
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4
6 changed files with 159 additions and 75 deletions

34
dist/index.mjs generated vendored
View File

@ -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);
}
}

View File

@ -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"],
},

View File

@ -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",
});
}

View File

@ -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,
});

View File

@ -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 != ""),
},
};
}

View File

@ -14,7 +14,7 @@ try {
`build-dir=${context.buildDir}${os.EOL}`,
);
if (context.runBuild) {
if (context.build.enabled) {
buildProject(context);
}
} catch (err) {