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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
import * as vscode from 'vscode';
import * as path from 'path';
import { inVCPKGRoot, createLogger, telemetry } from 'qt-lib';
import { getFilenameWithoutExtension } from '@util/util';
import { EXTENSION_ID } from '@/constants';
import { getQtInsRoot, getSelectedKit } from '@cmd/register-qt-path';
import { coreAPI } from '@/extension';
const logger = createLogger('launch-variables');
export function registerlaunchTargetFilenameWithoutExtension() {
return vscode.commands.registerCommand(
`${EXTENSION_ID}.launchTargetFilenameWithoutExtension`,
async () => {
telemetry.sendAction('launchTargetFilenameWithoutExtension');
const launchTargetFilename = await vscode.commands.executeCommand<string>(
'cmake.launchTargetFilename'
);
if (!launchTargetFilename) {
return '';
}
return getFilenameWithoutExtension(launchTargetFilename);
}
);
}
export function registerbuildDirectoryName() {
return vscode.commands.registerCommand(
`${EXTENSION_ID}.buildDirectoryName`,
async () => {
telemetry.sendAction('buildDirectoryName');
const activeFolder = await vscode.commands.executeCommand<string>(
'cmake.activeFolderPath'
);
const buildDirectory = await vscode.commands.executeCommand<string>(
'cmake.buildDirectory',
activeFolder
);
return path.basename(buildDirectory);
}
);
}
export function qtDirCommand() {
return vscode.commands.registerCommand(`${EXTENSION_ID}.qtDir`, async () => {
telemetry.sendAction('qtDir');
const kit = await getSelectedKit();
if (!kit) {
return undefined;
}
const insRoot = getQtInsRoot(kit);
if (insRoot) {
return path.join(insRoot, 'bin');
}
const pathsExe = kit.environmentVariables?.VSCODE_QT_QTPATHS_EXE;
if (pathsExe) {
const isValidKey = (key: string) => {
const keysShouldStartWith = ['QT_INSTALL', 'QT_HOST'];
for (const k of keysShouldStartWith) {
if (key.startsWith(k)) {
return true;
}
}
return false;
};
const info = coreAPI?.getQtInfoFromPath(pathsExe);
const paths: string[] = [];
if (info) {
const keys = info.data;
const isInVCPKG = kit.toolchainFile && inVCPKGRoot(kit.toolchainFile);
for (const [key, value] of keys) {
if (!isValidKey(key)) {
continue;
}
if (value) {
paths.push(value);
// vcpkg spacial case
if (isInVCPKG) {
if (value.endsWith('bin')) {
const installPrefix = info.get('QT_INSTALL_PREFIX');
if (installPrefix) {
const installPrefixDebug = path.join(installPrefix, 'debug');
const newValue = value.replace(
installPrefix,
installPrefixDebug
);
paths.push(newValue);
}
}
}
}
}
}
const ret = paths.join(path.delimiter);
return ret;
}
logger.error('Could not find the selected Qt installation path');
return undefined;
});
}
// Keep this function due to the backward compatibility
export function registerKitDirectoryCommand() {
return vscode.commands.registerCommand(
`${EXTENSION_ID}.kitDirectory`,
async () => {
telemetry.sendAction('kitDirectory');
const kit = await getSelectedKit();
if (!kit) {
return undefined;
}
const insRoot = getQtInsRoot(kit);
if (insRoot) {
return insRoot;
}
const message = `Could not find VSCODE_QT_FOLDER in the selected kit: ${kit.name}`;
void vscode.window.showErrorMessage(message);
return undefined;
}
);
}
export function qpaPlatformPluginPathCommand() {
return vscode.commands.registerCommand(
`${EXTENSION_ID}.QT_QPA_PLATFORM_PLUGIN_PATH`,
async () => {
telemetry.sendAction('QT_QPA_PLATFORM_PLUGIN_PATH');
const kit = await getSelectedKit();
if (kit?.environmentVariables?.VSCODE_QT_QTPATHS_EXE) {
if (kit.toolchainFile && inVCPKGRoot(kit.toolchainFile)) {
const info = coreAPI?.getQtInfoFromPath(
kit.environmentVariables.VSCODE_QT_QTPATHS_EXE
);
if (info) {
const buildType =
await vscode.commands.executeCommand('cmake.buildType');
let pluginPath = info.get('QT_INSTALL_PLUGINS');
if (pluginPath) {
pluginPath = path.join(pluginPath, 'platforms');
if (buildType !== 'Debug') {
return pluginPath;
}
// If code reaches here, it means that the build type is Debug and
// we need to return the debug version of the plugin path
const installPrefix = info.get('QT_INSTALL_PREFIX');
if (installPrefix) {
const installPrefixDebug = path.join(installPrefix, 'debug');
if (pluginPath) {
const pluginPathDebug = pluginPath.replace(
path.normalize(installPrefix),
installPrefixDebug
);
return pluginPathDebug;
}
}
}
}
}
}
return process.env.QT_QPA_PLATFORM_PLUGIN_PATH ?? '';
}
);
}
export function qmlImportPathCommand() {
return vscode.commands.registerCommand(
`${EXTENSION_ID}.QML_IMPORT_PATH`,
async () => {
telemetry.sendAction('QML_IMPORT_PATH');
const kit = await getSelectedKit();
if (kit?.environmentVariables?.VSCODE_QT_QTPATHS_EXE) {
if (kit.toolchainFile && inVCPKGRoot(kit.toolchainFile)) {
const info = coreAPI?.getQtInfoFromPath(
kit.environmentVariables.VSCODE_QT_QTPATHS_EXE
);
if (info) {
const buildType =
await vscode.commands.executeCommand('cmake.buildType');
let importPath = info.get('QT_INSTALL_QML');
if (importPath) {
importPath = path.normalize(importPath);
if (buildType !== 'Debug') {
return importPath;
}
// If code reaches here, it means that the build type is Debug and
// we need to return the debug version of the import path
const installPrefix = info.get('QT_INSTALL_PREFIX');
if (installPrefix) {
const installPrefixDebug = path.join(installPrefix, 'debug');
if (importPath) {
const importPathDebug = importPath.replace(
path.normalize(installPrefix),
installPrefixDebug
);
return importPathDebug;
}
}
}
}
}
}
return process.env.QML_IMPORT_PATH ?? '';
}
);
}
|