mirror of
https://github.com/threeal/cmake-action.git
synced 2025-06-08 10:11:21 +00:00
feat: utilize GitHub Actions Utilities package (#406)
* feat: utilize GitHub Actions Utilities package * test: modify get action context test by mocking `getInput` function
This commit is contained in:
parent
d1c539d005
commit
571e81560b
44
dist/index.mjs
generated
vendored
44
dist/index.mjs
generated
vendored
@ -3,7 +3,34 @@ import os from 'node:os';
|
|||||||
import { execFileSync } from 'node:child_process';
|
import { execFileSync } from 'node:child_process';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
|
||||||
function r(r){return function(r){if("object"==typeof(e=r)&&null!==e&&"message"in e&&"string"==typeof e.message)return r;var e;try{return new Error(JSON.stringify(r))}catch(e){return new Error(String(r))}}(r).message}
|
/**
|
||||||
|
* 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 of the GitHub Actions output
|
||||||
|
*/
|
||||||
|
function setOutput(name, value) {
|
||||||
|
fs.appendFileSync(process.env["GITHUB_OUTPUT"], `${name}=${value}${os.EOL}`);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Logs an error message on GitHub Actions.
|
||||||
|
*
|
||||||
|
* @param err - The error, which can be of any type.
|
||||||
|
*/
|
||||||
|
function error(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.
|
* Configures the build system of a CMake project.
|
||||||
@ -34,15 +61,6 @@ function buildProject(context) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves an action input.
|
|
||||||
* @param key - The key of the action input.
|
|
||||||
* @returns The action input value as a string.
|
|
||||||
*/
|
|
||||||
function getInput(key) {
|
|
||||||
const value = process.env[`INPUT_${key.toUpperCase()}`] || "";
|
|
||||||
return value.trim();
|
|
||||||
}
|
|
||||||
function getContext() {
|
function getContext() {
|
||||||
const sourceDir = getInput("source-dir");
|
const sourceDir = getInput("source-dir");
|
||||||
const options = [];
|
const options = [];
|
||||||
@ -91,12 +109,12 @@ function getContext() {
|
|||||||
try {
|
try {
|
||||||
const context = getContext();
|
const context = getContext();
|
||||||
configureProject(context);
|
configureProject(context);
|
||||||
fs.appendFileSync(process.env["GITHUB_OUTPUT"], `build-dir=${context.buildDir}${os.EOL}`);
|
setOutput("build-dir", context.buildDir);
|
||||||
if (context.build.enabled) {
|
if (context.build.enabled) {
|
||||||
buildProject(context);
|
buildProject(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
process.exitCode = 1;
|
error(err);
|
||||||
process.stdout.write(`::error::${r(err)}${os.EOL}`);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
"test": "jest"
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"catched-error-message": "^0.0.1"
|
"gha-utils": "^0.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^9.8.0",
|
"@eslint/js": "^9.8.0",
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
|
import { jest } from "@jest/globals";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { Context, getContext } from "./context.js";
|
import type { Context } from "./context.js";
|
||||||
|
|
||||||
|
jest.unstable_mockModule("gha-utils", () => ({
|
||||||
|
getInput: jest.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
describe("get action context", () => {
|
describe("get action context", () => {
|
||||||
interface TestCase {
|
interface TestCase {
|
||||||
name: string;
|
name: string;
|
||||||
env?: Record<string, string>;
|
inputs?: Record<string, string>;
|
||||||
expectedContext?: Partial<Context>;
|
expectedContext?: Partial<Context>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,7 +19,7 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with source directory specified",
|
name: "with source directory specified",
|
||||||
env: { "INPUT_SOURCE-DIR": "project" },
|
inputs: { "source-dir": "project" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
sourceDir: "project",
|
sourceDir: "project",
|
||||||
buildDir: path.join("project", "build"),
|
buildDir: path.join("project", "build"),
|
||||||
@ -22,14 +27,14 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with build directory specified",
|
name: "with build directory specified",
|
||||||
env: { "INPUT_BUILD-DIR": "output" },
|
inputs: { "build-dir": "output" },
|
||||||
expectedContext: { buildDir: "output" },
|
expectedContext: { buildDir: "output" },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with source and build directories specified",
|
name: "with source and build directories specified",
|
||||||
env: {
|
inputs: {
|
||||||
"INPUT_SOURCE-DIR": "project",
|
"source-dir": "project",
|
||||||
"INPUT_BUILD-DIR": "output",
|
"build-dir": "output",
|
||||||
},
|
},
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
sourceDir: "project",
|
sourceDir: "project",
|
||||||
@ -38,7 +43,7 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with generator specified",
|
name: "with generator specified",
|
||||||
env: { INPUT_GENERATOR: "Ninja" },
|
inputs: { generator: "Ninja" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
configure: {
|
configure: {
|
||||||
generator: "Ninja",
|
generator: "Ninja",
|
||||||
@ -49,7 +54,7 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C compiler specified",
|
name: "with C compiler specified",
|
||||||
env: { "INPUT_C-COMPILER": "clang" },
|
inputs: { "c-compiler": "clang" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
configure: {
|
configure: {
|
||||||
generator: "",
|
generator: "",
|
||||||
@ -60,7 +65,7 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C++ compiler specified",
|
name: "with C++ compiler specified",
|
||||||
env: { "INPUT_CXX-COMPILER": "clang++" },
|
inputs: { "cxx-compiler": "clang++" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
configure: {
|
configure: {
|
||||||
generator: "",
|
generator: "",
|
||||||
@ -71,7 +76,7 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C flags specified",
|
name: "with C flags specified",
|
||||||
env: { "INPUT_C-FLAGS": "-Werror -Wall\n-Wextra" },
|
inputs: { "c-flags": "-Werror -Wall\n-Wextra" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
configure: {
|
configure: {
|
||||||
generator: "",
|
generator: "",
|
||||||
@ -82,7 +87,7 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with C++ flags specified",
|
name: "with C++ flags specified",
|
||||||
env: { "INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic" },
|
inputs: { "cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
configure: {
|
configure: {
|
||||||
generator: "",
|
generator: "",
|
||||||
@ -93,8 +98,8 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with additional options specified",
|
name: "with additional options specified",
|
||||||
env: {
|
inputs: {
|
||||||
INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
|
options: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
|
||||||
},
|
},
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
configure: {
|
configure: {
|
||||||
@ -106,7 +111,7 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with additional arguments specified",
|
name: "with additional arguments specified",
|
||||||
env: { INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh" },
|
inputs: { args: "-Wdev -Wdeprecated\n--fresh" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
configure: {
|
configure: {
|
||||||
generator: "",
|
generator: "",
|
||||||
@ -117,12 +122,12 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with run build specified",
|
name: "with run build specified",
|
||||||
env: { "INPUT_RUN-BUILD": "true" },
|
inputs: { "run-build": "true" },
|
||||||
expectedContext: { build: { enabled: true, args: [] } },
|
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" },
|
inputs: { "build-args": "--target foo\n--parallel 8" },
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
build: {
|
build: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
@ -132,18 +137,18 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with all specified",
|
name: "with all specified",
|
||||||
env: {
|
inputs: {
|
||||||
"INPUT_SOURCE-DIR": "project",
|
"source-dir": "project",
|
||||||
"INPUT_BUILD-DIR": "output",
|
"build-dir": "output",
|
||||||
INPUT_GENERATOR: "Ninja",
|
generator: "Ninja",
|
||||||
"INPUT_C-COMPILER": "clang",
|
"c-compiler": "clang",
|
||||||
"INPUT_CXX-COMPILER": "clang++",
|
"cxx-compiler": "clang++",
|
||||||
"INPUT_C-FLAGS": "-Werror -Wall\n-Wextra",
|
"c-flags": "-Werror -Wall\n-Wextra",
|
||||||
"INPUT_CXX-FLAGS": "-Werror -Wall\n-Wextra -Wpedantic",
|
"cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic",
|
||||||
INPUT_OPTIONS: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
|
options: "BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON",
|
||||||
INPUT_ARGS: "-Wdev -Wdeprecated\n--fresh",
|
args: "-Wdev -Wdeprecated\n--fresh",
|
||||||
"INPUT_RUN-BUILD": "true",
|
"run-build": "true",
|
||||||
"INPUT_BUILD-ARGS": "--target foo\n--parallel 8",
|
"build-args": "--target foo\n--parallel 8",
|
||||||
},
|
},
|
||||||
expectedContext: {
|
expectedContext: {
|
||||||
sourceDir: "project",
|
sourceDir: "project",
|
||||||
@ -171,11 +176,13 @@ describe("get action context", () => {
|
|||||||
|
|
||||||
for (const testCase of testCases) {
|
for (const testCase of testCases) {
|
||||||
it(`should get the action context ${testCase.name}`, async () => {
|
it(`should get the action context ${testCase.name}`, async () => {
|
||||||
const prevEnv = process.env;
|
const { getInput } = await import("gha-utils");
|
||||||
process.env = {
|
const { getContext } = await import("./context.js");
|
||||||
...process.env,
|
|
||||||
...testCase.env,
|
const inputs = testCase.inputs || {};
|
||||||
};
|
jest.mocked(getInput).mockImplementation((name) => {
|
||||||
|
return inputs[name] || "";
|
||||||
|
});
|
||||||
|
|
||||||
expect(getContext()).toStrictEqual({
|
expect(getContext()).toStrictEqual({
|
||||||
sourceDir: "",
|
sourceDir: "",
|
||||||
@ -191,8 +198,6 @@ describe("get action context", () => {
|
|||||||
},
|
},
|
||||||
...testCase.expectedContext,
|
...testCase.expectedContext,
|
||||||
});
|
});
|
||||||
|
|
||||||
process.env = prevEnv;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { getInput } from "gha-utils";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
export interface Context {
|
export interface Context {
|
||||||
@ -14,16 +15,6 @@ export interface Context {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves an action input.
|
|
||||||
* @param key - The key of the action input.
|
|
||||||
* @returns The action input value as a string.
|
|
||||||
*/
|
|
||||||
function getInput(key: string): string {
|
|
||||||
const value = process.env[`INPUT_${key.toUpperCase()}`] || "";
|
|
||||||
return value.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getContext(): Context {
|
export function getContext(): Context {
|
||||||
const sourceDir = getInput("source-dir");
|
const sourceDir = getInput("source-dir");
|
||||||
const options: string[] = [];
|
const options: string[] = [];
|
||||||
|
13
src/index.ts
13
src/index.ts
@ -1,6 +1,4 @@
|
|||||||
import { getErrorMessage } from "catched-error-message";
|
import { error, setOutput } from "gha-utils";
|
||||||
import fs from "node:fs";
|
|
||||||
import os from "node:os";
|
|
||||||
import { buildProject, configureProject } from "./cmake.js";
|
import { buildProject, configureProject } from "./cmake.js";
|
||||||
import { getContext } from "./context.js";
|
import { getContext } from "./context.js";
|
||||||
|
|
||||||
@ -9,15 +7,12 @@ try {
|
|||||||
|
|
||||||
configureProject(context);
|
configureProject(context);
|
||||||
|
|
||||||
fs.appendFileSync(
|
setOutput("build-dir", context.buildDir);
|
||||||
process.env["GITHUB_OUTPUT"] as string,
|
|
||||||
`build-dir=${context.buildDir}${os.EOL}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (context.build.enabled) {
|
if (context.build.enabled) {
|
||||||
buildProject(context);
|
buildProject(context);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
process.exitCode = 1;
|
error(err);
|
||||||
process.stdout.write(`::error::${getErrorMessage(err)}${os.EOL}`);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
16
yarn.lock
generated
16
yarn.lock
generated
@ -1670,13 +1670,6 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
"catched-error-message@npm:^0.0.1":
|
|
||||||
version: 0.0.1
|
|
||||||
resolution: "catched-error-message@npm:0.0.1"
|
|
||||||
checksum: 10c0/1f10cd4323a73bec7a57b7495730e5dad9995120b04e85b5f654f7b40dc6a36320d95cc55e49cdf78753158e18790ef725e2ec7309ebced3acd6c9a557ac075b
|
|
||||||
languageName: node
|
|
||||||
linkType: hard
|
|
||||||
|
|
||||||
"chalk@npm:^2.4.2":
|
"chalk@npm:^2.4.2":
|
||||||
version: 2.4.2
|
version: 2.4.2
|
||||||
resolution: "chalk@npm:2.4.2"
|
resolution: "chalk@npm:2.4.2"
|
||||||
@ -2387,6 +2380,13 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
linkType: hard
|
||||||
|
|
||||||
|
"gha-utils@npm:^0.1.0":
|
||||||
|
version: 0.1.0
|
||||||
|
resolution: "gha-utils@npm:0.1.0"
|
||||||
|
checksum: 10c0/8c1cc68e89434a8af8a64d71a2dae9a9248ce581b522718ba51f78b5447cad9c16b8d70097a885f797d547bbafdb7bc481d9d41ac1ce0b2db084d500ebfbc838
|
||||||
|
languageName: node
|
||||||
|
linkType: hard
|
||||||
|
|
||||||
"glob-parent@npm:^5.1.2":
|
"glob-parent@npm:^5.1.2":
|
||||||
version: 5.1.2
|
version: 5.1.2
|
||||||
resolution: "glob-parent@npm:5.1.2"
|
resolution: "glob-parent@npm:5.1.2"
|
||||||
@ -4132,8 +4132,8 @@ __metadata:
|
|||||||
"@rollup/plugin-typescript": "npm:^11.1.6"
|
"@rollup/plugin-typescript": "npm:^11.1.6"
|
||||||
"@types/jest": "npm:^29.5.12"
|
"@types/jest": "npm:^29.5.12"
|
||||||
"@types/node": "npm:^22.1.0"
|
"@types/node": "npm:^22.1.0"
|
||||||
catched-error-message: "npm:^0.0.1"
|
|
||||||
eslint: "npm:^9.8.0"
|
eslint: "npm:^9.8.0"
|
||||||
|
gha-utils: "npm:^0.1.0"
|
||||||
jest: "npm:^29.7.0"
|
jest: "npm:^29.7.0"
|
||||||
prettier: "npm:^3.3.3"
|
prettier: "npm:^3.3.3"
|
||||||
rollup: "npm:^4.20.0"
|
rollup: "npm:^4.20.0"
|
||||||
|
Loading…
Reference in New Issue
Block a user