Skip to content

Commit 76d156b

Browse files
committed
fix:打包版本把python编译成exe,提高兼容性
1 parent 9a71cd6 commit 76d156b

File tree

2 files changed

+74
-19
lines changed

2 files changed

+74
-19
lines changed

.github/workflows/release.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,44 @@ on:
66
workflow_dispatch: # 允许手动触发
77

88
jobs:
9+
build-python-exe:
10+
runs-on: windows-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: '3.10'
19+
20+
- name: Install PyInstaller
21+
run: pip install pyinstaller
22+
23+
- name: Install Python dependencies
24+
run: pip install -r src/python/requirements.txt
25+
26+
- name: Build EXE
27+
run: pyinstaller --onefile --clean --noconsole --distpath dist src/python/rag.py
28+
29+
- name: Upload EXE artifact
30+
uses: actions/upload-artifact@v3
31+
with:
32+
name: rag.exe
33+
path: dist/rag.exe
34+
935
build-and-publish:
1036
runs-on: ubuntu-latest
1137
steps:
1238
- name: Checkout code
1339
uses: actions/checkout@v4
1440

41+
- name: Download EXE artifact
42+
uses: actions/download-artifact@v3
43+
with:
44+
name: rag.exe
45+
path: dist # 下载到 dist 目录
46+
1547
- name: Setup Node.js
1648
uses: actions/setup-node@v4
1749
with:

src/ragService.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@ import * as os from 'os';
1010
let EXTENSION_PATH: string = '';
1111

1212
const isDevMode = __dirname.includes('dist');
13-
const pythonScriptPath = isDevMode
14-
? path.join(__dirname, '..', 'src', 'python', 'rag.py') // Navigate from dist to src in dev
15-
: path.join(EXTENSION_PATH, 'src', 'python', 'rag.py');
13+
// 动态设置 Python 脚本或 EXE 路径
14+
const getPythonScriptPath = (extensionPath: string) => {
15+
if (isDevMode) {
16+
return path.join(extensionPath, 'src', 'python', 'rag.py'); // 开发模式:直接运行 Python 脚本
17+
} else {
18+
return path.join(extensionPath, 'dist', 'rag.exe'); // 发布模式:运行 EXE
19+
}
20+
};
1621

1722
// 配置常量 (use a function to resolve paths dynamically)
1823
export const CONFIG = {
1924
STORAGE_PATH: path.join(os.homedir(), 'CodeReDesignMemory', 'rag_storage'),
2025
PORT: 7111,
2126
LOCK_FILE: 'rag.lock',
22-
PYTHON_SCRIPT: pythonScriptPath
27+
PYTHON_SCRIPT: getPythonScriptPath(EXTENSION_PATH)
2328
};
2429

2530
// 类型定义
@@ -175,22 +180,39 @@ class RagService {
175180
}
176181

177182
private startPythonProcess(): ChildProcess {
178-
const pythonPath = process.platform === 'win32'
179-
? this.findWindowsPython()
180-
: this.findUnixPython();
181-
182-
// 通过命令行参数传递配置
183-
const pythonArgs = [
184-
CONFIG.PYTHON_SCRIPT,
185-
`--port=${CONFIG.PORT}`, // 传递端口参数
186-
`--storage_path=${CONFIG.STORAGE_PATH}` // 可选:传递其他参数
187-
];
183+
const scriptPath = CONFIG.PYTHON_SCRIPT;
184+
const isExe = scriptPath.endsWith('.exe');
185+
186+
if (!fs.access(scriptPath)) {
187+
throw new Error(`未找到脚本或可执行文件: ${scriptPath}`);
188+
}
188189

189-
return spawn(pythonPath, pythonArgs, {
190-
env: { ...process.env, PORT: CONFIG.PORT.toString() },
191-
stdio: 'pipe',
192-
shell: true // 启用shell解析路径
193-
});
190+
if (isExe) {
191+
// 直接运行 EXE
192+
return spawn(scriptPath, [
193+
`--port=${CONFIG.PORT}`,
194+
`--storage_path=${CONFIG.STORAGE_PATH}`
195+
], {
196+
env: { ...process.env, PORT: CONFIG.PORT.toString() },
197+
stdio: 'pipe',
198+
shell: true
199+
});
200+
} else {
201+
// 运行 Python 脚本
202+
const pythonPath = process.platform === 'win32'
203+
? this.findWindowsPython()
204+
: this.findUnixPython();
205+
206+
return spawn(pythonPath, [
207+
scriptPath,
208+
`--port=${CONFIG.PORT}`,
209+
`--storage_path=${CONFIG.STORAGE_PATH}`
210+
], {
211+
env: { ...process.env, PORT: CONFIG.PORT.toString() },
212+
stdio: 'pipe',
213+
shell: true
214+
});
215+
}
194216
}
195217

196218
private findUnixPython(): string {
@@ -263,6 +285,7 @@ class RagService {
263285
export const ragService = RagService.getInstance();
264286

265287
export async function activate(context: vscode.ExtensionContext) {
288+
EXTENSION_PATH = context.extensionPath;
266289
await ragService.start();
267290
context.subscriptions.push({
268291
dispose: () => ragService.stop()

0 commit comments

Comments
 (0)