aboutsummaryrefslogtreecommitdiffstats
path: root/qt-core/src/qtpaths.ts
blob: a0389d38e08cdad3e4b649a283e78997b08cf585 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

import * as vscode from 'vscode';

import {
  AdditionalQtPathsName,
  createLogger,
  generateDefaultQtPathsName,
  GlobalWorkspace,
  QtAdditionalPath,
  telemetry
} from 'qt-lib';
import { convertAdditionalQtPaths, getConfiguration } from '@/util';
import { onAdditionalQtPathsUpdated } from '@/installation-root';
import { coreAPI } from '@/extension';
import { EXTENSION_ID } from '@/constants';

const logger = createLogger('qtpaths');

export function registerQtByQtpaths() {
  return vscode.commands.registerCommand(
    `${EXTENSION_ID}.registerQtByQtpaths`,
    () => {
      telemetry.sendAction('registerQtByQtpaths');
      const options: vscode.OpenDialogOptions = {
        canSelectMany: false,
        openLabel: 'Select',
        title: 'Select a qtpaths or qmake executable',
        canSelectFiles: true,
        canSelectFolders: false
      };
      void vscode.window.showOpenDialog(options).then((selected) => {
        if (selected) {
          const selectedPath = selected[0]?.fsPath;
          if (!selectedPath) {
            return;
          }
          addQtPathToSettings({ path: selectedPath });
        }
      });
    }
  );
}

export function addQtPathToSettings(qtPath: QtAdditionalPath) {
  const config = getConfiguration();
  const additionalQtPaths = config.inspect<(string | object)[]>(
    AdditionalQtPathsName
  );
  let valueToSet: (string | object)[] = [];
  const info = coreAPI?.getQtInfo(qtPath);
  if (!info) {
    throw new Error(`Failed to get Qt info for ${qtPath.path}`);
  }
  const name = generateDefaultQtPathsName(info);
  const valueToAdd = { name: name, path: qtPath.path };
  if (additionalQtPaths?.globalValue) {
    additionalQtPaths.globalValue.push(valueToAdd);
    valueToSet = additionalQtPaths.globalValue;
  } else {
    logger.info(`${AdditionalQtPathsName} not found in the settings`);
    valueToSet = [valueToAdd];
  }
  logger.info(`Adding ${qtPath.path} to the settings`);
  void config.update(
    AdditionalQtPathsName,
    valueToSet,
    vscode.ConfigurationTarget.Global
  );
  const convertedValue = convertAdditionalQtPaths(valueToSet);
  onAdditionalQtPathsUpdated(convertedValue, GlobalWorkspace);
}