fix(packaging): distribute plugin archives without tracking dist

This commit is contained in:
gitea committed 2026-10-26 11:54:37 +08:00
1 parent 8571390f15
commit f4788375a1
11 files changed
+159 -2430

No files matched your search

+5 -1
View File
@@ -5,6 +5,10 @@ import { fileURLToPath } from "node:url";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const bun = process.env.BUN_BIN || "bun";
const args = process.argv.slice(2);
if (args.some(arg => !arg.startsWith("--outfile="))) {
throw new Error("Usage: build-plugin.mjs [--outfile=<path>]");
}
function run(args) {
const result = spawnSync(bun, args, {
@@ -17,7 +21,7 @@ function run(args) {
}
}
const mcpOutput = resolve(root, "dist/mcp/server.js");
const mcpOutput = resolve(args.find(arg => arg.startsWith("--outfile="))?.slice("--outfile=".length) || resolve(root, "dist/mcp/server.js"));
mkdirSync(dirname(mcpOutput), { recursive: true });
// The shared CLI uses bun:sqlite; the plugin manifest must use this runtime too.
// Embedded CommonJS loaders must use the bundle path, not the build host's absolute checkout path.
+35
View File
@@ -0,0 +1,35 @@
import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { spawnSync } from "node:child_process";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const args = process.argv.slice(2);
if (args.some(arg => !arg.startsWith("--out-dir="))) {
throw new Error("Usage: package-plugin.mjs [--out-dir=<path>]");
}
const outputDir = resolve(args.find(arg => arg.startsWith("--out-dir="))?.slice("--out-dir=".length) || resolve(root, "dist/bin"));
const { version } = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
const archive = join(outputDir, `hose-use-plugin-v${version}.tar.gz`);
const stage = mkdtempSync(join(tmpdir(), "hose-plugin-package-"));
function run(command, commandArgs) {
const result = spawnSync(command, commandArgs, { cwd: root, stdio: "inherit" });
if (result.error) throw result.error;
if (result.status !== 0) throw new Error(`${command} failed with status ${result.status}`);
}
try {
// Explicit package inputs keep credentials, local notes and dependencies out of the archive.
for (const path of [".zcode-plugin/plugin.json", ".mcp.json", "skills"]) {
cpSync(join(root, path), join(stage, path), { recursive: true });
}
// Always build from current source; never package a stale local dist/ bundle.
run(process.execPath, [join(root, "scripts/build-plugin.mjs"), `--outfile=${join(stage, "dist/mcp/server.js")}`]);
mkdirSync(outputDir, { recursive: true });
run("tar", ["-czf", archive, "-C", stage, ".zcode-plugin", ".mcp.json", "skills", "dist"]);
console.log(`✓ Packaged plugin: ${archive}`);
} finally {
rmSync(stage, { recursive: true, force: true });
}
+31 -6
View File
@@ -1,12 +1,37 @@
import { expect, test } from "bun:test";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
test("source and committed plugin preserve offline review execution contracts", () => {
test("source and freshly packaged plugin preserve offline review and MCP contracts without a prebuilt dist", () => {
const root = resolve(import.meta.dir, "..");
const temp = mkdtempSync(join(tmpdir(), "hose-review-plugin-"));
const temp = mkdtempSync(join(tmpdir(), "hose review plugin "));
try {
const packaged = Bun.spawnSync([
process.execPath, join(root, "scripts/package-plugin.mjs"), `--out-dir=${join(temp, "artifacts")}`,
], { cwd: temp, env: { ...process.env, BUN_BIN: process.execPath }, timeout: 30_000 });
expect(packaged.exitCode, packaged.stderr.toString()).toBe(0);
const version = JSON.parse(readFileSync(join(root, "package.json"), "utf8")).version;
const archive = join(temp, "artifacts", `hose-use-plugin-v${version}.tar.gz`);
const pluginRoot = join(temp, "unpacked");
mkdirSync(pluginRoot);
const extracted = Bun.spawnSync(["tar", "-xzf", archive, "-C", pluginRoot]);
expect(extracted.exitCode, extracted.stderr.toString()).toBe(0);
expect(readdirSync(pluginRoot).sort()).toEqual([".mcp.json", ".zcode-plugin", "dist", "skills"]);
const manifest = JSON.parse(readFileSync(join(pluginRoot, ".zcode-plugin/plugin.json"), "utf8"));
expect(manifest.version).toBe(version);
expect(manifest.skills).toBe("skills");
const mcp = JSON.parse(readFileSync(join(pluginRoot, manifest.mcpServers), "utf8")).mcpServers["hose-use"];
expect(mcp.command).toBe("bun");
const bundle = mcp.args[0].replace("${CLAUDE_PLUGIN_ROOT}", pluginRoot);
expect(existsSync(bundle)).toBe(true);
for (const file of readdirSync(join(root, "skills"), { recursive: true })) {
expect(existsSync(join(pluginRoot, "skills", file))).toBe(true);
}
const protocol = Bun.spawnSync([
process.execPath, join(root, "scripts/verify-binary.ts"), bundle, "--plugin",
], { cwd: temp, timeout: 30_000 });
expect(protocol.exitCode, protocol.stderr.toString()).toBe(0);
const enterprises = join(temp, "enterprises");
mkdirSync(enterprises);
writeFileSync(join(enterprises, "smoke.json"), JSON.stringify({
@@ -29,11 +54,11 @@ test("source and committed plugin preserve offline review execution contracts",
for (const fixture of fixtures) {
const file = join(temp, `${fixture.name}.script`);
writeFileSync(file, fixture.code);
for (const entry of [["src/cli.ts"], ["dist/mcp/server.js", "--hose-use-cli"]]) {
for (const entry of [[join(root, "src/cli.ts")], [bundle, "--hose-use-cli"]]) {
const result = Bun.spawnSync([
process.execPath, ...entry, "workflow", "eval-script", "--file", file,
"--type", fixture.type, "--corp", "smoke", "--stateless", "--json",
], { cwd: root, env, timeout: 10000 });
], { cwd: pluginRoot, env, timeout: 10000 });
expect(result.exitCode).toBe(fixture.error ? 1 : 0);
const report = JSON.parse(result.stdout.toString());
expect(report.success).toBe(!fixture.error);
@@ -44,4 +69,4 @@ test("source and committed plugin preserve offline review execution contracts",
} finally {
rmSync(temp, { recursive: true, force: true });
}
});
}, 60_000);
+4 -2
View File
@@ -38,7 +38,7 @@ function mirrorFixture(mode: "retry" | "skip" | "concurrency", mirrorOnly = fals
const f = repository();
const tools = join(f.root, "tools"), published = join(f.root, "published");
mkdirSync(tools); mkdirSync(published); mkdirSync(join(f.root, "dist/bin"), { recursive: true });
const names = mode === "concurrency" ? Array.from({ length: 5 }, (_, i) => `hose-use-fixture-${i}`) : ["hose-use-linux-x64"];
const names = mode === "concurrency" ? Array.from({ length: 5 }, (_, i) => `hose-use-fixture-${i}`) : ["hose-use-linux-x64", "hose-use-plugin-v1.0.0.tar.gz"];
const manifest = names.map(name => {
const data = `published bytes for ${name}`;
writeFileSync(join(published, name), data);
@@ -138,9 +138,11 @@ describe("release preparation and publication guards", () => {
for (const step of [...ci.jobs.validate.steps, ...steps]) if (step.uses) expect(step.uses).toMatch(/@[a-f0-9]{40}$/);
expect(JSON.parse(readFileSync(join(root, ".mcp.json"), "utf8")).mcpServers["hose-use"].command).toBe("bun");
expect(release.on.workflow_dispatch.inputs.mirror_only.type).toBe("boolean");
for (const name of ["Compile multi-target binaries", "Package release archives", "Publish GitHub Release"]) {
for (const name of ["Compile multi-target binaries", "Package plugin archive", "Package release archives", "Publish GitHub Release"]) {
expect(steps.find((s: { name?: string }) => s.name === name).if).toBe("${{ !inputs.mirror_only }}");
}
expect(runs.findIndex((s: string) => s.includes("package:plugin"))).toBeLessThan(runs.findIndex((s: string) => s.includes("sha256sum hose-use-*")));
expect(steps.find((s: { name?: string }) => s.name === "Package plugin archive").run).toBe("bun run package:plugin");
}, 60_000);
test("a failed background Gitea upload makes the Release step fail", () => {