package-native-search-tools.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env node
  2. import { createWriteStream } from "node:fs";
  3. import { copyFile, mkdir, mkdtemp, readdir, rm } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { createRequire } from "node:module";
  6. import { dirname, join, resolve } from "node:path";
  7. import process from "node:process";
  8. import { pipeline } from "node:stream/promises";
  9. import { fileURLToPath } from "node:url";
  10. import { buildNativeSearchTools } from "./build-native-search-tools.mjs";
  11. import { packSourceAsDeterministicTarGzip } from "./deterministic-tar-archive.mjs";
  12. import { resolveNativeSearchPrebuiltPlan } from "./native-search-tools-config.mjs";
  13. import { stageNativeSearchNotices } from "./third-party-notices.mjs";
  14. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  15. const require = createRequire(join(repoRoot, "packages/desktop/package.json"));
  16. export function resolveNativeSearchPackagedArtifactPath({ artifact, artifactsDir }) {
  17. return join(
  18. resolve(artifactsDir),
  19. "native-search-tools",
  20. `${artifact.toolId}-${artifact.release}`,
  21. artifact.releaseFileName,
  22. );
  23. }
  24. async function packWindowsZip(directory, binaryName, archivePath) {
  25. const { ZipFile } = require("yazl");
  26. const zip = new ZipFile();
  27. // yazl 按本地时区编码 DOS 时间;固定本地时间并禁用扩展时间戳才能跨时区复现。
  28. for (const name of (await readdir(directory)).sort()) {
  29. zip.addFile(join(directory, name), name, {
  30. mtime: new Date(1980, 0, 1, 0, 0, 0, 0),
  31. mode: name === binaryName ? 0o100755 : 0o100644,
  32. forceDosTimestamp: true,
  33. });
  34. }
  35. zip.end();
  36. await pipeline(zip.outputStream, createWriteStream(archivePath));
  37. }
  38. export async function packageNativeSearchTools({
  39. platform = process.env.ZCODE_TARGET_OS || process.platform,
  40. arch = process.env.ZCODE_TARGET_ARCH || process.arch,
  41. artifactsDir = join(repoRoot, "dist/native-search-tools-deps"),
  42. buildOutputDir,
  43. jobs,
  44. } = {}) {
  45. const buildPlan = buildNativeSearchTools({
  46. platform,
  47. arch,
  48. outputDir: buildOutputDir,
  49. jobs,
  50. });
  51. const prebuiltPlan = resolveNativeSearchPrebuiltPlan({
  52. platform: buildPlan.platform,
  53. arch: buildPlan.arch,
  54. outputDir: buildPlan.outputDir,
  55. });
  56. await packNativeSearchPrebuiltArtifacts({ prebuiltPlan, artifactsDir });
  57. return prebuiltPlan;
  58. }
  59. export async function packNativeSearchPrebuiltArtifacts({ prebuiltPlan, artifactsDir }) {
  60. for (const artifact of prebuiltPlan.artifacts.filter(({ source }) => source === "producer")) {
  61. const archivePath = resolveNativeSearchPackagedArtifactPath({ artifact, artifactsDir });
  62. await mkdir(dirname(archivePath), { recursive: true });
  63. const staging = await mkdtemp(join(tmpdir(), "native-search-archive-"));
  64. try {
  65. const binaryPath = join(staging, artifact.binaryName);
  66. await copyFile(artifact.binaryPath, binaryPath);
  67. // 二进制发布归档必须自带通知,不能只在仓库根目录提供声明。
  68. await stageNativeSearchNotices({ artifacts: [{ ...artifact, binaryPath }] }, repoRoot, {
  69. builtFromSource: true,
  70. });
  71. if (artifact.archiveExt === "zip") {
  72. await packWindowsZip(staging, artifact.binaryName, archivePath);
  73. } else {
  74. packSourceAsDeterministicTarGzip(staging, archivePath);
  75. }
  76. } finally {
  77. await rm(staging, { recursive: true, force: true });
  78. }
  79. console.log(`==> Packaged ${artifact.toolId} ${artifact.release} -> ${archivePath}`);
  80. }
  81. }
  82. function readOption(name) {
  83. const prefix = `--${name}=`;
  84. const inline = process.argv.find((arg) => arg.startsWith(prefix));
  85. if (inline) return inline.slice(prefix.length);
  86. const index = process.argv.indexOf(`--${name}`);
  87. return index >= 0 ? process.argv[index + 1] : undefined;
  88. }
  89. function readJobs() {
  90. const value = readOption("jobs");
  91. if (value === undefined) return undefined;
  92. const jobs = Number.parseInt(value, 10);
  93. if (!Number.isInteger(jobs) || jobs < 1) throw new Error(`invalid --jobs value: ${value}`);
  94. return jobs;
  95. }
  96. async function main() {
  97. await packageNativeSearchTools({
  98. platform: readOption("platform") ?? process.env.ZCODE_TARGET_OS ?? process.platform,
  99. arch: readOption("arch") ?? process.env.ZCODE_TARGET_ARCH ?? process.arch,
  100. artifactsDir: readOption("artifacts-dir"),
  101. buildOutputDir: readOption("build-output-dir"),
  102. jobs: readJobs(),
  103. });
  104. }
  105. if (resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) {
  106. try {
  107. await main();
  108. } catch (error) {
  109. console.error(error instanceof Error ? error.stack : error);
  110. process.exitCode = 1;
  111. }
  112. }