feat: make development builds visibly identifiable

This commit is contained in:
gitea committed 2026-09-13 14:24:27 +08:00
1 parent 0554f41d12
commit 2ed9bf0236
13 files changed
+285 -35

No files matched your search

+2 -1
View File
@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { runAiProCli } from "./aipro-cli.js";
import { closeApprovalDatabase, getApprovalDatabase } from "./domains/approval/db.js";
import { ApprovalRepository } from "./domains/approval/repository.js";
import { getVersionInfo } from "./version.js";
describe("CLI - hose-aipro 审批流与网关阻拦全链路测试", () => {
beforeEach(() => {
@@ -29,7 +30,7 @@ describe("CLI - hose-aipro 审批流与网关阻拦全链路测试", () => {
const version = await runAiProCli(["version", "--json"]);
const help = await runAiProCli(["help", "--json"]);
expect(JSON.parse(version.stdout)).toEqual({ name: "hose-aipro", version: expect.any(String) });
expect(JSON.parse(version.stdout)).toEqual(getVersionInfo("hose-aipro"));
expect(JSON.parse(help.stdout).help).toContain("hose-aipro");
expect(version.stderr).toBe("");
expect(help.stderr).toBe("");
+3 -3
View File
@@ -17,7 +17,7 @@ import {
import { getRawBillFlow } from "./domains/bill/api.js";
import { approveBill, rejectBill, getTranslatorContext } from "./domains/bill/tools.js";
import { translateBillFlowToMarkdown } from "./domains/bill/translator.js";
import { VERSION } from "./version.js";
import { VERSION, formatVersionLabel, getVersionInfo } from "./version.js";
export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string; stderr: string; exitCode: number }> {
const parsed = parseArgs(rawArgs);
@@ -77,8 +77,8 @@ export async function runAiProCli(rawArgs: string[]): Promise<{ stdout: string;
if (command === "version" || command === "v" || booleanOption(parsed.options, ["version", "v"])) {
return {
stdout: jsonMode
? printJson({ name: "hose-aipro", version: VERSION }) + "\n"
: `hose-aipro v${VERSION}\n`,
? printJson(getVersionInfo("hose-aipro")) + "\n"
: `${formatVersionLabel("hose-aipro")}\n`,
stderr: "",
exitCode: 0,
};
+2 -2
View File
@@ -1,5 +1,5 @@
import { writeFileSync } from "node:fs";
import { VERSION } from "../version.js";
import { formatVersionLabel } from "../version.js";
import type { OutputOptions } from "./types.js";
import { PayloadTooLargeError } from "./errors.js";
import { renderScopedHelp } from "./help.js";
@@ -115,7 +115,7 @@ export function printTable(
export function printHelp(target: string[] = []): string {
if (target.length) return renderScopedHelp(target);
return `
合思/易快报企业级 CLI 工具 (hose-use) v${VERSION}
合思/易快报企业级 CLI 工具 (${formatVersionLabel("hose-use")})
用法:
hose-use <命令域> <子命令> [参数] [选项]
+4 -4
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "bun:test";
import { runCli } from "./runner.js";
import { VERSION } from "../version.js";
import { VERSION_INFO, formatVersionLabel } from "../version.js";
describe("CLI runner - version and help aliases", () => {
const versionVariants = [
@@ -25,7 +25,7 @@ describe("CLI runner - version and help aliases", () => {
const result = await runCli(args);
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe("");
expect(result.stdout).toBe(`hose-use v${VERSION}\n`);
expect(result.stdout).toBe(`${formatVersionLabel("hose-use")}\n`);
});
}
@@ -50,7 +50,7 @@ describe("CLI runner - version and help aliases", () => {
const result = await runCli(args);
expect(result.exitCode).toBe(0);
expect(result.stderr).toBe("");
expect(result.stdout).toContain(`合思/易快报企业级 CLI 工具 (hose-use) v${VERSION}`);
expect(result.stdout).toContain(`合思/易快报企业级 CLI 工具 (${formatVersionLabel("hose-use")})`);
expect(result.stdout).toContain("用法:");
});
}
@@ -94,7 +94,7 @@ describe("CLI runner - version and help aliases", () => {
const version = await runCli(["version", "--json"]);
expect(JSON.parse(help.stdout).help).toContain("workflow -");
expect(JSON.parse(version.stdout)).toEqual({ name: "hose-use", version: VERSION });
expect(JSON.parse(version.stdout)).toEqual(VERSION_INFO);
expect(help.stderr).toBe("");
expect(version.stderr).toBe("");
});
+2 -2
View File
@@ -1,4 +1,4 @@
import { VERSION } from "../version.js";
import { VERSION_INFO, formatVersionLabel } from "../version.js";
import { parseArgs, stringOption, booleanOption, outputOptions } from "./parser.js";
import { printHelp, printJson, redactSensitiveText } from "./printer.js";
import { classifyCliError } from "./errors.js";
@@ -144,7 +144,7 @@ async function dispatchCli(
if (isVersionRequested(parsed)) {
return {
stdout: jsonMode ? printJson({ name: "hose-use", version: VERSION }) : `hose-use v${VERSION}\n`,
stdout: jsonMode ? printJson(VERSION_INFO) : `${formatVersionLabel("hose-use")}\n`,
stderr: "",
exitCode: 0,
};
+61
View File
@@ -0,0 +1,61 @@
import { describe, expect, test } from "bun:test";
import { formatDevVersion, parseGitDescribe, resolveVersionInfo } from "./version.js";
describe("build version metadata", () => {
test("parses tagged commits and dirty state", () => {
expect(parseGitDescribe("v0.8.6-3-gabc1234")).toMatchObject({
baseVersion: "0.8.6",
commitsAhead: 3,
commit: "abc1234",
dirty: false,
});
expect(parseGitDescribe("v0.8.6-dirty")).toMatchObject({
baseVersion: "0.8.6",
commitsAhead: 0,
dirty: true,
});
});
test("formats a visible semver development version", () => {
expect(formatDevVersion("0.8.6", 2, "abc1234", false)).toBe("0.8.6-dev.2+gabc1234");
expect(formatDevVersion("0.8.6", 2, "abc1234", true)).toBe("0.8.6-dev.2+gabc1234.dirty");
});
test("uses compile-time release metadata when a binary provides it", () => {
expect(resolveVersionInfo({
env: {
HOSE_BUILD_VERSION: "0.8.6",
HOSE_BUILD_CHANNEL: "release",
HOSE_BUILD_IS_RELEASE: "true",
},
})).toEqual({ name: "hose-use", version: "0.8.6", channel: "release", isRelease: true });
});
test("includes git metadata in development mode", () => {
const info = resolveVersionInfo({
env: {
HOSE_BUILD_VERSION: "0.8.6-dev.2+gabc1234",
HOSE_BUILD_CHANNEL: "dev",
HOSE_BUILD_IS_RELEASE: "false",
HOSE_BUILD_GIT_BASE: "0.8.6",
HOSE_BUILD_GIT_COMMITS_AHEAD: "2",
HOSE_BUILD_GIT_COMMIT: "abc1234",
HOSE_BUILD_GIT_DIRTY: "true",
HOSE_BUILD_GIT_DESCRIBE: "v0.8.6-2-gabc1234-dirty",
},
});
expect(info).toEqual({
name: "hose-use",
version: "0.8.6-dev.2+gabc1234",
channel: "dev",
isRelease: false,
git: {
baseVersion: "0.8.6",
commitsAhead: 2,
commit: "abc1234",
dirty: true,
describe: "v0.8.6-2-gabc1234-dirty",
},
});
});
});
+126 -1
View File
@@ -1 +1,126 @@
export const VERSION = "0.8.6";
import { spawnSync } from "node:child_process";
import packageJson from "../package.json" with { type: "json" };
export interface GitBuildInfo {
baseVersion: string;
commitsAhead: number;
commit: string;
dirty: boolean;
describe: string;
}
export interface VersionInfo {
name: "hose-use" | "hose-aipro";
version: string;
channel: "release" | "dev";
isRelease: boolean;
git?: GitBuildInfo;
}
export function parseGitDescribe(output: string, fallbackVersion = packageJson.version): GitBuildInfo | null {
const trimmed = output.trim();
if (!trimmed) return null;
const dirty = trimmed.endsWith("-dirty");
const clean = dirty ? trimmed.slice(0, -6) : trimmed;
const longMatch = clean.match(/^v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?)-(\d+)-g([0-9a-f]+)$/);
if (longMatch && longMatch[1] && longMatch[2] && longMatch[3]) {
return { baseVersion: longMatch[1], commitsAhead: Number.parseInt(longMatch[2], 10), commit: longMatch[3], dirty, describe: trimmed };
}
const tagMatch = clean.match(/^v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.]+)?)$/);
if (tagMatch && tagMatch[1]) {
return { baseVersion: tagMatch[1], commitsAhead: 0, commit: "", dirty, describe: trimmed };
}
const commitMatch = clean.match(/^([0-9a-f]{7,40})$/);
if (commitMatch && commitMatch[1]) {
return { baseVersion: fallbackVersion, commitsAhead: 0, commit: commitMatch[1], dirty, describe: trimmed };
}
return null;
}
export function formatDevVersion(baseVersion: string, commitsAhead: number, commit: string, dirty: boolean): string {
const dirtySuffix = dirty ? ".dirty" : "";
const commitSuffix = commit ? `+g${commit}${dirtySuffix}` : dirty ? "+dirty" : "";
return `${baseVersion}-dev.${commitsAhead}${commitSuffix}`;
}
export function resolveVersionInfo(options?: { env?: Record<string, string | undefined>; cwd?: string }): VersionInfo {
const customEnv = options?.env;
// Keep these as direct property accesses: Bun can inline them into compiled binaries.
const buildVersion = customEnv?.HOSE_BUILD_VERSION ?? process.env.HOSE_BUILD_VERSION;
const buildIsRelease = customEnv?.HOSE_BUILD_IS_RELEASE ?? process.env.HOSE_BUILD_IS_RELEASE;
const buildChannel = customEnv?.HOSE_BUILD_CHANNEL ?? process.env.HOSE_BUILD_CHANNEL;
const buildGitBase = customEnv?.HOSE_BUILD_GIT_BASE ?? process.env.HOSE_BUILD_GIT_BASE;
const buildGitCommitsAhead = customEnv?.HOSE_BUILD_GIT_COMMITS_AHEAD ?? process.env.HOSE_BUILD_GIT_COMMITS_AHEAD;
const buildGitCommit = customEnv?.HOSE_BUILD_GIT_COMMIT ?? process.env.HOSE_BUILD_GIT_COMMIT;
const buildGitDirty = customEnv?.HOSE_BUILD_GIT_DIRTY ?? process.env.HOSE_BUILD_GIT_DIRTY;
const buildGitDescribe = customEnv?.HOSE_BUILD_GIT_DESCRIBE ?? process.env.HOSE_BUILD_GIT_DESCRIBE;
const forceRelease = customEnv?.HOSE_FORCE_RELEASE ?? process.env.HOSE_FORCE_RELEASE;
if (buildVersion) {
const isRelease = buildIsRelease === "true";
const channel = (buildChannel as "release" | "dev") || (isRelease ? "release" : "dev");
const result: VersionInfo = { name: "hose-use", version: buildVersion, channel, isRelease };
if (buildGitBase) {
result.git = {
baseVersion: buildGitBase,
commitsAhead: Number.parseInt(buildGitCommitsAhead || "0", 10),
commit: buildGitCommit || "",
dirty: buildGitDirty === "true",
describe: buildGitDescribe || buildVersion,
};
}
return result;
}
if (buildIsRelease === "true" || forceRelease === "1") {
return { name: "hose-use", version: packageJson.version, channel: "release", isRelease: true };
}
try {
const result = spawnSync("git", ["describe", "--tags", "--long", "--dirty", "--always"], {
cwd: options?.cwd,
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
if (result.status === 0 && result.stdout) {
const gitInfo = parseGitDescribe(result.stdout.trim(), packageJson.version);
if (gitInfo) {
if (!gitInfo.commit) {
const revision = spawnSync("git", ["rev-parse", "--short", "HEAD"], {
cwd: options?.cwd,
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
if (revision.status === 0 && revision.stdout) gitInfo.commit = revision.stdout.trim();
}
return {
name: "hose-use",
version: formatDevVersion(gitInfo.baseVersion, gitInfo.commitsAhead, gitInfo.commit, gitInfo.dirty),
channel: "dev",
isRelease: false,
git: gitInfo,
};
}
}
} catch {
// Fall back below if git is unavailable.
}
return { name: "hose-use", version: `${packageJson.version}-dev.0+local`, channel: "dev", isRelease: false };
}
export const VERSION_INFO = resolveVersionInfo();
export const VERSION = VERSION_INFO.version;
export const IS_RELEASE = VERSION_INFO.isRelease;
export const CHANNEL = VERSION_INFO.channel;
export function getVersionInfo(name: VersionInfo["name"]): VersionInfo {
return { ...VERSION_INFO, name };
}
export function formatVersionLabel(name: VersionInfo["name"]): string {
return `${name} v${VERSION}${CHANNEL === "dev" ? " [DEV BUILD]" : ""}`;
}