-
Notifications
You must be signed in to change notification settings - Fork 29.7k
/
extensionsUtil.ts
127 lines (108 loc) · 5.78 KB
/
extensionsUtil.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IExtensionManifest, ExtensionKind, ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { ExtensionsRegistry } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import { getGalleryExtensionId } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { isNonEmptyArray } from 'vs/base/common/arrays';
import { IProductService } from 'vs/platform/product/common/productService';
export function prefersExecuteOnUI(manifest: IExtensionManifest, productService: IProductService, configurationService: IConfigurationService): boolean {
const extensionKind = getExtensionKind(manifest, productService, configurationService);
return (extensionKind.length > 0 && extensionKind[0] === 'ui');
}
export function prefersExecuteOnWorkspace(manifest: IExtensionManifest, productService: IProductService, configurationService: IConfigurationService): boolean {
const extensionKind = getExtensionKind(manifest, productService, configurationService);
return (extensionKind.length > 0 && extensionKind[0] === 'workspace');
}
export function canExecuteOnUI(manifest: IExtensionManifest, productService: IProductService, configurationService: IConfigurationService): boolean {
const extensionKind = getExtensionKind(manifest, productService, configurationService);
return extensionKind.some(kind => kind === 'ui');
}
export function canExecuteOnWorkspace(manifest: IExtensionManifest, productService: IProductService, configurationService: IConfigurationService): boolean {
const extensionKind = getExtensionKind(manifest, productService, configurationService);
return extensionKind.some(kind => kind === 'workspace');
}
export function canExecuteOnWeb(manifest: IExtensionManifest, productService: IProductService, configurationService: IConfigurationService): boolean {
const extensionKind = getExtensionKind(manifest, productService, configurationService);
return extensionKind.some(kind => kind === 'web');
}
export function getExtensionKind(manifest: IExtensionManifest, productService: IProductService, configurationService: IConfigurationService): ExtensionKind[] {
// check in config
let result = getConfiguredExtensionKind(manifest, configurationService);
if (typeof result !== 'undefined') {
return toArray(result);
}
// check product.json
result = getProductExtensionKind(manifest, productService);
if (typeof result !== 'undefined') {
return toArray(result);
}
// check the manifest itself
result = manifest.extensionKind;
if (typeof result !== 'undefined') {
return toArray(result);
}
// Not an UI extension if it has main
if (manifest.main) {
return ['workspace'];
}
// Not an UI extension if it has dependencies or an extension pack
if (isNonEmptyArray(manifest.extensionDependencies) || isNonEmptyArray(manifest.extensionPack)) {
return ['workspace'];
}
if (manifest.contributes) {
// Not an UI extension if it has no ui contributions
for (const contribution of Object.keys(manifest.contributes)) {
if (!isUIExtensionPoint(contribution)) {
return ['workspace'];
}
}
}
return ['ui', 'workspace'];
}
let _uiExtensionPoints: Set<string> | null = null;
function isUIExtensionPoint(extensionPoint: string): boolean {
if (_uiExtensionPoints === null) {
const uiExtensionPoints = new Set<string>();
ExtensionsRegistry.getExtensionPoints().filter(e => e.defaultExtensionKind !== 'workspace').forEach(e => {
uiExtensionPoints.add(e.name);
});
_uiExtensionPoints = uiExtensionPoints;
}
return _uiExtensionPoints.has(extensionPoint);
}
let _productExtensionKindsMap: Map<string, ExtensionKind | ExtensionKind[]> | null = null;
function getProductExtensionKind(manifest: IExtensionManifest, productService: IProductService): ExtensionKind | ExtensionKind[] | undefined {
if (_productExtensionKindsMap === null) {
const productExtensionKindsMap = new Map<string, ExtensionKind | ExtensionKind[]>();
if (productService.extensionKind) {
for (const id of Object.keys(productService.extensionKind)) {
productExtensionKindsMap.set(ExtensionIdentifier.toKey(id), productService.extensionKind[id]);
}
}
_productExtensionKindsMap = productExtensionKindsMap;
}
const extensionId = getGalleryExtensionId(manifest.publisher, manifest.name);
return _productExtensionKindsMap.get(ExtensionIdentifier.toKey(extensionId));
}
let _configuredExtensionKindsMap: Map<string, ExtensionKind | ExtensionKind[]> | null = null;
function getConfiguredExtensionKind(manifest: IExtensionManifest, configurationService: IConfigurationService): ExtensionKind | ExtensionKind[] | undefined {
if (_configuredExtensionKindsMap === null) {
const configuredExtensionKindsMap = new Map<string, ExtensionKind | ExtensionKind[]>();
const configuredExtensionKinds = configurationService.getValue<{ [key: string]: ExtensionKind | ExtensionKind[] }>('remote.extensionKind') || {};
for (const id of Object.keys(configuredExtensionKinds)) {
configuredExtensionKindsMap.set(ExtensionIdentifier.toKey(id), configuredExtensionKinds[id]);
}
_configuredExtensionKindsMap = configuredExtensionKindsMap;
}
const extensionId = getGalleryExtensionId(manifest.publisher, manifest.name);
return _configuredExtensionKindsMap.get(ExtensionIdentifier.toKey(extensionId));
}
function toArray(extensionKind: ExtensionKind | ExtensionKind[]): ExtensionKind[] {
if (Array.isArray(extensionKind)) {
return extensionKind;
}
return [extensionKind];
}