blob: 1e325fa5afe851fb52a3e1cab63c236feb10fb44 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
import * as vscode from 'vscode';
import * as fs from 'fs';
import { createLogger, askForKitSelection, isError } from 'qt-lib';
import { CMAKE_GLOBAL_KITS_FILEPATH, Kit, KitManager } from '@/kit-manager';
const logger = createLogger('register-qt-path');
export function IsQtKit(kit: Kit) {
return IsQtInsKit(kit) || IsQtPathsKit(kit);
}
export function IsQtInsKit(kit: Kit) {
return getQtInsRoot(kit) !== undefined;
}
export function IsQtPathsKit(kit: Kit) {
return getQtPathsExe(kit) !== undefined;
}
export function getQtInsRoot(kit: Kit) {
// Keep VSCODE_QT_FOLDER for backward compatibility.
return (
kit.environmentVariables?.VSCODE_QT_INSTALLATION ??
kit.environmentVariables?.VSCODE_QT_FOLDER
);
}
export function getQtPathsExe(kit: Kit) {
return kit.environmentVariables?.VSCODE_QT_QTPATHS_EXE;
}
async function getActiveFolder() {
const activeFolder = await vscode.commands.executeCommand<string>(
'cmake.activeFolderPath'
);
if (activeFolder === '') {
logger.error('No active folder found.');
throw new Error('No active folder found.');
}
return vscode.workspace.getWorkspaceFolder(vscode.Uri.file(activeFolder));
}
async function getSelectedKitName(folder?: vscode.WorkspaceFolder) {
if (folder === undefined) {
folder = await getActiveFolder();
}
const selectedKit = await vscode.commands.executeCommand<string>(
'cmake.buildKit',
folder
);
logger.info('Selected CMake kit:', selectedKit);
if (!selectedKit || selectedKit === '__unspec__' || selectedKit === '') {
return undefined;
}
return selectedKit;
}
export async function getSelectedKit(
folder?: vscode.WorkspaceFolder,
silent = false
) {
if (folder === undefined) {
folder = await getActiveFolder();
}
const selectedKitName = await getSelectedKitName(folder);
if (selectedKitName === undefined) {
if (!silent) {
askForKitSelection();
}
return undefined;
}
const addtionalKits = vscode.workspace
.getConfiguration('cmake')
.get<string[]>('additionalKits');
const workspaceFolderKitsPath =
folder !== undefined
? KitManager.getCMakeWorkspaceKitsFilepath(folder)
: '';
const kitFiles = [workspaceFolderKitsPath, CMAKE_GLOBAL_KITS_FILEPATH];
if (addtionalKits) {
kitFiles.push(...addtionalKits);
}
for (const file of kitFiles) {
if (!fs.existsSync(file)) {
continue;
}
const contentPromise = fs.promises.readFile(file, 'utf8');
let kits: Kit[] = [];
try {
kits = JSON.parse(await contentPromise) as Kit[];
} catch (error) {
if (isError(error)) {
logger.error('Failed to parse kits file:', error.message);
}
}
const selectedQtKit = kits.find((kit) => kit.name === selectedKitName);
if (selectedQtKit) {
return selectedQtKit;
}
}
// Note: If a workspace is added to a workspacefile, the below message may be
// shown. Becase cmake.buildKit at the beggining if this function is called
// before the cmake extension resolves the cmake kit in the newly added
// workspace folder.
// TODO: Wait until the cmake extension resolves the cmake kit.
// TODO: Make this error silent in some cases.
const errorMessage = selectedKitName + ' is not a valid Qt kit.';
logger.error(errorMessage);
void vscode.window.showErrorMessage(errorMessage);
return undefined;
}
|