prepare-native-search-tools.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 { resolveNativeSearchPrebuiltPlan } from "./native-search-tools-config.mjs";
  11. import { verifyNativeSearchBinaryTarget } from "./native-search-tools-verify.mjs";
  12. import { extractPrebuiltBinary, verifyPrebuiltArchiveSha256 } from "./prebuilt-binary-extract.mjs";
  13. import { stageNativeSearchNotices } from "./third-party-notices.mjs";
  14. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  15. function ensureCachedBinaryExecutable(binaryPath, targetPlatform) {
  16. if (process.platform === "win32" || targetPlatform === "win32") return;
  17. if ((statSync(binaryPath).mode & 0o111) !== 0) return;
  18. // 缓存元数据只校验内容,不能让意外丢失的 Unix 可执行权限随 skip 流程继续传递。
  19. chmodSync(binaryPath, 0o755);
  20. }
  21. export async function prepareNativeSearchTools({
  22. platform = process.env.ZCODE_TARGET_OS || process.platform,
  23. arch = process.env.ZCODE_TARGET_ARCH || process.arch,
  24. outputDir,
  25. dependenciesDir,
  26. prebuiltPlan,
  27. } = {}) {
  28. const plan =
  29. prebuiltPlan ?? resolveNativeSearchPrebuiltPlan({ platform, arch, outputDir, dependenciesDir });
  30. console.log("==> Preparing native search tools from repository archives");
  31. console.log(` platform: ${plan.platformKey}`);
  32. console.log(` target: ${plan.outputDir}`);
  33. // Even a warm binary cache must not hide a missing or modified repository dependency.
  34. // Check the entire target before replacing any prepared binaries.
  35. for (const artifact of plan.artifacts) {
  36. try {
  37. verifyPrebuiltArchiveSha256(artifact.archivePath, artifact.archiveSha256);
  38. } catch (error) {
  39. const reason = error instanceof Error ? error.message : String(error);
  40. throw new Error(
  41. `Invalid local native search archive ${artifact.archivePath}: ${reason}. ` +
  42. "Restore the repository dependency before packaging.",
  43. { cause: error },
  44. );
  45. }
  46. }
  47. for (const artifact of plan.artifacts) {
  48. const validateBinary = (binaryPath) =>
  49. verifyNativeSearchBinaryTarget(binaryPath, {
  50. platform: plan.platform,
  51. arch: plan.arch,
  52. });
  53. if (isNativeSearchBundleCurrent(artifact, plan.platformKey)) {
  54. try {
  55. validateBinary(artifact.binaryPath);
  56. ensureCachedBinaryExecutable(artifact.binaryPath, plan.platform);
  57. console.log(` [skip] ${artifact.toolId} ${artifact.release} 已存在`);
  58. continue;
  59. } catch (error) {
  60. const reason = error instanceof Error ? error.message : String(error);
  61. console.log(` [repair] ${artifact.toolId} 目标校验失败: ${reason}`);
  62. }
  63. }
  64. console.log(` [extract] ${artifact.archivePath}`);
  65. await extractPrebuiltBinary({
  66. archiveExt: artifact.archiveExt,
  67. archivePath: artifact.archivePath,
  68. archiveSha256: artifact.archiveSha256,
  69. binaryName: artifact.binaryName,
  70. binaryPath: artifact.binaryPath,
  71. cwd: repoRoot,
  72. targetPlatform: plan.platform,
  73. validateBinary,
  74. });
  75. writeNativeSearchBundleMeta(artifact, plan.platformKey);
  76. }
  77. await stageNativeSearchNotices(plan);
  78. console.log(`==> Done! native search tools (${plan.platformKey}) -> ${plan.outputDir}`);
  79. return plan;
  80. }
  81. function readOption(name) {
  82. const prefix = `--${name}=`;
  83. const inline = process.argv.find((arg) => arg.startsWith(prefix));
  84. if (inline) return inline.slice(prefix.length);
  85. const index = process.argv.indexOf(`--${name}`);
  86. return index >= 0 ? process.argv[index + 1] : undefined;
  87. }
  88. async function main() {
  89. await prepareNativeSearchTools({
  90. platform: readOption("platform") ?? process.env.ZCODE_TARGET_OS ?? process.platform,
  91. arch: readOption("arch") ?? process.env.ZCODE_TARGET_ARCH ?? process.arch,
  92. outputDir: readOption("output-dir"),
  93. });
  94. }
  95. if (resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) {
  96. try {
  97. await main();
  98. } catch (error) {
  99. console.error(error instanceof Error ? error.stack : error);
  100. process.exitCode = 1;
  101. }
  102. }