cmake-action/src/context.ts
Alfi Maulana 571e81560b
feat: utilize GitHub Actions Utilities package (#406)
* feat: utilize GitHub Actions Utilities package

* test: modify get action context test by mocking `getInput` function
2024-08-08 16:29:56 +08:00

66 lines
1.5 KiB
TypeScript

import { getInput } from "gha-utils";
import path from "node:path";
export interface Context {
sourceDir: string;
buildDir: string;
configure: {
generator: string;
options: string[];
args: string[];
};
build: {
enabled: boolean;
args: string[];
};
}
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 != ""),
},
};
}