Merge pull request #103 from covespace/support_pipx_devchat

Fix devchat not found error
This commit is contained in:
boob.yang 2023-05-19 16:44:59 +08:00 committed by GitHub
commit 0b0945cf3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 104 additions and 15 deletions

View File

@ -2,7 +2,7 @@
"name": "devchat", "name": "devchat",
"displayName": "DevChat", "displayName": "DevChat",
"description": "Write prompts, not code", "description": "Write prompts, not code",
"version": "0.0.42", "version": "0.0.45",
"icon": "assets/devchat.png", "icon": "assets/devchat.png",
"publisher": "merico", "publisher": "merico",
"engines": { "engines": {
@ -23,6 +23,7 @@
"dist/*", "dist/*",
"bin/*", "bin/*",
"assets/*", "assets/*",
"tools/*",
"workflows/*", "workflows/*",
"LICENSE", "LICENSE",
"README.md" "README.md"

View File

@ -6,17 +6,32 @@ import * as childProcess from 'child_process';
import { DevChatViewProvider } from '../panel/devchatView'; import { DevChatViewProvider } from '../panel/devchatView';
import ExtensionContextHolder from '../util/extensionContext'; import ExtensionContextHolder from '../util/extensionContext';
import * as process from 'process';
export function checkDevChatDependency() { export function checkDevChatDependency(): boolean {
// 执行系统命令,检查依赖程序是否已经安装 try {
try { // 获取pipx环境信息
const result = childProcess.execSync('devchat --help'); const pipxEnvOutput = childProcess.execSync('python3 -m pipx environment').toString();
// 命令执行成功,依赖程序已经安装 const binPathRegex = /PIPX_BIN_DIR=\s*(.*)/;
return true;
} catch (error) { // 提取BIN路径
// 命令执行失败,依赖程序未安装 const match = pipxEnvOutput.match(binPathRegex);
return false; if (match && match[1]) {
} const binPath = match[1];
// 将BIN路径添加到环境变量中
process.env.PATH = `${binPath}:${process.env.PATH}`;
// 检查devchat是否已经安装
childProcess.execSync('devchat --help');
return true;
} else {
return false;
}
} catch (error) {
// 命令执行失败,依赖程序未安装或其他异常
return false;
}
} }
export async function checkOpenAiAPIKey() { export async function checkOpenAiAPIKey() {

View File

@ -85,7 +85,7 @@ function activate(context: vscode.ExtensionContext) {
if (devchatStatus === 'not ready') { if (devchatStatus === 'not ready') {
// auto install devchat // auto install devchat
const terminal = vscode.window.createTerminal("DevChat Install"); const terminal = vscode.window.createTerminal("DevChat Install");
terminal.sendText("pip3 install --upgrade devchat"); terminal.sendText(`python ${context.extensionUri.fsPath+"/tools/install.py"}`);
terminal.show(); terminal.show();
devchatStatus = 'waiting install devchat'; devchatStatus = 'waiting install devchat';
} }

View File

@ -80,6 +80,18 @@ class DevChat {
reject({ code, stdout, stderr }); reject({ code, stdout, stderr });
} }
}); });
// Add error event listener to handle command not found exception
this.childProcess.on('error', (error: any) => {
if (error.code === 'ENOENT') {
logger.channel()?.error(`Command not found: ${command}`);
logger.channel()?.show();
} else {
logger.channel()?.error(`Error occurred: ${error.message}`);
logger.channel()?.show();
}
reject({ code: error.code, stdout: "", stderr: error.message });
});
}); });
}; };
@ -290,6 +302,7 @@ class DevChat {
}, },
}, (partialResponse: string) => { }); }, (partialResponse: string) => { });
logger.channel()?.info(`Finish devchat with args: ${args.join(" ")}`);
if (stderr) { if (stderr) {
logger.channel()?.error(`Error getting log: ${stderr}`); logger.channel()?.error(`Error getting log: ${stderr}`);
logger.channel()?.show(); logger.channel()?.show();

60
tools/install.py Normal file
View File

@ -0,0 +1,60 @@
import os
import subprocess
import sys
def check_pipx_installed():
try:
subprocess.run(["pipx", "--version"], check=True)
return True
except Exception as e:
return False
def install_pipx():
print("Installing pipx...")
try:
subprocess.run(["python3", "-m", "pip", "install", "pipx"], check=True)
print("pipx installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing pipx:", e, file=sys.stderr)
sys.exit(1)
def add_pipx_to_path():
print("Adding pipx to PATH...")
subprocess.run(["python3", "-m", "pipx", "ensurepath"], check=True)
result = subprocess.run(["python3", "-m", "pipx", "environment"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
pipx_path_line = [line for line in result.stdout.splitlines() if "PIPX_BIN_DIR" in line]
if pipx_path_line:
pipx_path = pipx_path_line[0].split('=')[-1].strip()
os.environ["PATH"] += os.pathsep + pipx_path
print("pipx path added to environment variables.")
else:
print("Error: Could not find pipx path in environment output.", file=sys.stderr)
sys.exit(1)
def install_devchat():
print("Installing devchat...")
try:
subprocess.run(["pipx", "install", "devchat"], check=True)
print("devchat installed successfully.")
except subprocess.CalledProcessError as e:
print("Error installing devchat:", e, file=sys.stderr)
sys.exit(1)
def upgrade_devchat():
print("Upgrading devchat...")
try:
subprocess.run(["pipx", "upgrade", "devchat"], check=True)
print("devchat upgraded successfully.")
except subprocess.CalledProcessError as e:
print("Error upgrading devchat:", e, file=sys.stderr)
sys.exit(1)
def main():
if not check_pipx_installed():
install_pipx()
add_pipx_to_path()
install_devchat()
upgrade_devchat()
if __name__ == "__main__":
main()