ensure-local-runtime-assets.mjs 5.9 KB

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