Skip to content

Commit

Permalink
Enable scanning user-defined kit files
Browse files Browse the repository at this point in the history
* Add `VSCODE_QT_FOLDER` to check qtFolder because checking the first
variable in the environment path is not reliable.
* Scan `cmake.additionalKits` as well.

Change-Id: Idf0d4baedd170983190962be7eae120ffd5da857
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
  • Loading branch information
OrkunTokdemir committed Mar 5, 2024
1 parent cc9a83b commit e6b5332
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/commands/detect-qt-cmake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function* generateCMakeKitsOfQtInstallationPath(
let newKit: Kit = {
name: qtpath.mangleQtInstallation(installation),
environmentVariables: {
VSCODE_QT_FOLDER: installation,
PATH: qtPathEnv
},
isTrusted: true,
Expand Down
67 changes: 51 additions & 16 deletions src/commands/register-qt-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import * as util from '../util/util';
import * as fs from 'fs';
import * as path from 'path';
import { stateManager } from '../state';
import { QtCMakeKits, updateCMakeKitsJson } from './detect-qt-cmake';
import { updateCMakeKitsJson } from './detect-qt-cmake';
import { Home, IsLinux, IsMacOS, IsWindows } from '../util/os';
import { Kit } from '../util/cmake-kit-files';
import { CMakeKitFiles, Kit } from '../util/cmake-kit-files';

export const RegisterQtCommandId = 'vscode-qt-tools.registerQt';
let RegisterQtCommandTitle = '';
Expand Down Expand Up @@ -171,23 +171,58 @@ export async function getSelectedQtInstallationPath(): Promise<string> {
});
throw new Error('No CMake kit selected');
}
const content = fs.readFileSync(QtCMakeKits.qtKitsFilePath, 'utf8');
const kits = JSON.parse(content) as Kit[];
const selectedQtKit = kits.find((kit) => kit.name === selectedCMakeKit);
let selectedQtKitPath = '';
if (selectedQtKit === undefined) {
throw new Error('Selected CMake kit not found');
const addtionalKits = vscode.workspace
.getConfiguration('cmake')
.get<string[]>('additionalKits');

const kitFiles = [CMakeKitFiles.CMAKE_KITS_FILEPATH];
if (addtionalKits) {
kitFiles.push(...addtionalKits);
}
if (selectedQtKit.environmentVariables.PATH === undefined) {
throw new Error('Selected Qt installation path not found');
const promises = kitFiles.map(async (file) => {
const content = await fs.promises.readFile(file, 'utf8');
const kits = JSON.parse(content) as Kit[];
const selectedQtKit = kits.find((kit) => kit.name === selectedCMakeKit);

if (selectedQtKit === undefined) {
return null;
}
if (selectedQtKit.environmentVariables.VSCODE_QT_FOLDER === undefined) {
void vscode.window.showErrorMessage(
'"VSCODE_QT_FOLDER" environment variable is not set for "' +
selectedCMakeKit +
'".'
);
return null;
}

const selectedQtKitPath =
selectedQtKit.environmentVariables.VSCODE_QT_FOLDER;

if (fs.existsSync(selectedQtKitPath)) {
return selectedQtKitPath;
}
void vscode.window.showErrorMessage(
`"${selectedQtKitPath}" does not exist in "${selectedCMakeKit}".`
);
});

const results = await Promise.all(promises);
const validResults = results.filter((result) => result !== null);

if (validResults.length === 1) {
return validResults[0] ?? '';
}
selectedQtKitPath = selectedQtKit.environmentVariables.PATH.split(
path.delimiter
)[0];

if (!fs.existsSync(selectedQtKitPath)) {
throw new Error('Selected Qt installation path does not exist');
if (validResults.length > 1) {
void vscode.window.showErrorMessage(
'Multiple CMake kits with the same name are found for "' +
selectedCMakeKit +
'".'
);
return '';
}

return selectedQtKitPath;
void vscode.window.showErrorMessage(selectedCMakeKit + ' is not found.');
return '';
}

0 comments on commit e6b5332

Please sign in to comment.