Check cmake command on client side

This commit is contained in:
全卓 2022-11-27 15:43:48 +08:00
parent 1140368757
commit 10ffc04c10
3 changed files with 53 additions and 4 deletions

View File

@ -1,12 +1,15 @@
import * as path from 'path';
import { getConfigLogLevel, Logger } from './logging';
import { workspace, ExtensionContext } from 'vscode';
import { workspace, ExtensionContext, window, commands } from 'vscode';
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';
import { existsSync } from 'fs';
import { isAbsolute } from 'path';
import { which } from './utils';
export const SERVER_ID = 'cmakeIntelliSence';
@ -14,15 +17,33 @@ export const SERVER_NAME = 'CMake Language Server';
let client: LanguageClient;
export function activate(context: ExtensionContext) {
async function checkCMakePath(cmakePath: string) {
if (!existsSync(cmakePath)) {
if (which(cmakePath) === null) {
let select = await window.showErrorMessage(`Can not find cmakePath: ${cmakePath}`,
'Open Settings', 'Ignore');
if (select === 'Open Settings') {
commands.executeCommand('workbench.action.openSettings', 'cmakeIntelliSence.cmakePath');
}
}
}
}
export async function activate(context: ExtensionContext) {
const config = workspace.getConfiguration(SERVER_ID);
const logger = new Logger();
logger.setLogLevel(getConfigLogLevel(config));
checkCMakePath(config.cmakePath);
context.subscriptions.push(workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration(`${SERVER_ID}.loggingLevel`)) {
logger.setLogLevel(getConfigLogLevel(config));
}
if (e.affectsConfiguration(`${SERVER_ID}.cmakePath`)) {
const cmakePath = workspace.getConfiguration(SERVER_ID).get<string>('cmakePath');
checkCMakePath(cmakePath);
}
}));
const serverModule = context.asAbsolutePath(

26
client/src/utils.ts Normal file
View File

@ -0,0 +1,26 @@
import * as os from 'os';
import * as path from 'path';
import { existsSync } from 'fs';
export function which(cmd: string): string {
let command: string;
let pathEnvSep: string;
if (os.type() === 'Windows_NT') {
if (!cmd.endsWith('.exe')) {
command = cmd + ".exe";
}
pathEnvSep = ';';
} else {
command = cmd;
pathEnvSep = ':';
}
for (const dir of process.env.PATH.split(pathEnvSep)) {
const absPath: string = dir + path.sep + command;
if (existsSync(absPath)) {
return absPath;
}
}
return null;
}

View File

@ -94,11 +94,13 @@ export function getIncludeFileUri(baseDir: URI, includeFileName: string): URI {
return null;
}
function which(cmd: string): string {
export function which(cmd: string): string {
let command: string;
let pathEnvSep: string;
if (os.type() === 'Windows_NT') {
command = cmd + ".exe";
if (!cmd.endsWith('.exe')) {
command = cmd + ".exe";
}
pathEnvSep = ';';
} else {
command = cmd;