aboutsummaryrefslogtreecommitdiffstats
path: root/qt-lib/src/core-api.ts
blob: 85b168d95faac4d1c02ce956e6617614e7728931 (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
117
118
119
120
121
122
123
124
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

import * as vscode from 'vscode';
import { CORE_EXTENSION_ID } from './constants';

export interface QtAdditionalPath {
  name?: string | undefined;
  path: string;
  isVCPKG?: boolean;
}

// Implement sorter for QtAdditionalPath
export function compareQtAdditionalPath(
  a: QtAdditionalPath,
  b: QtAdditionalPath
): number {
  if (a.name === undefined && b.name === undefined) {
    return a.path.localeCompare(b.path);
  }
  if (a.name === undefined) {
    return -1;
  }
  if (b.name === undefined) {
    return 1;
  }
  return a.name.localeCompare(b.name);
}

export type ConfigType =
  | string
  | QtAdditionalPath[]
  | QtWorkspaceType
  | undefined;

export type QtWorkspaceConfig = Map<string, ConfigType>;

type MessageConfigs = Set<string>;
export class QtWorkspaceConfigMessage {
  workspaceFolder: vscode.WorkspaceFolder | string;
  config: MessageConfigs;
  constructor(folder?: vscode.WorkspaceFolder | string) {
    this.workspaceFolder = folder ?? 'global';
    this.config = new Set() as MessageConfigs;
  }
  toString(): string {
    const configs = Array.from(this.config).join(', ');
    let folder: vscode.WorkspaceFolder | string;
    if (typeof this.workspaceFolder === 'string') {
      folder = this.workspaceFolder;
    } else {
      folder = this.workspaceFolder.name;
    }
    return `[${folder}]: ${configs}`;
  }
}

export enum QtWorkspaceType {
  CMakeExt = 'CMakeExt',
  CMakeCMD = 'CMakeCMD',
  PythonExt = 'PythonExt'
}

export type QtPathsData = Map<string, string>;

export class QtInfo {
  data: QtPathsData;

  constructor(
    public readonly qtPathsBin: string,
    public name?: string,
    public isVCPKG?: boolean
  ) {
    this.data = new Map() as QtPathsData;
  }
  public get(key: string): string | undefined {
    return this.data.get(key);
  }
  public set(key: string, value: string): void {
    this.data.set(key, value);
  }
}

export interface CoreAPI {
  notify(config: QtWorkspaceConfigMessage): void;
  getValue<T>(
    folder: vscode.WorkspaceFolder | string,
    key: string
  ): T | undefined;
  setValue(
    folder: vscode.WorkspaceFolder | string,
    key: string,
    value: ConfigType
  ): void;
  onValueChanged: vscode.Event<QtWorkspaceConfigMessage>;
  getQtInfo(qtPathsExecutable: QtAdditionalPath): QtInfo | undefined;
  getQtInfoFromPath(qtPathsExe: string): QtInfo | undefined;
  reset(): void;
}

export async function getCoreApi(): Promise<CoreAPI | undefined> {
  const extension = vscode.extensions.getExtension(
    `theqtcompany.${CORE_EXTENSION_ID}`
  );
  if (!extension) {
    console.error(`[theqtcompany.${CORE_EXTENSION_ID}] is not installed`);
    return undefined;
  }
  let exports: CoreAPI | undefined;
  if (!extension.isActive) {
    try {
      exports = (await extension.activate()) as CoreAPI;
    } catch (e) {
      console.error(
        `Failed to activate [theqtcompany.${CORE_EXTENSION_ID}]`,
        e
      );
      return undefined;
    }
  } else {
    exports = extension.exports as CoreAPI;
  }
  return exports;
}