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
3296 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?: any;
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
const builtinExtensionsElement = mainWindow.document.getElementById('vscode-workbench-builtin-extensions');
60
const builtinExtensionsElementAttribute = builtinExtensionsElement ? builtinExtensionsElement.getAttribute('data-settings') : undefined;
61
if (builtinExtensionsElementAttribute) {
62
try {
63
bundledExtensions = JSON.parse(builtinExtensionsElementAttribute);
64
} catch (error) { /* ignore error*/ }
65
}
66
}
67
68
this.builtinExtensionsPromises = bundledExtensions.map(async e => {
69
const id = getGalleryExtensionId(e.packageJSON.publisher, e.packageJSON.name);
70
return {
71
identifier: { id },
72
location: uriIdentityService.extUri.joinPath(builtinExtensionsServiceUrl, e.extensionPath),
73
type: ExtensionType.System,
74
isBuiltin: true,
75
manifest: e.packageNLS ? await this.localizeManifest(id, e.packageJSON, e.packageNLS) : e.packageJSON,
76
readmeUrl: e.readmePath ? uriIdentityService.extUri.joinPath(builtinExtensionsServiceUrl, e.readmePath) : undefined,
77
changelogUrl: e.changelogPath ? uriIdentityService.extUri.joinPath(builtinExtensionsServiceUrl, e.changelogPath) : undefined,
78
targetPlatform: TargetPlatform.WEB,
79
validations: [],
80
isValid: true,
81
preRelease: false,
82
};
83
});
84
}
85
}
86
}
87
88
async scanBuiltinExtensions(): Promise<IExtension[]> {
89
return [...await Promise.all(this.builtinExtensionsPromises)];
90
}
91
92
private async localizeManifest(extensionId: string, manifest: IExtensionManifest, fallbackTranslations: ITranslations): Promise<IExtensionManifest> {
93
if (!this.nlsUrl) {
94
return localizeManifest(this.logService, manifest, fallbackTranslations);
95
}
96
// the `package` endpoint returns the translations in a key-value format similar to the package.nls.json file.
97
const uri = URI.joinPath(this.nlsUrl, extensionId, 'package');
98
try {
99
const res = await this.extensionResourceLoaderService.readExtensionResource(uri);
100
const json = JSON.parse(res.toString());
101
return localizeManifest(this.logService, manifest, json, fallbackTranslations);
102
} catch (e) {
103
this.logService.error(e);
104
return localizeManifest(this.logService, manifest, fallbackTranslations);
105
}
106
}
107
}
108
109
registerSingleton(IBuiltinExtensionsScannerService, BuiltinExtensionsScannerService, InstantiationType.Delayed);
110
111