build-macos-window-bounds.mjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env node
  2. // 编译 macOS 窗口 bounds 辅助程序(CUA 权限浮窗的吸附数据源)。
  3. //
  4. // 非 darwin 直接跳过:这个二进制只服务 macOS 的 TCC 授权引导,其他平台没有对应流程。
  5. // 缺少 swiftc(未装 Xcode CLT)时也只警告不失败 —— 吸附是观感增强,拿不到 bounds 时浮窗
  6. // 会 fail-open 到屏幕底部照样可用,不该因此让整个 desktop 构建挂掉。
  7. import { execFileSync } from "node:child_process";
  8. import { existsSync, mkdirSync } from "node:fs";
  9. import { dirname, join, resolve } from "node:path";
  10. import { fileURLToPath } from "node:url";
  11. const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  12. const sourcePath = join(packageRoot, "native", "macos-window-bounds", "main.swift");
  13. const outputDir = join(packageRoot, "resources", "macos-window-bounds");
  14. const outputPath = join(outputDir, "zcode-window-bounds");
  15. if (process.platform !== "darwin") {
  16. console.log("[window-bounds] 跳过:仅 macOS 需要");
  17. process.exit(0);
  18. }
  19. if (!existsSync(sourcePath)) {
  20. console.error(`[window-bounds] 源文件缺失:${sourcePath}`);
  21. process.exit(1);
  22. }
  23. function hasSwiftc() {
  24. try {
  25. execFileSync("xcrun", ["--find", "swiftc"], { stdio: "ignore" });
  26. return true;
  27. } catch {
  28. return false;
  29. }
  30. }
  31. if (!hasSwiftc()) {
  32. console.warn("[window-bounds] 未找到 swiftc(需 Xcode Command Line Tools);跳过构建。");
  33. console.warn("[window-bounds] 权限浮窗仍可用,但不会吸附到系统设置窗口。");
  34. process.exit(0);
  35. }
  36. mkdirSync(outputDir, { recursive: true });
  37. try {
  38. // 同时产出 arm64 与 x86_64 的 universal 二进制,避免发布包在另一架构上无法执行。
  39. execFileSync(
  40. "xcrun",
  41. ["swiftc", "-O", "-target", "arm64-apple-macos11", sourcePath, "-o", `${outputPath}-arm64`],
  42. { stdio: "inherit" },
  43. );
  44. execFileSync(
  45. "xcrun",
  46. ["swiftc", "-O", "-target", "x86_64-apple-macos11", sourcePath, "-o", `${outputPath}-x86_64`],
  47. { stdio: "inherit" },
  48. );
  49. execFileSync(
  50. "lipo",
  51. ["-create", `${outputPath}-arm64`, `${outputPath}-x86_64`, "-output", outputPath],
  52. { stdio: "inherit" },
  53. );
  54. execFileSync("rm", ["-f", `${outputPath}-arm64`, `${outputPath}-x86_64`]);
  55. console.log(`[window-bounds] 已构建 universal 二进制:${outputPath}`);
  56. } catch (error) {
  57. console.warn(
  58. "[window-bounds] 构建失败;权限浮窗仍可用但不会吸附:",
  59. error instanceof Error ? error.message : String(error),
  60. );
  61. process.exit(0);
  62. }