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

'use strict';

import nls = require('vs/nls');
import * as path from 'path';
import * as pfs from 'vs/base/node/pfs';
11
import * as errors from 'vs/base/common/errors';
E
Erich Gamma 已提交
12
import { assign } from 'vs/base/common/objects';
13
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
14
import { flatten, distinct } from 'vs/base/common/arrays';
E
Erich Gamma 已提交
15 16
import { extract, buffer } from 'vs/base/node/zip';
import { Promise, TPromise } from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
17 18
import {
	IExtensionManagementService, IExtensionGalleryService, ILocalExtension,
19
	IGalleryExtension, IExtensionManifest, IGalleryMetadata,
S
Sandeep Somavarapu 已提交
20
	InstallExtensionEvent, DidInstallExtensionEvent, DidUninstallExtensionEvent, LocalExtensionType
J
Joao Moreno 已提交
21
} from 'vs/platform/extensionManagement/common/extensionManagement';
S
Sandeep Somavarapu 已提交
22
import { getLocalExtensionIdFromGallery, getLocalExtensionIdFromManifest, getGalleryExtensionIdFromLocal, getIdAndVersionFromLocalExtensionId, adoptToGalleryExtensionId } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
23
import { localizeManifest } from '../common/extensionNls';
J
Joao Moreno 已提交
24
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
E
Erich Gamma 已提交
25
import { Limiter } from 'vs/base/common/async';
J
Joao Moreno 已提交
26
import Event, { Emitter } from 'vs/base/common/event';
J
Joao Moreno 已提交
27
import * as semver from 'semver';
28
import { groupBy, values } from 'vs/base/common/collections';
J
João Moreno 已提交
29
import URI from 'vs/base/common/uri';
30
import { IChoiceService, Severity } from 'vs/platform/message/common/message';
E
Erich Gamma 已提交
31

J
Joao Moreno 已提交
32 33
const SystemExtensionsRoot = path.normalize(path.join(URI.parse(require.toUrl('')).fsPath, '..', 'extensions'));

J
Joao Moreno 已提交
34
function parseManifest(raw: string): TPromise<{ manifest: IExtensionManifest; metadata: IGalleryMetadata; }> {
E
Erich Gamma 已提交
35 36
	return new Promise((c, e) => {
		try {
J
Joao Moreno 已提交
37 38 39 40
			const manifest = JSON.parse(raw);
			const metadata = manifest.__metadata || null;
			delete manifest.__metadata;
			c({ manifest, metadata });
E
Erich Gamma 已提交
41 42 43 44 45 46
		} catch (err) {
			e(new Error(nls.localize('invalidManifest', "Extension invalid: package.json is not a JSON file.")));
		}
	});
}

47
function validate(zipPath: string): TPromise<IExtensionManifest> {
E
Erich Gamma 已提交
48 49
	return buffer(zipPath, 'extension/package.json')
		.then(buffer => parseManifest(buffer.toString('utf8')))
50
		.then(({ manifest }) => TPromise.as(manifest));
E
Erich Gamma 已提交
51 52
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
function readManifest(extensionPath: string): TPromise<{ manifest: IExtensionManifest; metadata: IGalleryMetadata; }> {
	const promises = [
		pfs.readFile(path.join(extensionPath, 'package.json'), 'utf8')
			.then(raw => parseManifest(raw)),
		pfs.readFile(path.join(extensionPath, 'package.nls.json'), 'utf8')
			.then<string>(null, err => err.code !== 'ENOENT' ? TPromise.wrapError(err) : '{}')
			.then(raw => JSON.parse(raw))
	];

	return TPromise.join<any>(promises).then(([{ manifest, metadata }, translations]) => {
		return {
			manifest: localizeManifest(manifest, translations),
			metadata
		};
	});
}

J
Joao Moreno 已提交
70
export class ExtensionManagementService implements IExtensionManagementService {
E
Erich Gamma 已提交
71

72
	_serviceBrand: any;
E
Erich Gamma 已提交
73 74

	private extensionsPath: string;
75 76
	private obsoletePath: string;
	private obsoleteFileLimiter: Limiter<void>;
77
	private disposables: IDisposable[] = [];
E
Erich Gamma 已提交
78

J
Joao Moreno 已提交
79 80
	private _onInstallExtension = new Emitter<InstallExtensionEvent>();
	onInstallExtension: Event<InstallExtensionEvent> = this._onInstallExtension.event;
E
Erich Gamma 已提交
81

J
Joao Moreno 已提交
82 83
	private _onDidInstallExtension = new Emitter<DidInstallExtensionEvent>();
	onDidInstallExtension: Event<DidInstallExtensionEvent> = this._onDidInstallExtension.event;
E
Erich Gamma 已提交
84

J
Joao Moreno 已提交
85 86
	private _onUninstallExtension = new Emitter<string>();
	onUninstallExtension: Event<string> = this._onUninstallExtension.event;
E
Erich Gamma 已提交
87

S
Sandeep Somavarapu 已提交
88 89
	private _onDidUninstallExtension = new Emitter<DidUninstallExtensionEvent>();
	onDidUninstallExtension: Event<DidUninstallExtensionEvent> = this._onDidUninstallExtension.event;
E
Erich Gamma 已提交
90 91

	constructor(
92
		@IEnvironmentService private environmentService: IEnvironmentService,
93
		@IChoiceService private choiceService: IChoiceService,
94
		@IExtensionGalleryService private galleryService: IExtensionGalleryService
E
Erich Gamma 已提交
95
	) {
J
Joao Moreno 已提交
96
		this.extensionsPath = environmentService.extensionsPath;
97 98
		this.obsoletePath = path.join(this.extensionsPath, '.obsolete');
		this.obsoleteFileLimiter = new Limiter(1);
E
Erich Gamma 已提交
99 100
	}

101
	install(zipPath: string): TPromise<void> {
102 103
		zipPath = path.resolve(zipPath);

104
		return validate(zipPath).then<void>(manifest => {
105
			const id = getLocalExtensionIdFromManifest(manifest);
106

107 108 109 110
			return this.isObsolete(id).then(isObsolete => {
				if (isObsolete) {
					return TPromise.wrapError(new Error(nls.localize('restartCode', "Please restart Code before reinstalling {0}.", manifest.displayName || manifest.name)));
				}
J
Joao Moreno 已提交
111

112
				this._onInstallExtension.fire({ id, zipPath });
J
Joao Moreno 已提交
113

114 115
				return this.installExtension(zipPath, id)
					.then(
J
Johannes Rieken 已提交
116 117
					local => this._onDidInstallExtension.fire({ id, zipPath, local }),
					error => { this._onDidInstallExtension.fire({ id, zipPath, error }); return TPromise.wrapError(error); }
118
					);
119
			});
120
		});
E
Erich Gamma 已提交
121 122
	}

S
Sandeep Somavarapu 已提交
123
	installFromGallery(extension: IGalleryExtension, promptToInstallDependencies: boolean = true): TPromise<void> {
124
		const id = getLocalExtensionIdFromGallery(extension, extension.version);
J
Joao Moreno 已提交
125

126 127 128 129 130
		return this.isObsolete(id).then(isObsolete => {
			if (isObsolete) {
				return TPromise.wrapError<void>(new Error(nls.localize('restartCode', "Please restart Code before reinstalling {0}.", extension.displayName || extension.name)));
			}
			this._onInstallExtension.fire({ id, gallery: extension });
S
Sandeep Somavarapu 已提交
131
			return this.installCompatibleVersion(extension, true, promptToInstallDependencies)
S
Sandeep Somavarapu 已提交
132
				.then(
J
Johannes Rieken 已提交
133 134 135 136 137
				local => this._onDidInstallExtension.fire({ id, local, gallery: extension }),
				error => {
					this._onDidInstallExtension.fire({ id, gallery: extension, error });
					return TPromise.wrapError(error);
				}
S
Sandeep Somavarapu 已提交
138
				);
139 140 141
		});
	}

142
	private installCompatibleVersion(extension: IGalleryExtension, installDependencies: boolean, promptToInstallDependencies: boolean): TPromise<ILocalExtension> {
143
		return this.galleryService.loadCompatibleVersion(extension)
144
			.then(compatibleVersion => this.getDependenciesToInstall(extension, installDependencies)
145 146
				.then(dependencies => {
					if (!dependencies.length) {
147
						return this.downloadAndInstall(compatibleVersion);
148 149
					}
					if (promptToInstallDependencies) {
150
						const message = nls.localize('installDependeciesConfirmation', "Installing '{0}' also installs its dependencies. Would you like to continue?", extension.displayName);
151
						const options = [
152 153
							nls.localize('install', "Yes"),
							nls.localize('doNotInstall', "No")
154
						];
S
Sandeep Somavarapu 已提交
155
						return this.choiceService.choose(Severity.Info, message, options, true)
156
							.then(value => {
157 158
								if (value === 0) {
									return this.installWithDependencies(compatibleVersion);
159
								}
160
								return TPromise.wrapError(errors.canceled());
161 162 163
							}, error => TPromise.wrapError(errors.canceled()));
					} else {
						return this.installWithDependencies(compatibleVersion);
164 165
					}
				})
J
Johannes Rieken 已提交
166
			);
167 168
	}

169 170 171 172 173
	private getDependenciesToInstall(extension: IGalleryExtension, checkDependecies: boolean): TPromise<string[]> {
		if (!checkDependecies) {
			return TPromise.wrap([]);
		}
		// Filter out self
S
Sandeep Somavarapu 已提交
174
		const dependencies = extension.properties.dependencies ? extension.properties.dependencies.filter(id => id !== extension.id) : [];
175 176 177 178 179 180 181
		if (!dependencies.length) {
			return TPromise.wrap([]);
		}
		// Filter out installed dependencies
		return this.getInstalled().then(installed => {
			return dependencies.filter(dep => installed.every(i => `${i.manifest.publisher}.${i.manifest.name}` !== dep));
		});
182 183 184 185
	}

	private installWithDependencies(extension: IGalleryExtension): TPromise<ILocalExtension> {
		return this.galleryService.getAllDependencies(extension)
186
			.then(allDependencies => this.filterDependenciesToInstall(extension, allDependencies))
187
			.then(toInstall => this.filterObsolete(...toInstall.map(i => getLocalExtensionIdFromGallery(i, i.version)))
188 189 190 191 192 193 194
				.then((obsolete) => {
					if (obsolete.length) {
						return TPromise.wrapError<ILocalExtension>(new Error(nls.localize('restartCode', "Please restart Code before reinstalling {0}.", extension.displayName || extension.name)));
					}
					return this.bulkInstallWithDependencies(extension, toInstall);
				})
			);
195 196 197
	}

	private bulkInstallWithDependencies(extension: IGalleryExtension, dependecies: IGalleryExtension[]): TPromise<ILocalExtension> {
198
		for (const dependency of dependecies) {
199
			const id = getLocalExtensionIdFromGallery(dependency, dependency.version);
200 201
			this._onInstallExtension.fire({ id, gallery: dependency });
		}
202
		return this.downloadAndInstall(extension)
203
			.then(localExtension => {
204
				return TPromise.join(dependecies.map((dep) => this.installCompatibleVersion(dep, false, false)))
205
					.then(installedLocalExtensions => {
206 207 208 209 210
						for (const installedLocalExtension of installedLocalExtensions) {
							const gallery = this.getGalleryExtensionForLocalExtension(dependecies, installedLocalExtension);
							this._onDidInstallExtension.fire({ id: installedLocalExtension.id, local: installedLocalExtension, gallery });
						}
						return localExtension;
211
					}, error => {
212 213 214 215
						return this.rollback(localExtension, dependecies).then(() => {
							return TPromise.wrapError(Array.isArray(error) ? error[error.length - 1] : error);
						});
					});
216 217 218
			})
			.then(localExtension => localExtension, error => {
				for (const dependency of dependecies) {
219
					this._onDidInstallExtension.fire({ id: getLocalExtensionIdFromGallery(dependency, dependency.version), gallery: dependency, error });
220 221
				}
				return TPromise.wrapError(error);
222
			});
223 224 225
	}

	private rollback(localExtension: ILocalExtension, dependecies: IGalleryExtension[]): TPromise<void> {
S
Sandeep Somavarapu 已提交
226
		return this.doUninstall(localExtension.id)
J
Joao Moreno 已提交
227
			.then(() => this.filterOutUninstalled(dependecies))
S
Sandeep Somavarapu 已提交
228
			.then(installed => TPromise.join(installed.map((i) => this.doUninstall(i.id))))
J
Joao Moreno 已提交
229
			.then(() => null);
230 231
	}

232
	private filterDependenciesToInstall(extension: IGalleryExtension, dependencies: IGalleryExtension[]): TPromise<IGalleryExtension[]> {
J
Joao Moreno 已提交
233 234
		return this.getInstalled()
			.then(local => {
235
				return dependencies.filter(d => {
236
					if (extension.uuid === d.uuid) {
237 238
						return false;
					}
239
					const extensionId = getLocalExtensionIdFromGallery(d, d.version);
J
Joao Moreno 已提交
240 241
					return local.every(local => local.id !== extensionId);
				});
242
			});
243 244
	}

J
Joao Moreno 已提交
245 246 247
	private filterOutUninstalled(extensions: IGalleryExtension[]): TPromise<ILocalExtension[]> {
		return this.getInstalled()
			.then(installed => installed.filter(local => !!this.getGalleryExtensionForLocalExtension(extensions, local)));
248
	}
249

250
	private getGalleryExtensionForLocalExtension(galleryExtensions: IGalleryExtension[], localExtension: ILocalExtension): IGalleryExtension {
251
		const filtered = galleryExtensions.filter(galleryExtension => getLocalExtensionIdFromGallery(galleryExtension, galleryExtension.version) === localExtension.id);
252 253 254
		return filtered.length ? filtered[0] : null;
	}

255
	private downloadAndInstall(extension: IGalleryExtension): TPromise<ILocalExtension> {
256
		const id = getLocalExtensionIdFromGallery(extension, extension.version);
257
		const metadata = {
258
			id: extension.uuid,
J
Joao Moreno 已提交
259 260 261 262
			publisherId: extension.publisherId,
			publisherDisplayName: extension.publisherDisplayName,
		};

263 264 265
		return this.galleryService.download(extension)
			.then(zipPath => validate(zipPath).then(() => zipPath))
			.then(zipPath => this.installExtension(zipPath, id, metadata));
266
	}
J
Joao Moreno 已提交
267

268
	private installExtension(zipPath: string, id: string, metadata: IGalleryMetadata = null): TPromise<ILocalExtension> {
J
Joao Moreno 已提交
269 270 271
		const extensionPath = path.join(this.extensionsPath, id);

		return extract(zipPath, extensionPath, { sourcePath: 'extension', overwrite: true })
272
			.then(() => readManifest(extensionPath))
J
Joao Moreno 已提交
273
			.then(({ manifest }) => {
J
Joao Moreno 已提交
274 275
				return pfs.readdir(extensionPath).then(children => {
					const readme = children.filter(child => /^readme(\.txt|\.md|)$/i.test(child))[0];
J
João Moreno 已提交
276
					const readmeUrl = readme ? URI.file(path.join(extensionPath, readme)).toString() : null;
277 278
					const changelog = children.filter(child => /^changelog(\.txt|\.md|)$/i.test(child))[0];
					const changelogUrl = changelog ? URI.file(path.join(extensionPath, changelog)).toString() : null;
J
Joao Moreno 已提交
279
					const type = LocalExtensionType.User;
280

J
Joao Moreno 已提交
281
					const local: ILocalExtension = { type, id, manifest, metadata, path: extensionPath, readmeUrl, changelogUrl };
282
					const manifestPath = path.join(extensionPath, 'package.json');
J
Joao Moreno 已提交
283

284 285 286 287
					return pfs.readFile(manifestPath, 'utf8')
						.then(raw => parseManifest(raw))
						.then(({ manifest }) => assign(manifest, { __metadata: metadata }))
						.then(manifest => pfs.writeFile(manifestPath, JSON.stringify(manifest, null, '\t')))
288
						.then(() => local);
J
Joao Moreno 已提交
289
				});
290
			});
E
Erich Gamma 已提交
291 292
	}

J
Joao Moreno 已提交
293
	uninstall(extension: ILocalExtension): TPromise<void> {
J
Joao Moreno 已提交
294 295 296 297
		return this.removeOutdatedExtensions().then(() => {
			return this.scanUserExtensions().then<void>(installed => {
				const promises = installed
					.filter(e => e.manifest.publisher === extension.manifest.publisher && e.manifest.name === extension.manifest.name)
S
Sandeep Somavarapu 已提交
298
					.map(e => this.checkForDependenciesAndUninstall(e, installed));
J
Joao Moreno 已提交
299 300
				return TPromise.join(promises);
			});
J
Joao Moreno 已提交
301 302 303
		});
	}

S
Sandeep Somavarapu 已提交
304
	private checkForDependenciesAndUninstall(extension: ILocalExtension, installed: ILocalExtension[]): TPromise<void> {
305
		return this.preUninstallExtension(extension.id)
S
Sandeep Somavarapu 已提交
306
			.then(() => this.hasDependencies(extension, installed) ? this.promptForDependenciesAndUninstall(extension, installed) : this.promptAndUninstall(extension, installed))
307 308 309 310 311
			.then(() => this.postUninstallExtension(extension.id),
			error => {
				this.postUninstallExtension(extension.id, error);
				return TPromise.wrapError(error);
			});
S
Sandeep Somavarapu 已提交
312 313
	}

314 315
	private hasDependencies(extension: ILocalExtension, installed: ILocalExtension[]): boolean {
		if (extension.manifest.extensionDependencies && extension.manifest.extensionDependencies.length) {
S
Sandeep Somavarapu 已提交
316
			return installed.some(i => extension.manifest.extensionDependencies.indexOf(getGalleryExtensionIdFromLocal(i)) !== -1);
317 318 319 320
		}
		return false;
	}

S
Sandeep Somavarapu 已提交
321
	private promptForDependenciesAndUninstall(extension: ILocalExtension, installed: ILocalExtension[]): TPromise<void> {
322
		const message = nls.localize('uninstallDependeciesConfirmation', "Would you like to uninstall '{0}' only or its dependencies also?", extension.manifest.displayName || extension.manifest.name);
S
Sandeep Somavarapu 已提交
323 324 325 326 327
		const options = [
			nls.localize('uninstallOnly', "Only"),
			nls.localize('uninstallAll', "All"),
			nls.localize('cancel', "Cancel")
		];
S
Sandeep Somavarapu 已提交
328
		return this.choiceService.choose(Severity.Info, message, options, true)
S
Sandeep Somavarapu 已提交
329 330
			.then<void>(value => {
				if (value === 0) {
331
					return this.uninstallWithDependencies(extension, [], installed);
S
Sandeep Somavarapu 已提交
332 333
				}
				if (value === 1) {
334 335
					const dependencies = distinct(this.getDependenciesToUninstallRecursively(extension, installed, [])).filter(e => e !== extension);
					return this.uninstallWithDependencies(extension, dependencies, installed);
S
Sandeep Somavarapu 已提交
336 337 338 339 340
				}
				return TPromise.wrapError(errors.canceled());
			}, error => TPromise.wrapError(errors.canceled()));
	}

S
Sandeep Somavarapu 已提交
341 342 343 344 345 346
	private promptAndUninstall(extension: ILocalExtension, installed: ILocalExtension[]): TPromise<void> {
		const message = nls.localize('uninstallConfirmation', "Are you sure you want to uninstall '{0}'?", extension.manifest.displayName || extension.manifest.name);
		const options = [
			nls.localize('ok', "Ok"),
			nls.localize('cancel', "Cancel")
		];
S
Sandeep Somavarapu 已提交
347
		return this.choiceService.choose(Severity.Info, message, options, true)
S
Sandeep Somavarapu 已提交
348 349 350 351 352 353 354 355
			.then<void>(value => {
				if (value === 0) {
					return this.uninstallWithDependencies(extension, [], installed);
				}
				return TPromise.wrapError(errors.canceled());
			}, error => TPromise.wrapError(errors.canceled()));
	}

356 357
	private uninstallWithDependencies(extension: ILocalExtension, dependencies: ILocalExtension[], installed: ILocalExtension[]): TPromise<void> {
		const dependenciesToUninstall = this.filterDependents(extension, dependencies, installed);
358
		let dependents = this.getDependents(extension, installed).filter(dependent => extension !== dependent && dependenciesToUninstall.indexOf(dependent) === -1);
359
		if (dependents.length) {
S
Sandeep Somavarapu 已提交
360
			return TPromise.wrapError<void>(this.getDependentsErrorMessage(extension, dependents));
361
		}
362 363 364
		return TPromise.join([this.uninstallExtension(extension.id), ...dependenciesToUninstall.map(d => this.doUninstall(d.id))]).then(() => null);
	}

S
Sandeep Somavarapu 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377
	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);
	}

378 379 380 381 382 383 384 385
	private getDependenciesToUninstallRecursively(extension: ILocalExtension, installed: ILocalExtension[], checked: ILocalExtension[]): ILocalExtension[] {
		if (checked.indexOf(extension) !== -1) {
			return [];
		}
		checked.push(extension);
		if (!extension.manifest.extensionDependencies || extension.manifest.extensionDependencies.length === 0) {
			return [];
		}
S
Sandeep Somavarapu 已提交
386
		const dependenciesToUninstall = installed.filter(i => extension.manifest.extensionDependencies.indexOf(getGalleryExtensionIdFromLocal(i)) !== -1);
387 388 389 390 391 392 393 394 395 396 397 398
		const depsOfDeps = [];
		for (const dep of dependenciesToUninstall) {
			depsOfDeps.push(...this.getDependenciesToUninstallRecursively(dep, installed, checked));
		}
		return [...dependenciesToUninstall, ...depsOfDeps];
	}

	private filterDependents(extension: ILocalExtension, dependencies: ILocalExtension[], installed: ILocalExtension[]): ILocalExtension[] {
		installed = installed.filter(i => i !== extension && i.manifest.extensionDependencies && i.manifest.extensionDependencies.length > 0);
		let result = dependencies.slice(0);
		for (let i = 0; i < dependencies.length; i++) {
			const dep = dependencies[i];
399
			const dependents = this.getDependents(dep, installed).filter(e => dependencies.indexOf(e) === -1);
400 401 402
			if (dependents.length) {
				result.splice(i - (dependencies.length - result.length), 1);
			}
S
Sandeep Somavarapu 已提交
403
		}
404
		return result;
S
Sandeep Somavarapu 已提交
405 406
	}

407
	private getDependents(extension: ILocalExtension, installed: ILocalExtension[]): ILocalExtension[] {
S
Sandeep Somavarapu 已提交
408
		return installed.filter(e => e.manifest.extensionDependencies && e.manifest.extensionDependencies.indexOf(getGalleryExtensionIdFromLocal(extension)) !== -1);
409 410
	}

S
Sandeep Somavarapu 已提交
411 412 413 414 415 416 417 418 419
	private doUninstall(id: string): TPromise<void> {
		return this.preUninstallExtension(id)
			.then(() => this.uninstallExtension(id))
			.then(() => this.postUninstallExtension(id),
			error => {
				this.postUninstallExtension(id, error);
				return TPromise.wrapError(error);
			});
	}
E
Erich Gamma 已提交
420

S
Sandeep Somavarapu 已提交
421 422
	private preUninstallExtension(id: string): TPromise<void> {
		const extensionPath = path.join(this.extensionsPath, id);
E
Erich Gamma 已提交
423 424
		return pfs.exists(extensionPath)
			.then(exists => exists ? null : Promise.wrapError(new Error(nls.localize('notExists', "Could not find extension"))))
S
Sandeep Somavarapu 已提交
425 426 427 428 429 430
			.then(() => this._onUninstallExtension.fire(id));
	}

	private uninstallExtension(id: string): TPromise<void> {
		const extensionPath = path.join(this.extensionsPath, id);
		return this.setObsolete(id)
E
Erich Gamma 已提交
431
			.then(() => pfs.rimraf(extensionPath))
S
Sandeep Somavarapu 已提交
432 433 434 435 436
			.then(() => this.unsetObsolete(id));
	}

	private postUninstallExtension(id: string, error?: any): TPromise<void> {
		return this._onDidUninstallExtension.fire({ id, error });
E
Erich Gamma 已提交
437 438
	}

J
Joao Moreno 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	getInstalled(type: LocalExtensionType = null): TPromise<ILocalExtension[]> {
		const promises = [];

		if (type === null || type === LocalExtensionType.System) {
			promises.push(this.scanSystemExtensions());
		}

		if (type === null || type === LocalExtensionType.User) {
			promises.push(this.scanUserExtensions());
		}

		return TPromise.join(promises).then(flatten);
	}

	private scanSystemExtensions(): TPromise<ILocalExtension[]> {
		return this.scanExtensions(SystemExtensionsRoot, LocalExtensionType.System);
	}

	private scanUserExtensions(): TPromise<ILocalExtension[]> {
		return this.scanExtensions(this.extensionsPath, LocalExtensionType.User).then(extensions => {
459
			const byId = values(groupBy(extensions, p => getGalleryExtensionIdFromLocal(p)));
J
Joao Moreno 已提交
460
			return byId.map(p => p.sort((a, b) => semver.rcompare(a.manifest.version, b.manifest.version))[0]);
J
Joao Moreno 已提交
461 462 463
		});
	}

J
Joao Moreno 已提交
464
	private scanExtensions(root: string, type: LocalExtensionType): TPromise<ILocalExtension[]> {
E
Erich Gamma 已提交
465 466
		const limiter = new Limiter(10);

J
Joao Moreno 已提交
467 468 469 470 471 472 473 474 475 476 477
		return this.scanExtensionFolders(root)
			.then(extensionIds => Promise.join(extensionIds.map(id => {
				const extensionPath = path.join(root, id);

				const each = () => pfs.readdir(extensionPath).then(children => {
					const readme = children.filter(child => /^readme(\.txt|\.md|)$/i.test(child))[0];
					const readmeUrl = readme ? URI.file(path.join(extensionPath, readme)).toString() : null;
					const changelog = children.filter(child => /^changelog(\.txt|\.md|)$/i.test(child))[0];
					const changelogUrl = changelog ? URI.file(path.join(extensionPath, changelog)).toString() : null;

					return readManifest(extensionPath)
S
Sandeep Somavarapu 已提交
478 479 480 481 482 483
						.then<ILocalExtension>(({ manifest, metadata }) => {
							if (manifest.extensionDependencies) {
								manifest.extensionDependencies = manifest.extensionDependencies.map(id => adoptToGalleryExtensionId(id));
							}
							return { type, id, manifest, metadata, path: extensionPath, readmeUrl, changelogUrl };
						});
J
Joao Moreno 已提交
484 485 486 487 488 489 490 491
				}).then(null, () => null);

				return limiter.queue(each);
			})))
			.then(result => result.filter(a => !!a));
	}

	private scanExtensionFolders(root: string): TPromise<string[]> {
492
		return this.getObsoleteExtensions()
J
Joao Moreno 已提交
493
			.then(obsolete => pfs.readdir(root).then(extensions => extensions.filter(id => !obsolete[id])));
E
Erich Gamma 已提交
494 495
	}

J
Joao Moreno 已提交
496 497 498 499 500 501
	removeDeprecatedExtensions(): TPromise<any> {
		return TPromise.join([
			this.removeOutdatedExtensions(),
			this.removeObsoleteExtensions()
		]);
	}
502

J
Joao Moreno 已提交
503 504 505 506
	private removeOutdatedExtensions(): TPromise<any> {
		return this.getOutdatedExtensionIds()
			.then(extensionIds => this.removeExtensions(extensionIds));
	}
507

J
Joao Moreno 已提交
508 509 510 511 512 513 514 515 516 517 518
	private removeObsoleteExtensions(): TPromise<any> {
		return this.getObsoleteExtensions()
			.then(obsolete => Object.keys(obsolete))
			.then(extensionIds => this.removeExtensions(extensionIds));
	}

	private removeExtensions(extensionsIds: string[]): TPromise<any> {
		return TPromise.join(extensionsIds.map(id => {
			return pfs.rimraf(path.join(this.extensionsPath, id))
				.then(() => this.withObsoleteExtensions(obsolete => delete obsolete[id]));
		}));
519 520
	}

J
Joao Moreno 已提交
521 522 523 524
	private getOutdatedExtensionIds(): TPromise<string[]> {
		return this.scanExtensionFolders(this.extensionsPath)
			.then(folders => {
				const galleryFolders = folders
525 526
					.map(folder => (assign({ folder }, getIdAndVersionFromLocalExtensionId(folder))))
					.filter(({ id, version }) => !!id && !!version);
J
Joao Moreno 已提交
527 528 529 530 531 532

				const byId = values(groupBy(galleryFolders, p => p.id));

				return flatten(byId.map(p => p.sort((a, b) => semver.rcompare(a.version, b.version)).slice(1)))
					.map(a => a.folder);
			});
J
Joao Moreno 已提交
533 534
	}

J
Joao Moreno 已提交
535
	private isObsolete(id: string): TPromise<boolean> {
536 537 538 539 540 541 542 543 544 545 546 547 548
		return this.filterObsolete(id).then(obsolete => obsolete.length === 1);
	}

	private filterObsolete(...ids: string[]): TPromise<string[]> {
		return this.withObsoleteExtensions(allObsolete => {
			const obsolete = [];
			for (const id of ids) {
				if (!!allObsolete[id]) {
					obsolete.push(id);
				}
			}
			return obsolete;
		});
549 550
	}

J
Joao Moreno 已提交
551
	private setObsolete(id: string): TPromise<void> {
J
Joao Moreno 已提交
552
		return this.withObsoleteExtensions(obsolete => assign(obsolete, { [id]: true }));
553 554
	}

J
Joao Moreno 已提交
555
	private unsetObsolete(id: string): TPromise<void> {
J
Joao Moreno 已提交
556
		return this.withObsoleteExtensions<void>(obsolete => delete obsolete[id]);
557 558
	}

J
Johannes Rieken 已提交
559
	private getObsoleteExtensions(): TPromise<{ [id: string]: boolean; }> {
J
Joao Moreno 已提交
560
		return this.withObsoleteExtensions(obsolete => obsolete);
561 562
	}

J
Johannes Rieken 已提交
563
	private withObsoleteExtensions<T>(fn: (obsolete: { [id: string]: boolean; }) => T): TPromise<T> {
564 565 566
		return this.obsoleteFileLimiter.queue(() => {
			let result: T = null;
			return pfs.readFile(this.obsoletePath, 'utf8')
D
Dirk Baeumer 已提交
567
				.then<string>(null, err => err.code === 'ENOENT' ? TPromise.as('{}') : TPromise.wrapError(err))
J
Johannes Rieken 已提交
568
				.then<{ [id: string]: boolean }>(raw => { try { return JSON.parse(raw); } catch (e) { return {}; } })
569 570 571 572 573 574 575 576 577 578 579 580
				.then(obsolete => { result = fn(obsolete); return obsolete; })
				.then(obsolete => {
					if (Object.keys(obsolete).length === 0) {
						return pfs.rimraf(this.obsoletePath);
					} else {
						const raw = JSON.stringify(obsolete);
						return pfs.writeFile(this.obsoletePath, raw);
					}
				})
				.then(() => result);
		});
	}
581

582 583 584
	dispose() {
		this.disposables = dispose(this.disposables);
	}
E
Erich Gamma 已提交
585
}