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.
This commit is contained in:
bobo.yang 2023-07-06 07:42:44 +08:00
parent 892c3638cc
commit d474957fe8
3 changed files with 57 additions and 6 deletions

View File

@ -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"

View File

@ -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.`);
}

View File

@ -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;
}