Update macOS build process and enhance artifact finalization
- Replaced the macOS build command in package.json to use a new script for better architecture handling. - Added a new script for building macOS applications that supports both arm64 and x64 architectures. - Enhanced the finalize-desktop-artifacts script to improve artifact discovery and cleanup, ensuring proper handling of macOS DMG and app bundles. - Updated functions to streamline the process of finding and publishing artifacts, improving overall reliability.
This commit is contained in:
parent
6ed2347214
commit
90da336696
@ -14,7 +14,7 @@
|
|||||||
"build:server": "bun run scripts/build-server.mjs",
|
"build:server": "bun run scripts/build-server.mjs",
|
||||||
"build:renderer": "vite build src/app",
|
"build:renderer": "vite build src/app",
|
||||||
"build:app": "bun run build && electrobun build --env=stable",
|
"build:app": "bun run build && electrobun build --env=stable",
|
||||||
"build:app:mac": "bun run build && electrobun build --env=stable",
|
"build:app:mac": "bun scripts/build-macos.mjs",
|
||||||
"build:linux": "bun run cleanBuild && bun run build:server && bun run build:linux-binary && bun run build:linux-packages",
|
"build:linux": "bun run cleanBuild && bun run build:server && bun run build:linux-binary && bun run build:linux-packages",
|
||||||
"build:linux-binary": "bun scripts/build-linux-binary.mjs",
|
"build:linux-binary": "bun scripts/build-linux-binary.mjs",
|
||||||
"build:linux-packages": "bash scripts/build-linux-packages.sh",
|
"build:linux-packages": "bash scripts/build-linux-packages.sh",
|
||||||
|
|||||||
53
scripts/build-macos.mjs
Normal file
53
scripts/build-macos.mjs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import { spawnSync } from "node:child_process";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||||
|
const hostArch = process.arch === "arm64" ? "arm64" : "x64";
|
||||||
|
const targetArchs = ["arm64", "x64"];
|
||||||
|
|
||||||
|
function run(command, args, options = {}) {
|
||||||
|
const result = spawnSync(command, args, {
|
||||||
|
cwd: rootDir,
|
||||||
|
stdio: "inherit",
|
||||||
|
env: process.env,
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.status !== 0) {
|
||||||
|
process.exit(result.status ?? 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getElectrobunCommand(targetArch) {
|
||||||
|
const electrobunArgs = ["electrobun", "build", "--env=stable"];
|
||||||
|
|
||||||
|
if (targetArch === hostArch) {
|
||||||
|
return { command: "bun", args: electrobunArgs };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hostArch === "arm64" && targetArch === "x64") {
|
||||||
|
return { command: "arch", args: ["-x86_64", "bun", ...electrobunArgs] };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hostArch === "x64" && targetArch === "arm64") {
|
||||||
|
return { command: "arch", args: ["-arm64", "bun", ...electrobunArgs] };
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
run("bun", ["run", "build"]);
|
||||||
|
|
||||||
|
for (const targetArch of targetArchs) {
|
||||||
|
const invocation = getElectrobunCommand(targetArch);
|
||||||
|
if (!invocation) {
|
||||||
|
console.warn(
|
||||||
|
`Skipping macOS ${targetArch} build: unsupported cross-compile from ${hostArch}.`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`\n=== Building macOS ${targetArch} ===\n`);
|
||||||
|
run(invocation.command, invocation.args);
|
||||||
|
}
|
||||||
@ -23,6 +23,7 @@ const packageJson = JSON.parse(
|
|||||||
const version = getReleaseVersion(packageJson);
|
const version = getReleaseVersion(packageJson);
|
||||||
const artifactDir = path.join(rootDir, "app_dist");
|
const artifactDir = path.join(rootDir, "app_dist");
|
||||||
const identifier = "com.tombutcher.farmcontrolserver";
|
const identifier = "com.tombutcher.farmcontrolserver";
|
||||||
|
const artifactPrefix = `farmcontrol-server-${version}-`;
|
||||||
|
|
||||||
function walkFiles(dir) {
|
function walkFiles(dir) {
|
||||||
const files = [];
|
const files = [];
|
||||||
@ -49,28 +50,42 @@ function findByExtension(root, extension) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function findMacAppBundle() {
|
function findMacAppBundle(arch) {
|
||||||
const buildDir = path.join(rootDir, "build");
|
const platformDir = path.join(rootDir, "build", `stable-macos-${arch}`);
|
||||||
if (!existsSync(buildDir)) {
|
if (!existsSync(platformDir)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const entry of readdirSync(buildDir)) {
|
for (const child of readdirSync(platformDir)) {
|
||||||
if (!entry.startsWith("stable-macos-")) {
|
if (child.endsWith(".app")) {
|
||||||
continue;
|
return path.join(platformDir, child);
|
||||||
}
|
|
||||||
|
|
||||||
const platformDir = path.join(buildDir, entry);
|
|
||||||
for (const child of readdirSync(platformDir)) {
|
|
||||||
if (child.endsWith(".app")) {
|
|
||||||
return path.join(platformDir, child);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findMacDmgSource(arch) {
|
||||||
|
const prefixedArtifact = walkFiles(artifactDir).find(
|
||||||
|
(filePath) =>
|
||||||
|
filePath.includes(`stable-macos-${arch}`) &&
|
||||||
|
filePath.toLowerCase().endsWith(".dmg"),
|
||||||
|
);
|
||||||
|
if (prefixedArtifact) {
|
||||||
|
return prefixedArtifact;
|
||||||
|
}
|
||||||
|
|
||||||
|
const buildDmg = findByExtension(
|
||||||
|
path.join(rootDir, "build", `stable-macos-${arch}`),
|
||||||
|
".dmg",
|
||||||
|
);
|
||||||
|
if (buildDmg) {
|
||||||
|
return buildDmg;
|
||||||
|
}
|
||||||
|
|
||||||
|
return findByExtension(artifactDir, ".dmg");
|
||||||
|
}
|
||||||
|
|
||||||
function findWindowsSetupExe() {
|
function findWindowsSetupExe() {
|
||||||
const buildDir = path.join(rootDir, "build");
|
const buildDir = path.join(rootDir, "build");
|
||||||
if (!existsSync(buildDir)) {
|
if (!existsSync(buildDir)) {
|
||||||
@ -96,7 +111,9 @@ function findWindowsSetupExe() {
|
|||||||
|
|
||||||
function publishArtifact(sourcePath, arch, ext) {
|
function publishArtifact(sourcePath, arch, ext) {
|
||||||
if (!sourcePath || !existsSync(sourcePath)) {
|
if (!sourcePath || !existsSync(sourcePath)) {
|
||||||
throw new Error(`Missing source artifact for ${arch}.${ext}: ${sourcePath ?? "not found"}`);
|
throw new Error(
|
||||||
|
`Missing source artifact for ${arch}.${ext}: ${sourcePath ?? "not found"}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
mkdirSync(artifactDir, { recursive: true });
|
mkdirSync(artifactDir, { recursive: true });
|
||||||
@ -109,17 +126,24 @@ function publishArtifact(sourcePath, arch, ext) {
|
|||||||
return destination;
|
return destination;
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanArtifactDir(keepNames) {
|
function cleanStagingArtifacts(keepNames) {
|
||||||
if (!existsSync(artifactDir)) {
|
if (!existsSync(artifactDir)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const entry of readdirSync(artifactDir)) {
|
for (const entry of readdirSync(artifactDir)) {
|
||||||
if (keepNames.includes(entry)) {
|
if (keepNames.includes(entry) || entry.startsWith(artifactPrefix)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
rmSync(path.join(artifactDir, entry), { recursive: true, force: true });
|
if (
|
||||||
|
entry.includes("stable-macos-") ||
|
||||||
|
entry.includes("stable-win-") ||
|
||||||
|
entry.endsWith("-update.json") ||
|
||||||
|
entry.endsWith(".tar.gz")
|
||||||
|
) {
|
||||||
|
rmSync(path.join(artifactDir, entry), { recursive: true, force: true });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,23 +220,26 @@ function buildWindowsMsi(setupExePath, arch) {
|
|||||||
return msiPath;
|
return msiPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
const published = [];
|
|
||||||
|
|
||||||
if (process.platform === "darwin") {
|
if (process.platform === "darwin") {
|
||||||
const arch = getReleaseArch();
|
const buildArch = getReleaseArch(
|
||||||
const dmgSource =
|
process.env.ELECTROBUN_ARCH || process.arch,
|
||||||
findByExtension(artifactDir, ".dmg") ?? findByExtension(path.join(rootDir, "build"), ".dmg");
|
);
|
||||||
const appBundle = findMacAppBundle();
|
const dmgSource = findMacDmgSource(buildArch);
|
||||||
|
const appBundle = findMacAppBundle(buildArch);
|
||||||
|
|
||||||
if (!dmgSource) {
|
if (!dmgSource || !appBundle) {
|
||||||
throw new Error("Could not find a macOS DMG artifact to publish");
|
console.log(
|
||||||
}
|
`finalize-desktop-artifacts: no macOS ${buildArch} release artifacts found, skipping`,
|
||||||
if (!appBundle) {
|
);
|
||||||
throw new Error("Could not find a macOS .app bundle to build a PKG");
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
published.push(publishArtifact(dmgSource, arch, "dmg"));
|
const published = [
|
||||||
published.push(buildMacPkg(appBundle, arch));
|
publishArtifact(dmgSource, buildArch, "dmg"),
|
||||||
|
buildMacPkg(appBundle, buildArch),
|
||||||
|
];
|
||||||
|
|
||||||
|
cleanStagingArtifacts(published.map((filePath) => path.basename(filePath)));
|
||||||
} else if (process.platform === "win32") {
|
} else if (process.platform === "win32") {
|
||||||
const arch = "x64";
|
const arch = "x64";
|
||||||
const setupExe = findWindowsSetupExe();
|
const setupExe = findWindowsSetupExe();
|
||||||
@ -221,11 +248,13 @@ if (process.platform === "darwin") {
|
|||||||
throw new Error("Could not find the Windows setup executable to publish");
|
throw new Error("Could not find the Windows setup executable to publish");
|
||||||
}
|
}
|
||||||
|
|
||||||
published.push(publishArtifact(setupExe, arch, "exe"));
|
const published = [
|
||||||
published.push(buildWindowsMsi(setupExe, arch));
|
publishArtifact(setupExe, arch, "exe"),
|
||||||
|
buildWindowsMsi(setupExe, arch),
|
||||||
|
];
|
||||||
|
|
||||||
|
cleanStagingArtifacts(published.map((filePath) => path.basename(filePath)));
|
||||||
} else {
|
} else {
|
||||||
console.log("finalize-desktop-artifacts: skipping unsupported platform", process.platform);
|
console.log("finalize-desktop-artifacts: skipping unsupported platform", process.platform);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanArtifactDir(published.map((filePath) => path.basename(filePath)));
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user