download-ripgrep.mjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env node
  2. import { existsSync, mkdirSync } from "node:fs";
  3. import { dirname, join, resolve } from "node:path";
  4. import process from "node:process";
  5. import { fileURLToPath, pathToFileURL } from "node:url";
  6. import { createRequire } from "node:module";
  7. import { resolveIntranetDepsBaseUrl } from "./intranetDefaults.mjs";
  8. import {
  9. LEGACY_REMOTE_RIPGREP_ARCHIVE_SHA256_BY_TARGET,
  10. LEGACY_REMOTE_RIPGREP_VERSION,
  11. } from "./native-search-tools-config.mjs";
  12. import { downloadAndExtractPrebuiltBinary } from "./prebuilt-binary-download.mjs";
  13. const require = createRequire(import.meta.url);
  14. const scriptDir = dirname(fileURLToPath(import.meta.url));
  15. const repoRoot = resolve(scriptDir, "..");
  16. const RIPGREP_VERSION = process.argv[2] || LEGACY_REMOTE_RIPGREP_VERSION;
  17. function normalizePlatform(raw) {
  18. switch ((raw ?? "").toLowerCase()) {
  19. case "mac":
  20. case "macos":
  21. case "darwin":
  22. case "osx":
  23. return "darwin";
  24. case "win":
  25. case "windows":
  26. case "win32":
  27. return "win32";
  28. case "linux":
  29. return "linux";
  30. default:
  31. return raw;
  32. }
  33. }
  34. function normalizeArch(raw) {
  35. switch ((raw ?? "").toLowerCase()) {
  36. case "x86_64":
  37. case "x64":
  38. case "amd64":
  39. return "x64";
  40. case "aarch64":
  41. case "arm64":
  42. return "arm64";
  43. default:
  44. return raw;
  45. }
  46. }
  47. function resolveRipgrepTarget(platform, arch) {
  48. switch (platform) {
  49. case "darwin":
  50. return arch === "arm64" ? "aarch64-apple-darwin.tar.gz" : "x86_64-apple-darwin.tar.gz";
  51. case "win32":
  52. return arch === "arm64" ? "aarch64-pc-windows-msvc.zip" : "x86_64-pc-windows-msvc.zip";
  53. case "linux":
  54. return arch === "arm64"
  55. ? "aarch64-unknown-linux-gnu.tar.gz"
  56. : "x86_64-unknown-linux-musl.tar.gz";
  57. default:
  58. throw new Error(`Unsupported ripgrep platform: ${platform}-${arch}`);
  59. }
  60. }
  61. export function resolveRipgrepDownloadBaseUrl(version, env = process.env) {
  62. return (
  63. env.RIPGREP_BINARY_DOWNLOAD_BASE_URL?.trim().replace(/\/+$/, "") ||
  64. `${resolveIntranetDepsBaseUrl(env)}/ripgrep-${version}`
  65. );
  66. }
  67. export function resolveRipgrepDownloadPlan({
  68. version,
  69. targetPlatform,
  70. targetArch,
  71. localPlatform,
  72. localArch,
  73. packageVersion,
  74. forceRemoteOutput = false,
  75. outputDir,
  76. env = process.env,
  77. }) {
  78. const normalizedTargetPlatform = normalizePlatform(targetPlatform);
  79. const normalizedTargetArch = normalizeArch(targetArch);
  80. const normalizedLocalPlatform = normalizePlatform(localPlatform);
  81. const normalizedLocalArch = normalizeArch(localArch);
  82. const platformKey = `${normalizedTargetPlatform}-${normalizedTargetArch}`;
  83. const localKey = `${normalizedLocalPlatform}-${normalizedLocalArch}`;
  84. const target = resolveRipgrepTarget(normalizedTargetPlatform, normalizedTargetArch);
  85. const binaryName = normalizedTargetPlatform === "win32" ? "rg.exe" : "rg";
  86. const releaseFileName = `ripgrep-${version}-${target}`;
  87. const archiveSha256 =
  88. version === LEGACY_REMOTE_RIPGREP_VERSION
  89. ? LEGACY_REMOTE_RIPGREP_ARCHIVE_SHA256_BY_TARGET[platformKey]
  90. : undefined;
  91. const localOutputDir = join(repoRoot, "packages/desktop/bundled-tools", platformKey, "ripgrep");
  92. const remoteOutputDir = join(
  93. repoRoot,
  94. "packages/desktop/mock-cdn/releases",
  95. packageVersion,
  96. "tools",
  97. platformKey,
  98. "ripgrep",
  99. );
  100. const resolvedOutputDir = outputDir
  101. ? resolve(outputDir)
  102. : forceRemoteOutput
  103. ? remoteOutputDir
  104. : platformKey === localKey
  105. ? localOutputDir
  106. : remoteOutputDir;
  107. return {
  108. version,
  109. platformKey,
  110. binaryName,
  111. outputDir: resolvedOutputDir,
  112. remoteOutputDir,
  113. releaseFileName,
  114. downloadUrl: `${resolveRipgrepDownloadBaseUrl(version, env)}/${releaseFileName}`,
  115. archiveExt: target.endsWith(".zip") ? "zip" : "tar.gz",
  116. archiveSha256,
  117. };
  118. }
  119. export async function downloadRipgrepBinary({
  120. version = LEGACY_REMOTE_RIPGREP_VERSION,
  121. targetPlatform = process.env.ZCODE_TARGET_OS || process.platform,
  122. targetArch = process.env.ZCODE_TARGET_ARCH || process.arch,
  123. localPlatform = process.env.ZCODE_TARGET_OS || process.platform,
  124. localArch = process.env.ZCODE_TARGET_ARCH || process.arch,
  125. packageVersion = require(join(repoRoot, "package.json")).version,
  126. forceRemoteOutput = process.env.ZCODE_FORCE_REMOTE_MOCK_CDN === "1",
  127. outputDir,
  128. env = process.env,
  129. } = {}) {
  130. const plan = resolveRipgrepDownloadPlan({
  131. version,
  132. targetPlatform,
  133. targetArch,
  134. localPlatform,
  135. localArch,
  136. packageVersion,
  137. forceRemoteOutput,
  138. outputDir,
  139. env,
  140. });
  141. const binaryPath = join(plan.outputDir, plan.binaryName);
  142. console.log("==> ripgrep download");
  143. console.log(` version: ${plan.version}`);
  144. console.log(` platform: ${plan.platformKey}`);
  145. console.log(` target: ${plan.outputDir}`);
  146. if (existsSync(binaryPath)) {
  147. console.log(" [skip] 已存在,跳过");
  148. return;
  149. }
  150. mkdirSync(plan.outputDir, { recursive: true });
  151. console.log(` [download] ${plan.downloadUrl}`);
  152. await downloadAndExtractPrebuiltBinary({
  153. archiveExt: plan.archiveExt,
  154. archiveSha256: plan.archiveSha256,
  155. binaryName: plan.binaryName,
  156. binaryPath,
  157. cwd: repoRoot,
  158. downloadUrl: plan.downloadUrl,
  159. targetPlatform: normalizePlatform(targetPlatform),
  160. });
  161. console.log(`==> Done! ripgrep ${plan.version} (${plan.platformKey}) -> ${plan.outputDir}`);
  162. }
  163. async function main() {
  164. await downloadRipgrepBinary({
  165. version: RIPGREP_VERSION,
  166. targetPlatform: process.argv[3] || process.env.ZCODE_TARGET_OS || process.platform,
  167. targetArch: process.argv[4] || process.env.ZCODE_TARGET_ARCH || process.arch,
  168. });
  169. }
  170. const entryHref = process.argv[1] ? pathToFileURL(process.argv[1]).href : null;
  171. if (entryHref === import.meta.url) {
  172. await main();
  173. }