diff --git a/src/contributes/commands.ts b/src/contributes/commands.ts index 9424273..a24a866 100644 --- a/src/contributes/commands.ts +++ b/src/contributes/commands.ts @@ -6,17 +6,32 @@ import * as childProcess from 'child_process'; import { DevChatViewProvider } from '../panel/devchatView'; import ExtensionContextHolder from '../util/extensionContext'; +import * as process from 'process'; -export function checkDevChatDependency() { - // 执行系统命令,检查依赖程序是否已经安装 - try { - const result = childProcess.execSync('devchat --help'); - // 命令执行成功,依赖程序已经安装 - return true; - } catch (error) { - // 命令执行失败,依赖程序未安装 - return false; - } +export function checkDevChatDependency(): boolean { + try { + // 获取pipx环境信息 + const pipxEnvOutput = childProcess.execSync('python3 -m pipx environment').toString(); + const binPathRegex = /PIPX_BIN_DIR=\s*(.*)/; + + // 提取BIN路径 + const match = pipxEnvOutput.match(binPathRegex); + 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() { diff --git a/src/extension.ts b/src/extension.ts index 953a61c..304dab5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -85,7 +85,7 @@ function activate(context: vscode.ExtensionContext) { if (devchatStatus === 'not ready') { // auto install devchat const terminal = vscode.window.createTerminal("DevChat Install"); - terminal.sendText("pip3 install --upgrade devchat"); + terminal.sendText(`python ${context.extensionUri.fsPath+"/tools/install.py"}`); terminal.show(); devchatStatus = 'waiting install devchat'; } diff --git a/tools/install.py b/tools/install.py new file mode 100644 index 0000000..090cf41 --- /dev/null +++ b/tools/install.py @@ -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() \ No newline at end of file