|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Download all ripgrep binaries for cross-platform MCPB bundles |
| 5 | + * |
| 6 | + * This script downloads ripgrep binaries for all supported platforms |
| 7 | + * and places them in node_modules/@vscode/ripgrep/bin/ with platform-specific names. |
| 8 | + */ |
| 9 | + |
| 10 | +const https = require('https'); |
| 11 | +const fs = require('fs'); |
| 12 | +const path = require('path'); |
| 13 | +const { promisify } = require('util'); |
| 14 | +const yauzl = require('yauzl'); |
| 15 | +const { pipeline } = require('stream'); |
| 16 | + |
| 17 | +const streamPipeline = promisify(pipeline); |
| 18 | + |
| 19 | +// Ripgrep versions from @vscode/ripgrep |
| 20 | +const VERSION = 'v13.0.0-10'; |
| 21 | +const MULTI_ARCH_VERSION = 'v13.0.0-4'; |
| 22 | + |
| 23 | +// All supported platforms |
| 24 | +const PLATFORMS = [ |
| 25 | + { name: 'x86_64-apple-darwin', ext: 'tar.gz', version: VERSION }, |
| 26 | + { name: 'aarch64-apple-darwin', ext: 'tar.gz', version: VERSION }, |
| 27 | + { name: 'x86_64-pc-windows-msvc', ext: 'zip', version: VERSION }, |
| 28 | + { name: 'aarch64-pc-windows-msvc', ext: 'zip', version: VERSION }, |
| 29 | + { name: 'i686-pc-windows-msvc', ext: 'zip', version: VERSION }, |
| 30 | + { name: 'x86_64-unknown-linux-musl', ext: 'tar.gz', version: VERSION }, |
| 31 | + { name: 'aarch64-unknown-linux-musl', ext: 'tar.gz', version: VERSION }, |
| 32 | + { name: 'i686-unknown-linux-musl', ext: 'tar.gz', version: VERSION }, |
| 33 | + { name: 'arm-unknown-linux-gnueabihf', ext: 'tar.gz', version: MULTI_ARCH_VERSION }, |
| 34 | + { name: 'powerpc64le-unknown-linux-gnu', ext: 'tar.gz', version: MULTI_ARCH_VERSION }, |
| 35 | + { name: 's390x-unknown-linux-gnu', ext: 'tar.gz', version: VERSION } |
| 36 | +]; |
| 37 | + |
| 38 | +const TEMP_DIR = path.join(__dirname, '../.ripgrep-downloads'); |
| 39 | +const OUTPUT_DIR = path.join(__dirname, '../node_modules/@vscode/ripgrep/bin'); |
| 40 | + |
| 41 | +function ensureDir(dir) { |
| 42 | + if (!fs.existsSync(dir)) { |
| 43 | + fs.mkdirSync(dir, { recursive: true }); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async function downloadFile(url, dest) { |
| 48 | + return new Promise((resolve, reject) => { |
| 49 | + console.log(` 📥 ${path.basename(dest)}...`); |
| 50 | + |
| 51 | + https.get(url, { |
| 52 | + headers: { |
| 53 | + 'User-Agent': 'vscode-ripgrep', |
| 54 | + 'Accept': 'application/octet-stream' |
| 55 | + } |
| 56 | + }, (response) => { |
| 57 | + if (response.statusCode === 302 || response.statusCode === 301) { |
| 58 | + // Follow redirect |
| 59 | + downloadFile(response.headers.location, dest).then(resolve).catch(reject); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (response.statusCode !== 200) { |
| 64 | + reject(new Error(`Failed to download: ${response.statusCode}`)); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const file = fs.createWriteStream(dest); |
| 69 | + response.pipe(file); |
| 70 | + |
| 71 | + file.on('finish', () => { |
| 72 | + file.close(); |
| 73 | + resolve(); |
| 74 | + }); |
| 75 | + |
| 76 | + file.on('error', (err) => { |
| 77 | + fs.unlink(dest, () => reject(err)); |
| 78 | + }); |
| 79 | + }).on('error', reject); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +async function extractZip(zipPath, outputDir, targetBinary) { |
| 84 | + return new Promise((resolve, reject) => { |
| 85 | + yauzl.open(zipPath, { lazyEntries: true }, (err, zipfile) => { |
| 86 | + if (err) return reject(err); |
| 87 | + |
| 88 | + zipfile.readEntry(); |
| 89 | + zipfile.on('entry', (entry) => { |
| 90 | + if (entry.fileName.endsWith('rg.exe')) { |
| 91 | + zipfile.openReadStream(entry, (err, readStream) => { |
| 92 | + if (err) return reject(err); |
| 93 | + |
| 94 | + const outputPath = path.join(outputDir, targetBinary); |
| 95 | + const writeStream = fs.createWriteStream(outputPath); |
| 96 | + |
| 97 | + readStream.pipe(writeStream); |
| 98 | + writeStream.on('close', () => { |
| 99 | + zipfile.close(); |
| 100 | + resolve(); |
| 101 | + }); |
| 102 | + writeStream.on('error', reject); |
| 103 | + }); |
| 104 | + } else { |
| 105 | + zipfile.readEntry(); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + zipfile.on('end', resolve); |
| 110 | + zipfile.on('error', reject); |
| 111 | + }); |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +async function extractTarGz(tarPath, outputDir, targetBinary) { |
| 116 | + return new Promise((resolve, reject) => { |
| 117 | + const { execSync } = require('child_process'); |
| 118 | + const tempExtractDir = path.join(TEMP_DIR, 'extract-' + Date.now()); |
| 119 | + |
| 120 | + try { |
| 121 | + ensureDir(tempExtractDir); |
| 122 | + |
| 123 | + // Extract tar.gz |
| 124 | + execSync(`tar -xzf "${tarPath}" -C "${tempExtractDir}"`, { stdio: 'pipe' }); |
| 125 | + |
| 126 | + // Find the rg binary |
| 127 | + const files = fs.readdirSync(tempExtractDir, { recursive: true }); |
| 128 | + const rgFile = files.find(f => f.endsWith('/rg') || f === 'rg'); |
| 129 | + |
| 130 | + if (rgFile) { |
| 131 | + const sourcePath = path.join(tempExtractDir, rgFile); |
| 132 | + const destPath = path.join(outputDir, targetBinary); |
| 133 | + fs.copyFileSync(sourcePath, destPath); |
| 134 | + fs.chmodSync(destPath, 0o755); |
| 135 | + } |
| 136 | + |
| 137 | + // Cleanup |
| 138 | + fs.rmSync(tempExtractDir, { recursive: true, force: true }); |
| 139 | + resolve(); |
| 140 | + } catch (error) { |
| 141 | + reject(error); |
| 142 | + } |
| 143 | + }); |
| 144 | +} |
| 145 | + |
| 146 | +async function downloadAndExtractPlatform(platform) { |
| 147 | + const { name, ext, version } = platform; |
| 148 | + const fileName = `ripgrep-${version}-${name}.${ext}`; |
| 149 | + const url = `https://github.com/microsoft/ripgrep-prebuilt/releases/download/${version}/${fileName}`; |
| 150 | + const tempFile = path.join(TEMP_DIR, fileName); |
| 151 | + |
| 152 | + // Determine output binary name |
| 153 | + const isWindows = name.includes('windows'); |
| 154 | + const binaryName = isWindows ? `rg-${name}.exe` : `rg-${name}`; |
| 155 | + const outputPath = path.join(OUTPUT_DIR, binaryName); |
| 156 | + |
| 157 | + // Skip if binary already exists |
| 158 | + if (fs.existsSync(outputPath)) { |
| 159 | + console.log(` ✓ ${name} (cached)`); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + try { |
| 164 | + // Download if not already cached |
| 165 | + if (!fs.existsSync(tempFile)) { |
| 166 | + await downloadFile(url, tempFile); |
| 167 | + } |
| 168 | + |
| 169 | + // Extract |
| 170 | + ensureDir(OUTPUT_DIR); |
| 171 | + |
| 172 | + if (ext === 'zip') { |
| 173 | + await extractZip(tempFile, OUTPUT_DIR, binaryName); |
| 174 | + } else { |
| 175 | + await extractTarGz(tempFile, OUTPUT_DIR, binaryName); |
| 176 | + } |
| 177 | + |
| 178 | + console.log(` ✓ ${name}`); |
| 179 | + } catch (error) { |
| 180 | + console.error(` ✗ Failed: ${name} - ${error.message}`); |
| 181 | + throw error; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +async function main() { |
| 186 | + console.log('🌍 Downloading ripgrep binaries for all platforms...\n'); |
| 187 | + |
| 188 | + ensureDir(TEMP_DIR); |
| 189 | + ensureDir(OUTPUT_DIR); |
| 190 | + |
| 191 | + // Download and extract all platforms |
| 192 | + for (const platform of PLATFORMS) { |
| 193 | + await downloadAndExtractPlatform(platform); |
| 194 | + } |
| 195 | + |
| 196 | + console.log('\n✅ All ripgrep binaries downloaded successfully!'); |
| 197 | + console.log(`📁 Binaries location: ${OUTPUT_DIR}`); |
| 198 | + console.log('\nFiles created:'); |
| 199 | + const files = fs.readdirSync(OUTPUT_DIR).filter(f => f.startsWith('rg-')); |
| 200 | + files.forEach(f => console.log(` - ${f}`)); |
| 201 | +} |
| 202 | + |
| 203 | +// Run if called directly |
| 204 | +if (require.main === module) { |
| 205 | + main().catch((error) => { |
| 206 | + console.error('❌ Error:', error); |
| 207 | + process.exit(1); |
| 208 | + }); |
| 209 | +} |
| 210 | + |
| 211 | +module.exports = { downloadAndExtractPlatform, PLATFORMS }; |
0 commit comments