ensure-local-runtime-assets.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env node
  2. import { existsSync, rmSync } from "node:fs";
  3. import { join, resolve } from "node:path";
  4. import process from "node:process";
  5. import {
  6. resolveNativeSearchBuildPlan,
  7. resolveNativeSearchReleasePlan,
  8. } from "../../../scripts/native-search-tools-config.mjs";
  9. import { verifyBuiltNativeSearchTools } from "../../../scripts/native-search-tools-verify.mjs";
  10. import { runCommand } from "../../../scripts/spawn-command.mjs";
  11. import { getTargetPlatform } from "./target-platform.mjs";
  12. const desktopRoot = resolve(import.meta.dirname, "..");
  13. const target = getTargetPlatform();
  14. const bundledToolsRoot = join(desktopRoot, "bundled-tools", target.key);
  15. const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  16. const nativeSearchReleasePlan = resolveNativeSearchReleasePlan({
  17. platform: target.os,
  18. arch: target.arch,
  19. });
  20. const nativeSearchBuildPlan = nativeSearchReleasePlan.enabled
  21. ? resolveNativeSearchBuildPlan({
  22. platform: target.os,
  23. arch: target.arch,
  24. outputDir: bundledToolsRoot,
  25. })
  26. : undefined;
  27. // Windows Chrome 导入入口未启用,默认 dev 启动不应把可选 helper 当成本地必需资源。
  28. // 显式 opt-in 时继续沿用原有按需构建,避免删除未来恢复所需代码。
  29. const shouldRequireWindowsBrowserImportHelper =
  30. target.os === "win32" && process.env.ZCODE_ENABLE_WINDOWS_BROWSER_IMPORT === "1";
  31. // CUA 权限浮窗靠 zcode-window-bounds 读系统设置窗口 bounds 才能吸附。该 Swift 产物被
  32. // .gitignore 排除(仓库卫生门禁禁产物入库),生产链 prepare:runtime-assets 会在 darwin 上编它,
  33. // dev 链也必须 ensure —— 新 checkout、换 worktree 或清过 resources 后二进制缺失,watcher spawn
  34. // ENOENT 后 fail-open:浮窗照常显示、只是不再跟随系统设置窗口,且全程无报错,问题只能靠翻日志发现。
  35. // 缺 Xcode CLT 时 build 脚本自身 warn 后 exit 0,这里仍保持 not ready,至多每次 dev 多跑一次秒级脚本。
  36. const shouldRequireMacosWindowBounds = target.os === "darwin";
  37. function isNativeSearchReady() {
  38. if (!nativeSearchBuildPlan) return true;
  39. const requiredPaths = nativeSearchReleasePlan.runtimeToolIds.map(
  40. (toolId) => nativeSearchBuildPlan.binaries[toolId],
  41. );
  42. if (requiredPaths.some((binaryPath) => !binaryPath || !existsSync(binaryPath))) {
  43. return false;
  44. }
  45. try {
  46. verifyBuiltNativeSearchTools({
  47. bfsPath: nativeSearchBuildPlan.bfsPath,
  48. rgPath: nativeSearchBuildPlan.rgPath,
  49. ugrepPath: nativeSearchBuildPlan.ugrepPath,
  50. platform: nativeSearchBuildPlan.platform,
  51. arch: nativeSearchBuildPlan.arch,
  52. });
  53. return true;
  54. } catch (error) {
  55. // native sidecar 不进 Git,切分支后可能留下版本、架构或 ABI 已过期的文件。
  56. // 复用正式构建 verifier 判定 ready,避免仅凭路径存在继续运行旧产物。
  57. console.warn(
  58. `[ensure-local-runtime-assets] embedded search validation failed: ${error instanceof Error ? error.message : String(error)}`,
  59. );
  60. // ABI 或功能合同校验失败时清除生成物,确保下一次 prepare 从仓库归档重新解压。
  61. for (const binaryPath of requiredPaths) {
  62. rmSync(binaryPath, { force: true });
  63. }
  64. return false;
  65. }
  66. }
  67. const REQUIRED_LOCAL_RUNTIME_ASSETS = [
  68. ...(nativeSearchReleasePlan.enabled
  69. ? [
  70. {
  71. label: "embedded search",
  72. script: "prepare:native-search",
  73. isReady: isNativeSearchReady,
  74. },
  75. ]
  76. : []),
  77. ...(shouldRequireWindowsBrowserImportHelper
  78. ? [
  79. {
  80. label: "Windows browser import helper",
  81. script: "prepare:browser-import-helper",
  82. isReady: () =>
  83. existsSync(join(bundledToolsRoot, "browser-import", "zcode-browser-import-helper.exe")),
  84. },
  85. ]
  86. : []),
  87. ...(shouldRequireMacosWindowBounds
  88. ? [
  89. {
  90. label: "macOS window bounds helper",
  91. script: "prepare:macos-window-bounds",
  92. isReady: () =>
  93. existsSync(
  94. join(desktopRoot, "resources", "macos-window-bounds", "zcode-window-bounds"),
  95. ),
  96. },
  97. ]
  98. : []),
  99. ];
  100. const missingAssets = [];
  101. for (const asset of REQUIRED_LOCAL_RUNTIME_ASSETS) {
  102. if (!(await asset.isReady())) {
  103. missingAssets.push(asset);
  104. }
  105. }
  106. if (missingAssets.length === 0) {
  107. console.log(`[ensure-local-runtime-assets] all local runtime assets are ready for ${target.key}`);
  108. process.exit(0);
  109. }
  110. // 开发态需要在 Electron 启动前发现缺失的本地 sidecar/helper,而不是等到真正使用时才报错。
  111. // 这里在 Electron 启动前只自检当前开发路径需要的 embedded search sidecar 和平台 helper。
  112. // Agent bundle 由后续 build-desktop-agent-cli 构建,开发态 resolver 优先使用 workspace dist,
  113. // 不在此自检范围。
  114. for (const asset of missingAssets) {
  115. console.log(
  116. `[ensure-local-runtime-assets] preparing ${asset.label} because local runtime asset is missing or incomplete`,
  117. );
  118. runCommand(pnpmCommand, [asset.script], {
  119. cwd: desktopRoot,
  120. env: process.env,
  121. });
  122. }