From d474957fe8c8b6b6cabd543c5a73ae29e5ca6d47 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Thu, 6 Jul 2023 07:42:44 +0800 Subject: [PATCH] Add Python Path configuration and command to package.json - Added a new configuration 'DevChat.PythonPath' in package.json. - This configuration allows users to specify the location of the Python binary. - Also added a new command 'DevChat.PythonPath' to the command palette. - Updated commandsBase.ts to use the Python Path from the configuration. - If the Python Path is not set in the configuration, it will use the default Python command. - If the DevChat status is 'Missing required dependency: Python3', the status bar item will trigger the 'devchat.PythonPath' command. --- package.json | 18 ++++++++++++++++ src/contributes/commandsBase.ts | 37 ++++++++++++++++++++++++++++----- src/panel/statusBarView.ts | 8 ++++++- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index df3d039..7762a5e 100644 --- a/package.json +++ b/package.json @@ -131,6 +131,19 @@ } }, "description": "Where is the devchat binary located?" + }, + "DevChat.PythonPath": { + "type": "string", + "default": "", + "input": { + "type": "file", + "filter": { + "All files": [ + "python*" + ] + } + }, + "description": "Where is the python binary located?" } } }, @@ -196,6 +209,11 @@ "title": "Input Access Key", "category": "DevChat" }, + { + "command": "DevChat.PythonPath", + "title": "Set Python Path", + "category": "DevChat" + }, { "command": "devchat.openChatPanel", "title": "DevChat" diff --git a/src/contributes/commandsBase.ts b/src/contributes/commandsBase.ts index 41b22af..659e97d 100644 --- a/src/contributes/commandsBase.ts +++ b/src/contributes/commandsBase.ts @@ -1,5 +1,6 @@ // src/contributes/commandsBase.ts +import { UiUtilWrapper } from "../util/uiUtil"; import { runCommand } from "../util/commonUtil"; import { logger } from "../util/logger"; @@ -29,15 +30,23 @@ export function checkDevChatDependency(pythonCommand: string): boolean { } } -export function getValidPythonCommand(): string | undefined { +function getDefaultPythonCommand(): string | undefined { try { runCommand('python3 -V'); - return 'python3'; + try { + return runCommand('where python3').toString().trim(); + } catch (error) { + return runCommand('which python3').toString().trim(); + } } catch (error) { try { const version = runCommand('python -V'); if (version.includes('Python 3')) { - return 'python'; + try { + return runCommand('where python').toString().trim(); + } catch (error) { + return runCommand('which python').toString().trim(); + } } return undefined; } catch (error) { @@ -46,9 +55,27 @@ export function getValidPythonCommand(): string | undefined { } } +export function getValidPythonCommand(): string | undefined { + try { + const pythonCommand = UiUtilWrapper.getConfiguration('DevChat', 'PythonPath'); + if (pythonCommand) { + return pythonCommand; + } + + const defaultPythonCommand = getDefaultPythonCommand(); + if (defaultPythonCommand) { + UiUtilWrapper.updateConfiguration('DevChat', 'PythonPath', defaultPythonCommand); + } + + return defaultPythonCommand; + } catch (error) { + return undefined; + } +} + export function getPipxEnvironmentPath(pythonCommand: string): string | null { // Get pipx environment - const pipxEnvOutput = runCommand(`${pythonCommand} -m pipx environment`).toString(); + const pipxEnvOutput = runCommand(`"${pythonCommand}" -m pipx environment`).toString(); const binPathRegex = /PIPX_BIN_DIR=\s*(.*)/; // Get BIN path from pipx environment @@ -62,7 +89,7 @@ export function getPipxEnvironmentPath(pythonCommand: string): string | null { function updateEnvironmentPath(binPath: string): void { // Add BIN path to PATH - if (process.env.PATH?.indexOf(binPath) === undefined) { + if (process.env.PATH?.indexOf(binPath) === undefined || process.env.PATH?.indexOf(binPath) < 0) { process.env.PATH = `${binPath}:${process.env.PATH}`; logger.channel()?.info(`Added ${binPath} to PATH.`); } diff --git a/src/panel/statusBarView.ts b/src/panel/statusBarView.ts index 8fd8430..dfdd070 100644 --- a/src/panel/statusBarView.ts +++ b/src/panel/statusBarView.ts @@ -18,7 +18,13 @@ export function createStatusBarItem(context: vscode.ExtensionContext): vscode.St if (devchatStatus !== 'ready') { statusBarItem.text = `$(warning)DevChat`; statusBarItem.tooltip = `${devchatStatus}`; - statusBarItem.command = undefined; + + if (devchatStatus === 'Missing required dependency: Python3') { + statusBarItem.command = "devchat.PythonPath"; + } else { + statusBarItem.command = undefined; + } + // set statusBarItem warning color return; }