farmcontrol-server/scripts/finalize-desktop-artifacts.mjs
Tom Butcher 8b8475d11d
Some checks failed
farmcontrol/farmcontrol-server/pipeline/head There was a failure building this commit
Add Windows installer files to MSI build process
- Updated the farmcontrol-server.wxs file to include SetupArchive and SetupMetadata files for the MSI packaging.
- Modified the build-windows-msi.ps1 script to accept new parameters for SetupArchive and SetupMetadata, enhancing the MSI build process.
- Refactored the finalize-desktop-artifacts script to find and handle multiple Windows installer files, ensuring proper artifact publication.
2026-08-01 20:04:14 +01:00

279 lines
6.6 KiB
JavaScript

import {
cpSync,
existsSync,
mkdirSync,
readdirSync,
readFileSync,
rmSync,
statSync,
} from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import {
getReleaseArch,
getReleaseArtifactName,
getReleaseVersion,
} from "./release-artifact-utils.mjs";
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const packageJson = JSON.parse(
readFileSync(path.join(rootDir, "package.json"), "utf8"),
);
const version = getReleaseVersion(packageJson);
const artifactDir = path.join(rootDir, "app_dist");
const identifier = "com.tombutcher.farmcontrolserver";
const artifactPrefix = `farmcontrol-server-${version}-`;
function walkFiles(dir) {
const files = [];
if (!existsSync(dir)) {
return files;
}
for (const entry of readdirSync(dir)) {
const fullPath = path.join(dir, entry);
const stats = statSync(fullPath);
if (stats.isDirectory()) {
files.push(...walkFiles(fullPath));
} else {
files.push(fullPath);
}
}
return files;
}
function findByExtension(root, extension) {
return walkFiles(root).find((filePath) =>
filePath.toLowerCase().endsWith(extension.toLowerCase()),
);
}
function findMacAppBundle(arch) {
const platformDir = path.join(rootDir, "build", `stable-macos-${arch}`);
if (!existsSync(platformDir)) {
return null;
}
for (const child of readdirSync(platformDir)) {
if (child.endsWith(".app")) {
return path.join(platformDir, child);
}
}
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 findWindowsInstallerFiles() {
const buildDir = path.join(rootDir, "build");
if (!existsSync(buildDir)) {
return null;
}
for (const entry of readdirSync(buildDir)) {
if (!entry.startsWith("stable-win-")) {
continue;
}
const platformDir = path.join(buildDir, entry);
const files = walkFiles(platformDir);
const setupExe = files.find((filePath) =>
/-setup\.exe$/i.test(filePath),
);
const setupArchive = files.find((filePath) =>
/-setup\.tar\.zst$/i.test(filePath),
);
const setupMetadata = files.find((filePath) =>
/-setup\.metadata\.json$/i.test(filePath),
);
const setupZip = files.find((filePath) =>
/-setup\.zip$/i.test(filePath),
);
if (setupExe && setupArchive && setupMetadata) {
return { setupExe, setupArchive, setupMetadata, setupZip };
}
}
return null;
}
function publishArtifact(sourcePath, arch, ext) {
if (!sourcePath || !existsSync(sourcePath)) {
throw new Error(
`Missing source artifact for ${arch}.${ext}: ${sourcePath ?? "not found"}`,
);
}
mkdirSync(artifactDir, { recursive: true });
const destination = path.join(
artifactDir,
getReleaseArtifactName(version, arch, ext),
);
cpSync(sourcePath, destination);
console.log(`Published ${destination}`);
return destination;
}
function cleanStagingArtifacts(keepNames) {
if (!existsSync(artifactDir)) {
return;
}
for (const entry of readdirSync(artifactDir)) {
if (keepNames.includes(entry) || entry.startsWith(artifactPrefix)) {
continue;
}
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 });
}
}
}
function buildMacPkg(appBundlePath, arch) {
const pkgPath = path.join(
artifactDir,
getReleaseArtifactName(version, arch, "pkg"),
);
const result = spawnSync(
"pkgbuild",
[
"--component",
appBundlePath,
"--install-location",
"/Applications",
"--identifier",
identifier,
"--version",
version,
pkgPath,
],
{ stdio: "inherit" },
);
if (result.status !== 0) {
throw new Error(`pkgbuild failed with exit code ${result.status ?? 1}`);
}
console.log(`Published ${pkgPath}`);
return pkgPath;
}
function buildWindowsMsi(installerFiles, arch) {
const scriptPath = path.join(rootDir, "scripts/build-windows-msi.ps1");
const msiPath = path.join(
artifactDir,
getReleaseArtifactName(version, arch, "msi"),
);
const powershell =
process.env.SystemRoot
? path.join(
process.env.SystemRoot,
"System32",
"WindowsPowerShell",
"v1.0",
"powershell.exe",
)
: "powershell.exe";
const result = spawnSync(
powershell,
[
"-NoProfile",
"-ExecutionPolicy",
"Bypass",
"-File",
scriptPath,
"-SetupExe",
installerFiles.setupExe,
"-SetupArchive",
installerFiles.setupArchive,
"-SetupMetadata",
installerFiles.setupMetadata,
"-OutputMsi",
msiPath,
"-Version",
version,
],
{ stdio: "inherit" },
);
if (result.status !== 0) {
throw new Error(`build-windows-msi.ps1 failed with exit code ${result.status ?? 1}`);
}
console.log(`Published ${msiPath}`);
return msiPath;
}
if (process.platform === "darwin") {
const buildArch = getReleaseArch(
process.env.ELECTROBUN_ARCH || process.arch,
);
const dmgSource = findMacDmgSource(buildArch);
const appBundle = findMacAppBundle(buildArch);
if (!dmgSource || !appBundle) {
console.log(
`finalize-desktop-artifacts: no macOS ${buildArch} release artifacts found, skipping`,
);
process.exit(0);
}
const published = [
publishArtifact(dmgSource, buildArch, "dmg"),
buildMacPkg(appBundle, buildArch),
];
cleanStagingArtifacts(published.map((filePath) => path.basename(filePath)));
} else if (process.platform === "win32") {
const arch = "x64";
const installerFiles = findWindowsInstallerFiles();
if (!installerFiles) {
throw new Error(
"Could not find the Windows installer files (setup exe, archive, and metadata)",
);
}
const published = [buildWindowsMsi(installerFiles, arch)];
if (installerFiles.setupZip) {
published.push(publishArtifact(installerFiles.setupZip, arch, "zip"));
}
cleanStagingArtifacts(published.map((filePath) => path.basename(filePath)));
} else {
console.log("finalize-desktop-artifacts: skipping unsupported platform", process.platform);
process.exit(0);
}