|
5 | 5 | 'use strict'; |
6 | 6 |
|
7 | 7 | import Severity from 'vs/base/common/severity'; |
8 | | -import { TPromise } from 'vs/base/common/winjs.base'; |
9 | | -import pkg from 'vs/platform/node/package'; |
10 | | -import { localize } from 'vs/nls'; |
11 | | -import * as path from 'path'; |
12 | | -import URI from 'vs/base/common/uri'; |
13 | | -import { AbstractExtensionService, ActivatedExtension } from 'vs/platform/extensions/common/abstractExtensionService'; |
14 | | -import { IMessage, IExtensionDescription, IExtensionsStatus, IExtensionService } from 'vs/platform/extensions/common/extensions'; |
15 | | -import { IExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionManagement'; |
16 | | -import { areSameExtensions, getGloballyDisabledExtensions } from 'vs/platform/extensionManagement/common/extensionManagementUtil'; |
17 | | -import { ExtensionsRegistry, ExtensionPoint, IExtensionPointUser, ExtensionMessageCollector } from 'vs/platform/extensions/common/extensionsRegistry'; |
18 | | -import { ExtensionScanner, MessagesCollector } from 'vs/workbench/node/extensionPoints'; |
19 | | -import { IMessageService } from 'vs/platform/message/common/message'; |
20 | | -import { IThreadService, ProxyIdentifier } from 'vs/workbench/services/thread/common/threadService'; |
21 | | -import { ExtHostContext, ExtHostExtensionServiceShape, MainProcessExtensionServiceShape } from '../node/extHost.protocol'; |
22 | | -import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry'; |
23 | | -import { IEnvironmentService } from 'vs/platform/environment/common/environment'; |
24 | | -import { IStorageService } from 'vs/platform/storage/common/storage'; |
25 | | -import { IInstantiationService } from "vs/platform/instantiation/common/instantiation"; |
26 | | -import { ExtensionHostProcessWorker } from "vs/workbench/electron-browser/extensionHost"; |
27 | | -import { MainThreadService } from "vs/workbench/services/thread/electron-browser/threadService"; |
28 | | - |
29 | | -const SystemExtensionsRoot = path.normalize(path.join(URI.parse(require.toUrl('')).fsPath, '..', 'extensions')); |
30 | | - |
31 | | -/** |
32 | | - * Represents a failed extension in the ext host. |
33 | | - */ |
34 | | -class MainProcessFailedExtension extends ActivatedExtension { |
35 | | - constructor() { |
36 | | - super(true); |
37 | | - } |
38 | | -} |
39 | | - |
40 | | -/** |
41 | | - * Represents an extension that was successfully loaded or an |
42 | | - * empty extension in the ext host. |
43 | | - */ |
44 | | -class MainProcessSuccessExtension extends ActivatedExtension { |
45 | | - constructor() { |
46 | | - super(false); |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -function messageWithSource(msg: IMessage): string { |
51 | | - return (msg.source ? '[' + msg.source + ']: ' : '') + msg.message; |
52 | | -} |
53 | | - |
54 | | -const hasOwnProperty = Object.hasOwnProperty; |
55 | | - |
56 | | -export class MainProcessExtensionService extends AbstractExtensionService<ActivatedExtension> implements IThreadService { |
57 | | - |
58 | | - private _proxy: ExtHostExtensionServiceShape; |
59 | | - private _isDev: boolean; |
60 | | - private _extensionsStatus: { [id: string]: IExtensionsStatus }; |
61 | | - private _threadService: IThreadService; |
62 | | - |
63 | | - /** |
64 | | - * This class is constructed manually because it is a service, so it doesn't use any ctor injection |
65 | | - */ |
66 | | - constructor( |
67 | | - @IInstantiationService private readonly _instantiationService: IInstantiationService, |
68 | | - @IMessageService private readonly _messageService: IMessageService, |
69 | | - @IEnvironmentService private readonly environmentService: IEnvironmentService, |
70 | | - @ITelemetryService private readonly _telemetryService: ITelemetryService, |
71 | | - @IExtensionEnablementService extensionEnablementService: IExtensionEnablementService, |
72 | | - @IStorageService storageService: IStorageService, |
73 | | - ) { |
74 | | - super(false); |
75 | | - this._isDev = !environmentService.isBuilt || environmentService.isExtensionDevelopment; |
76 | | - |
77 | | - this._extensionsStatus = {}; |
78 | | - |
79 | | - const extensionHostProcessWorker = this._instantiationService.createInstance(ExtensionHostProcessWorker); |
80 | | - this._threadService = this._instantiationService.createInstance(MainThreadService, extensionHostProcessWorker.messagingProtocol); |
81 | | - this._proxy = this._threadService.get(ExtHostContext.ExtHostExtensionService); |
82 | | - extensionHostProcessWorker.start(this); |
83 | | - |
84 | | - this.scanExtensions().done(extensionDescriptions => { |
85 | | - const disabledExtensions = [ |
86 | | - ...getGloballyDisabledExtensions(extensionEnablementService, storageService, extensionDescriptions), |
87 | | - ...extensionEnablementService.getWorkspaceDisabledExtensions() |
88 | | - ]; |
89 | | - |
90 | | - _telemetryService.publicLog('extensionsScanned', { |
91 | | - totalCount: extensionDescriptions.length, |
92 | | - disabledCount: disabledExtensions.length |
93 | | - }); |
94 | | - |
95 | | - this._onExtensionDescriptions(disabledExtensions.length ? extensionDescriptions.filter(e => disabledExtensions.every(id => !areSameExtensions({ id }, e))) : extensionDescriptions); |
96 | | - }); |
97 | | - } |
98 | | - |
99 | | - // ---- begin IThreadService |
100 | | - |
101 | | - public get<T>(identifier: ProxyIdentifier<T>): T { |
102 | | - return this._threadService.get(identifier); |
103 | | - } |
104 | | - |
105 | | - /** |
106 | | - * Register instance. |
107 | | - */ |
108 | | - public set<T>(identifier: ProxyIdentifier<T>, value: T): void { |
109 | | - this._threadService.set(identifier, value); |
110 | | - } |
111 | | - |
112 | | - // ---- end IThreadService |
113 | | - |
114 | | - private _handleMessage(msg: IMessage) { |
115 | | - |
116 | | - if (!this._extensionsStatus[msg.source]) { |
117 | | - this._extensionsStatus[msg.source] = { messages: [] }; |
118 | | - } |
119 | | - this._extensionsStatus[msg.source].messages.push(msg); |
120 | | - |
121 | | - this._localShowMessage( |
122 | | - msg.type, messageWithSource(msg), |
123 | | - this.environmentService.extensionDevelopmentPath === msg.source |
124 | | - ); |
125 | | - |
126 | | - if (!this._isDev && msg.extensionId) { |
127 | | - const { type, extensionId, extensionPointId, message } = msg; |
128 | | - this._telemetryService.publicLog('extensionsMessage', { |
129 | | - type, extensionId, extensionPointId, message |
130 | | - }); |
131 | | - } |
132 | | - } |
133 | | - |
134 | | - public _localShowMessage(severity: Severity, msg: string, useMessageService: boolean = this._isDev): void { |
135 | | - // Only show nasty intrusive messages if doing extension development |
136 | | - // and print all other messages to the console |
137 | | - if (useMessageService && (severity === Severity.Error || severity === Severity.Warning)) { |
138 | | - this._messageService.show(severity, msg); |
139 | | - } else if (severity === Severity.Error) { |
140 | | - console.error(msg); |
141 | | - } else if (severity === Severity.Warning) { |
142 | | - console.warn(msg); |
143 | | - } else { |
144 | | - console.log(msg); |
145 | | - } |
146 | | - } |
147 | | - |
148 | | - // -- overwriting AbstractExtensionService |
149 | | - |
150 | | - public getExtensionsStatus(): { [id: string]: IExtensionsStatus } { |
151 | | - return this._extensionsStatus; |
152 | | - } |
153 | | - |
154 | | - protected _showMessage(severity: Severity, msg: string): void { |
155 | | - this._localShowMessage(severity, msg); |
156 | | - } |
157 | | - |
158 | | - protected _createFailedExtension(): ActivatedExtension { |
159 | | - return new MainProcessFailedExtension(); |
160 | | - } |
161 | | - |
162 | | - protected _actualActivateExtension(extensionDescription: IExtensionDescription): TPromise<ActivatedExtension> { |
163 | | - |
164 | | - // redirect extension activation to the extension host |
165 | | - return this._proxy.$activateExtension(extensionDescription).then(_ => { |
166 | | - |
167 | | - // the extension host calls $onExtensionActivated, where we write to `_activatedExtensions` |
168 | | - return this._activatedExtensions[extensionDescription.id]; |
169 | | - }); |
170 | | - } |
171 | | - |
172 | | - // -- called by extension host |
173 | | - |
174 | | - private _onExtensionDescriptions(extensionDescriptions: IExtensionDescription[]): void { |
175 | | - this._registry.registerExtensions(extensionDescriptions); |
176 | | - |
177 | | - let availableExtensions = this._registry.getAllExtensionDescriptions(); |
178 | | - let extensionPoints = ExtensionsRegistry.getExtensionPoints(); |
179 | | - |
180 | | - for (let i = 0, len = extensionPoints.length; i < len; i++) { |
181 | | - this._handleExtensionPoint(extensionPoints[i], availableExtensions); |
182 | | - } |
183 | | - |
184 | | - this._triggerOnReady(); |
185 | | - } |
186 | | - |
187 | | - private _handleExtensionPoint<T>(extensionPoint: ExtensionPoint<T>, availableExtensions: IExtensionDescription[]): void { |
188 | | - let messageHandler = (msg: IMessage) => this._handleMessage(msg); |
189 | | - |
190 | | - let users: IExtensionPointUser<T>[] = [], usersLen = 0; |
191 | | - for (let i = 0, len = availableExtensions.length; i < len; i++) { |
192 | | - let desc = availableExtensions[i]; |
193 | | - |
194 | | - if (desc.contributes && hasOwnProperty.call(desc.contributes, extensionPoint.name)) { |
195 | | - users[usersLen++] = { |
196 | | - description: desc, |
197 | | - value: desc.contributes[extensionPoint.name], |
198 | | - collector: new ExtensionMessageCollector(messageHandler, desc, extensionPoint.name) |
199 | | - }; |
200 | | - } |
201 | | - } |
202 | | - |
203 | | - extensionPoint.acceptUsers(users); |
204 | | - } |
205 | | - |
206 | | - public _onExtensionActivated(extensionId: string): void { |
207 | | - this._activatedExtensions[extensionId] = new MainProcessSuccessExtension(); |
208 | | - } |
209 | | - |
210 | | - public _onExtensionActivationFailed(extensionId: string): void { |
211 | | - this._activatedExtensions[extensionId] = new MainProcessFailedExtension(); |
212 | | - } |
213 | | - |
214 | | - private scanExtensions(): TPromise<IExtensionDescription[]> { |
215 | | - const collector = new MessagesCollector(); |
216 | | - const version = pkg.version; |
217 | | - const builtinExtensions = ExtensionScanner.scanExtensions(version, collector, SystemExtensionsRoot, true); |
218 | | - const userExtensions = this.environmentService.disableExtensions || !this.environmentService.extensionsPath ? TPromise.as([]) : ExtensionScanner.scanExtensions(version, collector, this.environmentService.extensionsPath, false); |
219 | | - const developedExtensions = this.environmentService.disableExtensions || !this.environmentService.isExtensionDevelopment ? TPromise.as([]) : ExtensionScanner.scanOneOrMultipleExtensions(version, collector, this.environmentService.extensionDevelopmentPath, false); |
220 | | - |
221 | | - return TPromise.join([builtinExtensions, userExtensions, developedExtensions]).then((extensionDescriptions: IExtensionDescription[][]) => { |
222 | | - let builtinExtensions = extensionDescriptions[0]; |
223 | | - let userExtensions = extensionDescriptions[1]; |
224 | | - let developedExtensions = extensionDescriptions[2]; |
225 | | - |
226 | | - let result: { [extensionId: string]: IExtensionDescription; } = {}; |
227 | | - builtinExtensions.forEach((builtinExtension) => { |
228 | | - result[builtinExtension.id] = builtinExtension; |
229 | | - }); |
230 | | - userExtensions.forEach((userExtension) => { |
231 | | - if (result.hasOwnProperty(userExtension.id)) { |
232 | | - collector.warn(userExtension.extensionFolderPath, localize('overwritingExtension', "Overwriting extension {0} with {1}.", result[userExtension.id].extensionFolderPath, userExtension.extensionFolderPath)); |
233 | | - } |
234 | | - result[userExtension.id] = userExtension; |
235 | | - }); |
236 | | - developedExtensions.forEach(developedExtension => { |
237 | | - collector.info('', localize('extensionUnderDevelopment', "Loading development extension at {0}", developedExtension.extensionFolderPath)); |
238 | | - if (result.hasOwnProperty(developedExtension.id)) { |
239 | | - collector.warn(developedExtension.extensionFolderPath, localize('overwritingExtension', "Overwriting extension {0} with {1}.", result[developedExtension.id].extensionFolderPath, developedExtension.extensionFolderPath)); |
240 | | - } |
241 | | - result[developedExtension.id] = developedExtension; |
242 | | - }); |
243 | | - |
244 | | - return Object.keys(result).map(name => result[name]); |
245 | | - }).then(null, err => { |
246 | | - collector.error('', err); |
247 | | - return []; |
248 | | - }).then(extensions => { |
249 | | - collector.getMessages().forEach(entry => this._localShowMessage(entry.type, this._isDev ? (entry.source ? '[' + entry.source + ']: ' : '') + entry.message : entry.message)); |
250 | | - return extensions; |
251 | | - }); |
252 | | - } |
253 | | -} |
| 8 | +import { IExtensionService } from 'vs/platform/extensions/common/extensions'; |
| 9 | +import { MainProcessExtensionServiceShape } from '../node/extHost.protocol'; |
| 10 | +import { MainProcessExtensionService } from "vs/workbench/services/extensions/electron-browser/extensionService"; |
254 | 11 |
|
255 | 12 | export class MainProcessExtensionServiceAPI extends MainProcessExtensionServiceShape { |
256 | 13 |
|
|
0 commit comments