prepare-native-search-tools.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env node
  2. import { chmodSync, statSync } from "node:fs";
  3. import { dirname, resolve } from "node:path";
  4. import process from "node:process";
  5. import { fileURLToPath } from "node:url";
  6. import {
  7. isNativeSearchBundleCurrent,
  8. writeNativeSearchBundleMeta,
  9. } from "./native-search-tools-bundle-meta.mjs";
  10. import { downloadNativeSearchTools } from "./download-native-search-tools.mjs";
  11. import {
  12. resolveNativeSearchArtifactSource,
  13. resolveNativeSearchPrebuiltPlan,
  14. } from "./native-search-tools-config.mjs";
  15. import { verifyNativeSearchBinaryTarget } from "./native-search-tools-verify.mjs";
  16. import { extractPrebuiltBinary, verifyPrebuiltArchiveSha256 } from "./prebuilt-binary-extract.mjs";
  17. import { stageNativeSearchNotices } from "./third-party-notices.mjs";
  18. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  19. function ensureCachedBinaryExecutable(binaryPath, targetPlatform) {
  20. if (process.platform === "win32" || targetPlatform === "win32") return;
  21. if ((statSync(binaryPath).mode & 0o111) !== 0) return;
  22. // 缓存元数据只校验内容,不能让意外丢失的 Unix 可执行权限随 skip 流程继续传递。
  23. chmodSync(binaryPath, 0o755);
  24. }
  25. /**
  26. * native search 预编译工具的唯一准备入口(desktop prepare:native-search、SEA、server-cli、remote 共用)。
  27. *
  28. * 来源由 resolveNativeSearchArtifactSource 判定:内网依赖源已配置时原样走既有
  29. * downloadNativeSearchTools(同一 release plan、SHA-256 校验与 bundle meta 缓存语义);
  30. * 未配置时才解包仓库内置归档。两条路径都在返回前刷新工具目录旁的许可声明。
  31. */
  32. export async function prepareNativeSearchTools({
  33. platform = process.env.ZCODE_TARGET_OS || process.platform,
  34. arch = process.env.ZCODE_TARGET_ARCH || process.arch,
  35. outputDir,
  36. dependenciesDir,
  37. env = process.env,
  38. source = resolveNativeSearchArtifactSource(env),
  39. prebuiltPlan,
  40. } = {}) {
  41. if (source === "intranet") {
  42. const downloadedPlan = await downloadNativeSearchTools({
  43. platform,
  44. arch,
  45. outputDir,
  46. env,
  47. prebuiltPlan,
  48. });
  49. await stageNativeSearchNotices(downloadedPlan, repoRoot, { origin: "intranet-mirror" });
  50. return downloadedPlan;
  51. }
  52. if (source !== "repository") {
  53. throw new Error(`Unsupported native search artifact source: ${String(source)}`);
  54. }
  55. // 来源已判定为 repository,生成 plan 时必须固定同一来源:否则默认内网地址可解析的环境会
  56. // 重新推断为 intranet,返回的 plan 带上 downloadUrl,与实际字节来源不一致。
  57. const plan =
  58. prebuiltPlan ??
  59. resolveNativeSearchPrebuiltPlan({
  60. platform,
  61. arch,
  62. outputDir,
  63. dependenciesDir,
  64. env: { ...env, NATIVE_SEARCH_TOOLS_ARTIFACT_SOURCE: "repository" },
  65. });
  66. console.log("==> Preparing native search tools from repository archives");
  67. console.log(` platform: ${plan.platformKey}`);
  68. console.log(` target: ${plan.outputDir}`);
  69. // Even a warm binary cache must not hide a missing or modified repository dependency.
  70. // Check the entire target before replacing any prepared binaries.
  71. for (const artifact of plan.artifacts) {
  72. try {
  73. verifyPrebuiltArchiveSha256(artifact.archivePath, artifact.archiveSha256);
  74. } catch (error) {
  75. const reason = error instanceof Error ? error.message : String(error);
  76. throw new Error(
  77. `Invalid local native search archive ${artifact.archivePath}: ${reason}. ` +
  78. "Restore the repository dependency before packaging.",
  79. { cause: error },
  80. );
  81. }
  82. }
  83. for (const artifact of plan.artifacts) {
  84. const validateBinary = (binaryPath) =>
  85. verifyNativeSearchBinaryTarget(binaryPath, {
  86. platform: plan.platform,
  87. arch: plan.arch,
  88. });
  89. if (isNativeSearchBundleCurrent(artifact, plan.platformKey)) {
  90. try {
  91. validateBinary(artifact.binaryPath);
  92. ensureCachedBinaryExecutable(artifact.binaryPath, plan.platform);
  93. console.log(` [skip] ${artifact.toolId} ${artifact.release} 已存在`);
  94. continue;
  95. } catch (error) {
  96. const reason = error instanceof Error ? error.message : String(error);
  97. console.log(` [repair] ${artifact.toolId} 目标校验失败: ${reason}`);
  98. }
  99. }
  100. console.log(` [extract] ${artifact.archivePath}`);
  101. await extractPrebuiltBinary({
  102. archiveExt: artifact.archiveExt,
  103. archivePath: artifact.archivePath,
  104. archiveSha256: artifact.archiveSha256,
  105. binaryName: artifact.binaryName,
  106. binaryPath: artifact.binaryPath,
  107. cwd: repoRoot,
  108. targetPlatform: plan.platform,
  109. validateBinary,
  110. });
  111. writeNativeSearchBundleMeta(artifact, plan.platformKey);
  112. }
  113. await stageNativeSearchNotices(plan);
  114. console.log(`==> Done! native search tools (${plan.platformKey}) -> ${plan.outputDir}`);
  115. return plan;
  116. }
  117. function readOption(name) {
  118. const prefix = `--${name}=`;
  119. const inline = process.argv.find((arg) => arg.startsWith(prefix));
  120. if (inline) return inline.slice(prefix.length);
  121. const index = process.argv.indexOf(`--${name}`);
  122. return index >= 0 ? process.argv[index + 1] : undefined;
  123. }
  124. async function main() {
  125. await prepareNativeSearchTools({
  126. platform: readOption("platform") ?? process.env.ZCODE_TARGET_OS ?? process.platform,
  127. arch: readOption("arch") ?? process.env.ZCODE_TARGET_ARCH ?? process.arch,
  128. outputDir: readOption("output-dir"),
  129. });
  130. }
  131. if (resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) {
  132. try {
  133. await main();
  134. } catch (error) {
  135. console.error(error instanceof Error ? error.stack : error);
  136. process.exitCode = 1;
  137. }
  138. }