Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/extensionManagement/browser/builtinExtensionsScannerService.ts
5240 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IBuiltinExtensionsScannerService, ExtensionType, IExtensionManifest, TargetPlatform, IExtension } from '../../../../platform/extensions/common/extensions.js';
7
import { isWeb, Language } from '../../../../base/common/platform.js';
8
import { IWorkbenchEnvironmentService } from '../../environment/common/environmentService.js';
9
import { IUriIdentityService } from '../../../../platform/uriIdentity/common/uriIdentity.js';
10
import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';
11
import { getGalleryExtensionId } from '../../../../platform/extensionManagement/common/extensionManagementUtil.js';
12
import { builtinExtensionsPath, FileAccess } from '../../../../base/common/network.js';
13
import { URI } from '../../../../base/common/uri.js';
14
import { IExtensionResourceLoaderService } from '../../../../platform/extensionResourceLoader/common/extensionResourceLoader.js';
15
import { IProductService } from '../../../../platform/product/common/productService.js';
16
import { ITranslations, localizeManifest } from '../../../../platform/extensionManagement/common/extensionNls.js';
17
import { ILogService } from '../../../../platform/log/common/log.js';
18
import { mainWindow } from '../../../../base/browser/window.js';
19
20
interface IBundledExtension {
21
extensionPath: string;
22
packageJSON: IExtensionManifest;
23
packageNLS?: ITranslations;
24
readmePath?: string;
25
changelogPath?: string;
26
}
27
28
export class BuiltinExtensionsScannerService implements IBuiltinExtensionsScannerService {
29
30
declare readonly _serviceBrand: undefined;
31
32
private readonly builtinExtensionsPromises: Promise<IExtension>[] = [];
33
34
private nlsUrl: URI | undefined;
35
36
constructor(
37
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,
38
@IUriIdentityService uriIdentityService: IUriIdentityService,
39
@IExtensionResourceLoaderService private readonly extensionResourceLoaderService: IExtensionResourceLoaderService,
40
@IProductService productService: IProductService,
41
@ILogService private readonly logService: ILogService
42
) {
43
if (isWeb) {
44
const nlsBaseUrl = productService.extensionsGallery?.nlsBaseUrl;
45
// Only use the nlsBaseUrl if we are using a language other than the default, English.
46
if (nlsBaseUrl && productService.commit && !Language.isDefaultVariant()) {
47
this.nlsUrl = URI.joinPath(URI.parse(nlsBaseUrl), productService.commit, productService.version, Language.value());
48
}
49
50
const builtinExtensionsServiceUrl = FileAccess.asBrowserUri(builtinExtensionsPath);
51
if (builtinExtensionsServiceUrl) {
52
let bundledExtensions: IBundledExtension[] = [];
53
54
if (environmentService.isBuilt) {
55
// Built time configuration (do NOT modify)
56
bundledExtensions = [/*BUILD->INSERT_BUILTIN_EXTENSIONS*/];
57
} else {
58
// Find builtin extensions by checking for DOM
59
// eslint-disable-next-line no-restricted-syntax
60
const builtinExtensionsElement = mainWindow.document.getElementById('vscode-workbench-builtin-extensions');
61
const builtinExtensionsElementAttribute = builtinExtensionsElement ? builtinExtensionsElement.getAttribute('data-settings') : undefined;
62
if (builtinExtensionsElementAttribute) {
63
try {
64
bundledExtensions = JSON.parse(builtinExtensionsElementAttribute);
65
} catch (error) { /* ignore error*/ }
66
}
67
}
68
69
this.builtinExtensionsPromises = bundledExtensions.map(async e => {
70
const id = getGalleryExtensionId(e.packageJSON.publisher, e.packageJSON.name);
71
return {
72
identifier: { id },
73
location: uriIdentityService.extUri.joinPath(builtinExtensionsServiceUrl, e.extensionPath),
74
type: ExtensionType.System,
75
isBuiltin: true,
76
manifest: e.packageNLS ? await this.localizeManifest(id, e.packageJSON, e.packageNLS) : e.packageJSON,
77
readmeUrl: e.readmePath ? uriIdentityService.extUri.joinPath(builtinExtensionsServiceUrl, e.readmePath) : undefined,
78
changelogUrl: e.changelogPath ? uriIdentityService.extUri.joinPath(builtinExtensionsServiceUrl, e.changelogPath) : undefined,
79
targetPlatform: TargetPlatform.WEB,
80
validations: [],
81
isValid: true,
82
preRelease: false,
83
};
84
});
85
}
86
}
87
}
88
89
async scanBuiltinExtensions(): Promise<IExtension[]> {
90
return [...await Promise.all(this.builtinExtensionsPromises)];
91
}
92
93
private async localizeManifest(extensionId: string, manifest: IExtensionManifest, fallbackTranslations: ITranslations): Promise<IExtensionManifest> {
94
if (!this.nlsUrl) {
95
return localizeManifest(this.logService, manifest, fallbackTranslations);
96
}
97
// the `package` endpoint returns the translations in a key-value format similar to the package.nls.json file.
98
const uri = URI.joinPath(this.nlsUrl, extensionId, 'package');
99
try {
100
const res = await this.extensionResourceLoaderService.readExtensionResource(uri);
101
const json = JSON.parse(res.toString());
102
return localizeManifest(this.logService, manifest, json, fallbackTranslations);
103
} catch (e) {
104
this.logService.error(e);
105
return localizeManifest(this.logService, manifest, fallbackTranslations);
106
}
107
}
108
}
109
110
registerSingleton(IBuiltinExtensionsScannerService, BuiltinExtensionsScannerService, InstantiationType.Delayed);
111
112