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-05-31 16:10:53 +08:00
|
|
|
|
2023-07-06 07:42:44 +08:00
|
|
|
let devchatStatus = '';
|
|
|
|
|
2023-11-03 11:13:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
function locateCommand(command): string | undefined {
|
|
|
|
try {
|
|
|
|
// split lines and choose first line
|
|
|
|
const binPaths = runCommand(`where ${command}`).toString().trim().split('\n');
|
|
|
|
return binPaths[0].trim();
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
const binPaths = runCommand(`which ${command}`).toString().trim().split('\n');
|
|
|
|
return binPaths[0].trim();
|
|
|
|
} catch (error) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultPythonCommand(): string | undefined {
|
|
|
|
try {
|
|
|
|
runCommand('python3 -V');
|
|
|
|
return locateCommand('python3');
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
const version = runCommand('python -V');
|
|
|
|
if (version.includes('Python 3')) {
|
|
|
|
return locateCommand('python');
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
} catch (error) {
|
|
|
|
return 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 11:52:00 +08:00
|
|
|
export function checkDevChatDependency(showError: boolean = true): boolean {
|
2023-07-12 08:17:13 +08:00
|
|
|
let devChat: string | undefined = UiUtilWrapper.getConfiguration('DevChat', 'DevChatPath');
|
|
|
|
if (!devChat) {
|
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-10-23 22:15:35 +08:00
|
|
|
const expectVersion = 'DevChat 0.2.10';
|
2023-07-19 16:06:45 +08:00
|
|
|
const devchatVersion = runCommand(`"${devChat}" --version`).toString().trim();
|
|
|
|
if (devchatVersion < expectVersion) {
|
2023-07-25 12:35:49 +08:00
|
|
|
logger.channel()?.info(`devchat version: ${devchatVersion}, but expect version: ${expectVersion}`);
|
2023-07-19 16:06:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-12 17:37:00 +08:00
|
|
|
logger.channel()?.info("devchat has installed.")
|
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}`;
|
2023-07-07 08:56:28 +08:00
|
|
|
if (devchatStatus !== error_status && showError) {
|
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-05-31 16:10:53 +08:00
|
|
|
|