extensionManagementService.ts 49.1 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as nls from 'vs/nls';
7
import * as path from 'vs/base/common/path';
E
Erich Gamma 已提交
8 9
import * as pfs from 'vs/base/node/pfs';
import { assign } from 'vs/base/common/objects';
10
import { toDisposable, Disposable } from 'vs/base/common/lifecycle';
S
Sandeep Somavarapu 已提交
11
import { flatten, isNonEmptyArray } from 'vs/base/common/arrays';
S
Sandeep Somavarapu 已提交
12
import { extract, ExtractError, zip, IFile } from 'vs/base/node/zip';
J
Johannes Rieken 已提交
13 14
import {
	IExtensionManagementService, IExtensionGalleryService, ILocalExtension,
15 16
	IGalleryExtension, IGalleryMetadata,
	InstallExtensionEvent, DidInstallExtensionEvent, DidUninstallExtensionEvent,
S
Sandeep Somavarapu 已提交
17
	StatisticType,
J
Joao Moreno 已提交
18
	IExtensionIdentifier,
19
	IReportedExtension,
S
Sandeep Somavarapu 已提交
20 21 22
	InstallOperation,
	INSTALL_ERROR_MALICIOUS,
	INSTALL_ERROR_INCOMPATIBLE
J
Joao Moreno 已提交
23
} from 'vs/platform/extensionManagement/common/extensionManagement';
24
import { areSameExtensions, getGalleryExtensionId, groupByExtension, getMaliciousExtensionsSet, getGalleryExtensionTelemetryData, getLocalExtensionTelemetryData, ExtensionIdentifierWithVersion } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
25
import { localizeManifest } from '../common/extensionNls';
J
Joao Moreno 已提交
26
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
27
import { INativeEnvironmentService } from 'vs/platform/environment/node/environmentService';
28
import { Limiter, createCancelablePromise, CancelablePromise, Queue } from 'vs/base/common/async';
M
Matt Bierner 已提交
29
import { Event, Emitter } from 'vs/base/common/event';
S
Sandeep Somavarapu 已提交
30
import * as semver from 'semver-umd';
31
import { URI } from 'vs/base/common/uri';
32
import product from 'vs/platform/product/common/product';
33
import { isMacintosh, isWindows } from 'vs/base/common/platform';
34
import { ILogService } from 'vs/platform/log/common/log';
35
import { ExtensionsManifestCache } from 'vs/platform/extensionManagement/node/extensionsManifestCache';
S
Sandeep Somavarapu 已提交
36
import { ExtensionsLifecycle } from 'vs/platform/extensionManagement/node/extensionLifecycle';
37
import { toErrorMessage } from 'vs/base/common/errorMessage';
38
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
39
import { isEngineValid } from 'vs/platform/extensions/common/extensionValidator';
40 41 42
import { tmpdir } from 'os';
import { generateUuid } from 'vs/base/common/uuid';
import { IDownloadService } from 'vs/platform/download/common/download';
43
import { optional } from 'vs/platform/instantiation/common/instantiation';
44
import { Schemas } from 'vs/base/common/network';
45
import { CancellationToken } from 'vs/base/common/cancellation';
46
import { getPathFromAmdModule } from 'vs/base/common/amd';
47
import { getManifest } from 'vs/platform/extensionManagement/node/extensionManagementUtil';
A
Alex Dima 已提交
48
import { IExtensionManifest, ExtensionType } from 'vs/platform/extensions/common/extensions';
E
Erich Gamma 已提交
49

50 51
const ERROR_SCANNING_SYS_EXTENSIONS = 'scanningSystem';
const ERROR_SCANNING_USER_EXTENSIONS = 'scanningUser';
52
const INSTALL_ERROR_UNSET_UNINSTALLED = 'unsetUninstalled';
S
Sandeep Somavarapu 已提交
53 54
const INSTALL_ERROR_DOWNLOADING = 'downloading';
const INSTALL_ERROR_VALIDATING = 'validating';
55
const INSTALL_ERROR_LOCAL = 'local';
56
const INSTALL_ERROR_EXTRACTING = 'extracting';
S
Sandeep Somavarapu 已提交
57
const INSTALL_ERROR_RENAMING = 'renaming';
58
const INSTALL_ERROR_DELETING = 'deleting';
59
const ERROR_UNKNOWN = 'unknown';
S
Sandeep Somavarapu 已提交
60

61
export class ExtensionManagementError extends Error {
S
Sandeep Somavarapu 已提交
62 63 64 65
	constructor(message: string, readonly code: string) {
		super(message);
	}
}
J
Joao Moreno 已提交
66

S
Sandeep Somavarapu 已提交
67 68
function parseManifest(raw: string): Promise<{ manifest: IExtensionManifest; metadata: IGalleryMetadata; }> {
	return new Promise((c, e) => {
E
Erich Gamma 已提交
69
		try {
J
Joao Moreno 已提交
70 71 72 73
			const manifest = JSON.parse(raw);
			const metadata = manifest.__metadata || null;
			delete manifest.__metadata;
			c({ manifest, metadata });
E
Erich Gamma 已提交
74 75 76 77 78 79
		} catch (err) {
			e(new Error(nls.localize('invalidManifest', "Extension invalid: package.json is not a JSON file.")));
		}
	});
}

S
Sandeep Somavarapu 已提交
80
function readManifest(extensionPath: string): Promise<{ manifest: IExtensionManifest; metadata: IGalleryMetadata; }> {
81 82 83 84
	const promises = [
		pfs.readFile(path.join(extensionPath, 'package.json'), 'utf8')
			.then(raw => parseManifest(raw)),
		pfs.readFile(path.join(extensionPath, 'package.nls.json'), 'utf8')
R
Rob Lourens 已提交
85
			.then(undefined, err => err.code !== 'ENOENT' ? Promise.reject<string>(err) : '{}')
86 87 88
			.then(raw => JSON.parse(raw))
	];

89
	return Promise.all(promises).then(([{ manifest, metadata }, translations]) => {
90 91 92 93 94 95 96
		return {
			manifest: localizeManifest(manifest, translations),
			metadata
		};
	});
}

S
Sandeep Somavarapu 已提交
97 98
interface InstallableExtension {
	zipPath: string;
99
	identifierWithVersion: ExtensionIdentifierWithVersion;
100
	metadata: IGalleryMetadata | null;
S
Sandeep Somavarapu 已提交
101 102
}

103
export class ExtensionManagementService extends Disposable implements IExtensionManagementService {
E
Erich Gamma 已提交
104

105
	_serviceBrand: undefined;
E
Erich Gamma 已提交
106

107
	private systemExtensionsPath: string;
E
Erich Gamma 已提交
108
	private extensionsPath: string;
109
	private uninstalledPath: string;
J
Joao Moreno 已提交
110
	private uninstalledFileLimiter: Queue<any>;
S
Sandeep Somavarapu 已提交
111
	private reportedExtensions: Promise<IReportedExtension[]> | undefined;
J
Joao Moreno 已提交
112
	private lastReportTimestamp = 0;
113
	private readonly installingExtensions: Map<string, CancelablePromise<ILocalExtension>> = new Map<string, CancelablePromise<ILocalExtension>>();
114
	private readonly uninstallingExtensions: Map<string, CancelablePromise<void>> = new Map<string, CancelablePromise<void>>();
115
	private readonly manifestCache: ExtensionsManifestCache;
S
Sandeep Somavarapu 已提交
116
	private readonly extensionLifecycle: ExtensionsLifecycle;
E
Erich Gamma 已提交
117

118
	private readonly _onInstallExtension = this._register(new Emitter<InstallExtensionEvent>());
S
Sandeep Somavarapu 已提交
119
	readonly onInstallExtension: Event<InstallExtensionEvent> = this._onInstallExtension.event;
E
Erich Gamma 已提交
120

121
	private readonly _onDidInstallExtension = this._register(new Emitter<DidInstallExtensionEvent>());
S
Sandeep Somavarapu 已提交
122
	readonly onDidInstallExtension: Event<DidInstallExtensionEvent> = this._onDidInstallExtension.event;
E
Erich Gamma 已提交
123

124
	private readonly _onUninstallExtension = this._register(new Emitter<IExtensionIdentifier>());
S
Sandeep Somavarapu 已提交
125 126
	readonly onUninstallExtension: Event<IExtensionIdentifier> = this._onUninstallExtension.event;

127
	private _onDidUninstallExtension = this._register(new Emitter<DidUninstallExtensionEvent>());
S
Sandeep Somavarapu 已提交
128
	onDidUninstallExtension: Event<DidUninstallExtensionEvent> = this._onDidUninstallExtension.event;
E
Erich Gamma 已提交
129 130

	constructor(
131
		@IEnvironmentService private readonly environmentService: INativeEnvironmentService,
132 133
		@IExtensionGalleryService private readonly galleryService: IExtensionGalleryService,
		@ILogService private readonly logService: ILogService,
134
		@optional(IDownloadService) private downloadService: IDownloadService,
135
		@ITelemetryService private readonly telemetryService: ITelemetryService,
E
Erich Gamma 已提交
136
	) {
137
		super();
138
		this.systemExtensionsPath = environmentService.builtinExtensionsPath;
139
		this.extensionsPath = environmentService.extensionsPath!;
140
		this.uninstalledPath = path.join(this.extensionsPath, '.obsolete');
J
Joao Moreno 已提交
141
		this.uninstalledFileLimiter = new Queue();
142
		this.manifestCache = this._register(new ExtensionsManifestCache(environmentService, this));
143
		this.extensionLifecycle = this._register(new ExtensionsLifecycle(environmentService, this.logService));
S
Sandeep Somavarapu 已提交
144 145 146 147 148 149 150

		this._register(toDisposable(() => {
			this.installingExtensions.forEach(promise => promise.cancel());
			this.uninstallingExtensions.forEach(promise => promise.cancel());
			this.installingExtensions.clear();
			this.uninstallingExtensions.clear();
		}));
A
Alex Dima 已提交
151 152
	}

S
Sandeep Somavarapu 已提交
153
	zip(extension: ILocalExtension): Promise<URI> {
154
		this.logService.trace('ExtensionManagementService#zip', extension.identifier.id);
S
Sandeep Somavarapu 已提交
155
		return this.collectFiles(extension)
156
			.then(files => zip(path.join(tmpdir(), generateUuid()), files))
B
Benjamin Pasero 已提交
157
			.then<URI>(path => URI.file(path));
158 159
	}

160
	unzip(zipLocation: URI, type: ExtensionType): Promise<IExtensionIdentifier> {
S
Sandeep Somavarapu 已提交
161
		this.logService.trace('ExtensionManagementService#unzip', zipLocation.toString());
162
		return this.install(zipLocation, type).then(local => local.identifier);
163 164
	}

165 166 167 168 169 170
	async getManifest(vsix: URI): Promise<IExtensionManifest> {
		const downloadLocation = await this.downloadVsix(vsix);
		const zipPath = path.resolve(downloadLocation.fsPath);
		return getManifest(zipPath);
	}

S
Sandeep Somavarapu 已提交
171 172
	private collectFiles(extension: ILocalExtension): Promise<IFile[]> {

M
Matt Bierner 已提交
173
		const collectFilesFromDirectory = async (dir: string): Promise<string[]> => {
S
Sandeep Somavarapu 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186
			let entries = await pfs.readdir(dir);
			entries = entries.map(e => path.join(dir, e));
			const stats = await Promise.all(entries.map(e => pfs.stat(e)));
			let promise: Promise<string[]> = Promise.resolve([]);
			stats.forEach((stat, index) => {
				const entry = entries[index];
				if (stat.isFile()) {
					promise = promise.then(result => ([...result, entry]));
				}
				if (stat.isDirectory()) {
					promise = promise
						.then(result => collectFilesFromDirectory(entry)
							.then(files => ([...result, ...files])));
187 188
				}
			});
S
Sandeep Somavarapu 已提交
189 190 191 192 193 194
			return promise;
		};

		return collectFilesFromDirectory(extension.location.fsPath)
			.then(files => files.map(f => (<IFile>{ path: `extension/${path.relative(extension.location.fsPath, f)}`, localPath: f })));

195 196
	}

197
	install(vsix: URI, type: ExtensionType = ExtensionType.User): Promise<ILocalExtension> {
S
Sandeep Somavarapu 已提交
198
		this.logService.trace('ExtensionManagementService#install', vsix.toString());
S
Sandeep Somavarapu 已提交
199
		return createCancelablePromise(token => {
M
Matt Bierner 已提交
200 201 202 203 204
			return this.downloadVsix(vsix).then(downloadLocation => {
				const zipPath = path.resolve(downloadLocation.fsPath);

				return getManifest(zipPath)
					.then(manifest => {
205 206
						const identifier = { id: getGalleryExtensionId(manifest.publisher, manifest.name) };
						let operation: InstallOperation = InstallOperation.Install;
207 208
						if (manifest.engines && manifest.engines.vscode && !isEngineValid(manifest.engines.vscode, product.version)) {
							return Promise.reject(new Error(nls.localize('incompatible', "Unable to install extension '{0}' as it is not compatible with VS Code '{1}'.", identifier.id, product.version)));
M
Matt Bierner 已提交
209
						}
210 211 212 213 214 215 216 217 218 219 220
						const identifierWithVersion = new ExtensionIdentifierWithVersion(identifier, manifest.version);
						return this.getInstalled(ExtensionType.User)
							.then(installedExtensions => {
								const existing = installedExtensions.filter(i => areSameExtensions(identifier, i.identifier))[0];
								if (existing) {
									operation = InstallOperation.Update;
									if (identifierWithVersion.equals(new ExtensionIdentifierWithVersion(existing.identifier, existing.manifest.version))) {
										return this.removeExtension(existing, 'existing').then(null, e => Promise.reject(new Error(nls.localize('restartCode', "Please restart VS Code before reinstalling {0}.", manifest.displayName || manifest.name))));
									} else if (semver.gt(existing.manifest.version, manifest.version)) {
										return this.uninstall(existing, true);
									}
S
Sandeep Somavarapu 已提交
221 222 223 224 225 226 227 228 229 230
								} else {
									// Remove the extension with same version if it is already uninstalled.
									// Installing a VSIX extension shall replace the existing extension always.
									return this.unsetUninstalledAndGetLocal(identifierWithVersion)
										.then(existing => {
											if (existing) {
												return this.removeExtension(existing, 'existing').then(null, e => Promise.reject(new Error(nls.localize('restartCode', "Please restart VS Code before reinstalling {0}.", manifest.displayName || manifest.name))));
											}
											return undefined;
										});
231 232 233 234 235 236 237 238 239 240 241
								}
								return undefined;
							})
							.then(() => {
								this.logService.info('Installing the extension:', identifier.id);
								this._onInstallExtension.fire({ identifier, zipPath });
								return this.getMetadata(getGalleryExtensionId(manifest.publisher, manifest.name))
									.then(
										metadata => this.installFromZipPath(identifierWithVersion, zipPath, metadata, type, operation, token),
										() => this.installFromZipPath(identifierWithVersion, zipPath, null, type, operation, token))
									.then(
242
										local => { this.logService.info('Successfully installed the extension:', identifier.id); return local; },
243 244 245 246 247
										e => {
											this.logService.error('Failed to install the extension:', identifier.id, e.message);
											return Promise.reject(e);
										});
							});
M
Matt Bierner 已提交
248 249
					});
			});
S
Sandeep Somavarapu 已提交
250
		});
251 252
	}

S
Sandeep Somavarapu 已提交
253
	private downloadVsix(vsix: URI): Promise<URI> {
254
		if (vsix.scheme === Schemas.file) {
S
Sandeep Somavarapu 已提交
255
			return Promise.resolve(vsix);
256 257 258 259 260
		}
		if (!this.downloadService) {
			throw new Error('Download service is not available');
		}
		const downloadedLocation = path.join(tmpdir(), generateUuid());
261
		return this.downloadService.download(vsix, URI.file(downloadedLocation)).then(() => URI.file(downloadedLocation));
262 263
	}

264 265
	private installFromZipPath(identifierWithVersion: ExtensionIdentifierWithVersion, zipPath: string, metadata: IGalleryMetadata | null, type: ExtensionType, operation: InstallOperation, token: CancellationToken): Promise<ILocalExtension> {
		return this.toNonCancellablePromise(this.installExtension({ zipPath, identifierWithVersion, metadata }, type, token)
S
Sandeep Somavarapu 已提交
266 267 268 269 270 271 272 273 274 275 276 277
			.then(local => this.installDependenciesAndPackExtensions(local, null)
				.then(
					() => local,
					error => {
						if (isNonEmptyArray(local.manifest.extensionDependencies)) {
							this.logService.warn(`Cannot install dependencies of extension:`, local.identifier.id, error.message);
						}
						if (isNonEmptyArray(local.manifest.extensionPack)) {
							this.logService.warn(`Cannot install packed extensions of extension:`, local.identifier.id, error.message);
						}
						return local;
					}))
278 279 280 281
			.then(
				local => { this._onDidInstallExtension.fire({ identifier: identifierWithVersion.identifier, zipPath, local, operation }); return local; },
				error => { this._onDidInstallExtension.fire({ identifier: identifierWithVersion.identifier, zipPath, operation, error }); return Promise.reject(error); }
			));
E
Erich Gamma 已提交
282 283
	}

284
	async installFromGallery(extension: IGalleryExtension): Promise<ILocalExtension> {
285 286 287
		if (!this.galleryService.isEnabled()) {
			return Promise.reject(new Error(nls.localize('MarketPlaceDisabled', "Marketplace is not enabled")));
		}
288 289 290 291
		const startTime = new Date().getTime();

		const onDidInstallExtensionSuccess = (extension: IGalleryExtension, operation: InstallOperation, local: ILocalExtension) => {
			this.logService.info(`Extensions installed successfully:`, extension.identifier.id);
292
			this._onDidInstallExtension.fire({ identifier: extension.identifier, gallery: extension, local, operation });
R
Rob Lourens 已提交
293
			this.reportTelemetry(this.getTelemetryEvent(operation), getGalleryExtensionTelemetryData(extension), new Date().getTime() - startTime, undefined);
294 295
		};

M
Matt Bierner 已提交
296
		const onDidInstallExtensionFailure = (extension: IGalleryExtension, operation: InstallOperation, error: Error) => {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
			const errorCode = error && (<ExtensionManagementError>error).code ? (<ExtensionManagementError>error).code : ERROR_UNKNOWN;
			this.logService.error(`Failed to install extension:`, extension.identifier.id, error ? error.message : errorCode);
			this._onDidInstallExtension.fire({ identifier: extension.identifier, gallery: extension, operation, error: errorCode });
			this.reportTelemetry(this.getTelemetryEvent(operation), getGalleryExtensionTelemetryData(extension), new Date().getTime() - startTime, error);
			if (error instanceof Error) {
				error.name = errorCode;
			}
		};

		try {
			extension = await this.checkAndGetCompatibleVersion(extension);
		} catch (error) {
			onDidInstallExtensionFailure(extension, InstallOperation.Install, error);
			return Promise.reject(error);
		}

313
		const key = new ExtensionIdentifierWithVersion(extension.identifier, extension.version).key();
314
		let cancellablePromise = this.installingExtensions.get(key);
315
		if (!cancellablePromise) {
316

S
Sandeep Somavarapu 已提交
317 318 319
			this.logService.info('Installing extension:', extension.identifier.id);
			this._onInstallExtension.fire({ identifier: extension.identifier, gallery: extension });

320
			let operation: InstallOperation = InstallOperation.Install;
321
			let cancellationToken: CancellationToken, successCallback: (local: ILocalExtension) => void, errorCallback: (e?: any) => any | null;
S
Sandeep Somavarapu 已提交
322
			cancellablePromise = createCancelablePromise(token => { cancellationToken = token; return new Promise((c, e) => { successCallback = c; errorCallback = e; }); });
323
			this.installingExtensions.set(key, cancellablePromise);
324
			try {
325
				const installed = await this.getInstalled(ExtensionType.User);
326
				const existingExtension = installed.filter(i => areSameExtensions(i.identifier, extension.identifier))[0];
327 328 329 330 331
				if (existingExtension) {
					operation = InstallOperation.Update;
				}

				this.downloadInstallableExtension(extension, operation)
332
					.then(installableExtension => this.installExtension(installableExtension, ExtensionType.User, cancellationToken)
333
						.then(local => pfs.rimraf(installableExtension.zipPath).finally(() => { }).then(() => local)))
334 335
					.then(local => this.installDependenciesAndPackExtensions(local, existingExtension)
						.then(() => local, error => this.uninstall(local, true).then(() => Promise.reject(error), () => Promise.reject(error))))
336
					.then(
S
Sandeep Somavarapu 已提交
337 338 339 340
						async local => {
							if (existingExtension && semver.neq(existingExtension.manifest.version, extension.version)) {
								await this.setUninstalled(existingExtension);
							}
341 342
							this.installingExtensions.delete(key);
							onDidInstallExtensionSuccess(extension, operation, local);
343
							successCallback(local);
344 345
						},
						error => {
346 347
							this.installingExtensions.delete(key);
							onDidInstallExtensionFailure(extension, operation, error);
348 349 350 351
							errorCallback(error);
						});

			} catch (error) {
352 353
				this.installingExtensions.delete(key);
				onDidInstallExtensionFailure(extension, operation, error);
354
				return Promise.reject(error);
355 356 357
			}

		}
358

S
Sandeep Somavarapu 已提交
359
		return cancellablePromise;
S
Sandeep Somavarapu 已提交
360 361
	}

362 363 364 365 366
	private async checkAndGetCompatibleVersion(extension: IGalleryExtension): Promise<IGalleryExtension> {
		if (await this.isMalicious(extension)) {
			return Promise.reject(new ExtensionManagementError(nls.localize('malicious extension', "Can't install extension since it was reported to be problematic."), INSTALL_ERROR_MALICIOUS));
		}

S
Sandeep Somavarapu 已提交
367
		const compatibleExtension = await this.galleryService.getCompatibleExtension(extension);
368

369
		if (!compatibleExtension) {
370
			return Promise.reject(new ExtensionManagementError(nls.localize('notFoundCompatibleDependency', "Unable to install '{0}' extension because it is not compatible with the current version of VS Code (version {1}).", extension.identifier.id, product.version), INSTALL_ERROR_INCOMPATIBLE));
371 372
		}

373
		return compatibleExtension;
374 375
	}

S
Sandeep Somavarapu 已提交
376
	reinstallFromGallery(extension: ILocalExtension): Promise<void> {
377
		this.logService.trace('ExtensionManagementService#reinstallFromGallery', extension.identifier.id);
S
Sandeep Somavarapu 已提交
378
		if (!this.galleryService.isEnabled()) {
S
Sandeep Somavarapu 已提交
379
			return Promise.reject(new Error(nls.localize('MarketPlaceDisabled', "Marketplace is not enabled")));
S
Sandeep Somavarapu 已提交
380 381 382 383
		}
		return this.findGalleryExtension(extension)
			.then(galleryExtension => {
				if (galleryExtension) {
S
Sandeep Somavarapu 已提交
384
					return this.setUninstalled(extension)
385 386
						.then(() => this.removeUninstalledExtension(extension)
							.then(
B
Benjamin Pasero 已提交
387
								() => this.installFromGallery(galleryExtension).then(),
S
Sandeep Somavarapu 已提交
388
								e => Promise.reject(new Error(nls.localize('removeError', "Error while removing the extension: {0}. Please Quit and Start VS Code before trying again.", toErrorMessage(e))))));
S
Sandeep Somavarapu 已提交
389
				}
S
Sandeep Somavarapu 已提交
390
				return Promise.reject(new Error(nls.localize('Not a Marketplace extension', "Only Marketplace Extensions can be reinstalled")));
S
Sandeep Somavarapu 已提交
391 392 393
			});
	}

394 395
	private getTelemetryEvent(operation: InstallOperation): string {
		return operation === InstallOperation.Update ? 'extensionGallery:update' : 'extensionGallery:install';
S
Sandeep Somavarapu 已提交
396 397
	}

398
	private isMalicious(extension: IGalleryExtension): Promise<boolean> {
399
		return this.getExtensionsReport()
400
			.then(report => getMaliciousExtensionsSet(report).has(extension.identifier.id));
S
Sandeep Somavarapu 已提交
401 402
	}

S
Sandeep Somavarapu 已提交
403
	private downloadInstallableExtension(extension: IGalleryExtension, operation: InstallOperation): Promise<InstallableExtension> {
S
Sandeep Somavarapu 已提交
404
		const metadata = <IGalleryMetadata>{
S
Sandeep Somavarapu 已提交
405
			id: extension.identifier.uuid,
S
Sandeep Somavarapu 已提交
406 407 408
			publisherId: extension.publisherId,
			publisherDisplayName: extension.publisherDisplayName,
		};
S
Sandeep Somavarapu 已提交
409

S
Sandeep Somavarapu 已提交
410
		this.logService.trace('Started downloading extension:', extension.identifier.id);
411
		return this.galleryService.download(extension, URI.file(tmpdir()), operation)
S
Sandeep Somavarapu 已提交
412
			.then(
413 414
				zip => {
					const zipPath = zip.fsPath;
S
Sandeep Somavarapu 已提交
415
					this.logService.info('Downloaded extension:', extension.identifier.id, zipPath);
416 417
					return getManifest(zipPath)
						.then(
418
							manifest => (<InstallableExtension>{ zipPath, identifierWithVersion: new ExtensionIdentifierWithVersion(extension.identifier, manifest.version), metadata }),
419 420
							error => Promise.reject(new ExtensionManagementError(this.joinErrors(error).message, INSTALL_ERROR_VALIDATING))
						);
M
Matt Bierner 已提交
421
				},
422
				error => Promise.reject(new ExtensionManagementError(this.joinErrors(error).message, INSTALL_ERROR_DOWNLOADING)));
S
Sandeep Somavarapu 已提交
423 424
	}

425
	private installExtension(installableExtension: InstallableExtension, type: ExtensionType, token: CancellationToken): Promise<ILocalExtension> {
426
		return this.unsetUninstalledAndGetLocal(installableExtension.identifierWithVersion)
427
			.then(
M
Matt Bierner 已提交
428 429 430 431
				local => {
					if (local) {
						return local;
					}
432
					return this.extractAndInstall(installableExtension, type, token);
M
Matt Bierner 已提交
433 434 435
				},
				e => {
					if (isMacintosh) {
S
Sandeep Somavarapu 已提交
436
						return Promise.reject(new ExtensionManagementError(nls.localize('quitCode', "Unable to install the extension. Please Quit and Start VS Code before reinstalling."), INSTALL_ERROR_UNSET_UNINSTALLED));
M
Matt Bierner 已提交
437
					}
S
Sandeep Somavarapu 已提交
438
					return Promise.reject(new ExtensionManagementError(nls.localize('exitCode', "Unable to install the extension. Please Exit and Start VS Code before reinstalling."), INSTALL_ERROR_UNSET_UNINSTALLED));
M
Matt Bierner 已提交
439
				});
440
	}
J
Joao Moreno 已提交
441

442 443
	private unsetUninstalledAndGetLocal(identifierWithVersion: ExtensionIdentifierWithVersion): Promise<ILocalExtension | null> {
		return this.isUninstalled(identifierWithVersion)
444
			.then<ILocalExtension | null>(isUninstalled => {
445
				if (isUninstalled) {
446
					this.logService.trace('Removing the extension from uninstalled list:', identifierWithVersion.identifier.id);
447
					// If the same version of extension is marked as uninstalled, remove it from there and return the local.
448
					return this.unsetUninstalled(identifierWithVersion)
449
						.then(() => {
450
							this.logService.info('Removed the extension from uninstalled list:', identifierWithVersion.identifier.id);
451
							return this.getInstalled(ExtensionType.User);
452
						})
453
						.then(installed => installed.filter(i => new ExtensionIdentifierWithVersion(i.identifier, i.manifest.version).equals(identifierWithVersion))[0]);
454 455 456 457 458
				}
				return null;
			});
	}

459 460
	private extractAndInstall({ zipPath, identifierWithVersion, metadata }: InstallableExtension, type: ExtensionType, token: CancellationToken): Promise<ILocalExtension> {
		const { identifier } = identifierWithVersion;
461
		const location = type === ExtensionType.User ? this.extensionsPath : this.systemExtensionsPath;
462 463 464
		const folderName = identifierWithVersion.key();
		const tempPath = path.join(location, `.${folderName}`);
		const extensionPath = path.join(location, folderName);
S
Sandeep Somavarapu 已提交
465
		return pfs.rimraf(extensionPath)
466 467
			.then(() => this.extractAndRename(identifier, zipPath, tempPath, extensionPath, token), e => Promise.reject(new ExtensionManagementError(nls.localize('errorDeleting', "Unable to delete the existing folder '{0}' while installing the extension '{1}'. Please delete the folder manually and try again", extensionPath, identifier.id), INSTALL_ERROR_DELETING)))
			.then(() => this.scanExtension(folderName, location, type))
S
Sandeep Somavarapu 已提交
468
			.then(local => {
469 470 471
				if (!local) {
					return Promise.reject(nls.localize('cannot read', "Cannot read the extension from {0}", location));
				}
472
				this.logService.info('Installation completed.', identifier.id);
S
Sandeep Somavarapu 已提交
473
				if (metadata) {
S
Sandeep Somavarapu 已提交
474
					this.setMetadata(local, metadata);
S
Sandeep Somavarapu 已提交
475 476 477 478
					return this.saveMetadataForLocalExtension(local);
				}
				return local;
			}, error => pfs.rimraf(extensionPath).then(() => Promise.reject(error), () => Promise.reject(error)));
E
Erich Gamma 已提交
479 480
	}

481 482 483
	private extractAndRename(identifier: IExtensionIdentifier, zipPath: string, extractPath: string, renamePath: string, token: CancellationToken): Promise<void> {
		return this.extract(identifier, zipPath, extractPath, token)
			.then(() => this.rename(identifier, extractPath, renamePath, Date.now() + (2 * 60 * 1000) /* Retry for 2 minutes */)
S
Sandeep Somavarapu 已提交
484 485 486 487
				.then(
					() => this.logService.info('Renamed to', renamePath),
					e => {
						this.logService.info('Rename failed. Deleting from extracted location', extractPath);
488
						return pfs.rimraf(extractPath).finally(() => { }).then(() => Promise.reject(e));
S
Sandeep Somavarapu 已提交
489 490 491
					}));
	}

492
	private extract(identifier: IExtensionIdentifier, zipPath: string, extractPath: string, token: CancellationToken): Promise<void> {
493 494 495
		this.logService.trace(`Started extracting the extension from ${zipPath} to ${extractPath}`);
		return pfs.rimraf(extractPath)
			.then(
S
Sandeep Somavarapu 已提交
496
				() => extract(zipPath, extractPath, { sourcePath: 'extension', overwrite: true }, token)
M
Matt Bierner 已提交
497
					.then(
498
						() => this.logService.info(`Extracted extension to ${extractPath}:`, identifier.id),
499
						e => pfs.rimraf(extractPath).finally(() => { })
500
							.then(() => Promise.reject(new ExtensionManagementError(e.message, e instanceof ExtractError && e.type ? e.type : INSTALL_ERROR_EXTRACTING)))),
S
Sandeep Somavarapu 已提交
501
				e => Promise.reject(new ExtensionManagementError(this.joinErrors(e).message, INSTALL_ERROR_DELETING)));
502 503
	}

504
	private rename(identifier: IExtensionIdentifier, extractPath: string, renamePath: string, retryUntil: number): Promise<void> {
505
		return pfs.rename(extractPath, renamePath)
R
Rob Lourens 已提交
506
			.then(undefined, error => {
S
Sandeep Somavarapu 已提交
507
				if (isWindows && error && error.code === 'EPERM' && Date.now() < retryUntil) {
508 509
					this.logService.info(`Failed renaming ${extractPath} to ${renamePath} with 'EPERM' error. Trying again...`, identifier.id);
					return this.rename(identifier, extractPath, renamePath, retryUntil);
S
Sandeep Somavarapu 已提交
510
				}
S
Sandeep Somavarapu 已提交
511
				return Promise.reject(new ExtensionManagementError(error.message || nls.localize('renameError', "Unknown error while renaming {0} to {1}", extractPath, renamePath), error.code || INSTALL_ERROR_RENAMING));
S
Sandeep Somavarapu 已提交
512
			});
S
Sandeep Somavarapu 已提交
513 514
	}

515
	private async installDependenciesAndPackExtensions(installed: ILocalExtension, existing: ILocalExtension | null): Promise<void> {
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
		if (this.galleryService.isEnabled()) {
			const dependenciesAndPackExtensions: string[] = installed.manifest.extensionDependencies || [];
			if (installed.manifest.extensionPack) {
				for (const extension of installed.manifest.extensionPack) {
					// add only those extensions which are new in currently installed extension
					if (!(existing && existing.manifest.extensionPack && existing.manifest.extensionPack.some(old => areSameExtensions({ id: old }, { id: extension })))) {
						if (dependenciesAndPackExtensions.every(e => !areSameExtensions({ id: e }, { id: extension }))) {
							dependenciesAndPackExtensions.push(extension);
						}
					}
				}
			}
			if (dependenciesAndPackExtensions.length) {
				return this.getInstalled()
					.then(installed => {
531 532
						// filter out installed extensions
						const names = dependenciesAndPackExtensions.filter(id => installed.every(({ identifier: galleryIdentifier }) => !areSameExtensions(galleryIdentifier, { id })));
533
						if (names.length) {
S
Sandeep Somavarapu 已提交
534
							return this.galleryService.query({ names, pageSize: dependenciesAndPackExtensions.length }, CancellationToken.None)
535 536
								.then(galleryResult => {
									const extensionsToInstall = galleryResult.firstPage;
537
									return Promise.all(extensionsToInstall.map(e => this.installFromGallery(e)))
B
Benjamin Pasero 已提交
538
										.then(undefined, errors => this.rollback(extensionsToInstall).then(() => Promise.reject(errors), () => Promise.reject(errors)));
539 540
								});
						}
B
Benjamin Pasero 已提交
541
						return;
542 543 544
					});
			}
		}
R
Rob Lourens 已提交
545
		return Promise.resolve(undefined);
546 547
	}

S
Sandeep Somavarapu 已提交
548
	private rollback(extensions: IGalleryExtension[]): Promise<void> {
549
		return this.getInstalled(ExtensionType.User)
S
Sandeep Somavarapu 已提交
550
			.then(installed =>
551
				Promise.all(installed.filter(local => extensions.some(galleryExtension => new ExtensionIdentifierWithVersion(local.identifier, local.manifest.version).equals(new ExtensionIdentifierWithVersion(galleryExtension.identifier, galleryExtension.version)))) // Check with version because we want to rollback the exact version
552
					.map(local => this.uninstall(local, true))))
R
Rob Lourens 已提交
553
			.then(() => undefined, () => undefined);
S
Sandeep Somavarapu 已提交
554 555
	}

S
Sandeep Somavarapu 已提交
556
	uninstall(extension: ILocalExtension, force = false): Promise<void> {
557
		this.logService.trace('ExtensionManagementService#uninstall', extension.identifier.id);
558
		return this.toNonCancellablePromise(this.getInstalled(ExtensionType.User)
S
Sandeep Somavarapu 已提交
559
			.then(installed => {
560 561
				const extensionToUninstall = installed.filter(e => areSameExtensions(e.identifier, extension.identifier))[0];
				if (extensionToUninstall) {
B
Benjamin Pasero 已提交
562
					return this.checkForDependenciesAndUninstall(extensionToUninstall, installed).then(undefined, error => Promise.reject(this.joinErrors(error)));
563
				} else {
S
Sandeep Somavarapu 已提交
564
					return Promise.reject(new Error(nls.localize('notInstalled', "Extension '{0}' is not installed.", extension.manifest.displayName || extension.manifest.name)));
565
				}
S
Sandeep Somavarapu 已提交
566
			}));
S
Sandeep Somavarapu 已提交
567 568
	}

S
Sandeep Somavarapu 已提交
569
	updateMetadata(local: ILocalExtension, metadata: IGalleryMetadata): Promise<ILocalExtension> {
570
		this.logService.trace('ExtensionManagementService#updateMetadata', local.identifier.id);
571
		local.metadata = metadata;
572 573 574 575 576
		return this.saveMetadataForLocalExtension(local)
			.then(localExtension => {
				this.manifestCache.invalidate();
				return localExtension;
			});
577 578
	}

S
Sandeep Somavarapu 已提交
579
	private saveMetadataForLocalExtension(local: ILocalExtension): Promise<ILocalExtension> {
580
		if (!local.metadata) {
S
Sandeep Somavarapu 已提交
581
			return Promise.resolve(local);
582
		}
583
		const manifestPath = path.join(local.location.fsPath, 'package.json');
584 585 586 587 588 589 590
		return pfs.readFile(manifestPath, 'utf8')
			.then(raw => parseManifest(raw))
			.then(({ manifest }) => assign(manifest, { __metadata: local.metadata }))
			.then(manifest => pfs.writeFile(manifestPath, JSON.stringify(manifest, null, '\t')))
			.then(() => local);
	}

591
	private getMetadata(extensionName: string): Promise<IGalleryMetadata | null> {
S
Sandeep Somavarapu 已提交
592 593 594 595
		return this.findGalleryExtensionByName(extensionName)
			.then(galleryExtension => galleryExtension ? <IGalleryMetadata>{ id: galleryExtension.identifier.uuid, publisherDisplayName: galleryExtension.publisherDisplayName, publisherId: galleryExtension.publisherId } : null);
	}

S
Sandeep Somavarapu 已提交
596
	private findGalleryExtension(local: ILocalExtension): Promise<IGalleryExtension> {
597 598 599
		if (local.identifier.uuid) {
			return this.findGalleryExtensionById(local.identifier.uuid)
				.then(galleryExtension => galleryExtension ? galleryExtension : this.findGalleryExtensionByName(local.identifier.id));
S
Sandeep Somavarapu 已提交
600
		}
601
		return this.findGalleryExtensionByName(local.identifier.id);
S
Sandeep Somavarapu 已提交
602 603
	}

S
Sandeep Somavarapu 已提交
604
	private findGalleryExtensionById(uuid: string): Promise<IGalleryExtension> {
S
Sandeep Somavarapu 已提交
605
		return this.galleryService.query({ ids: [uuid], pageSize: 1 }, CancellationToken.None).then(galleryResult => galleryResult.firstPage[0]);
S
Sandeep Somavarapu 已提交
606 607
	}

S
Sandeep Somavarapu 已提交
608
	private findGalleryExtensionByName(name: string): Promise<IGalleryExtension> {
S
Sandeep Somavarapu 已提交
609
		return this.galleryService.query({ names: [name], pageSize: 1 }, CancellationToken.None).then(galleryResult => galleryResult.firstPage[0]);
S
Sandeep Somavarapu 已提交
610 611
	}

612
	private joinErrors(errorOrErrors: (Error | string) | (Array<Error | string>)): Error {
613
		const errors = Array.isArray(errorOrErrors) ? errorOrErrors : [errorOrErrors];
S
Sandeep Somavarapu 已提交
614 615 616 617
		if (errors.length === 1) {
			return errors[0] instanceof Error ? <Error>errors[0] : new Error(<string>errors[0]);
		}
		return errors.reduce<Error>((previousValue: Error, currentValue: Error | string) => {
S
Sandeep Somavarapu 已提交
618
			return new Error(`${previousValue.message}${previousValue.message ? ',' : ''}${currentValue instanceof Error ? currentValue.message : currentValue}`);
S
Sandeep Somavarapu 已提交
619
		}, new Error(''));
J
Joao Moreno 已提交
620 621
	}

S
Sandeep Somavarapu 已提交
622
	private checkForDependenciesAndUninstall(extension: ILocalExtension, installed: ILocalExtension[]): Promise<void> {
J
Joao Moreno 已提交
623
		return this.preUninstallExtension(extension)
624
			.then(() => {
625 626 627
				const packedExtensions = this.getAllPackExtensionsToUninstall(extension, installed);
				if (packedExtensions.length) {
					return this.uninstallExtensions(extension, packedExtensions, installed);
S
Sandeep Somavarapu 已提交
628
				}
S
Sandeep Somavarapu 已提交
629
				return this.uninstallExtensions(extension, [], installed);
630
			})
631
			.then(() => this.postUninstallExtension(extension),
M
Matt Bierner 已提交
632
				error => {
633
					this.postUninstallExtension(extension, new ExtensionManagementError(error instanceof Error ? error.message : error, INSTALL_ERROR_LOCAL));
S
Sandeep Somavarapu 已提交
634
					return Promise.reject(error);
M
Matt Bierner 已提交
635
				});
S
Sandeep Somavarapu 已提交
636 637
	}

S
Sandeep Somavarapu 已提交
638
	private uninstallExtensions(extension: ILocalExtension, otherExtensionsToUninstall: ILocalExtension[], installed: ILocalExtension[]): Promise<void> {
639 640 641 642
		const dependents = this.getDependents(extension, installed);
		if (dependents.length) {
			const remainingDependents = dependents.filter(dependent => extension !== dependent && otherExtensionsToUninstall.indexOf(dependent) === -1);
			if (remainingDependents.length) {
S
Sandeep Somavarapu 已提交
643
				return Promise.reject(new Error(this.getDependentsErrorMessage(extension, remainingDependents)));
644 645
			}
		}
R
Rob Lourens 已提交
646
		return Promise.all([this.uninstallExtension(extension), ...otherExtensionsToUninstall.map(d => this.doUninstall(d))]).then(() => undefined);
647 648
	}

S
Sandeep Somavarapu 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661
	private getDependentsErrorMessage(extension: ILocalExtension, dependents: ILocalExtension[]): string {
		if (dependents.length === 1) {
			return nls.localize('singleDependentError', "Cannot uninstall extension '{0}'. Extension '{1}' depends on this.",
				extension.manifest.displayName || extension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name);
		}
		if (dependents.length === 2) {
			return nls.localize('twoDependentsError', "Cannot uninstall extension '{0}'. Extensions '{1}' and '{2}' depend on this.",
				extension.manifest.displayName || extension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
		}
		return nls.localize('multipleDependentsError', "Cannot uninstall extension '{0}'. Extensions '{1}', '{2}' and others depend on this.",
			extension.manifest.displayName || extension.manifest.name, dependents[0].manifest.displayName || dependents[0].manifest.name, dependents[1].manifest.displayName || dependents[1].manifest.name);
	}

662 663 664 665 666
	private getAllPackExtensionsToUninstall(extension: ILocalExtension, installed: ILocalExtension[], checked: ILocalExtension[] = []): ILocalExtension[] {
		if (checked.indexOf(extension) !== -1) {
			return [];
		}
		checked.push(extension);
667 668
		const extensionsPack = extension.manifest.extensionPack ? extension.manifest.extensionPack : [];
		if (extensionsPack.length) {
669
			const packedExtensions = installed.filter(i => extensionsPack.some(id => areSameExtensions({ id }, i.identifier)));
670 671 672 673 674
			const packOfPackedExtensions: ILocalExtension[] = [];
			for (const packedExtension of packedExtensions) {
				packOfPackedExtensions.push(...this.getAllPackExtensionsToUninstall(packedExtension, installed, checked));
			}
			return [...packedExtensions, ...packOfPackedExtensions];
675
		}
676
		return [];
677 678 679
	}

	private getDependents(extension: ILocalExtension, installed: ILocalExtension[]): ILocalExtension[] {
680
		return installed.filter(e => e.manifest.extensionDependencies && e.manifest.extensionDependencies.some(id => areSameExtensions({ id }, extension.identifier)));
681 682
	}

S
Sandeep Somavarapu 已提交
683
	private doUninstall(extension: ILocalExtension): Promise<void> {
J
Joao Moreno 已提交
684
		return this.preUninstallExtension(extension)
S
Sandeep Somavarapu 已提交
685
			.then(() => this.uninstallExtension(extension))
686
			.then(() => this.postUninstallExtension(extension),
M
Matt Bierner 已提交
687
				error => {
688
					this.postUninstallExtension(extension, new ExtensionManagementError(error instanceof Error ? error.message : error, INSTALL_ERROR_LOCAL));
S
Sandeep Somavarapu 已提交
689
					return Promise.reject(error);
M
Matt Bierner 已提交
690
				});
S
Sandeep Somavarapu 已提交
691
	}
E
Erich Gamma 已提交
692

S
Sandeep Somavarapu 已提交
693
	private preUninstallExtension(extension: ILocalExtension): Promise<void> {
694
		return Promise.resolve(pfs.exists(extension.location.fsPath))
S
Sandeep Somavarapu 已提交
695
			.then(exists => exists ? null : Promise.reject(new Error(nls.localize('notExists', "Could not find extension"))))
696
			.then(() => {
697
				this.logService.info('Uninstalling extension:', extension.identifier.id);
698 699
				this._onUninstallExtension.fire(extension.identifier);
			});
S
Sandeep Somavarapu 已提交
700 701
	}

S
Sandeep Somavarapu 已提交
702
	private uninstallExtension(local: ILocalExtension): Promise<void> {
703
		let promise = this.uninstallingExtensions.get(local.identifier.id);
S
Sandeep Somavarapu 已提交
704 705
		if (!promise) {
			// Set all versions of the extension as uninstalled
706
			promise = createCancelablePromise(token => this.scanUserExtensions(false)
707 708 709
				.then(userExtensions => this.setUninstalled(...userExtensions.filter(u => areSameExtensions(u.identifier, local.identifier))))
				.then(() => { this.uninstallingExtensions.delete(local.identifier.id); }));
			this.uninstallingExtensions.set(local.identifier.id, promise);
S
Sandeep Somavarapu 已提交
710
		}
S
Sandeep Somavarapu 已提交
711
		return promise;
S
Sandeep Somavarapu 已提交
712 713
	}

J
Joao Moreno 已提交
714
	private async postUninstallExtension(extension: ILocalExtension, error?: Error): Promise<void> {
715
		if (error) {
716
			this.logService.error('Failed to uninstall extension:', extension.identifier.id, error.message);
S
Sandeep Somavarapu 已提交
717
		} else {
718
			this.logService.info('Successfully uninstalled extension:', extension.identifier.id);
719
			// only report if extension has a mapped gallery extension. UUID identifies the gallery extension.
720
			if (extension.identifier.uuid) {
721 722
				await this.galleryService.reportStatistic(extension.manifest.publisher, extension.manifest.name, extension.manifest.version, StatisticType.Uninstall);
			}
723
		}
R
Rob Lourens 已提交
724 725
		this.reportTelemetry('extensionGallery:uninstall', getLocalExtensionTelemetryData(extension), undefined, error);
		const errorcode = error ? error instanceof ExtensionManagementError ? error.code : ERROR_UNKNOWN : undefined;
726
		this._onDidUninstallExtension.fire({ identifier: extension.identifier, error: errorcode });
E
Erich Gamma 已提交
727 728
	}

729
	getInstalled(type: ExtensionType | null = null): Promise<ILocalExtension[]> {
730
		const promises: Promise<ILocalExtension[]>[] = [];
J
Joao Moreno 已提交
731

732
		if (type === null || type === ExtensionType.System) {
733
			promises.push(this.scanSystemExtensions().then(null, e => Promise.reject(new ExtensionManagementError(this.joinErrors(e).message, ERROR_SCANNING_SYS_EXTENSIONS))));
J
Joao Moreno 已提交
734 735
		}

736
		if (type === null || type === ExtensionType.User) {
737
			promises.push(this.scanUserExtensions(true).then(null, e => Promise.reject(new ExtensionManagementError(this.joinErrors(e).message, ERROR_SCANNING_USER_EXTENSIONS))));
J
Joao Moreno 已提交
738 739
		}

S
Sandeep Somavarapu 已提交
740
		return Promise.all<ILocalExtension[]>(promises).then(flatten, errors => Promise.reject(this.joinErrors(errors)));
J
Joao Moreno 已提交
741 742
	}

S
Sandeep Somavarapu 已提交
743
	private scanSystemExtensions(): Promise<ILocalExtension[]> {
744
		this.logService.trace('Started scanning system extensions');
745
		const systemExtensionsPromise = this.scanExtensions(this.systemExtensionsPath, ExtensionType.System)
746
			.then(result => {
S
Sandeep Somavarapu 已提交
747
				this.logService.trace('Scanned system extensions:', result.length);
748 749
				return result;
			});
750 751 752 753 754 755 756
		if (this.environmentService.isBuilt) {
			return systemExtensionsPromise;
		}

		// Scan other system extensions during development
		const devSystemExtensionsPromise = this.getDevSystemExtensionsList()
			.then(devSystemExtensionsList => {
757
				console.log(devSystemExtensionsList);
758
				if (devSystemExtensionsList.length) {
759
					return this.scanExtensions(this.devSystemExtensionsPath, ExtensionType.System)
760
						.then(result => {
S
Sandeep Somavarapu 已提交
761
							this.logService.trace('Scanned dev system extensions:', result.length);
762
							return result.filter(r => devSystemExtensionsList.some(id => areSameExtensions(r.identifier, { id })));
763 764 765 766 767 768 769
						});
				} else {
					return [];
				}
			});
		return Promise.all([systemExtensionsPromise, devSystemExtensionsPromise])
			.then(([systemExtensions, devSystemExtensions]) => [...systemExtensions, ...devSystemExtensions]);
J
Joao Moreno 已提交
770 771
	}

S
Sandeep Somavarapu 已提交
772
	private scanUserExtensions(excludeOutdated: boolean): Promise<ILocalExtension[]> {
773
		this.logService.trace('Started scanning user extensions');
774
		return Promise.all([this.getUninstalledExtensions(), this.scanExtensions(this.extensionsPath, ExtensionType.User)])
S
Sandeep Somavarapu 已提交
775
			.then(([uninstalled, extensions]) => {
776
				extensions = extensions.filter(e => !uninstalled[new ExtensionIdentifierWithVersion(e.identifier, e.manifest.version).key()]);
S
Sandeep Somavarapu 已提交
777
				if (excludeOutdated) {
778
					const byExtension: ILocalExtension[][] = groupByExtension(extensions, e => e.identifier);
S
Sandeep Somavarapu 已提交
779
					extensions = byExtension.map(p => p.sort((a, b) => semver.rcompare(a.manifest.version, b.manifest.version))[0]);
S
Sandeep Somavarapu 已提交
780
				}
S
Sandeep Somavarapu 已提交
781
				this.logService.trace('Scanned user extensions:', extensions.length);
S
Sandeep Somavarapu 已提交
782 783
				return extensions;
			});
J
Joao Moreno 已提交
784 785
	}

786
	private scanExtensions(root: string, type: ExtensionType): Promise<ILocalExtension[]> {
J
Joao Moreno 已提交
787
		const limiter = new Limiter<any>(10);
S
Sandeep Somavarapu 已提交
788
		return pfs.readdir(root)
S
Sandeep Somavarapu 已提交
789
			.then(extensionsFolders => Promise.all<ILocalExtension>(extensionsFolders.map(extensionFolder => limiter.queue(() => this.scanExtension(extensionFolder, root, type)))))
S
Sandeep Somavarapu 已提交
790
			.then(extensions => extensions.filter(e => e && e.identifier));
S
Sandeep Somavarapu 已提交
791
	}
E
Erich Gamma 已提交
792

793 794
	private scanExtension(folderName: string, root: string, type: ExtensionType): Promise<ILocalExtension | null> {
		if (type === ExtensionType.User && folderName.indexOf('.') === 0) { // Do not consider user extension folder starting with `.`
S
Sandeep Somavarapu 已提交
795
			return Promise.resolve(null);
796
		}
S
Sandeep Somavarapu 已提交
797 798 799
		const extensionPath = path.join(root, folderName);
		return pfs.readdir(extensionPath)
			.then(children => readManifest(extensionPath)
800
				.then(({ manifest, metadata }) => {
J
Joao Moreno 已提交
801
					const readme = children.filter(child => /^readme(\.txt|\.md|)$/i.test(child))[0];
802
					const readmeUrl = readme ? URI.file(path.join(extensionPath, readme)) : null;
J
Joao Moreno 已提交
803
					const changelog = children.filter(child => /^changelog(\.txt|\.md|)$/i.test(child))[0];
804
					const changelogUrl = changelog ? URI.file(path.join(extensionPath, changelog)) : null;
S
Sandeep Somavarapu 已提交
805 806 807 808 809 810
					const identifier = { id: getGalleryExtensionId(manifest.publisher, manifest.name) };
					const local = <ILocalExtension>{ type, identifier, manifest, metadata, location: URI.file(extensionPath), readmeUrl, changelogUrl };
					if (metadata) {
						this.setMetadata(local, metadata);
					}
					return local;
S
Sandeep Somavarapu 已提交
811
				}))
R
Rob Lourens 已提交
812
			.then(undefined, () => null);
E
Erich Gamma 已提交
813 814
	}

S
Sandeep Somavarapu 已提交
815 816 817 818 819
	private setMetadata(local: ILocalExtension, metadata: IGalleryMetadata): void {
		local.metadata = metadata;
		local.identifier.uuid = metadata.id;
	}

S
Sandeep Somavarapu 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
	async removeDeprecatedExtensions(): Promise<void> {
		await this.removeUninstalledExtensions();
		await this.removeOutdatedExtensions();
	}

	private async removeUninstalledExtensions(): Promise<void> {
		const uninstalled = await this.getUninstalledExtensions();
		const extensions = await this.scanExtensions(this.extensionsPath, ExtensionType.User); // All user extensions
		const installed: Set<string> = new Set<string>();
		for (const e of extensions) {
			if (!uninstalled[new ExtensionIdentifierWithVersion(e.identifier, e.manifest.version).key()]) {
				installed.add(e.identifier.id.toLowerCase());
			}
		}
		const byExtension: ILocalExtension[][] = groupByExtension(extensions, e => e.identifier);
		await Promise.all(byExtension.map(async e => {
			const latest = e.sort((a, b) => semver.rcompare(a.manifest.version, b.manifest.version))[0];
			if (!installed.has(latest.identifier.id.toLowerCase())) {
				await this.extensionLifecycle.postUninstall(latest);
			}
		}));
		const toRemove: ILocalExtension[] = extensions.filter(e => uninstalled[new ExtensionIdentifierWithVersion(e.identifier, e.manifest.version).key()]);
		await Promise.all(toRemove.map(e => this.removeUninstalledExtension(e)));
S
Sandeep Somavarapu 已提交
843
	}
S
Sandeep Somavarapu 已提交
844

S
Sandeep Somavarapu 已提交
845
	private removeOutdatedExtensions(): Promise<void> {
846
		return this.scanExtensions(this.extensionsPath, ExtensionType.User) // All user extensions
S
Sandeep Somavarapu 已提交
847 848
			.then(extensions => {
				const toRemove: ILocalExtension[] = [];
S
Sandeep Somavarapu 已提交
849

S
Sandeep Somavarapu 已提交
850
				// Outdated extensions
851
				const byExtension: ILocalExtension[][] = groupByExtension(extensions, e => e.identifier);
S
Sandeep Somavarapu 已提交
852 853
				toRemove.push(...flatten(byExtension.map(p => p.sort((a, b) => semver.rcompare(a.manifest.version, b.manifest.version)).slice(1))));

S
Sandeep Somavarapu 已提交
854
				return Promise.all(toRemove.map(extension => this.removeExtension(extension, 'outdated')));
R
Rob Lourens 已提交
855
			}).then(() => undefined);
S
Sandeep Somavarapu 已提交
856 857
	}

S
Sandeep Somavarapu 已提交
858
	private removeUninstalledExtension(extension: ILocalExtension): Promise<void> {
859
		return this.removeExtension(extension, 'uninstalled')
860
			.then(() => this.withUninstalledExtensions(uninstalled => delete uninstalled[new ExtensionIdentifierWithVersion(extension.identifier, extension.manifest.version).key()]))
R
Rob Lourens 已提交
861
			.then(() => undefined);
S
Sandeep Somavarapu 已提交
862 863
	}

S
Sandeep Somavarapu 已提交
864
	private removeExtension(extension: ILocalExtension, type: string): Promise<void> {
865 866
		this.logService.trace(`Deleting ${type} extension from disk`, extension.identifier.id, extension.location.fsPath);
		return pfs.rimraf(extension.location.fsPath).then(() => this.logService.info('Deleted from disk', extension.identifier.id, extension.location.fsPath));
J
Joao Moreno 已提交
867 868
	}

869 870
	private isUninstalled(identifier: ExtensionIdentifierWithVersion): Promise<boolean> {
		return this.filterUninstalled(identifier).then(uninstalled => uninstalled.length === 1);
871 872
	}

873
	private filterUninstalled(...identifiers: ExtensionIdentifierWithVersion[]): Promise<string[]> {
874
		return this.withUninstalledExtensions(allUninstalled => {
M
Matt Bierner 已提交
875
			const uninstalled: string[] = [];
876 877 878
			for (const identifier of identifiers) {
				if (!!allUninstalled[identifier.key()]) {
					uninstalled.push(identifier.key());
879 880
				}
			}
881
			return uninstalled;
882
		});
883 884
	}

M
Matt Bierner 已提交
885
	private setUninstalled(...extensions: ILocalExtension[]): Promise<{ [id: string]: boolean }> {
886 887
		const ids: ExtensionIdentifierWithVersion[] = extensions.map(e => new ExtensionIdentifierWithVersion(e.identifier, e.manifest.version));
		return this.withUninstalledExtensions(uninstalled => assign(uninstalled, ids.reduce((result, id) => { result[id.key()] = true; return result; }, {} as { [id: string]: boolean })));
888 889
	}

890 891
	private unsetUninstalled(extensionIdentifier: ExtensionIdentifierWithVersion): Promise<void> {
		return this.withUninstalledExtensions<void>(uninstalled => delete uninstalled[extensionIdentifier.key()]);
892 893
	}

S
Sandeep Somavarapu 已提交
894
	private getUninstalledExtensions(): Promise<{ [id: string]: boolean; }> {
895
		return this.withUninstalledExtensions(uninstalled => uninstalled);
896 897
	}

898 899
	private async withUninstalledExtensions<T>(fn: (uninstalled: { [id: string]: boolean; }) => T): Promise<T> {
		return await this.uninstalledFileLimiter.queue(() => {
900
			let result: T | null = null;
901
			return pfs.readFile(this.uninstalledPath, 'utf8')
R
Rob Lourens 已提交
902
				.then(undefined, err => err.code === 'ENOENT' ? Promise.resolve('{}') : Promise.reject(err))
J
Johannes Rieken 已提交
903
				.then<{ [id: string]: boolean }>(raw => { try { return JSON.parse(raw); } catch (e) { return {}; } })
904 905 906 907
				.then(uninstalled => { result = fn(uninstalled); return uninstalled; })
				.then(uninstalled => {
					if (Object.keys(uninstalled).length === 0) {
						return pfs.rimraf(this.uninstalledPath);
908
					} else {
909 910
						const raw = JSON.stringify(uninstalled);
						return pfs.writeFile(this.uninstalledPath, raw);
911 912 913
					}
				})
				.then(() => result);
914
		});
915
	}
916

S
Sandeep Somavarapu 已提交
917
	getExtensionsReport(): Promise<IReportedExtension[]> {
918
		const now = new Date().getTime();
J
Joao Moreno 已提交
919

920 921 922 923
		if (!this.reportedExtensions || now - this.lastReportTimestamp > 1000 * 60 * 5) { // 5 minute cache freshness
			this.reportedExtensions = this.updateReportCache();
			this.lastReportTimestamp = now;
		}
J
Joao Moreno 已提交
924

925
		return this.reportedExtensions;
J
Joao Moreno 已提交
926 927
	}

S
Sandeep Somavarapu 已提交
928
	private updateReportCache(): Promise<IReportedExtension[]> {
J
Joao Moreno 已提交
929 930
		this.logService.trace('ExtensionManagementService.refreshReportedCache');

931
		return this.galleryService.getExtensionsReport()
J
Joao Moreno 已提交
932 933
			.then(result => {
				this.logService.trace(`ExtensionManagementService.refreshReportedCache - got ${result.length} reported extensions from service`);
934 935 936 937
				return result;
			}, err => {
				this.logService.trace('ExtensionManagementService.refreshReportedCache - failed to get extension report');
				return [];
J
Joao Moreno 已提交
938 939
			});
	}
940

941 942 943 944 945 946 947 948 949
	private _devSystemExtensionsPath: string | null = null;
	private get devSystemExtensionsPath(): string {
		if (!this._devSystemExtensionsPath) {
			this._devSystemExtensionsPath = path.normalize(path.join(getPathFromAmdModule(require, ''), '..', '.build', 'builtInExtensions'));
		}
		return this._devSystemExtensionsPath;
	}

	private getDevSystemExtensionsList(): Promise<string[]> {
950
		return Promise.resolve(product.builtInExtensions ? product.builtInExtensions.map(e => e.name) : []);
951 952
	}

S
Sandeep Somavarapu 已提交
953 954
	private toNonCancellablePromise<T>(promise: Promise<T>): Promise<T> {
		return new Promise((c, e) => promise.then(result => c(result), error => e(error)));
S
Sandeep Somavarapu 已提交
955 956
	}

957
	private reportTelemetry(eventName: string, extensionData: any, duration?: number, error?: Error): void {
R
Rob Lourens 已提交
958
		const errorcode = error ? error instanceof ExtensionManagementError ? error.code : ERROR_UNKNOWN : undefined;
959 960 961 962 963
		/* __GDPR__
			"extensionGallery:install" : {
				"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
				"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
				"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
964
				"recommendationReason": { "retiredFromVersion": "1.23.0", "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
				"${include}": [
					"${GalleryExtensionTelemetryData}"
				]
			}
		*/
		/* __GDPR__
			"extensionGallery:uninstall" : {
				"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
				"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
				"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
				"${include}": [
					"${GalleryExtensionTelemetryData}"
				]
			}
		*/
980 981 982 983 984 985 986 987 988 989
		/* __GDPR__
			"extensionGallery:update" : {
				"success": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
				"duration" : { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true },
				"errorcode": { "classification": "CallstackOrException", "purpose": "PerformanceAndHealth" },
				"${include}": [
					"${GalleryExtensionTelemetryData}"
				]
			}
		*/
990 991
		this.telemetryService.publicLog(eventName, assign(extensionData, { success: !error, duration, errorcode }));
	}
992
}