extensionsActions.ts 13.2 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.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6
import 'vs/css!./media/extensionActions';
E
Erich Gamma 已提交
7
import nls = require('vs/nls');
J
Joao Moreno 已提交
8
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
9
import { Action } from 'vs/base/common/actions';
J
Joao Moreno 已提交
10 11 12 13 14 15 16
// import { assign } from 'vs/base/common/objects';
// import Severity from 'vs/base/common/severity';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
// import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
// import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
// import { IMessageService } from 'vs/platform/message/common/message';
// import { ReloadWindowAction } from 'vs/workbench/electron-browser/actions';
17 18
import { IExtension, ExtensionState, IExtensionsWorkbenchService } from './extensions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
// import { extensionEquals, getTelemetryData } from 'vs/platform/extensionManagement/node/extensionManagementUtil';
// import { IQuickOpenService } from 'vs/workbench/services/quickopen/common/quickOpenService';

// const CloseAction = new Action('action.close', nls.localize('close', "Close"));

// export class ListExtensionsAction extends Action {

// 	static ID = 'workbench.extensions.action.listExtensions';
// 	static LABEL = nls.localize('showInstalledExtensions', "Show Installed Extensions");

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext ');
// 	}

// 	protected isEnabled(): boolean {
// 		return true;
// 	}
// }

// export class InstallExtensionAction extends Action {

// 	static ID = 'workbench.extensions.action.installExtension';
// 	static LABEL = nls.localize('installExtension', "Install Extension");

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext install ');
// 	}

// 	protected isEnabled(): boolean {
// 		return true;
// 	}
// }

// export class ListOutdatedExtensionsAction extends Action {

// 	static ID = 'workbench.extensions.action.listOutdatedExtensions';
// 	static LABEL = nls.localize('showOutdatedExtensions', "Show Outdated Extensions");

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext update ');
// 	}

// 	protected isEnabled(): boolean {
// 		return true;
// 	}
// }

// export class ListSuggestedExtensionsAction extends Action {

// 	static ID = 'workbench.extensions.action.listSuggestedExtensions';
// 	static LABEL = nls.localize('showExtensionRecommendations', "Show Extension Recommendations");

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext recommend ');
// 	}

// 	protected isEnabled(): boolean {
// 		return true;
// 	}
// }
115

116 117
export class InstallAction extends Action {

J
Joao Moreno 已提交
118 119
	private static InstallLabel = nls.localize('installAction', "Install");
	private static InstallingLabel = nls.localize('installing', "Installing");
J
Joao Moreno 已提交
120
	private disposables: IDisposable[] = [];
121

122 123 124 125
	constructor(
		private extension: IExtension,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
126
		super('extensions.install', InstallAction.InstallLabel, 'extension-action install', false);
127

128
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
J
Joao Moreno 已提交
129
		this.update();
130 131
	}

J
Joao Moreno 已提交
132
	private update(): void {
133
		this.enabled = this.extensionsWorkbenchService.canInstall(this.extension) && this.extension.state === ExtensionState.Uninstalled;
J
Joao Moreno 已提交
134
		this.label = this.extension.state === ExtensionState.Installing ? InstallAction.InstallingLabel : InstallAction.InstallLabel;
135 136
	}

J
Joao Moreno 已提交
137
	run(): TPromise<any> {
138
		return this.extensionsWorkbenchService.install(this.extension);
J
Joao Moreno 已提交
139

J
Joao Moreno 已提交
140
		// this.enabled = false;
141

J
Joao Moreno 已提交
142 143 144 145 146 147 148 149 150
		// return this.extensionManagementService.getInstalled()
		// 	.then(installed => installed.some(({ manifest }) => extensionEquals(manifest, extension)))
		// 	.then(isUpdate => {
		// 		return this.extensionManagementService
		// 			.install(extension)
		// 			.then(() => this.onSuccess(extension, isUpdate), err => this.onError(err, extension, isUpdate))
		// 			.then(() => this.enabled = true)
		// 			.then(() => null);
		// 	});
151 152
	}

J
Joao Moreno 已提交
153 154 155 156 157 158 159 160 161 162
	// private onSuccess(extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, true);
	// 	this.messageService.show(Severity.Info, {
	// 		message: nls.localize('success-installed', "'{0}' was successfully installed. Restart to enable it.", extension.displayName || extension.name),
	// 		actions: [
	// 			CloseAction,
	// 			this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, nls.localize('restartNow', "Restart Now"))
	// 		]
	// 	});
	// }
163

J
Joao Moreno 已提交
164 165 166 167
	// private onError(err: Error, extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, false);
	// 	this.messageService.show(Severity.Error, err);
	// }
168

J
Joao Moreno 已提交
169 170 171
	// private reportTelemetry(extension: IGalleryExtension, isUpdate: boolean, success: boolean) {
	// 	const event = isUpdate ? 'extensionGallery:update' : 'extensionGallery:install';
	// 	const data = assign(getTelemetryData(extension), { success });
172

J
Joao Moreno 已提交
173 174
	// 	this.telemetryService.publicLog(event, data);
	// }
J
Joao Moreno 已提交
175

J
Joao Moreno 已提交
176 177 178
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
179 180
	}
}
J
Joao Moreno 已提交
181

J
Joao Moreno 已提交
182
export class UninstallAction extends Action {
J
Joao Moreno 已提交
183

J
Joao Moreno 已提交
184
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
185

186 187 188 189
	constructor(
		private extension: IExtension,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
190
		super('extensions.uninstall', nls.localize('uninstall', "Uninstall"), 'extension-action uninstall', false);
J
Joao Moreno 已提交
191

192
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.updateEnablement()));
J
Joao Moreno 已提交
193 194
		this.updateEnablement();
	}
J
Joao Moreno 已提交
195

J
Joao Moreno 已提交
196 197 198
	private updateEnablement(): void {
		this.enabled = this.extension.state === ExtensionState.Installed;
	}
J
Joao Moreno 已提交
199

J
Joao Moreno 已提交
200 201
	run(): TPromise<any> {
		// const name = extension.manifest.displayName || extension.manifest.name;
J
Joao Moreno 已提交
202

J
Joao Moreno 已提交
203 204 205
		if (!window.confirm(nls.localize('deleteSure', "Are you sure you want to uninstall '{0}'?", this.extension.displayName))) {
			return TPromise.as(null);
		}
J
Joao Moreno 已提交
206

207
		return this.extensionsWorkbenchService.uninstall(this.extension);
J
Joao Moreno 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

		// this.enabled = false;

		// return this.extensionManagementService.getInstalled().then(localExtensions => {
		// 	const [local] = localExtensions.filter(local => extensionEquals(local.manifest, extension.manifest));

		// 	if (!local) {
		// 		return TPromise.wrapError(nls.localize('notFound', "Extension '{0}' not installed.", name));
		// 	}

		// 	return this.extensionManagementService.uninstall(local)
		// 		.then(() => this.onSuccess(local), err => this.onError(err, local))
		// 		.then(() => this.enabled = true)
		// 		.then(() => null);
		// });
	}
J
Joao Moreno 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

// 	private onSuccess(extension: ILocalExtension) {
// 		const name = extension.manifest.displayName || extension.manifest.name;
// 		this.reportTelemetry(extension, true);

// 		this.messageService.show(Severity.Info, {
// 			message: nls.localize('success-uninstalled', "'{0}' was successfully uninstalled. Restart to deactivate it.", name),
// 			actions: [
// 				CloseAction,
// 				this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, nls.localize('restartNow2', "Restart Now"))
// 			]
// 		});
// 	}

// 	private onError(err: Error, extension: ILocalExtension) {
// 		this.reportTelemetry(extension, false);
// 		this.messageService.show(Severity.Error, err);
// 	}

// 	private reportTelemetry(extension: ILocalExtension, success: boolean) {
// 		const data = assign(getTelemetryData(extension), { success });

// 		this.telemetryService.publicLog('extensionGallery:uninstall', data);
// 	}
J
Joao Moreno 已提交
248 249 250 251 252 253

	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}
J
Joao Moreno 已提交
254 255 256 257 258 259 260

export class CombinedInstallAction extends Action {

	private installAction: InstallAction;
	private uninstallAction: UninstallAction;
	private disposables: IDisposable[] = [];

261 262 263 264
	constructor(
		private extension: IExtension,
		@IInstantiationService instantiationService: IInstantiationService
	) {
J
Joao Moreno 已提交
265 266
		super('extensions.combinedInstall', '', '', false);

267 268
		this.installAction = instantiationService.createInstance(InstallAction, extension);
		this.uninstallAction = instantiationService.createInstance(UninstallAction, extension);
J
Joao Moreno 已提交
269 270 271
		this.disposables.push(this.installAction, this.uninstallAction);

		this.disposables.push(this.installAction.addListener2(Action.ENABLED, () => this.update()));
J
Joao Moreno 已提交
272
		this.disposables.push(this.installAction.addListener2(Action.LABEL, () => this.update()));
J
Joao Moreno 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285
		this.disposables.push(this.uninstallAction.addListener2(Action.ENABLED, () => this.update()));
		this.update();
	}

	private update(): void {
		if (this.installAction.enabled) {
			this.enabled = true;
			this.label = this.installAction.label;
			this.class = this.installAction.class;
		} else if (this.uninstallAction.enabled) {
			this.enabled = true;
			this.label = this.uninstallAction.label;
			this.class = this.uninstallAction.class;
J
Joao Moreno 已提交
286 287 288 289
		} else if (this.extension.state === ExtensionState.Installing) {
			this.enabled = false;
			this.label = this.installAction.label;
			this.class = this.installAction.class;
J
Joao Moreno 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
		} else {
			this.enabled = false;
		}
	}

	run(): TPromise<any> {
		if (this.installAction.enabled) {
			return this.installAction.run();
		} else if (this.uninstallAction.enabled) {
			return this.uninstallAction.run();
		}

		return TPromise.as(null);
	}

J
Joao Moreno 已提交
305 306 307 308 309 310 311 312
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class UpdateAction extends Action {

J
Joao Moreno 已提交
313
	private static EnabledClass = 'extension-action update';
J
Joao Moreno 已提交
314 315 316 317
	private static DisabledClass = `${ UpdateAction.EnabledClass } disabled`;

	private disposables: IDisposable[] = [];

318 319 320 321
	constructor(
		private extension: IExtension,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
322
		super('extensions.update', nls.localize('updateAction', "Update"), UpdateAction.DisabledClass, false);
J
Joao Moreno 已提交
323

324
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.updateEnablement()));
J
Joao Moreno 已提交
325 326 327 328
		this.updateEnablement();
	}

	private updateEnablement(): void {
329
		const canInstall = this.extensionsWorkbenchService.canInstall(this.extension);
J
Joao Moreno 已提交
330 331
		const isInstalled = this.extension.state === ExtensionState.Installed;

J
Joao Moreno 已提交
332
		this.enabled = canInstall && isInstalled && this.extension.outdated;
J
Joao Moreno 已提交
333 334 335 336
		this.class = this.enabled ? UpdateAction.EnabledClass : UpdateAction.DisabledClass;
	}

	run(): TPromise<any> {
337
		return this.extensionsWorkbenchService.install(this.extension);
J
Joao Moreno 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

		// this.enabled = false;

		// return this.extensionManagementService.getInstalled()
		// 	.then(installed => installed.some(({ manifest }) => extensionEquals(manifest, extension)))
		// 	.then(isUpdate => {
		// 		return this.extensionManagementService
		// 			.install(extension)
		// 			.then(() => this.onSuccess(extension, isUpdate), err => this.onError(err, extension, isUpdate))
		// 			.then(() => this.enabled = true)
		// 			.then(() => null);
		// 	});
	}

	// private onSuccess(extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, true);
	// 	this.messageService.show(Severity.Info, {
	// 		message: nls.localize('success-installed', "'{0}' was successfully installed. Restart to enable it.", extension.displayName || extension.name),
	// 		actions: [
	// 			CloseAction,
	// 			this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, nls.localize('restartNow', "Restart Now"))
	// 		]
	// 	});
	// }

	// private onError(err: Error, extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, false);
	// 	this.messageService.show(Severity.Error, err);
	// }

	// private reportTelemetry(extension: IGalleryExtension, isUpdate: boolean, success: boolean) {
	// 	const event = isUpdate ? 'extensionGallery:update' : 'extensionGallery:install';
	// 	const data = assign(getTelemetryData(extension), { success });

	// 	this.telemetryService.publicLog(event, data);
	// }

J
Joao Moreno 已提交
375 376 377 378 379
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}