sea-runtime-tool-assets.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { createHash } from "node:crypto";
  2. import { mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
  3. import { resolve } from "node:path";
  4. import {
  5. NATIVE_SEARCH_TOOL_VERSIONS,
  6. resolveNativeSearchReleasePlan,
  7. } from "../../../../../scripts/native-search-tools-config.mjs";
  8. import { targetParts } from "./sea-targets.mjs";
  9. import { readNativeSearchNotices } from "../../../../../scripts/third-party-notices.mjs";
  10. export const seaRuntimeToolAssetPrefix = "zcode-runtime-tools/";
  11. export const seaRuntimeToolManifestAssetKey = `${seaRuntimeToolAssetPrefix}manifest.json`;
  12. const binaryNames = Object.freeze({
  13. bfs: "bfs",
  14. ripgrep: "rg",
  15. ugrep: "ugrep",
  16. });
  17. export const collectSeaRuntimeToolAssets = async ({ root, stagingDirectory, target }) => {
  18. const { arch, releasePlatform } = targetParts(target);
  19. const releasePlan = resolveNativeSearchReleasePlan({
  20. arch,
  21. platform: releasePlatform,
  22. });
  23. const toolIds = [...releasePlan.runtimeToolIds].sort();
  24. const tools = [];
  25. const assets = {};
  26. await rm(stagingDirectory, {
  27. force: true,
  28. recursive: true,
  29. });
  30. for (const toolId of toolIds) {
  31. const binaryName = `${binaryNames[toolId]}${releasePlan.platform === "win32" ? ".exe" : ""}`;
  32. const sourcePath = resolve(
  33. root,
  34. "packages",
  35. "desktop",
  36. "bundled-tools",
  37. releasePlan.platformKey,
  38. toolId,
  39. binaryName,
  40. );
  41. const sourceStat = await readRequiredBinaryStat(sourcePath, target, toolId);
  42. const bytes = await readFile(sourcePath);
  43. const sha256 = createHash("sha256").update(bytes).digest("hex");
  44. const assetKey = `${seaRuntimeToolAssetPrefix}${toolId}/${sha256}/${binaryName}`;
  45. const version = NATIVE_SEARCH_TOOL_VERSIONS[toolId];
  46. assets[assetKey] = sourcePath;
  47. tools.push({
  48. binaryName,
  49. id: toolId,
  50. sha256,
  51. size: sourceStat.size,
  52. version,
  53. });
  54. }
  55. const manifest = {
  56. target,
  57. tools,
  58. version: 1,
  59. };
  60. const manifestPath = resolve(stagingDirectory, "manifest.json");
  61. await mkdir(stagingDirectory, {
  62. recursive: true,
  63. });
  64. await writeFile(manifestPath, JSON.stringify(manifest, null, 2));
  65. assets[seaRuntimeToolManifestAssetKey] = manifestPath;
  66. // 单文件 SEA 同样分发原生工具,许可声明与来源清单随二进制内嵌,不依赖 dist 旁的伴随文件。
  67. const legal = await readNativeSearchNotices(root);
  68. for (const [name, bytes] of [
  69. ["THIRD-PARTY-NOTICES.txt", legal.bytes],
  70. ["SOURCES.json", await readFile(resolve(root, "third-party/native-search/sources.json"))],
  71. ]) {
  72. const file = resolve(stagingDirectory, name);
  73. await writeFile(file, bytes);
  74. assets[`${seaRuntimeToolAssetPrefix}${name}`] = file;
  75. }
  76. return {
  77. assets,
  78. manifest,
  79. };
  80. };
  81. async function readRequiredBinaryStat(sourcePath, target, toolId) {
  82. try {
  83. const sourceStat = await stat(sourcePath);
  84. if (sourceStat.isFile()) return sourceStat;
  85. } catch (error) {
  86. if (error?.code !== "ENOENT") throw error;
  87. }
  88. throw new Error(
  89. `Missing SEA runtime tool ${toolId} for ${target}: ${sourcePath}. ` +
  90. "Prepare the target runtime assets before building the SEA binary.",
  91. );
  92. }