Skip to content

Commit 59182a9

Browse files
authored
feat(core): Copy downloadRipGrep with license. (google-gemini#8195)
1 parent 39af711 commit 59182a9

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed

third_party/get-ripgrep/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Lvce Editor
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@lvce-editor/ripgrep",
3+
"version": "0.0.0-dev",
4+
"description": "A module for using ripgrep in a Node project",
5+
"main": "src/index.js",
6+
"typings": "src/index.d.ts",
7+
"type": "module",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/lvce-editor/ripgrep"
11+
},
12+
"scripts": {
13+
"postinstall": "node ./src/postinstall.js",
14+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
15+
"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",
16+
"format": "prettier --write ."
17+
},
18+
"keywords": [
19+
"lvce-editor",
20+
"ripgrep"
21+
],
22+
"author": "Lvce Editor",
23+
"license": "MIT",
24+
"dependencies": {
25+
"@lvce-editor/verror": "^1.6.0",
26+
"execa": "^9.5.2",
27+
"extract-zip": "^2.0.1",
28+
"fs-extra": "^11.3.0",
29+
"got": "^14.4.5",
30+
"path-exists": "^5.0.0",
31+
"tempy": "^3.1.0",
32+
"xdg-basedir": "^5.1.0"
33+
},
34+
"devDependencies": {
35+
"@types/fs-extra": "^11.0.4",
36+
"@types/jest": "^29.5.14",
37+
"@types/node": "^22.13.0",
38+
"jest": "^29.7.0",
39+
"prettier": "^3.4.2",
40+
"typescript": "^5.7.3"
41+
},
42+
"prettier": {
43+
"semi": false,
44+
"singleQuote": true
45+
}
46+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* eslint-disable */
2+
/**
3+
* @license
4+
* Copyright 2023 Lvce Editor
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
import { VError } from '@lvce-editor/verror'
8+
import { execa } from 'execa'
9+
import extractZip from 'extract-zip'
10+
import fsExtra from 'fs-extra'
11+
import got from 'got'
12+
import * as os from 'node:os'
13+
import { dirname, join } from 'node:path'
14+
import { pathExists } from 'path-exists'
15+
import { pipeline } from 'node:stream/promises'
16+
import { temporaryFile } from 'tempy'
17+
import { fileURLToPath } from 'node:url'
18+
import { xdgCache } from 'xdg-basedir'
19+
20+
const { mkdir, createWriteStream, move } = fsExtra
21+
22+
const __dirname = dirname(fileURLToPath(import.meta.url))
23+
24+
const REPOSITORY = `microsoft/ripgrep-prebuilt`
25+
const VERSION = process.env.RIPGREP_VERSION || 'v13.0.0-10'
26+
console.log({ VERSION })
27+
const BIN_PATH = join(__dirname, '../bin')
28+
29+
const getTarget = () => {
30+
const arch = process.env.npm_config_arch || os.arch()
31+
const platform = process.env.platform || os.platform()
32+
switch (platform) {
33+
case 'darwin':
34+
switch (arch) {
35+
case 'arm64':
36+
return 'aarch64-apple-darwin.tar.gz'
37+
default:
38+
return 'x86_64-apple-darwin.tar.gz'
39+
}
40+
case 'win32':
41+
switch (arch) {
42+
case 'x64':
43+
return 'x86_64-pc-windows-msvc.zip'
44+
case 'arm':
45+
return 'aarch64-pc-windows-msvc.zip'
46+
default:
47+
return 'i686-pc-windows-msvc.zip'
48+
}
49+
case 'linux':
50+
switch (arch) {
51+
case 'x64':
52+
return 'x86_64-unknown-linux-musl.tar.gz'
53+
case 'arm':
54+
case 'armv7l':
55+
return 'arm-unknown-linux-gnueabihf.tar.gz'
56+
case 'arm64':
57+
return 'aarch64-unknown-linux-gnu.tar.gz'
58+
case 'ppc64':
59+
return 'powerpc64le-unknown-linux-gnu.tar.gz'
60+
case 's390x':
61+
return 's390x-unknown-linux-gnu.tar.gz'
62+
default:
63+
return 'i686-unknown-linux-musl.tar.gz'
64+
}
65+
default:
66+
throw new VError('Unknown platform: ' + platform)
67+
}
68+
}
69+
70+
export const downloadFile = async (url, outFile) => {
71+
try {
72+
const tmpFile = temporaryFile()
73+
await pipeline(got.stream(url), createWriteStream(tmpFile))
74+
await mkdir(dirname(outFile), { recursive: true })
75+
await move(tmpFile, outFile)
76+
} catch (error) {
77+
throw new VError(error, `Failed to download "${url}"`)
78+
}
79+
}
80+
81+
/**
82+
* @param {string} inFile
83+
* @param {string} outDir
84+
*/
85+
const unzip = async (inFile, outDir) => {
86+
try {
87+
await mkdir(outDir, { recursive: true })
88+
await extractZip(inFile, { dir: outDir })
89+
} catch (error) {
90+
throw new VError(error, `Failed to unzip "${inFile}"`)
91+
}
92+
}
93+
94+
/**
95+
* @param {string} inFile
96+
* @param {string} outDir
97+
*/
98+
const untarGz = async (inFile, outDir) => {
99+
try {
100+
await mkdir(outDir, { recursive: true })
101+
await execa('tar', ['xvf', inFile, '-C', outDir])
102+
} catch (error) {
103+
throw new VError(error, `Failed to extract "${inFile}"`)
104+
}
105+
}
106+
107+
export const downloadRipGrep = async () => {
108+
const target = getTarget()
109+
const url = `https://github.com/${REPOSITORY}/releases/download/${VERSION}/ripgrep-${VERSION}-${target}`
110+
const downloadPath = `${xdgCache}/vscode-ripgrep/ripgrep-${VERSION}-${target}`
111+
if (!(await pathExists(downloadPath))) {
112+
await downloadFile(url, downloadPath)
113+
} else {
114+
console.info(`File ${downloadPath} has been cached`)
115+
}
116+
if (downloadPath.endsWith('.tar.gz')) {
117+
await untarGz(downloadPath, BIN_PATH)
118+
} else if (downloadPath.endsWith('.zip')) {
119+
await unzip(downloadPath, BIN_PATH)
120+
} else {
121+
throw new VError(`Invalid downloadPath ${downloadPath}`)
122+
}
123+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable */
2+
/**
3+
* @license
4+
* Copyright 2023 Lvce Editor
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
import { dirname, join } from 'node:path'
8+
import { fileURLToPath } from 'node:url'
9+
10+
const __dirname = dirname(fileURLToPath(import.meta.url))
11+
12+
export const rgPath = join(
13+
__dirname,
14+
'..',
15+
'bin',
16+
`rg${process.platform === 'win32' ? '.exe' : ''}`,
17+
)

0 commit comments

Comments
 (0)