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(context.sourceDir);
} }
configureArgs.push("-B", context.buildDir); configureArgs.push("-B", context.buildDir);
if (context.generator) { if (context.configure.generator) {
configureArgs.push(...["-G", context.generator]); configureArgs.push(...["-G", context.configure.generator]);
} }
configureArgs.push(...context.options.map((opt) => "-D" + opt)); configureArgs.push(...context.configure.options.map((opt) => "-D" + opt));
configureArgs.push(...context.args); configureArgs.push(...context.configure.args);
execFileSync("cmake", configureArgs, { stdio: "inherit" }); execFileSync("cmake", configureArgs, { stdio: "inherit" });
} }
/** /**
@ -29,7 +29,7 @@ function configureProject(context) {
* @param context - The action context. * @param context - The action context.
*/ */
function buildProject(context) { function buildProject(context) {
execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], { execFileSync("cmake", ["--build", context.buildDir, ...context.build.args], {
stdio: "inherit", stdio: "inherit",
}); });
} }
@ -72,15 +72,19 @@ function getContext() {
return { return {
sourceDir, sourceDir,
buildDir: getInput("build-dir") || path.join(sourceDir, "build"), buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
generator: getInput("generator"), configure: {
options, generator: getInput("generator"),
args: getInput("args") options,
.split(/\s+/) args: getInput("args")
.filter((arg) => arg != ""), .split(/\s+/)
runBuild: getInput("run-build") == "true", .filter((arg) => arg != ""),
buildArgs: getInput("build-args") },
.split(/\s+/) build: {
.filter((arg) => arg != ""), enabled: getInput("run-build") == "true",
args: getInput("build-args")
.split(/\s+/)
.filter((arg) => arg != ""),
},
}; };
} }
@ -88,7 +92,7 @@ try {
const context = getContext(); const context = getContext();
configureProject(context); configureProject(context);
fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`); fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`);
if (context.runBuild) { if (context.build.enabled) {
buildProject(context); buildProject(context);
} }
} }

View File

@ -10,11 +10,15 @@ interface TestCase {
const defaultContext: Context = { const defaultContext: Context = {
sourceDir: "", sourceDir: "",
buildDir: "build", buildDir: "build",
generator: "", configure: {
options: [], generator: "",
args: [], options: [],
runBuild: true, args: [],
buildArgs: [], },
build: {
enabled: true,
args: [],
},
}; };
jest.unstable_mockModule("node:child_process", () => ({ jest.unstable_mockModule("node:child_process", () => ({
@ -39,12 +43,18 @@ describe("configure a CMake project", () => {
}, },
{ {
name: "with generator specified", name: "with generator specified",
context: { generator: "Ninja" }, context: { configure: { generator: "Ninja", options: [], args: [] } },
expectedArgs: ["-B", "build", "-G", "Ninja"], expectedArgs: ["-B", "build", "-G", "Ninja"],
}, },
{ {
name: "with additional options specified", 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: [ expectedArgs: [
"-B", "-B",
"build", "build",
@ -54,7 +64,13 @@ describe("configure a CMake project", () => {
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
context: { args: ["-Wdev", "-Wdeprecated"] }, context: {
configure: {
generator: "",
options: [],
args: ["-Wdev", "-Wdeprecated"],
},
},
expectedArgs: ["-B", "build", "-Wdev", "-Wdeprecated"], expectedArgs: ["-B", "build", "-Wdev", "-Wdeprecated"],
}, },
{ {
@ -62,9 +78,11 @@ describe("configure a CMake project", () => {
context: { context: {
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
generator: "Ninja", configure: {
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"], generator: "Ninja",
args: ["-Wdev", "-Wdeprecated"], options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON"],
args: ["-Wdev", "-Wdeprecated"],
},
}, },
expectedArgs: [ expectedArgs: [
"project", "project",
@ -112,14 +130,17 @@ describe("build a CMake project", () => {
}, },
{ {
name: "with additional arguments specified", name: "with additional arguments specified",
context: { 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",
context: { context: {
buildDir: "output", buildDir: "output",
buildArgs: ["--target", "foo"], build: {
enabled: true,
args: ["--target", "foo"],
},
}, },
expectedArgs: ["--build", "output", "--target", "foo"], expectedArgs: ["--build", "output", "--target", "foo"],
}, },

View File

@ -15,12 +15,12 @@ export function configureProject(context: Context): void {
configureArgs.push("-B", context.buildDir); configureArgs.push("-B", context.buildDir);
if (context.generator) { if (context.configure.generator) {
configureArgs.push(...["-G", context.generator]); configureArgs.push(...["-G", context.configure.generator]);
} }
configureArgs.push(...context.options.map((opt) => "-D" + opt)); configureArgs.push(...context.configure.options.map((opt) => "-D" + opt));
configureArgs.push(...context.args); configureArgs.push(...context.configure.args);
execFileSync("cmake", configureArgs, { stdio: "inherit" }); execFileSync("cmake", configureArgs, { stdio: "inherit" });
} }
@ -31,7 +31,7 @@ export function configureProject(context: Context): void {
* @param context - The action context. * @param context - The action context.
*/ */
export function buildProject(context: Context): void { export function buildProject(context: Context): void {
execFileSync("cmake", ["--build", context.buildDir, ...context.buildArgs], { execFileSync("cmake", ["--build", context.buildDir, ...context.build.args], {
stdio: "inherit", stdio: "inherit",
}); });
} }

View File

@ -39,28 +39,56 @@ describe("get action context", () => {
{ {
name: "with generator specified", name: "with generator specified",
env: { INPUT_GENERATOR: "Ninja" }, env: { INPUT_GENERATOR: "Ninja" },
expectedContext: { 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" },
expectedContext: { options: ["CMAKE_C_COMPILER=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++" },
expectedContext: { options: ["CMAKE_CXX_COMPILER=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" },
expectedContext: { options: ["CMAKE_C_FLAGS=-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" },
expectedContext: { 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", INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
}, },
expectedContext: { 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" },
expectedContext: { 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" },
expectedContext: { 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" },
expectedContext: { buildArgs: ["--target", "foo", "--parallel", "8"] }, expectedContext: {
build: {
enabled: false,
args: ["--target", "foo", "--parallel", "8"],
},
},
}, },
{ {
name: "with all specified", name: "with all specified",
@ -105,19 +148,23 @@ describe("get action context", () => {
expectedContext: { expectedContext: {
sourceDir: "project", sourceDir: "project",
buildDir: "output", buildDir: "output",
generator: "Ninja", configure: {
options: [ generator: "Ninja",
"CMAKE_C_COMPILER=clang", options: [
"CMAKE_CXX_COMPILER=clang++", "CMAKE_C_COMPILER=clang",
"CMAKE_C_FLAGS=-Werror -Wall -Wextra", "CMAKE_CXX_COMPILER=clang++",
"CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic", "CMAKE_C_FLAGS=-Werror -Wall -Wextra",
"BUILD_TESTING=ON", "CMAKE_CXX_FLAGS=-Werror -Wall -Wextra -Wpedantic",
"BUILD_EXAMPLES=ON", "BUILD_TESTING=ON",
"BUILD_DOCS=ON", "BUILD_EXAMPLES=ON",
], "BUILD_DOCS=ON",
args: ["-Wdev", "-Wdeprecated", "--fresh"], ],
runBuild: true, args: ["-Wdev", "-Wdeprecated", "--fresh"],
buildArgs: ["--target", "foo", "--parallel", "8"], },
build: {
enabled: true,
args: ["--target", "foo", "--parallel", "8"],
},
}, },
}, },
]; ];
@ -133,11 +180,15 @@ describe("get action context", () => {
expect(getContext()).toStrictEqual({ expect(getContext()).toStrictEqual({
sourceDir: "", sourceDir: "",
buildDir: "build", buildDir: "build",
generator: "", configure: {
options: [], generator: "",
args: [], options: [],
runBuild: false, args: [],
buildArgs: [], },
build: {
enabled: false,
args: [],
},
...testCase.expectedContext, ...testCase.expectedContext,
}); });

View File

@ -3,11 +3,15 @@ import path from "node:path";
export interface Context { export interface Context {
sourceDir: string; sourceDir: string;
buildDir: string; buildDir: string;
generator: string; configure: {
options: string[]; generator: string;
args: string[]; options: string[];
runBuild: boolean; args: string[];
buildArgs: string[]; };
build: {
enabled: boolean;
args: string[];
};
} }
/** /**
@ -53,14 +57,18 @@ export function getContext(): Context {
return { return {
sourceDir, sourceDir,
buildDir: getInput("build-dir") || path.join(sourceDir, "build"), buildDir: getInput("build-dir") || path.join(sourceDir, "build"),
generator: getInput("generator"), configure: {
options, generator: getInput("generator"),
args: getInput("args") options,
.split(/\s+/) args: getInput("args")
.filter((arg) => arg != ""), .split(/\s+/)
runBuild: getInput("run-build") == "true", .filter((arg) => arg != ""),
buildArgs: getInput("build-args") },
.split(/\s+/) build: {
.filter((arg) => arg != ""), 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}`, `build-dir=${context.buildDir}${os.EOL}`,
); );
if (context.runBuild) { if (context.build.enabled) {
buildProject(context); buildProject(context);
} }
} catch (err) { } catch (err) {