extensionsActions.ts 15.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';
J
Joao Moreno 已提交
7
import { localize } from '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';
10
import severity from 'vs/base/common/severity';
J
Joao Moreno 已提交
11
import Event from 'vs/base/common/event';
J
Joao Moreno 已提交
12
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
13
import { ReloadWindowAction } from 'vs/workbench/electron-browser/actions';
14
import { IExtension, ExtensionState, IExtensionsWorkbenchService, VIEWLET_ID, IExtensionsViewlet } from './extensions';
15
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
16
import { IMessageService, LaterAction } from 'vs/platform/message/common/message';
17 18 19
import { ToggleViewletAction } from 'vs/workbench/browser/viewlet';
import { IViewletService } from 'vs/workbench/services/viewlet/common/viewletService';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Joao Moreno 已提交
20
import { Query } from '../common/extensionQuery';
J
Joao Moreno 已提交
21

22 23
export class InstallAction extends Action {

J
Joao Moreno 已提交
24 25
	private static InstallLabel = localize('installAction', "Install");
	private static InstallingLabel = localize('installing', "Installing");
J
Joao Moreno 已提交
26
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
27 28 29
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
30

31 32 33
	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
34
		super('extensions.install', InstallAction.InstallLabel, 'extension-action install', false);
35

36
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
J
Joao Moreno 已提交
37
		this.update();
38 39
	}

J
Joao Moreno 已提交
40
	private update(): void {
J
Joao Moreno 已提交
41 42 43 44 45 46
		if (!this.extension) {
			this.enabled = false;
			this.label = InstallAction.InstallLabel;
			return;
		}

47
		this.enabled = this.extensionsWorkbenchService.canInstall(this.extension) && this.extension.state === ExtensionState.Uninstalled;
J
Joao Moreno 已提交
48
		this.label = this.extension.state === ExtensionState.Installing ? InstallAction.InstallingLabel : InstallAction.InstallLabel;
49 50
	}

J
Joao Moreno 已提交
51
	run(): TPromise<any> {
52
		return this.extensionsWorkbenchService.install(this.extension);
53 54
	}

J
Joao Moreno 已提交
55 56 57
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
58 59
	}
}
J
Joao Moreno 已提交
60

J
Joao Moreno 已提交
61
export class UninstallAction extends Action {
J
Joao Moreno 已提交
62

J
Joao Moreno 已提交
63
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
64 65 66
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
J
Joao Moreno 已提交
67

68
	constructor(
69 70 71
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService
72
	) {
J
Joao Moreno 已提交
73
		super('extensions.uninstall', localize('uninstall', "Uninstall"), 'extension-action uninstall', false);
J
Joao Moreno 已提交
74

J
Joao Moreno 已提交
75 76
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
		this.update();
J
Joao Moreno 已提交
77
	}
J
Joao Moreno 已提交
78

J
Joao Moreno 已提交
79 80 81 82 83 84
	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			return;
		}

J
Joao Moreno 已提交
85 86
		this.enabled = this.extension.state === ExtensionState.Installed
			|| this.extension.state === ExtensionState.NeedsRestart;
J
Joao Moreno 已提交
87
	}
J
Joao Moreno 已提交
88

J
Joao Moreno 已提交
89
	run(): TPromise<any> {
J
Joao Moreno 已提交
90
		if (!window.confirm(localize('deleteSure', "Are you sure you want to uninstall '{0}'?", this.extension.displayName))) {
J
Joao Moreno 已提交
91 92
			return TPromise.as(null);
		}
J
Joao Moreno 已提交
93

94 95 96
		return this.extensionsWorkbenchService.uninstall(this.extension).then(() => {
			this.messageService.show(severity.Info, {
				message: localize('postUninstallMessage', "{0} was successfully uninstalled. Restart to deactivate it.", this.extension.displayName),
97
				actions: [LaterAction, this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, localize('restartNow', "Restart Now"))]
98 99
			});
		});
J
Joao Moreno 已提交
100
	}
J
Joao Moreno 已提交
101

J
Joao Moreno 已提交
102 103 104 105 106
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}
J
Joao Moreno 已提交
107 108 109

export class CombinedInstallAction extends Action {

J
Joao Moreno 已提交
110
	private static NoExtensionClass = 'extension-action install no-extension';
J
Joao Moreno 已提交
111 112 113
	private installAction: InstallAction;
	private uninstallAction: UninstallAction;
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
114 115 116 117 118 119 120
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) {
		this._extension = extension;
		this.installAction.extension = extension;
		this.uninstallAction.extension = extension;
	}
J
Joao Moreno 已提交
121

122 123 124
	constructor(
		@IInstantiationService instantiationService: IInstantiationService
	) {
J
Joao Moreno 已提交
125 126
		super('extensions.combinedInstall', '', '', false);

J
Joao Moreno 已提交
127 128
		this.installAction = instantiationService.createInstance(InstallAction);
		this.uninstallAction = instantiationService.createInstance(UninstallAction);
J
Joao Moreno 已提交
129 130
		this.disposables.push(this.installAction, this.uninstallAction);

131
		this.installAction.onDidChange(this.update, this, this.disposables);
132
		this.uninstallAction.onDidChange(this.update, this, this.disposables);
J
Joao Moreno 已提交
133 134 135 136
		this.update();
	}

	private update(): void {
J
Joao Moreno 已提交
137 138 139 140
		if (!this.extension) {
			this.enabled = false;
			this.class = CombinedInstallAction.NoExtensionClass;
		} else if (this.installAction.enabled) {
J
Joao Moreno 已提交
141 142 143 144 145 146 147
			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 已提交
148 149 150 151
		} else if (this.extension.state === ExtensionState.Installing) {
			this.enabled = false;
			this.label = this.installAction.label;
			this.class = this.installAction.class;
J
Joao Moreno 已提交
152 153
		} else {
			this.enabled = false;
J
Joao Moreno 已提交
154 155
			this.label = this.installAction.label;
			this.class = this.installAction.class;
J
Joao Moreno 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168
		}
	}

	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 已提交
169 170 171 172 173 174 175 176
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class UpdateAction extends Action {

J
Joao Moreno 已提交
177
	private static EnabledClass = 'extension-action update';
J
Joao Moreno 已提交
178 179 180
	private static DisabledClass = `${ UpdateAction.EnabledClass } disabled`;

	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
181 182 183
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
J
Joao Moreno 已提交
184

185 186 187
	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
188
		super('extensions.update', localize('updateAction', "Update"), UpdateAction.DisabledClass, false);
J
Joao Moreno 已提交
189

J
Joao Moreno 已提交
190 191
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
		this.update();
J
Joao Moreno 已提交
192 193
	}

J
Joao Moreno 已提交
194 195 196 197 198 199 200
	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			this.class = UpdateAction.DisabledClass;
			return;
		}

201
		const canInstall = this.extensionsWorkbenchService.canInstall(this.extension);
J
Joao Moreno 已提交
202 203
		const isInstalled = this.extension.state === ExtensionState.Installed
			|| this.extension.state === ExtensionState.NeedsRestart;
J
Joao Moreno 已提交
204

J
Joao Moreno 已提交
205
		this.enabled = canInstall && isInstalled && this.extension.outdated;
J
Joao Moreno 已提交
206 207 208 209
		this.class = this.enabled ? UpdateAction.EnabledClass : UpdateAction.DisabledClass;
	}

	run(): TPromise<any> {
210
		return this.extensionsWorkbenchService.install(this.extension);
J
Joao Moreno 已提交
211 212
	}

J
Joao Moreno 已提交
213 214 215 216 217 218 219 220 221 222 223 224
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class EnableAction extends Action {

	private static EnabledClass = 'extension-action enable';
	private static DisabledClass = `${ EnableAction.EnabledClass } disabled`;

	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
225 226 227
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
J
Joao Moreno 已提交
228 229 230 231 232 233 234

	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super('extensions.enable', localize('enableAction', "Enable"), EnableAction.DisabledClass, false);

J
Joao Moreno 已提交
235 236
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
		this.update();
J
Joao Moreno 已提交
237 238
	}

J
Joao Moreno 已提交
239 240 241 242 243 244 245
	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			this.class = EnableAction.DisabledClass;
			return;
		}

J
Joao Moreno 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258
		this.enabled = this.extension.state === ExtensionState.NeedsRestart;
		this.class = this.enabled ? EnableAction.EnabledClass : EnableAction.DisabledClass;
	}

	run(): TPromise<any> {
		if (!window.confirm(localize('restart', "In order to enable this extension, this window of VS Code needs to be restarted.\n\nDo you want to continue?"))) {
			return TPromise.as(null);
		}

		const action = this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, localize('restartNow', "Restart Now"));
		return action.run();
	}

J
Joao Moreno 已提交
259 260 261 262
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
263
}
J
Joao Moreno 已提交
264 265 266

export class UpdateAllAction extends Action {

267 268 269
	static ID = 'extensions.update-all';
	static LABEL = localize('updateAll', "Update All Extensions");

J
Joao Moreno 已提交
270 271 272
	private disposables: IDisposable[] = [];

	constructor(
273 274
		id = UpdateAllAction.ID,
		label = UpdateAllAction.LABEL,
J
Joao Moreno 已提交
275 276
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
277
		super(id, label, '', false);
J
Joao Moreno 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
		this.update();
	}

	private get outdated(): IExtension[] {
		return this.extensionsWorkbenchService.local
			.filter(e => this.extensionsWorkbenchService.canInstall(e)
				&& (e.state === ExtensionState.Installed || e.state === ExtensionState.NeedsRestart)
				&& e.outdated);
	}

	private update(): void {
		this.enabled = this.outdated.length > 0;
	}

	run(): TPromise<any> {
		return TPromise.join(this.outdated.map(e => this.extensionsWorkbenchService.install(e)));
	}

	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}
303 304 305

export class OpenExtensionsViewletAction extends ToggleViewletAction {

J
Joao Moreno 已提交
306
	static ID = VIEWLET_ID;
307 308 309 310 311 312 313 314 315 316 317 318
	static LABEL = localize('toggleExtensionsViewlet', "Show Extensions");

	constructor(
		id: string,
		label: string,
		@IViewletService viewletService: IViewletService,
		@IWorkbenchEditorService editorService: IWorkbenchEditorService
	) {
		super(id, label, VIEWLET_ID, viewletService, editorService);
	}
}

I
isidor 已提交
319 320 321 322 323
export class InstallExtensionsAction extends OpenExtensionsViewletAction {
	static ID = 'workbench.extensions.action.installExtensions';
	static LABEL = localize('installExtensions', "Install Extensions");
}

J
Joao Moreno 已提交
324
export class ShowInstalledExtensionsAction extends Action {
325

J
Joao Moreno 已提交
326 327
	static ID = 'workbench.extensions.action.showInstalledExtensions';
	static LABEL = localize('showInstalledExtensions', "Show Installed Extensions");
328 329 330 331

	constructor(
		id: string,
		label: string,
332 333
		@IViewletService private viewletService: IViewletService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
334
	) {
335
		super(id, label, 'clear-extensions', true);
336 337 338 339 340 341
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
342
				viewlet.search('');
343 344 345 346 347
				viewlet.focus();
			});
	}
}

J
Joao Moreno 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
export class ClearExtensionsInputAction extends ShowInstalledExtensionsAction {

	static ID = 'workbench.extensions.action.clearExtensionsInput';
	static LABEL = localize('clearExtensionsInput', "Clear Extensions Input");

	private disposables: IDisposable[] = [];

	constructor(
		id: string,
		label: string,
		onSearchChange: Event<string>,
		@IViewletService viewletService: IViewletService,
		@IExtensionsWorkbenchService extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		super(id, label, viewletService, extensionsWorkbenchService);
363
		this.enabled = false;
J
Joao Moreno 已提交
364 365 366 367 368 369 370 371 372 373 374 375
		onSearchChange(this.onSearchChange, this, this.disposables);
	}

	private onSearchChange(value: string): void {
		this.enabled = !!value;
	}

	dispose(): void {
		this.disposables = dispose(this.disposables);
	}
}

J
Joao Moreno 已提交
376
export class ShowOutdatedExtensionsAction extends Action {
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

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

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
		super(id, label, null, true);
	}

	run(): TPromise<void> {

		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
394
				viewlet.search('@outdated');
395 396 397 398 399 400 401 402
				viewlet.focus();
			});
	}

	protected isEnabled(): boolean {
		return true;
	}
}
J
Joao Moreno 已提交
403 404 405 406 407 408 409 410 411 412 413

export class ShowPopularExtensionsAction extends Action {

	static ID = 'workbench.extensions.action.showPopularExtensions';
	static LABEL = localize('showPopularExtensions', "Show Popular Extensions");

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
414
		super(id, label, null, true);
J
Joao Moreno 已提交
415 416 417 418 419 420
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
421
				viewlet.search('@sort:installs');
J
Joao Moreno 已提交
422 423 424 425 426 427 428 429 430
				viewlet.focus();
			});
	}

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

J
Joao Moreno 已提交
431
export class ShowRecommendedExtensionsAction extends Action {
J
Joao Moreno 已提交
432

J
Joao Moreno 已提交
433
	static ID = 'workbench.extensions.action.showRecommendedExtensions';
J
Joao Moreno 已提交
434
	static LABEL = localize('showRecommendedExtensions', "Show Recommended Extensions");
J
Joao Moreno 已提交
435 436 437 438 439 440

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
441
		super(id, label, null, true);
J
Joao Moreno 已提交
442 443 444 445 446 447
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
448
				viewlet.search('@recommended');
J
Joao Moreno 已提交
449 450 451 452
				viewlet.focus();
			});
	}

J
Joao Moreno 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	protected isEnabled(): boolean {
		return true;
	}
}

export class ChangeSortAction extends Action {

	private query: Query;
	private disposables: IDisposable[] = [];

	constructor(
		id: string,
		label: string,
		onSearchChange: Event<string>,
		private sortBy: string,
		private sortOrder: string,
		@IViewletService private viewletService: IViewletService
	) {
		super(id, label, null, true);

J
Joao Moreno 已提交
473
		if (sortBy === undefined && sortOrder === undefined) {
J
Joao Moreno 已提交
474 475 476 477
			throw new Error('bad arguments');
		}

		this.query = Query.parse('');
J
Joao Moreno 已提交
478
		this.enabled = !!sortBy;
J
Joao Moreno 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
		onSearchChange(this.onSearchChange, this, this.disposables);
	}

	private onSearchChange(value: string): void {
		const query = Query.parse(value);
		this.query = new Query(query.value, this.sortBy || query.sortBy, this.sortOrder || query.sortOrder);
		this.enabled = this.query.isValid() && !this.query.equals(query);
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
				viewlet.search(this.query.toString());
				viewlet.focus();
			});
	}

J
Joao Moreno 已提交
497 498 499
	protected isEnabled(): boolean {
		return true;
	}
500
}