Add EveUnity UPM artifact smoke
This commit is contained in:
parent
718e9f773b
commit
bdafa0295a
15 changed files with 362 additions and 20 deletions
164
tools/eveunity/eveunity-release-artifact.mjs
Normal file
164
tools/eveunity/eveunity-release-artifact.mjs
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
export function buildUnityReleaseArtifactPlan({ request, packageManifest } = {}) {
|
||||
if (!request || typeof request !== "object") {
|
||||
throw new Error("release request is required");
|
||||
}
|
||||
if (!packageManifest || typeof packageManifest !== "object") {
|
||||
throw new Error("package manifest is required");
|
||||
}
|
||||
if (request.artifactKind !== "upm-package") {
|
||||
throw new Error(`release artifact kind must be upm-package: ${request.artifactKind || "missing"}`);
|
||||
}
|
||||
if (packageManifest.name !== request.packageName) {
|
||||
throw new Error(`package name mismatch: expected ${request.packageName || "missing"} got ${packageManifest.name || "missing"}`);
|
||||
}
|
||||
if (packageManifest.version !== request.version) {
|
||||
throw new Error(`package version mismatch: expected ${request.version || "missing"} got ${packageManifest.version || "missing"}`);
|
||||
}
|
||||
if (!request.packageRoot) {
|
||||
throw new Error("release request missing packageRoot");
|
||||
}
|
||||
if (!request.artifactPath || !request.artifactPath.endsWith(".tgz")) {
|
||||
throw new Error(`release artifact path must be a .tgz file: ${request.artifactPath || "missing"}`);
|
||||
}
|
||||
|
||||
const artifactDirectory = path.dirname(request.artifactPath);
|
||||
const expectedFileName = path.basename(request.artifactPath);
|
||||
return {
|
||||
packageRoot: request.packageRoot,
|
||||
artifactPath: request.artifactPath,
|
||||
artifactDirectory,
|
||||
expectedFileName,
|
||||
npmPackArguments: ["pack", request.packageRoot, "--pack-destination", artifactDirectory],
|
||||
};
|
||||
}
|
||||
|
||||
export function packUnityReleaseArtifact({
|
||||
request,
|
||||
projectRoot = process.cwd(),
|
||||
npmCommand = "",
|
||||
} = {}) {
|
||||
const packageRoot = resolveProjectPath(projectRoot, request?.packageRoot || "");
|
||||
const packageManifestPath = path.join(packageRoot, "package.json");
|
||||
if (!existsSync(packageManifestPath)) {
|
||||
throw new Error(`release package manifest not found: ${packageManifestPath}`);
|
||||
}
|
||||
|
||||
const packageManifest = loadJsonFile(packageManifestPath);
|
||||
const plan = buildUnityReleaseArtifactPlan({ request, packageManifest });
|
||||
const artifactDirectory = resolveProjectPath(projectRoot, plan.artifactDirectory);
|
||||
const artifactPath = resolveProjectPath(projectRoot, plan.artifactPath);
|
||||
mkdirSync(artifactDirectory, { recursive: true });
|
||||
|
||||
const npmInvocation = resolveNpmInvocation(npmCommand);
|
||||
const packArguments = ["pack", packageRoot, "--pack-destination", artifactDirectory];
|
||||
const result = spawnSync(npmInvocation.command, [...npmInvocation.prefixArguments, ...packArguments], {
|
||||
cwd: projectRoot,
|
||||
encoding: "utf8",
|
||||
shell: npmInvocation.shell,
|
||||
});
|
||||
if (result.status !== 0) {
|
||||
throw new Error(`npm pack failed with exit code ${result.status ?? "spawn-error"}: ${result.error?.message || result.stderr || result.stdout || "no output"}`);
|
||||
}
|
||||
|
||||
const packedFileName = result.stdout.trim().split(/\r?\n/).filter(Boolean).pop() || plan.expectedFileName;
|
||||
const packedPath = path.join(artifactDirectory, packedFileName);
|
||||
if (!existsSync(packedPath)) {
|
||||
throw new Error(`npm pack did not produce expected output: ${packedPath}`);
|
||||
}
|
||||
if (path.resolve(packedPath) !== path.resolve(artifactPath)) {
|
||||
throw new Error(`npm pack output mismatch: expected ${artifactPath} got ${packedPath}`);
|
||||
}
|
||||
|
||||
const stats = statSync(artifactPath);
|
||||
if (stats.size <= 0) {
|
||||
throw new Error(`release artifact is empty: ${artifactPath}`);
|
||||
}
|
||||
|
||||
return {
|
||||
schema: "gamecult.eve.runtime_release_artifact.v1",
|
||||
ownerRepo: request.ownerRepo,
|
||||
repository: request.repository,
|
||||
packageName: request.packageName,
|
||||
version: request.version,
|
||||
artifactKind: request.artifactKind,
|
||||
artifactPath: request.artifactPath,
|
||||
artifactSizeBytes: stats.size,
|
||||
packageRoot: request.packageRoot,
|
||||
sourceRequestPath: request.sourceRequestPath || "",
|
||||
};
|
||||
}
|
||||
|
||||
export function loadJsonFile(filePath) {
|
||||
return JSON.parse(readFileSync(filePath, "utf8"));
|
||||
}
|
||||
|
||||
export function writeJsonFile(document, outputPath) {
|
||||
mkdirSync(path.dirname(outputPath), { recursive: true });
|
||||
writeFileSync(outputPath, `${JSON.stringify(document, null, 2)}\n`, "utf8");
|
||||
}
|
||||
|
||||
function resolveProjectPath(projectRoot, filePath) {
|
||||
if (!filePath) return projectRoot;
|
||||
return path.isAbsolute(filePath) ? filePath : path.resolve(projectRoot, filePath);
|
||||
}
|
||||
|
||||
function resolveNpmInvocation(npmCommand) {
|
||||
if (npmCommand) {
|
||||
return { command: npmCommand, prefixArguments: [], shell: false };
|
||||
}
|
||||
|
||||
const npmCliPath = path.join(path.dirname(process.execPath), "node_modules", "npm", "bin", "npm-cli.js");
|
||||
if (existsSync(npmCliPath)) {
|
||||
return { command: process.execPath, prefixArguments: [npmCliPath], shell: false };
|
||||
}
|
||||
|
||||
return { command: "npm", prefixArguments: [], shell: true };
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {};
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const value = argv[index];
|
||||
if (!value.startsWith("--")) continue;
|
||||
const key = value.slice(2);
|
||||
const next = argv[index + 1];
|
||||
if (!next || next.startsWith("--")) {
|
||||
args[key] = true;
|
||||
} else {
|
||||
args[key] = next;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
function runCli() {
|
||||
const args = parseArgs(process.argv.slice(2));
|
||||
for (const required of ["request", "proof-output"]) {
|
||||
if (!args[required]) {
|
||||
throw new Error(`missing required --${required}`);
|
||||
}
|
||||
}
|
||||
|
||||
const projectRoot = args["project-root"] ? path.resolve(args["project-root"]) : process.cwd();
|
||||
const requestPath = resolveProjectPath(projectRoot, args.request);
|
||||
const request = {
|
||||
...loadJsonFile(requestPath),
|
||||
sourceRequestPath: args.request,
|
||||
};
|
||||
const proof = packUnityReleaseArtifact({ request, projectRoot });
|
||||
const proofOutputPath = resolveProjectPath(projectRoot, args["proof-output"]);
|
||||
writeJsonFile(proof, proofOutputPath);
|
||||
process.stdout.write(`${proof.artifactPath}\n`);
|
||||
}
|
||||
|
||||
const invokedPath = process.argv[1] ? path.resolve(process.argv[1]) : "";
|
||||
const modulePath = fileURLToPath(import.meta.url);
|
||||
if (invokedPath === modulePath) {
|
||||
runCli();
|
||||
}
|
||||
41
tools/eveunity/eveunity-release-artifact.test.mjs
Normal file
41
tools/eveunity/eveunity-release-artifact.test.mjs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import test from "node:test";
|
||||
import { buildUnityReleaseRequest } from "./eveunity-release-contract.mjs";
|
||||
import { buildUnityReleaseArtifactPlan } from "./eveunity-release-artifact.mjs";
|
||||
|
||||
const capabilityManifest = JSON.parse(readFileSync(new URL("../../packages/org.gamecult.eve.unity-uitoolkit/eve-runtime-capability.json", import.meta.url), "utf8"));
|
||||
const packageManifest = JSON.parse(readFileSync(new URL("../../packages/org.gamecult.eve.unity-uitoolkit/package.json", import.meta.url), "utf8"));
|
||||
|
||||
test("plans npm pack for the EveUnity UI Toolkit UPM artifact", () => {
|
||||
const request = buildUnityReleaseRequest({
|
||||
capabilityManifest,
|
||||
packageManifest,
|
||||
capabilityManifestPath: "packages/org.gamecult.eve.unity-uitoolkit/eve-runtime-capability.json",
|
||||
packageManifestPath: "packages/org.gamecult.eve.unity-uitoolkit/package.json",
|
||||
});
|
||||
const plan = buildUnityReleaseArtifactPlan({ request, packageManifest });
|
||||
|
||||
assert.equal(plan.packageRoot, "packages/org.gamecult.eve.unity-uitoolkit");
|
||||
assert.equal(plan.artifactPath, "artifacts/eveunity-uitoolkit-release/0.1.0/org.gamecult.eve.unity-uitoolkit-0.1.0.tgz");
|
||||
assert.equal(plan.artifactDirectory, "artifacts/eveunity-uitoolkit-release/0.1.0");
|
||||
assert.equal(plan.expectedFileName, "org.gamecult.eve.unity-uitoolkit-0.1.0.tgz");
|
||||
assert.deepEqual(plan.npmPackArguments, [
|
||||
"pack",
|
||||
"packages/org.gamecult.eve.unity-uitoolkit",
|
||||
"--pack-destination",
|
||||
"artifacts/eveunity-uitoolkit-release/0.1.0",
|
||||
]);
|
||||
});
|
||||
|
||||
test("rejects release artifact plans for mismatched package identity", () => {
|
||||
const request = buildUnityReleaseRequest({ capabilityManifest, packageManifest });
|
||||
|
||||
assert.throws(
|
||||
() => buildUnityReleaseArtifactPlan({
|
||||
request,
|
||||
packageManifest: { ...packageManifest, name: "org.gamecult.eve.wrong" },
|
||||
}),
|
||||
/package name mismatch/,
|
||||
);
|
||||
});
|
||||
Loading…
Reference in a new issue