2023-05-31 16:10:53 +08:00
|
|
|
// src/contributes/commandsBase.ts
|
2023-05-31 16:10:53 +08:00
|
|
|
|
2023-07-06 07:42:44 +08:00
|
|
|
import { UiUtilWrapper } from "../util/uiUtil";
|
2023-05-31 16:10:53 +08:00
|
|
|
import { runCommand } from "../util/commonUtil";
|
2023-06-12 08:39:02 +08:00
|
|
|
import { logger } from "../util/logger";
|
2023-07-06 07:42:44 +08:00
|
|
|
import path from "path";
|
2023-05-31 16:10:53 +08:00
|
|
|
|
2023-07-06 07:42:44 +08:00
|
|
|
let pipxPathStatus = '';
|
|
|
|
let devchatStatus = '';
|
|
|
|
|
|
|
|
function locateCommand(command): string | undefined {
|
|
|
|
try {
|
2023-07-06 07:42:44 +08:00
|
|
|
// split lines and choose first line
|
|
|
|
const binPaths = runCommand(`where ${command}`).toString().trim().split('\n');
|
|
|
|
return binPaths[0].trim();
|
2023-07-06 07:42:44 +08:00
|
|
|
} catch (error) {
|
|
|
|
try {
|
2023-07-06 07:42:44 +08:00
|
|
|
const binPaths = runCommand(`which ${command}`).toString().trim().split('\n');
|
|
|
|
return binPaths[0].trim();
|
2023-07-06 07:42:44 +08:00
|
|
|
} catch (error) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 16:10:53 +08:00
|
|
|
|
2023-06-13 11:56:03 +08:00
|
|
|
export function checkDevChatDependency(pythonCommand: string): boolean {
|
2023-07-06 07:42:44 +08:00
|
|
|
let pipxBinPath: string | undefined = undefined;
|
2023-06-12 08:39:02 +08:00
|
|
|
try {
|
2023-06-13 11:56:03 +08:00
|
|
|
const binPath = getPipxEnvironmentPath(pythonCommand);
|
2023-07-06 07:42:44 +08:00
|
|
|
pipxBinPath = binPath;
|
2023-06-12 08:39:02 +08:00
|
|
|
|
|
|
|
if (binPath) {
|
|
|
|
updateEnvironmentPath(binPath);
|
2023-07-06 07:42:44 +08:00
|
|
|
|
|
|
|
const error_status = `Updated pipx environment path.`;
|
|
|
|
if (pipxPathStatus !== error_status) {
|
|
|
|
logger.channel()?.info(error_status);
|
|
|
|
pipxPathStatus = error_status;
|
|
|
|
}
|
2023-06-12 08:39:02 +08:00
|
|
|
} else {
|
2023-07-06 07:42:44 +08:00
|
|
|
const error_status = `Failed to obtain the pipx environment path.`;
|
|
|
|
if (pipxPathStatus !== error_status) {
|
2023-07-06 07:42:44 +08:00
|
|
|
logger.channel()?.warn(error_status);
|
2023-07-06 07:42:44 +08:00
|
|
|
logger.channel()?.show();
|
|
|
|
pipxPathStatus = error_status;
|
|
|
|
}
|
2023-07-06 07:42:44 +08:00
|
|
|
|
|
|
|
return false;
|
2023-06-12 08:39:02 +08:00
|
|
|
}
|
2023-06-16 09:05:10 +08:00
|
|
|
} catch (error) {
|
|
|
|
// DevChat dependency check failed
|
|
|
|
// log out detail error message
|
2023-07-06 07:42:44 +08:00
|
|
|
const error_status = `Failed to check DevChat dependency due to error: ${error}`;
|
|
|
|
if (pipxPathStatus !== error_status) {
|
2023-07-06 07:42:44 +08:00
|
|
|
logger.channel()?.warn(error_status);
|
2023-07-06 07:42:44 +08:00
|
|
|
logger.channel()?.show();
|
|
|
|
pipxPathStatus = error_status;
|
|
|
|
}
|
2023-07-06 07:42:44 +08:00
|
|
|
|
|
|
|
return false;
|
2023-06-16 09:05:10 +08:00
|
|
|
}
|
2023-06-15 21:20:52 +08:00
|
|
|
|
2023-06-16 09:05:10 +08:00
|
|
|
try {
|
2023-06-15 21:20:52 +08:00
|
|
|
// Check if DevChat is installed
|
2023-07-06 07:42:44 +08:00
|
|
|
const pipxDevChat = path.join(pipxBinPath!, 'devchat');
|
2023-07-06 09:06:48 +08:00
|
|
|
runCommand(`"${pipxDevChat}" --help`);
|
2023-07-06 07:42:44 +08:00
|
|
|
|
2023-07-06 07:42:44 +08:00
|
|
|
UiUtilWrapper.updateConfiguration('DevChat', 'DevChatPath', pipxDevChat);
|
2023-07-06 07:42:44 +08:00
|
|
|
const error_status = `DevChat has installed.`;
|
|
|
|
if (devchatStatus !== error_status) {
|
|
|
|
logger.channel()?.info(error_status);
|
|
|
|
devchatStatus = error_status;
|
|
|
|
}
|
|
|
|
|
2023-06-15 21:20:52 +08:00
|
|
|
return true;
|
2023-06-16 09:05:10 +08:00
|
|
|
} catch(error) {
|
2023-07-06 07:42:44 +08:00
|
|
|
const error_status = `Failed to check DevChat dependency due to error: ${error}`;
|
|
|
|
if (devchatStatus !== error_status) {
|
2023-07-06 07:42:44 +08:00
|
|
|
logger.channel()?.warn(error_status);
|
2023-07-06 07:42:44 +08:00
|
|
|
logger.channel()?.show();
|
|
|
|
devchatStatus = error_status;
|
|
|
|
}
|
|
|
|
|
2023-06-12 08:39:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-31 16:10:53 +08:00
|
|
|
}
|
|
|
|
|
2023-07-06 07:42:44 +08:00
|
|
|
function getDefaultPythonCommand(): string | undefined {
|
2023-06-13 08:32:22 +08:00
|
|
|
try {
|
|
|
|
runCommand('python3 -V');
|
2023-07-06 07:42:44 +08:00
|
|
|
return locateCommand('python3');
|
2023-06-13 08:32:22 +08:00
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
const version = runCommand('python -V');
|
|
|
|
if (version.includes('Python 3')) {
|
2023-07-06 07:42:44 +08:00
|
|
|
return locateCommand('python');
|
2023-06-13 08:32:22 +08:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
} catch (error) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 07:42:44 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 07:42:44 +08:00
|
|
|
export function getPipxEnvironmentPath(pythonCommand: string): string | undefined {
|
2023-06-12 08:39:02 +08:00
|
|
|
// Get pipx environment
|
2023-07-06 07:42:44 +08:00
|
|
|
try {
|
|
|
|
const pipxEnvOutput = runCommand(`"${pythonCommand}" -m pipx environment`).toString();
|
|
|
|
const binPathRegex = /PIPX_BIN_DIR=\s*(.*)/;
|
|
|
|
|
|
|
|
// Get BIN path from pipx environment
|
|
|
|
const match = pipxEnvOutput.match(binPathRegex);
|
|
|
|
if (match && match[1]) {
|
|
|
|
return match[1];
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return undefined;
|
2023-06-12 08:39:02 +08:00
|
|
|
}
|
2023-05-31 16:10:53 +08:00
|
|
|
}
|
2023-05-31 16:10:53 +08:00
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
function updateEnvironmentPath(binPath: string): void {
|
2023-06-12 08:39:02 +08:00
|
|
|
// Add BIN path to PATH
|
2023-07-06 07:42:44 +08:00
|
|
|
if (process.env.PATH?.indexOf(binPath) === undefined || process.env.PATH?.indexOf(binPath) < 0) {
|
2023-06-16 09:08:51 +08:00
|
|
|
process.env.PATH = `${binPath}:${process.env.PATH}`;
|
|
|
|
logger.channel()?.info(`Added ${binPath} to PATH.`);
|
|
|
|
}
|
2023-05-31 16:10:53 +08:00
|
|
|
}
|