Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

5.7 KB
Raw Blame
/**
 * `tauri dev|build` for the local edition (Clipy Local).
 *
 *     node scripts/tauri-local.mjs dev   [extra tauri args]
 *     node scripts/tauri-local.mjs build [extra tauri args]
 *
 * Runs Tauri with this repository's config (plus the Windows overlay on
 * Windows) and the default Cargo features:
 *
 *     tauri <dev|build> [--config src-tauri/tauri.local.windows.conf.json]
 *
 * On Windows it first stages sherpa-onnx (scripts/stage-sherpa-onnx.mjs): the
 * archive is checked against its pinned SHA-256, the two DLLs are staged for
 * the bundle (the Windows overlay lists them as resources; macOS has none,
 * which is why that overlay is only passed on Windows), and the build gets
 * SHERPA_ONNX_ARCHIVE_DIR so `sherpa-onnx-sys` never downloads an unchecked
 * copy itself.
 */

import { spawnSync } from "node:child_process";
import { chmodSync, existsSync, lstatSync, mkdirSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
import path, { dirname, join, resolve } from "node:path";
import { argv, env, exit, platform } from "node:process";
import { fileURLToPath } from "node:url";

import { stageSherpaOnnx } from "./stage-sherpa-onnx.mjs";

const scriptsDir = dirname(fileURLToPath(import.meta.url));
const tauriTargetDir = resolve(scriptsDir, "..", "src-tauri", "target");

/**
 * macOS with only Xcode Command Line Tools (no full Xcode.app): `cidre`'s build
 * script invokes `xcodebuild` for `pomace.xcodeproj`, which fails under CLT.
 * Put a wrapper around `scripts/xcodebuild-cidre-shims.sh` first on PATH when
 * `/usr/bin/xcodebuild -version` fails.
 */
function ensureMacosXcodebuildShim(childEnv) {
  if (platform !== "darwin") return;
  const probe = spawnSync("/usr/bin/xcodebuild", ["-version"], { stdio: "ignore" });
  if (probe.status === 0) return;

  const shimDir = join(tauriTargetDir, "xcodebuild-shim");
  const shimBin = join(shimDir, "xcodebuild");
  const shimScript = join(scriptsDir, "xcodebuild-cidre-shims.sh");
  mkdirSync(shimDir, { recursive: true });
  writeFileSync(shimBin, `#!/bin/bash\nexec /bin/bash "${shimScript}" "$@"\n`, "utf8");
  chmodSync(shimBin, 0o755);
  childEnv.PATH = `${shimDir}:${childEnv.PATH ?? ""}`;
  console.log("[tauri-local] Xcode.app not found; using Command Line Tools cidre shim for xcodebuild");
}

/**
 * macOS `tauri dev` runs `target/debug/clipy-desktop` outside an `.app` bundle,
 * so `@executable_path/../Frameworks/Spacedrive.framework` resolves to
 * `target/Frameworks/Spacedrive.framework`. Symlink it to the unpacked native
 * FFmpeg framework (SIP strips `DYLD_FALLBACK_LIBRARY_PATH` across shells).
 */
function ensureMacosFfmpegFrameworkLink() {
  if (platform !== "darwin") return;
  const frameworkSrc = join(tauriTargetDir, "native-deps", "Spacedrive.framework");
  if (!existsSync(frameworkSrc)) return;
  const frameworksDir = join(tauriTargetDir, "Frameworks");
  const frameworkLink = join(frameworksDir, "Spacedrive.framework");
  mkdirSync(frameworksDir, { recursive: true });
  try {
    if (lstatSync(frameworkLink).isSymbolicLink()) unlinkSync(frameworkLink);
    else return;
  } catch {
    // Link does not exist yet.
  }
  symlinkSync(join("..", "native-deps", "Spacedrive.framework"), frameworkLink);
}

/**
 * The app icons are generated from `src-tauri/app-icon.svg`, not committed.
 * Generate them on the first run, so `pnpm tauri:dev` and `pnpm tauri:build`
 * work without running `pnpm tauri:icon` first.
 */
function ensureIcons(childEnv) {
  const icons = resolve(scriptsDir, "..", "src-tauri", "icons");
  const needed = ["32x32.png", "128x128.png", "[email protected]", "icon.icns", "icon.ico"];
  if (needed.every((name) => existsSync(join(icons, name)))) return;
  console.log("[tauri-local] generating the app icons from src-tauri/app-icon.svg");
  const generated = spawnSync("tauri", ["icon", "src-tauri/app-icon.svg"], {
    stdio: "inherit",
    env: childEnv,
    shell: platform === "win32",
  });
  if (generated.status !== 0) {
    console.error("[tauri-local] could not generate the app icons: run `pnpm tauri:icon` and try again");
    exit(1);
  }
}

const [command, ...extra] = argv.slice(2);
if (command !== "dev" && command !== "build") {
  console.error("usage: node scripts/tauri-local.mjs <dev|build> [tauri args]");
  exit(2);
}

// The default features build the local edition in this repository.
const CARGO_ARGS = [];

// tauri.conf.json is the local edition's config in this repository.
const configs = [];
const childEnv = { ...env };
ensureMacosXcodebuildShim(childEnv);
ensureIcons(childEnv);
ensureMacosFfmpegFrameworkLink();
if (platform === "win32") {
  try {
    const staged = await stageSherpaOnnx();
    childEnv.SHERPA_ONNX_ARCHIVE_DIR = staged.archiveDir;
  } catch (error) {
    console.error(`[tauri-local] ${error.message}`);
    exit(1);
  }

  // Tauri copies bundle resources beside the installed executable, but
  // `tauri dev` runs target/debug/clipy-desktop.exe directly. Make the FFmpeg
  // DLLs discoverable there without requiring contributors to mutate PATH by
  // hand. This also guarantees that bindgen, the linker and the dev app use
  // the same shared build named by FFMPEG_DIR.
  const ffmpegDir = childEnv.FFMPEG_DIR;
  if (ffmpegDir) {
    const pathKey = Object.keys(childEnv).find((key) => key.toLowerCase() === "path") ?? "PATH";
    childEnv[pathKey] = [path.join(ffmpegDir, "bin"), childEnv[pathKey]]
      .filter(Boolean)
      .join(path.delimiter);
  }

  configs.push("--config", "src-tauri/tauri.local.windows.conf.json");
}

const result = spawnSync("tauri", [command, ...configs, ...extra, "--", ...CARGO_ARGS], {
  stdio: "inherit",
  env: childEnv,
  // `tauri` is a .cmd shim on Windows.
  shell: platform === "win32",
});
if (result.error) {
  console.error(`[tauri-local] could not run tauri: ${result.error.message}`);
  exit(1);
}
exit(result.status ?? 1);