sea-runtime-tool-assets.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. const legal = await readNativeSearchNotices(root);
  67. for (const [name, bytes] of [
  68. ["THIRD-PARTY-NOTICES.txt", legal.bytes],
  69. ["SOURCES.json", await readFile(resolve(root, "third-party/native-search/sources.json"))],
  70. ]) {
  71. const file = resolve(stagingDirectory, name);
  72. await writeFile(file, bytes);
  73. assets[`${seaRuntimeToolAssetPrefix}${name}`] = file;
  74. }
  75. return {
  76. assets,
  77. manifest,
  78. };
  79. };
  80. async function readRequiredBinaryStat(sourcePath, target, toolId) {
  81. try {
  82. const sourceStat = await stat(sourcePath);
  83. if (sourceStat.isFile()) return sourceStat;
  84. } catch (error) {
  85. if (error?.code !== "ENOENT") throw error;
  86. }
  87. throw new Error(
  88. `Missing SEA runtime tool ${toolId} for ${target}: ${sourcePath}. ` +
  89. "Prepare the target runtime assets before building the SEA binary.",
  90. );
  91. }