173 lines
5.9 KiB
JavaScript
173 lines
5.9 KiB
JavaScript
import { mkdirSync, readFileSync } from "node:fs";
|
|
import { spawn, spawnSync } from "node:child_process";
|
|
import { dirname, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
const bun = process.env.BUN_BIN || "bun";
|
|
|
|
const TARGET_MAP = {
|
|
"darwin-arm64": { bunTarget: "bun-darwin-arm64", outfile: "dist/bin/seal-use-darwin-arm64" },
|
|
"darwin-x64": { bunTarget: "bun-darwin-x64", outfile: "dist/bin/seal-use-darwin-x64" },
|
|
"linux-x64": { bunTarget: "bun-linux-x64", outfile: "dist/bin/seal-use-linux-x64" },
|
|
"linux-arm64": { bunTarget: "bun-linux-arm64", outfile: "dist/bin/seal-use-linux-arm64" },
|
|
"windows-x64": { bunTarget: "bun-windows-x64", outfile: "dist/bin/seal-use-windows-x64.exe" }
|
|
};
|
|
|
|
const args = process.argv.slice(2);
|
|
const isAll = args.includes("--all") || args.includes("--matrix");
|
|
const targetArg = args.find((arg) => arg.startsWith("--target="))?.replace("--target=", "");
|
|
const shouldMinify = !args.includes("--no-minify");
|
|
const isRelease = args.includes("--release") ||
|
|
process.env.SEAL_BUILD_RELEASE === "1" ||
|
|
(Boolean(process.env.CI) && process.env.GITHUB_REF_TYPE === "tag");
|
|
|
|
function getBuildInfo(releaseMode) {
|
|
const pkg = JSON.parse(readFileSync(resolve(root, "package.json"), "utf8"));
|
|
if (releaseMode) {
|
|
return {
|
|
version: pkg.version,
|
|
channel: "release",
|
|
isRelease: true
|
|
};
|
|
}
|
|
|
|
try {
|
|
const res = spawnSync("git", ["describe", "--tags", "--long", "--dirty", "--always"], {
|
|
cwd: root,
|
|
encoding: "utf8"
|
|
});
|
|
if (res.status === 0 && res.stdout) {
|
|
const trimmed = res.stdout.trim();
|
|
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]+)$/);
|
|
let baseVersion = pkg.version;
|
|
let commitsAhead = 0;
|
|
let commit = "";
|
|
|
|
if (longMatch) {
|
|
baseVersion = longMatch[1];
|
|
commitsAhead = Number.parseInt(longMatch[2], 10);
|
|
commit = longMatch[3];
|
|
} else {
|
|
const rev = spawnSync("git", ["rev-parse", "--short", "HEAD"], { cwd: root, encoding: "utf8" });
|
|
if (rev.status === 0 && rev.stdout) commit = rev.stdout.trim();
|
|
}
|
|
|
|
const dirtySuffix = dirty ? ".dirty" : "";
|
|
const commitSuffix = commit ? `+g${commit}${dirtySuffix}` : dirty ? "+dirty" : "";
|
|
const version = `${baseVersion}-dev.${commitsAhead}${commitSuffix}`;
|
|
|
|
return {
|
|
version,
|
|
channel: "dev",
|
|
isRelease: false,
|
|
git: {
|
|
baseVersion,
|
|
commitsAhead,
|
|
commit,
|
|
dirty,
|
|
describe: trimmed
|
|
}
|
|
};
|
|
}
|
|
} catch {
|
|
// fallback
|
|
}
|
|
|
|
return {
|
|
version: `${pkg.version}-dev.0+local`,
|
|
channel: "dev",
|
|
isRelease: false
|
|
};
|
|
}
|
|
|
|
async function main() {
|
|
const startTime = Date.now();
|
|
const buildInfo = getBuildInfo(isRelease);
|
|
console.log(`[build:binary] version: ${buildInfo.version} (${buildInfo.channel}, isRelease: ${buildInfo.isRelease})`);
|
|
|
|
const buildEnv = {
|
|
...process.env,
|
|
SEAL_BUILD_VERSION: buildInfo.version,
|
|
SEAL_BUILD_CHANNEL: buildInfo.channel,
|
|
SEAL_BUILD_IS_RELEASE: String(buildInfo.isRelease),
|
|
...(buildInfo.git ? {
|
|
SEAL_BUILD_GIT_BASE: buildInfo.git.baseVersion,
|
|
SEAL_BUILD_GIT_COMMITS_AHEAD: String(buildInfo.git.commitsAhead),
|
|
SEAL_BUILD_GIT_COMMIT: buildInfo.git.commit,
|
|
SEAL_BUILD_GIT_DIRTY: String(buildInfo.git.dirty),
|
|
SEAL_BUILD_GIT_DESCRIBE: buildInfo.git.describe
|
|
} : {})
|
|
};
|
|
|
|
if (isAll) {
|
|
console.log(`[build:binary] compiling ${Object.keys(TARGET_MAP).length} targets in parallel (minify: ${shouldMinify})...`);
|
|
await Promise.all(
|
|
Object.entries(TARGET_MAP).map(([name, config]) =>
|
|
buildTarget(name, config.bunTarget, resolve(root, config.outfile), shouldMinify, buildEnv)
|
|
)
|
|
);
|
|
console.log(`[build:binary] all targets compiled in ${((Date.now() - startTime) / 1000).toFixed(2)}s`);
|
|
} else if (targetArg) {
|
|
const config = TARGET_MAP[targetArg];
|
|
if (!config) {
|
|
throw new Error(`Unknown target: ${targetArg}. Available targets: ${Object.keys(TARGET_MAP).join(", ")}`);
|
|
}
|
|
await buildTarget(targetArg, config.bunTarget, resolve(root, config.outfile), shouldMinify, buildEnv);
|
|
} else {
|
|
const output = resolve(root, "dist/bin/seal-use");
|
|
await buildTarget("host", null, output, shouldMinify, buildEnv);
|
|
}
|
|
}
|
|
|
|
async function buildTarget(name, bunTarget, output, minify = true, buildEnv = process.env) {
|
|
mkdirSync(dirname(output), { recursive: true });
|
|
const buildArgs = ["build", "--compile", "--env=SEAL_BUILD_*"];
|
|
if (minify) {
|
|
buildArgs.push("--minify");
|
|
}
|
|
if (bunTarget) {
|
|
buildArgs.push(`--target=${bunTarget}`);
|
|
}
|
|
buildArgs.push("--outfile", output, "src/cli.ts");
|
|
|
|
console.log(`[build:binary] start compiling: ${name} -> ${output}`);
|
|
await runAsync(bun, buildArgs, { env: buildEnv });
|
|
|
|
if (process.platform === "darwin" && (name.startsWith("darwin") || name === "host")) {
|
|
try {
|
|
await runAsync("/usr/bin/codesign", ["--remove-signature", output], { quiet: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
try {
|
|
await runAsync("/usr/bin/codesign", ["--force", "--sign", "-", output], { quiet: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
console.log(`✓ [build:binary] finished: ${name}`);
|
|
}
|
|
|
|
function runAsync(command, args, options = {}) {
|
|
return new Promise((res, rej) => {
|
|
const child = spawn(command, args, {
|
|
cwd: root,
|
|
stdio: options.quiet ? ["ignore", "ignore", "ignore"] : "inherit",
|
|
env: options.env ?? process.env
|
|
});
|
|
child.on("error", rej);
|
|
child.on("exit", (code) => {
|
|
if (code === 0) res();
|
|
else rej(new Error(`${command} ${args.join(" ")} failed with status ${code}`));
|
|
});
|
|
});
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|