Skip to content

Commit 6702e47

Browse files
authored
change tree-sitter to wasm (#2)
* init tree-sitter wasm * 修复一些问题 * 修复一些问题 * update fetchModels * update yml
1 parent f79c608 commit 6702e47

26 files changed

+704
-870
lines changed

.github/workflows/beta-pack.yml

+2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ jobs:
2020
with:
2121
submodules: recursive
2222
- name: Cache MKL
23+
id: mkl
2324
uses: actions/cache@v3
2425
with:
2526
path: C:\Program Files (x86)\Intel\oneAPI\mkl\latest
2627
key: ${{ runner.os }}-mkl-caches-w_BaseKit_p_2023.1.0.47256_offline
2728
restore-keys: |
2829
${{ runner.os }}-mkl-
2930
- name: Install MKL
31+
if: steps.mkl.outputs.cache-hit != 'true'
3032
run: |
3133
echo "MKLROOT=C:\Program Files (x86)\Intel\oneAPI\mkl\latest" >> $GITHUB_ENV
3234
.github/workflows/install_mkl_windows.bat "https:/registrationcenter-download.intel.com/akdlm/IRC_NAS/c961e083-5685-4f0b-ada5-c6cf16f561dd/w_BaseKit_p_2023.1.0.47256_offline.exe" "intel.oneapi.win.mkl.devel"

.github/workflows/release.yml

+2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ jobs:
2121
with:
2222
submodules: recursive
2323
- name: Cache MKL
24+
id: mkl
2425
uses: actions/cache@v3
2526
with:
2627
path: C:\Program Files (x86)\Intel\oneAPI\mkl\latest
2728
key: ${{ runner.os }}-mkl-caches-w_BaseKit_p_2023.1.0.47256_offline
2829
restore-keys: |
2930
${{ runner.os }}-mkl-
3031
- name: Install MKL
32+
if: steps.mkl.outputs.cache-hit != 'true'
3133
run: |
3234
echo "MKLROOT=C:\Program Files (x86)\Intel\oneAPI\mkl\latest" >> $GITHUB_ENV
3335
.github/workflows/install_mkl_windows.bat "https:/registrationcenter-download.intel.com/akdlm/IRC_NAS/c961e083-5685-4f0b-ada5-c6cf16f561dd/w_BaseKit_p_2023.1.0.47256_offline.exe" "intel.oneapi.win.mkl.devel"

build.js

+47-72
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,26 @@
11
const { exec } = require('child_process');
2-
const fs = require('fs');
32
const path = require('path');
4-
const tar = require('tar');
53
const fsExtra = require('fs-extra')
4+
const os = require('os');
5+
const { rimrafSync } = require('rimraf')
66

7-
8-
function buildLib({command, cwd, destination}){
7+
async function buildWasm({ languageName}){
8+
const command = 'npx tree-sitter build-wasm .'
9+
const cwd = path.join(__dirname, `./node_modules/tree-sitter-${languageName}`)
10+
const destination = path.join(__dirname, './src/assets/wasm')
911
return new Promise((resolve,reject)=>{
1012
exec(command, { cwd }, (error, stdout, stderr) => {
1113
if (error) {
1214
return reject(`执行的命令出错: ${error}`)
1315
}
1416

15-
console.log(`stdout: ${stdout}`);
16-
console.error(`stderr: ${stderr}`);
17-
18-
// 找到生成的压缩包
19-
const prebuildsDir = path.join(cwd, 'prebuilds');
20-
fs.readdir(prebuildsDir, (err, files) => {
21-
if (err) {
22-
return reject(`读取目录出错: ${err}`)
23-
}
24-
25-
// 找到 .tar.gz 文件
26-
const tarFile = files.find(file => file.endsWith('.tar.gz'));
27-
if (!tarFile) {
28-
return reject('没有找到 .tar.gz 文件')
29-
}
17+
console.log(`stdout: tree-sitter-${languageName} generate success`);
3018

31-
// 解压缩文件
32-
tar.x({
33-
file: path.join(prebuildsDir, tarFile),
34-
C: prebuildsDir,
35-
}, err => {
36-
if (err) {
37-
return reject(`解压缩文件出错: ${err}`)
38-
}
39-
fsExtra.copy(
40-
path.join(prebuildsDir,'build','Release'),
41-
destination
42-
).then(resolve)
43-
});
44-
});
19+
const wasmFile = `tree-sitter-${languageName}.wasm`
20+
fsExtra.copy(
21+
path.join(path.join(cwd, wasmFile)),
22+
path.join(destination, wasmFile)
23+
).then(resolve)
4524
});
4625
})
4726
}
@@ -54,47 +33,43 @@ async function serialPrommise(taskFunc = []){
5433
},Promise.resolve())
5534
}
5635

57-
const buildNodeModulesLib = ()=>{
58-
return ['javascript','cpp','go','java','php','python','ruby','rust','scala','markdown','html','solidity','kotlin'].map(language=>{
59-
return buildLib({
60-
command: 'npx prebuild -r electron -t 26.0.0 --strip',
61-
cwd: path.join(__dirname, 'node_modules', `tree-sitter-${language}`),
62-
destination: path.join(__dirname, 'src', 'assets', 'node', `tree-sitter-${language}`)
63-
})
64-
})
65-
}
66-
67-
const copyNodeTypes = ()=>{
68-
return ['javascript','cpp','go','java','php','python','ruby','rust','scala','markdown','html','solidity','kotlin'].map(language=>{
36+
const buildFaiss = async function(){
37+
const copyFaiss = async ()=>{
6938
return fsExtra.copy(
70-
path.join(__dirname, 'node_modules', `tree-sitter-${language}`, 'src', 'node-types.json'),
71-
path.join(__dirname, 'src', 'assets', 'node', `tree-sitter-${language}`, 'node-types.json')
39+
path.join(__dirname, 'node_modules', 'faiss-node','build', 'Release'),
40+
path.join(__dirname, 'src', 'assets', 'node', 'faiss-node', 'build')
7241
)
73-
})
42+
}
43+
if(os.platform() === 'darwin'){
44+
return new Promise((resolve, reject)=>{
45+
rimrafSync(
46+
path.join(__dirname,'node_modules','faiss-node','deps','faiss')
47+
)
48+
const command = 'git clone -b v1.7.4 --depth 1 https://github.com/facebookresearch/faiss.git deps/faiss && npm i cmake-js && npm run build'
49+
exec(command, { cwd: path.join(__dirname,'node_modules','faiss-node') }, (error, stdout, stderr) => {
50+
if (error) {
51+
return reject(`执行的命令出错: ${error}`)
52+
}
53+
copyFaiss().then(resolve)
54+
});
55+
})
56+
}
57+
else {
58+
await copyFaiss()
59+
}
7460
}
7561

76-
const taskFunc = [
77-
()=>{
78-
return Promise.all([
79-
buildLib({
80-
command: 'npx prebuild -r electron -t 26.0.0 --strip',
81-
cwd: path.join(__dirname, 'src', 'deps','tree-sitter'),
82-
destination: path.join(__dirname, 'src', 'assets', 'node', 'tree-sitter')
83-
}),
84-
...buildNodeModulesLib()
85-
])
86-
},
87-
()=>{
88-
return Promise.all([
89-
fsExtra.copy(
90-
path.join(__dirname, 'node_modules', 'faiss-node','build', 'Release'),
91-
path.join(__dirname, 'src', 'assets', 'node', 'faiss-node', 'build')
92-
),
93-
...copyNodeTypes()
94-
])
95-
}
96-
]
62+
// const taskFunc = [
63+
// buildFaiss,
64+
// ()=>{
65+
// return Promise.all(
66+
// ['javascript','cpp','go','java','php','python','ruby','rust','scala','markdown','html','solidity','kotlin'].map(language=> buildWasm({languageName: language}))
67+
// )
68+
// }
69+
// ]
70+
//
71+
// serialPrommise(taskFunc).then().catch(err=>{
72+
// console.log(err);
73+
// })
9774

98-
serialPrommise(taskFunc).then().catch(err=>{
99-
console.log(err);
100-
})
75+
buildFaiss().then()

0 commit comments

Comments
 (0)