Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

build(scripts): add build-desktop-binaries script

Richie McIlroye97f48a126a9
diff --git a/scripts/build-desktop-binaries.mjs b/scripts/build-desktop-binaries.mjs
new file mode 100644
index 000000000..66af8651a
--- /dev/null
+++ b/scripts/build-desktop-binaries.mjs
@@ -0,0 +1,155 @@
+import { spawnSync } from "node:child_process";
+import * as fs from "node:fs/promises";
+import * as path from "node:path";
+import { fileURLToPath } from "node:url";
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+const repoRoot = path.resolve(__dirname, "..");
+const binariesDir = path.join(
+ repoRoot,
+ "apps",
+ "desktop",
+ "src-tauri",
+ "binaries",
+);
+
+function detectHostTriple() {
+ const result = spawnSync("rustc", ["-vV"], { encoding: "utf8" });
+ if (result.status !== 0) {
+ const reason =
+ result.stderr || result.error?.message || `exit ${result.status}`;
+ throw new Error(`rustc -vV failed: ${reason}`);
+ }
+ const match = result.stdout.match(/^host:\s*(.+)$/m);
+ if (!match) throw new Error("Could not parse host triple from rustc -vV");
+ return match[1].trim();
+}
+
+async function fileExists(p) {
+ return await fs
+ .access(p)
+ .then(() => true)
+ .catch(() => false);
+}
+
+async function newestMtimeMs(targetPath) {
+ let max = 0;
+ const stack = [targetPath];
+ while (stack.length > 0) {
+ const current = stack.pop();
+ const stat = await fs.stat(current).catch(() => null);
+ if (!stat) continue;
+ if (stat.isFile()) {
+ if (stat.mtimeMs > max) max = stat.mtimeMs;
+ continue;
+ }
+ if (stat.isDirectory()) {
+ const entries = await fs.readdir(current);
+ for (const name of entries) stack.push(path.join(current, name));
+ }
+ }
+ return max;
+}
+
+async function isUpToDate(destPath, sourcePaths) {
+ if (!(await fileExists(destPath))) return false;
+ const destStat = await fs.stat(destPath);
+ for (const src of sourcePaths) {
+ const newest = await newestMtimeMs(src);
+ if (newest > destStat.mtimeMs) return false;
+ }
+ return true;
+}
+
+async function main() {
+ const target =
+ process.argv[2] || process.env.RUST_TARGET_TRIPLE || detectHostTriple();
+ const ext = target.includes("windows") ? ".exe" : "";
+
+ await fs.mkdir(binariesDir, { recursive: true });
+
+ for (const sidecar of [
+ {
+ packageName: "cap-muxer",
+ sourceBinary: "cap-muxer",
+ destBinaries: ["cap-muxer"],
+ watchPaths: [
+ path.join(repoRoot, "crates", "cap-muxer"),
+ path.join(repoRoot, "crates", "cap-muxer-protocol"),
+ path.join(repoRoot, "Cargo.lock"),
+ ],
+ },
+ {
+ packageName: "cap",
+ sourceBinary: "cap",
+ destBinaries: ["cap-cli", "cap-exporter"],
+ watchPaths: [
+ path.join(repoRoot, "apps", "cli"),
+ path.join(repoRoot, "crates", "cli-install"),
+ path.join(repoRoot, "crates", "editor"),
+ path.join(repoRoot, "crates", "enc-ffmpeg"),
+ path.join(repoRoot, "crates", "export"),
+ path.join(repoRoot, "crates", "media"),
+ path.join(repoRoot, "crates", "media-info"),
+ path.join(repoRoot, "crates", "project"),
+ path.join(repoRoot, "crates", "rendering"),
+ path.join(repoRoot, "Cargo.lock"),
+ ],
+ },
+ ]) {
+ await buildSidecar(sidecar, target, ext);
+ }
+}
+
+async function buildSidecar(sidecar, target, ext) {
+ const src = path.join(
+ repoRoot,
+ "target",
+ target,
+ "release",
+ `${sidecar.sourceBinary}${ext}`,
+ );
+ const dests = sidecar.destBinaries.map((destBinary) =>
+ path.join(binariesDir, `${destBinary}-${target}${ext}`),
+ );
+
+ if (
+ (
+ await Promise.all(
+ dests.map((dest) => isUpToDate(dest, sidecar.watchPaths)),
+ )
+ ).every(Boolean)
+ ) {
+ console.log(
+ `${sidecar.destBinaries.join(", ")} desktop binaries up to date`,
+ );
+ return;
+ }
+
+ console.log(
+ `Building ${sidecar.destBinaries.join(", ")} desktop binaries for ${target}...`,
+ );
+ const cargo = spawnSync(
+ "cargo",
+ ["build", "--release", "-p", sidecar.packageName, "--target", target],
+ { stdio: "inherit", cwd: repoRoot },
+ );
+ if (cargo.status !== 0) {
+ process.exit(cargo.status ?? 1);
+ }
+
+ if (!(await fileExists(src))) {
+ console.error(`error: built binary not found at ${src}`);
+ process.exit(1);
+ }
+
+ for (const dest of dests) {
+ await fs.copyFile(src, dest);
+ console.log(`Copied ${src} -> ${dest}`);
+ }
+}
+
+main().catch((err) => {
+ console.error(err);
+ process.exit(1);
+});
diff --git a/scripts/build-desktop-binaries.sh b/scripts/build-desktop-binaries.sh
new file mode 100755
index 000000000..ee08300f9
--- /dev/null
+++ b/scripts/build-desktop-binaries.sh
@@ -0,0 +1,43 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+TARGET="${1:-}"
+if [[ -z "$TARGET" ]]; then
+ TARGET="$(rustc -vV | sed -n 's|host: ||p')"
+fi
+
+REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
+BINARIES_DIR="$REPO_ROOT/apps/desktop/src-tauri/binaries"
+
+EXT=""
+case "$TARGET" in
+ *windows*) EXT=".exe" ;;
+esac
+
+mkdir -p "$BINARIES_DIR"
+
+build_sidecar() {
+ local package_name="$1"
+ local source_binary="$2"
+ shift 2
+ local dest_binaries=("$@")
+
+ echo "Building ${dest_binaries[*]} for $TARGET..."
+ cargo build --release -p "$package_name" --target "$TARGET"
+
+ local src="$REPO_ROOT/target/$TARGET/release/$source_binary$EXT"
+
+ if [[ ! -f "$src" ]]; then
+ echo "error: built binary not found at $src" >&2
+ exit 1
+ fi
+
+ for dest_binary in "${dest_binaries[@]}"; do
+ local dest="$BINARIES_DIR/$dest_binary-$TARGET$EXT"
+ cp "$src" "$dest"
+ echo "Copied $(realpath "$src" 2>/dev/null || echo "$src") -> $dest"
+ done
+}
+
+build_sidecar "cap-muxer" "cap-muxer" "cap-muxer"
+build_sidecar "cap" "cap" "cap-cli" "cap-exporter"