2023-05-31 16:10:53 +08:00
|
|
|
// src/contributes/commandsBase.ts
|
2023-05-31 16:10:53 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
export function checkDevChatDependency(): boolean {
|
2023-06-12 08:39:02 +08:00
|
|
|
try {
|
|
|
|
const binPath = getPipxEnvironmentPath();
|
|
|
|
|
|
|
|
if (binPath) {
|
|
|
|
updateEnvironmentPath(binPath);
|
|
|
|
|
|
|
|
// Check if DevChat is installed
|
|
|
|
runCommand('devchat --help');
|
|
|
|
return true;
|
|
|
|
} else {
|
2023-06-13 10:45:11 +08:00
|
|
|
logger.channel()?.error(`Failed to obtain the pipx environment path. Attempting to install pipx.`);
|
2023-06-12 08:39:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// DevChat dependency check failed
|
|
|
|
// log out detail error message
|
2023-06-13 10:45:11 +08:00
|
|
|
logger.channel()?.error(`Failed to check DevChat dependency due to error: ${error}`);
|
2023-06-12 08:39:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-31 16:10:53 +08:00
|
|
|
}
|
|
|
|
|
2023-06-13 08:32:22 +08:00
|
|
|
export function getValidPythonCommand(): string | undefined {
|
|
|
|
try {
|
|
|
|
runCommand('python3 -V');
|
|
|
|
return 'python3';
|
|
|
|
} catch (error) {
|
|
|
|
try {
|
|
|
|
const version = runCommand('python -V');
|
|
|
|
if (version.includes('Python 3')) {
|
|
|
|
return 'python';
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
} catch (error) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:10:53 +08:00
|
|
|
export function getPipxEnvironmentPath(): string | null {
|
2023-06-12 08:39:02 +08:00
|
|
|
// Get pipx environment
|
|
|
|
const pipxEnvOutput = runCommand('python3 -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 null;
|
|
|
|
}
|
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
|
|
|
|
process.env.PATH = `${binPath}:${process.env.PATH}`;
|
2023-05-31 16:10:53 +08:00
|
|
|
}
|