prepare-native-search-tools.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. const plan =
  56. prebuiltPlan ?? resolveNativeSearchPrebuiltPlan({ platform, arch, outputDir, dependenciesDir });
  57. console.log("==> Preparing native search tools from repository archives");
  58. console.log(` platform: ${plan.platformKey}`);
  59. console.log(` target: ${plan.outputDir}`);
  60. // Even a warm binary cache must not hide a missing or modified repository dependency.
  61. // Check the entire target before replacing any prepared binaries.
  62. for (const artifact of plan.artifacts) {
  63. try {
  64. verifyPrebuiltArchiveSha256(artifact.archivePath, artifact.archiveSha256);
  65. } catch (error) {
  66. const reason = error instanceof Error ? error.message : String(error);
  67. throw new Error(
  68. `Invalid local native search archive ${artifact.archivePath}: ${reason}. ` +
  69. "Restore the repository dependency before packaging.",
  70. { cause: error },
  71. );
  72. }
  73. }
  74. for (const artifact of plan.artifacts) {
  75. const validateBinary = (binaryPath) =>
  76. verifyNativeSearchBinaryTarget(binaryPath, {
  77. platform: plan.platform,
  78. arch: plan.arch,
  79. });
  80. if (isNativeSearchBundleCurrent(artifact, plan.platformKey)) {
  81. try {
  82. validateBinary(artifact.binaryPath);
  83. ensureCachedBinaryExecutable(artifact.binaryPath, plan.platform);
  84. console.log(` [skip] ${artifact.toolId} ${artifact.release} 已存在`);
  85. continue;
  86. } catch (error) {
  87. const reason = error instanceof Error ? error.message : String(error);
  88. console.log(` [repair] ${artifact.toolId} 目标校验失败: ${reason}`);
  89. }
  90. }
  91. console.log(` [extract] ${artifact.archivePath}`);
  92. await extractPrebuiltBinary({
  93. archiveExt: artifact.archiveExt,
  94. archivePath: artifact.archivePath,
  95. archiveSha256: artifact.archiveSha256,
  96. binaryName: artifact.binaryName,
  97. binaryPath: artifact.binaryPath,
  98. cwd: repoRoot,
  99. targetPlatform: plan.platform,
  100. validateBinary,
  101. });
  102. writeNativeSearchBundleMeta(artifact, plan.platformKey);
  103. }
  104. await stageNativeSearchNotices(plan);
  105. console.log(`==> Done! native search tools (${plan.platformKey}) -> ${plan.outputDir}`);
  106. return plan;
  107. }
  108. function readOption(name) {
  109. const prefix = `--${name}=`;
  110. const inline = process.argv.find((arg) => arg.startsWith(prefix));
  111. if (inline) return inline.slice(prefix.length);
  112. const index = process.argv.indexOf(`--${name}`);
  113. return index >= 0 ? process.argv[index + 1] : undefined;
  114. }
  115. async function main() {
  116. await prepareNativeSearchTools({
  117. platform: readOption("platform") ?? process.env.ZCODE_TARGET_OS ?? process.platform,
  118. arch: readOption("arch") ?? process.env.ZCODE_TARGET_ARCH ?? process.arch,
  119. outputDir: readOption("output-dir"),
  120. });
  121. }
  122. if (resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) {
  123. try {
  124. await main();
  125. } catch (error) {
  126. console.error(error instanceof Error ? error.stack : error);
  127. process.exitCode = 1;
  128. }
  129. }