cmake-action/dist/action.mjs
dependabot[bot] 5fc48bdef4
chore: bump gha-utils from 0.3.0 to 0.4.0 (#457)
Bumps [gha-utils](https://github.com/threeal/gha-utils) from 0.3.0 to 0.4.0.
- [Release notes](https://github.com/threeal/gha-utils/releases)
- [Commits](https://github.com/threeal/gha-utils/compare/v0.3.0...v0.4.0)

---
updated-dependencies:
- dependency-name: gha-utils
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-25 18:04:20 +07:00

140 lines
4.0 KiB
JavaScript
Generated

import 'node:fs';
import fsPromises from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
/**
* @internal
* Retrieves the value of an environment variable.
*
* @param name - The name of the environment variable.
* @returns The value of the environment variable.
* @throws Error if the environment variable is not defined.
*/
function mustGetEnvironment(name) {
const value = process.env[name];
if (value === undefined) {
throw new Error(`the ${name} environment variable must be defined`);
}
return value;
}
/**
* Retrieves the value of a GitHub Actions input.
*
* @param name - The name of the GitHub Actions input.
* @returns The value of the GitHub Actions input, or an empty string if not found.
*/
function getInput(name) {
const value = process.env[`INPUT_${name.toUpperCase()}`] ?? "";
return value.trim();
}
/**
* Sets the value of a GitHub Actions output.
*
* @param name - The name of the GitHub Actions output.
* @param value - The value to set for the GitHub Actions output.
* @returns A promise that resolves when the value is successfully set.
*/
async function setOutput(name, value) {
const filePath = mustGetEnvironment("GITHUB_OUTPUT");
await fsPromises.appendFile(filePath, `${name}=${value}${os.EOL}`);
}
/**
* Logs an error message in GitHub Actions.
*
* @param err - The error, which can be of any type.
*/
function logError(err) {
const message = err instanceof Error ? err.message : String(err);
process.stdout.write(`::error::${message}${os.EOL}`);
}
/**
* Configures the build system of a CMake project.
*
* @param context - The action context.
*/
function configureProject(context) {
const configureArgs = [];
if (context.sourceDir) {
configureArgs.push(context.sourceDir);
}
configureArgs.push("-B", context.buildDir);
if (context.configure.generator) {
configureArgs.push(...["-G", context.configure.generator]);
}
configureArgs.push(...context.configure.options.map((opt) => "-D" + opt));
configureArgs.push(...context.configure.args);
execFileSync("cmake", configureArgs, { stdio: "inherit" });
}
/**
* Build a CMake project.
*
* @param context - The action context.
*/
function buildProject(context) {
execFileSync("cmake", ["--build", context.buildDir, ...context.build.args], {
stdio: "inherit",
});
}
function getContext() {
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 {
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 != ""),
},
};
}
try {
const context = getContext();
configureProject(context);
await setOutput("build-dir", context.buildDir);
if (context.build.enabled) {
buildProject(context);
}
}
catch (err) {
logError(err);
process.exit(1);
}