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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import untildify from 'untildify';
import {
Home,
IsUnix,
IsWindows,
QtInsRootConfigName,
AdditionalQtPathsName,
createLogger,
isPathToQtPathsOrQMake,
QtWorkspaceConfigMessage,
QtAdditionalPath,
IsMacOS,
telemetry
} from 'qt-lib';
import { EXTENSION_ID } from '@/constants';
import { coreAPI } from '@/extension';
import { convertAdditionalQtPaths } from '@/util';
const logger = createLogger('installation-root');
async function setDoNotAskForDefaultQtInstallationRoot(value: boolean) {
await vscode.workspace
.getConfiguration(EXTENSION_ID)
.update(
'doNotAskForDefaultQtInstallationRoot',
value,
vscode.ConfigurationTarget.Global
);
}
function getDoNotAskForDefaultQtInstallationRoot(): boolean {
return (
vscode.workspace
.getConfiguration(EXTENSION_ID)
.get<boolean>('doNotAskForDefaultQtInstallationRoot') ?? false
);
}
export function getCurrentGlobalQtInstallationRoot(): string {
const qtInsRootConfig =
getConfiguration().inspect<string>(QtInsRootConfigName);
const insRoot = qtInsRootConfig?.globalValue;
return insRoot ? untildify(insRoot) : '';
}
export function getCurrentGlobalAdditionalQtPaths(): QtAdditionalPath[] {
const config = getConfiguration().inspect<(string | object)[]>(
AdditionalQtPathsName
);
if (config?.globalValue) {
return convertAdditionalQtPaths(config.globalValue);
}
return [];
}
function getConfiguration() {
return vscode.workspace.getConfiguration(EXTENSION_ID);
}
export function checkDefaultQtInsRootPath() {
if (getDoNotAskForDefaultQtInstallationRoot()) {
return;
}
if (getCurrentGlobalQtInstallationRoot()) {
// Qt installation root is already set. No need to check for default path
return;
}
if (!IsUnix && !IsWindows) {
const errorMessage = 'Unsupported OS';
logger.error(errorMessage);
throw new Error(errorMessage);
}
const defaultQtInsRootName = 'Qt';
const unixDefaultPaths = [
path.join(Home, defaultQtInsRootName),
path.join(Home, 'dev', defaultQtInsRootName),
path.join('/', 'opt', defaultQtInsRootName)
];
const winRoot =
process.env.WINDIR !== undefined
? path.parse(process.env.WINDIR).root
: 'C:';
const winDefaultPaths = [
path.join(winRoot, defaultQtInsRootName),
path.join(winRoot, 'dev', defaultQtInsRootName)
];
if (process.env.USERNAME) {
winDefaultPaths.push(
path.join(winRoot, 'Users', process.env.USERNAME, defaultQtInsRootName)
);
}
if (process.env.USERPROFILE) {
winDefaultPaths.push(
path.join(process.env.USERPROFILE, defaultQtInsRootName)
);
}
if (process.env.SYSTEMDRIVE) {
winDefaultPaths.push(
path.join(process.env.SYSTEMDRIVE, defaultQtInsRootName)
);
}
if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
winDefaultPaths.push(
path.join(
process.env.HOMEDRIVE,
process.env.HOMEPATH,
defaultQtInsRootName
)
);
}
if (IsMacOS) {
unixDefaultPaths.push(path.join('/', 'Applications', defaultQtInsRootName));
unixDefaultPaths.push(
path.join(Home, 'Applications', defaultQtInsRootName)
);
}
const defaultPaths = IsUnix ? unixDefaultPaths : winDefaultPaths;
const foundDefaultPath = defaultPaths.find((defPath) =>
fs.existsSync(defPath)
);
if (!foundDefaultPath) {
return;
}
const setDefaultPathButtonMessage = 'Set Qt Installation Root';
const doNotShowAgainButtonMessage = 'Do not show again';
void vscode.window
.showInformationMessage(
`Qt installation root was found at "${foundDefaultPath}". Do you want to use it?`,
setDefaultPathButtonMessage,
doNotShowAgainButtonMessage
)
.then((response) => {
if (response === setDefaultPathButtonMessage) {
telemetry.sendConfig('useDefaultQtInstallationRoot');
void setGlobalQtInstallationRoot(foundDefaultPath);
} else if (response === doNotShowAgainButtonMessage) {
telemetry.sendConfig('doNotAskForDefaultQtInstallationRoot');
void setDoNotAskForDefaultQtInstallationRoot(true);
}
});
}
export async function registerQt() {
const options: vscode.OpenDialogOptions = {
canSelectMany: false,
openLabel: 'Select Qt installation root',
canSelectFiles: false,
canSelectFolders: true
};
const selectedQtInsRootUri = await vscode.window.showOpenDialog(options);
if (selectedQtInsRootUri?.[0] === undefined) {
return;
}
const selectedQtInsRoot = selectedQtInsRootUri[0].fsPath;
if (selectedQtInsRoot) {
void setGlobalQtInstallationRoot(selectedQtInsRoot);
}
return 0;
}
async function setGlobalQtInstallationRoot(qtInsRoot: string) {
logger.info(`Setting global Qt installation root to: ${qtInsRoot}`);
const config = vscode.workspace.getConfiguration(EXTENSION_ID);
await config.update(
QtInsRootConfigName,
qtInsRoot,
vscode.ConfigurationTarget.Global
);
}
export function onQtInsRootUpdated(
newQtInstallationRoot: string,
folder: vscode.WorkspaceFolder | string
) {
if (newQtInstallationRoot) {
if (!fs.existsSync(newQtInstallationRoot)) {
logger.warn(`The specified Qt installation path does not exist.`);
void vscode.window.showWarningMessage(
`The specified Qt installation path does not exist.`
);
}
}
logger.info(`Qt installation root updated: "${newQtInstallationRoot}"`);
const message = new QtWorkspaceConfigMessage(folder);
coreAPI?.setValue(folder, QtInsRootConfigName, newQtInstallationRoot);
message.config.add(QtInsRootConfigName);
logger.info(`Notifying coreAPI with message: ${message.toString()}`);
coreAPI?.notify(message);
}
export function onAdditionalQtPathsUpdated(
newPaths: QtAdditionalPath[],
folder: vscode.WorkspaceFolder | string
) {
for (const p of newPaths) {
if (!fs.existsSync(p.path)) {
const msg = `The specified additional Qt installation '${p.path}' does not exist.`;
logger.warn(msg);
void vscode.window.showWarningMessage(msg);
} else if (!isPathToQtPathsOrQMake(p.path)) {
const msg = `The specified additional Qt installation '${p.path}' does not point to qtpaths nor qmake.`;
logger.error(msg);
void vscode.window.showWarningMessage(msg);
}
}
logger.info('Additional Qt Paths updated: ' + JSON.stringify(newPaths));
const message = new QtWorkspaceConfigMessage(folder);
coreAPI?.setValue(folder, AdditionalQtPathsName, newPaths);
message.config.add(AdditionalQtPathsName);
logger.info(`Notifying coreAPI with message: ${message.toString()}`);
coreAPI?.notify(message);
}
|