extensionsActions.ts 42.6 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';
9 10
import { IAction, Action } from 'vs/base/common/actions';
import * as DOM from 'vs/base/browser/dom';
11
import severity from 'vs/base/common/severity';
12
import paths = require('vs/base/common/paths');
J
Joao Moreno 已提交
13
import Event from 'vs/base/common/event';
14
import { ActionItem, IActionItem, Separator } from 'vs/base/browser/ui/actionbar/actionbar';
15
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
J
Joao Moreno 已提交
16
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
17
import { ReloadWindowAction } from 'vs/workbench/electron-browser/actions';
18
import { IExtension, ExtensionState, IExtensionsWorkbenchService, VIEWLET_ID, IExtensionsViewlet, ConfigurationKey } from '../common/extensions';
J
Joao Moreno 已提交
19
import { LocalExtensionType } from 'vs/platform/extensionManagement/common/extensionManagement';
20
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
21
import { IMessageService } from 'vs/platform/message/common/message';
22
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
23 24 25
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 已提交
26
import { Query } from '../common/extensionQuery';
B
Benjamin Pasero 已提交
27
import { remote } from 'electron';
S
Sandeep Somavarapu 已提交
28
import { ExtensionsConfigurationInitialContent } from 'vs/workbench/parts/extensions/electron-browser/extensionsFileTemplate';
J
Joao Moreno 已提交
29 30 31
import { IFileService } from 'vs/platform/files/common/files';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import URI from 'vs/base/common/uri';
32
import { IExtensionsRuntimeService } from 'vs/platform/extensions/common/extensions';
B
Benjamin Pasero 已提交
33
import { IWindowService } from 'vs/workbench/services/window/electron-browser/windowService';
J
Joao Moreno 已提交
34 35

const dialog = remote.dialog;
J
Joao Moreno 已提交
36

37 38
export class InstallAction extends Action {

J
Joao Moreno 已提交
39 40
	private static InstallLabel = localize('installAction', "Install");
	private static InstallingLabel = localize('installing', "Installing");
J
Joao Moreno 已提交
41 42 43 44

	private static Class = 'extension-action install';
	private static InstallingClass = 'extension-action install installing';

J
Joao Moreno 已提交
45
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
46 47 48
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
49

50 51 52
	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
53
		super('extensions.install', InstallAction.InstallLabel, InstallAction.Class, false);
54

55
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
J
Joao Moreno 已提交
56
		this.update();
57 58
	}

J
Joao Moreno 已提交
59
	private update(): void {
J
Joao Moreno 已提交
60
		if (!this.extension || this.extension.type === LocalExtensionType.System) {
J
Joao Moreno 已提交
61
			this.enabled = false;
J
Joao Moreno 已提交
62
			this.class = InstallAction.Class;
J
Joao Moreno 已提交
63 64 65 66
			this.label = InstallAction.InstallLabel;
			return;
		}

67
		this.enabled = this.extensionsWorkbenchService.canInstall(this.extension) && this.extension.state === ExtensionState.Uninstalled;
J
Joao Moreno 已提交
68 69 70 71 72 73 74 75

		if (this.extension.state === ExtensionState.Installing) {
			this.label = InstallAction.InstallingLabel;
			this.class = InstallAction.InstallingClass;
		} else {
			this.label = InstallAction.InstallLabel;
			this.class = InstallAction.Class;
		}
76 77
	}

J
Joao Moreno 已提交
78
	run(): TPromise<any> {
79
		return this.extensionsWorkbenchService.install(this.extension);
80 81
	}

J
Joao Moreno 已提交
82 83 84
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
85 86
	}
}
J
Joao Moreno 已提交
87

J
Joao Moreno 已提交
88
export class UninstallAction extends Action {
J
Joao Moreno 已提交
89

S
Sandeep Somavarapu 已提交
90 91 92 93 94 95
	private static UninstallLabel = localize('uninstallAction', "Uninstall");
	private static UninstallingLabel = localize('Uninstalling', "Uninstalling");

	private static UninstallClass = 'extension-action uninstall';
	private static UnInstallingClass = 'extension-action uninstall uninstalling';

J
Joao Moreno 已提交
96
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
97 98 99
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
J
Joao Moreno 已提交
100

101
	constructor(
102 103 104
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService
105
	) {
S
Sandeep Somavarapu 已提交
106
		super('extensions.uninstall', UninstallAction.UninstallLabel, UninstallAction.UninstallClass, false);
J
Joao Moreno 已提交
107

108
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
J
Joao Moreno 已提交
109
		this.update();
J
Joao Moreno 已提交
110
	}
J
Joao Moreno 已提交
111

J
Joao Moreno 已提交
112 113 114 115 116 117
	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			return;
		}

118 119
		const state = this.extension.state;

S
Sandeep Somavarapu 已提交
120 121 122 123 124 125 126 127
		if (state === ExtensionState.Uninstalling) {
			this.label = UninstallAction.UninstallingLabel;
			this.class = UninstallAction.UnInstallingClass;
		} else {
			this.label = UninstallAction.UninstallLabel;
			this.class = UninstallAction.UninstallClass;
		}

128 129 130 131 132
		if (ExtensionState.Installed === state) {
			this.enabled = true;
			return;
		}

J
Joao Moreno 已提交
133 134 135 136 137
		if (this.extension.type !== LocalExtensionType.User) {
			this.enabled = false;
			return;
		}

138
		this.enabled = state === ExtensionState.Enabled || state === ExtensionState.Disabled;
J
Joao Moreno 已提交
139
	}
J
Joao Moreno 已提交
140

J
Joao Moreno 已提交
141
	run(): TPromise<any> {
142
		return this.extensionsWorkbenchService.uninstall(this.extension);
J
Joao Moreno 已提交
143
	}
J
Joao Moreno 已提交
144

J
Joao Moreno 已提交
145 146 147 148 149
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}
J
Joao Moreno 已提交
150 151 152

export class CombinedInstallAction extends Action {

J
Joao Moreno 已提交
153
	private static NoExtensionClass = 'extension-action install no-extension';
J
Joao Moreno 已提交
154 155 156
	private installAction: InstallAction;
	private uninstallAction: UninstallAction;
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
157 158 159 160 161 162 163
	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 已提交
164

165 166 167
	constructor(
		@IInstantiationService instantiationService: IInstantiationService
	) {
J
Joao Moreno 已提交
168 169
		super('extensions.combinedInstall', '', '', false);

J
Joao Moreno 已提交
170 171
		this.installAction = instantiationService.createInstance(InstallAction);
		this.uninstallAction = instantiationService.createInstance(UninstallAction);
J
Joao Moreno 已提交
172 173
		this.disposables.push(this.installAction, this.uninstallAction);

174
		this.installAction.onDidChange(this.update, this, this.disposables);
175
		this.uninstallAction.onDidChange(this.update, this, this.disposables);
J
Joao Moreno 已提交
176 177 178 179
		this.update();
	}

	private update(): void {
J
Joao Moreno 已提交
180
		if (!this.extension || this.extension.type === LocalExtensionType.System) {
J
Joao Moreno 已提交
181 182 183
			this.enabled = false;
			this.class = CombinedInstallAction.NoExtensionClass;
		} else if (this.installAction.enabled) {
J
Joao Moreno 已提交
184 185 186 187 188 189 190
			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 已提交
191 192 193 194
		} else if (this.extension.state === ExtensionState.Installing) {
			this.enabled = false;
			this.label = this.installAction.label;
			this.class = this.installAction.class;
S
Sandeep Somavarapu 已提交
195 196 197 198
		} else if (this.extension.state === ExtensionState.Uninstalling) {
			this.enabled = false;
			this.label = this.uninstallAction.label;
			this.class = this.uninstallAction.class;
J
Joao Moreno 已提交
199 200
		} else {
			this.enabled = false;
J
Joao Moreno 已提交
201 202
			this.label = this.installAction.label;
			this.class = this.installAction.class;
J
Joao Moreno 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215
		}
	}

	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 已提交
216 217 218 219 220 221 222 223
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class UpdateAction extends Action {

J
Joao Moreno 已提交
224
	private static EnabledClass = 'extension-action update';
J
Johannes Rieken 已提交
225
	private static DisabledClass = `${UpdateAction.EnabledClass} disabled`;
J
Joao Moreno 已提交
226 227

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

232 233 234
	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
235
		super('extensions.update', localize('updateAction', "Update"), UpdateAction.DisabledClass, false);
J
Joao Moreno 已提交
236

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

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

J
Joao Moreno 已提交
248 249 250 251 252 253
		if (this.extension.type !== LocalExtensionType.User) {
			this.enabled = false;
			this.class = UpdateAction.DisabledClass;
			return;
		}

254
		const canInstall = this.extensionsWorkbenchService.canInstall(this.extension);
255 256 257
		const isInstalled = this.extension.state === ExtensionState.Enabled
			|| this.extension.state === ExtensionState.Disabled
			|| this.extension.state === ExtensionState.Installed;
J
Joao Moreno 已提交
258

259
		this.enabled = canInstall && isInstalled && this.extension.outdated;
260 261 262
		this.class = this.enabled ? UpdateAction.EnabledClass : UpdateAction.DisabledClass;
	}

J
Joao Moreno 已提交
263
	run(): TPromise<any> {
264
		return this.extensionsWorkbenchService.install(this.extension);
J
Joao Moreno 已提交
265 266
	}

J
Joao Moreno 已提交
267 268 269 270 271 272
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

273 274 275 276 277 278 279 280 281
export interface IExtensionAction extends IAction {
	extension: IExtension;
}

export class DropDownMenuActionItem extends ActionItem {

	private disposables: IDisposable[] = [];
	private _extension: IExtension;

S
Sandeep Somavarapu 已提交
282
	constructor(action: IAction, private menuActionGroups: IExtensionAction[][], private contextMenuService: IContextMenuService) {
283
		super(null, action, { icon: true, label: true });
S
Sandeep Somavarapu 已提交
284 285 286
		for (const menuActions of menuActionGroups) {
			this.disposables = [...this.disposables, ...menuActions];
		}
287 288 289 290 291 292
	}

	get extension(): IExtension { return this._extension; }

	set extension(extension: IExtension) {
		this._extension = extension;
S
Sandeep Somavarapu 已提交
293 294 295
		for (const menuActions of this.menuActionGroups) {
			for (const menuAction of menuActions) {
				menuAction.extension = extension;
296
			}
297 298 299
		}
	}

S
Sandeep Somavarapu 已提交
300
	public showMenu(): void {
S
Sandeep Somavarapu 已提交
301
		const actions = this.getActions();
302 303 304 305
		let elementPosition = DOM.getDomNodePagePosition(this.builder.getHTMLElement());
		const anchor = { x: elementPosition.left, y: elementPosition.top + elementPosition.height + 10 };
		this.contextMenuService.showContextMenu({
			getAnchor: () => anchor,
306
			getActions: () => TPromise.wrap(actions)
307 308 309
		});
	}

S
Sandeep Somavarapu 已提交
310 311 312 313 314 315 316 317 318 319 320
	private getActions(): IAction[] {
		let actions = [];
		for (const menuActions of this.menuActionGroups) {
			const filtered = menuActions.filter(a => a.enabled);
			if (filtered.length > 0) {
				actions = [...actions, ...filtered, new Separator()];
			}
		}
		return actions.length ? actions.slice(0, actions.length - 1) : actions;
	}

321 322 323 324 325 326
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

327 328 329 330 331
export class ManageExtensionActionItem extends DropDownMenuActionItem {
	constructor(
		action: IAction,
		@IInstantiationService instantiationService: IInstantiationService,
		@IContextMenuService contextMenuService: IContextMenuService) {
S
Sandeep Somavarapu 已提交
332 333 334 335 336 337 338
		super(action, [
			[instantiationService.createInstance(EnableForWorkspaceAction, localize('enableForWorkspaceAction.label', "Enable (Workspace)")),
			instantiationService.createInstance(EnableGloballyAction, localize('enableAlwaysAction.label', "Enable")),
			instantiationService.createInstance(DisableForWorkspaceAction, localize('disableForWorkspaceAction.label', "Disable (Workspace)")),
			instantiationService.createInstance(DisableGloballyAction, localize('disableAlwaysAction.label', "Disable"))],
			[instantiationService.createInstance(UninstallAction)]
		], contextMenuService);
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 375
	}
}

export class ManageExtensionAction extends Action {

	static ID = 'extensions.manage';

	private static Class = 'extension-action manage';
	private static NoExtensionClass = `${ManageExtensionAction.Class} no-extension`;

	private _actionItem: EnableActionItem;
	get actionItem(): IActionItem { return this._actionItem; }

	private disposables: IDisposable[] = [];
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this._actionItem.extension = extension; this.update(); }

	constructor(
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IExtensionsRuntimeService private extensionRuntimeService: IExtensionsRuntimeService,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super(ManageExtensionAction.ID);

		this._actionItem = this.instantiationService.createInstance(ManageExtensionActionItem, this);
		this.disposables.push(this._actionItem);

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

	private update(): void {
		this.class = ManageExtensionAction.NoExtensionClass;
		this.enabled = false;
		if (this.extension) {
376 377
			const state = this.extension.state;
			this.enabled = ExtensionState.Uninstalled !== state && ExtensionState.Installing !== state && ExtensionState.Uninstalling !== state;
378
			this.class = ExtensionState.Uninstalled === state || ExtensionState.Installing === state ? ManageExtensionAction.NoExtensionClass : ManageExtensionAction.Class;
379 380 381 382 383 384 385 386 387 388 389 390 391 392
		}
	}

	public run(): TPromise<any> {
		this._actionItem.showMenu();
		return TPromise.wrap(null);
	}

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

S
Sandeep Somavarapu 已提交
393
export class EnableForWorkspaceAction extends Action implements IExtensionAction {
394

395 396 397 398 399
	static ID = 'extensions.enableForWorkspace';
	static LABEL = localize('enableForWorkspaceAction', "Workspace");

	private disposables: IDisposable[] = [];

400 401 402 403
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }

404
	constructor(label: string,
405 406
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
407 408
		@IExtensionsRuntimeService private extensionsRuntimeService: IExtensionsRuntimeService,
		@IInstantiationService private instantiationService: IInstantiationService
409
	) {
410 411 412 413
		super(EnableForWorkspaceAction.ID, label);

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

	private update(): void {
417
		this.enabled = false;
418
		if (this.extension && this.workspaceContextService.getWorkspace()) {
419 420
			this.enabled = this.extension.type !== LocalExtensionType.System && ExtensionState.Disabled === this.extension.state
				&& !this.extensionsRuntimeService.isDisabledAlways(this.extension.identifier) && this.extensionsRuntimeService.canEnable(this.extension.identifier);
421 422 423 424 425 426
		}
	}

	run(): TPromise<any> {
		return this.extensionsWorkbenchService.setEnablement(this.extension, true, true);
	}
427 428 429 430 431

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

S
Sandeep Somavarapu 已提交
434
export class EnableGloballyAction extends Action implements IExtensionAction {
435

436 437 438 439 440
	static ID = 'extensions.enableGlobally';
	static LABEL = localize('enableGloballyAction', "Always");

	private disposables: IDisposable[] = [];

441 442
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
443
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
444

445
	constructor(label: string,
446
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
447 448
		@IExtensionsRuntimeService private extensionsRuntimeService: IExtensionsRuntimeService,
		@IInstantiationService private instantiationService: IInstantiationService
449
	) {
450 451 452 453
		super(EnableGloballyAction.ID, label);

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

	private update(): void {
457
		this.enabled = false;
458
		if (this.extension) {
459
			this.enabled = this.extension.type !== LocalExtensionType.System && this.extensionsRuntimeService.isDisabledAlways(this.extension.identifier) && this.extensionsRuntimeService.canEnable(this.extension.identifier);
460
		}
461 462 463 464 465
	}

	run(): TPromise<any> {
		return this.extensionsWorkbenchService.setEnablement(this.extension, true, false);
	}
466 467 468 469 470

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

J
Joao Moreno 已提交
473 474
export class EnableAction extends Action {

475
	static ID = 'extensions.enable';
J
Joao Moreno 已提交
476
	private static EnabledClass = 'extension-action enable';
J
Johannes Rieken 已提交
477
	private static DisabledClass = `${EnableAction.EnabledClass} disabled`;
J
Joao Moreno 已提交
478 479

	private disposables: IDisposable[] = [];
480 481 482 483

	private _actionItem: EnableActionItem;
	get actionItem(): IActionItem { return this._actionItem; }

J
Joao Moreno 已提交
484 485
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
486 487
	set extension(extension: IExtension) { this._extension = extension; this._actionItem.extension = extension; this.update(); }

J
Joao Moreno 已提交
488 489

	constructor(
490
		@IInstantiationService private instantiationService: IInstantiationService,
491 492
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IExtensionsRuntimeService private extensionsRuntimeService: IExtensionsRuntimeService
J
Joao Moreno 已提交
493
	) {
494 495 496 497
		super(EnableAction.ID, localize('enableAction', "Enable"), EnableAction.DisabledClass, false);

		this._actionItem = this.instantiationService.createInstance(EnableActionItem, this);
		this.disposables.push(this._actionItem);
J
Joao Moreno 已提交
498

J
Joao Moreno 已提交
499 500
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
		this.update();
J
Joao Moreno 已提交
501 502
	}

J
Joao Moreno 已提交
503 504 505 506 507 508 509
	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			this.class = EnableAction.DisabledClass;
			return;
		}

510
		this.enabled = this.extension.type !== LocalExtensionType.System && !this.extension.reload && ExtensionState.Disabled === this.extension.state && this.extensionsRuntimeService.canEnable(this.extension.identifier);
J
Joao Moreno 已提交
511 512 513
		this.class = this.enabled ? EnableAction.EnabledClass : EnableAction.DisabledClass;
	}

S
Sandeep Somavarapu 已提交
514 515 516 517 518
	public run(): TPromise<any> {
		this._actionItem.showMenu();
		return TPromise.wrap(null);
	}

519 520 521 522 523 524 525
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}

}

S
Sandeep Somavarapu 已提交
526
export class DisableForWorkspaceAction extends Action implements IExtensionAction {
527

528 529 530 531 532
	static ID = 'extensions.disableForWorkspace';
	static LABEL = localize('disableForWorkspaceAction', "Workspace");

	private disposables: IDisposable[] = [];

533 534
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
535
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
536

537
	constructor(label: string,
538
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
539 540
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IInstantiationService private instantiationService: IInstantiationService
541
	) {
542 543 544 545 546 547 548 549 550
		super(DisableForWorkspaceAction.ID, label);

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

	private update(): void {
		this.enabled = false;
		if (this.extension && this.workspaceContextService.getWorkspace()) {
551
			this.enabled = this.extension.type !== LocalExtensionType.System && ExtensionState.Enabled === this.extension.state;
552
		}
553 554 555 556 557
	}

	run(): TPromise<any> {
		return this.extensionsWorkbenchService.setEnablement(this.extension, false, true);
	}
558 559 560 561 562

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

S
Sandeep Somavarapu 已提交
565
export class DisableGloballyAction extends Action implements IExtensionAction {
566

567 568 569 570 571
	static ID = 'extensions.disableGlobally';
	static LABEL = localize('disableGloballyAction', "Always");

	private disposables: IDisposable[] = [];

572 573
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
574
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
575

576 577 578
	constructor(label: string,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IInstantiationService private instantiationService: IInstantiationService
579
	) {
580 581 582 583 584 585 586 587 588
		super(DisableGloballyAction.ID, label);

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

	private update(): void {
		this.enabled = false;
		if (this.extension) {
589
			this.enabled = this.extension.type !== LocalExtensionType.System && ExtensionState.Enabled === this.extension.state;
590
		}
591 592
	}

J
Joao Moreno 已提交
593
	run(): TPromise<any> {
594
		return this.extensionsWorkbenchService.setEnablement(this.extension, false, false);
595
	}
596 597 598 599 600

	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
601 602 603 604
}

export class DisableAction extends Action {

605 606
	static ID = 'extensions.disable';

607 608 609 610
	private static EnabledClass = 'extension-action disable';
	private static DisabledClass = `${DisableAction.EnabledClass} disabled`;

	private disposables: IDisposable[] = [];
611 612 613
	private _actionItem: DisableActionItem;
	get actionItem(): IActionItem { return this._actionItem; }

614 615
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
616 617
	set extension(extension: IExtension) { this._extension = extension; this._actionItem.extension = extension; this.update(); }

618 619

	constructor(
620
		@IInstantiationService private instantiationService: IInstantiationService,
621 622
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
	) {
623 624 625
		super(DisableAction.ID, localize('disableAction', "Disable"), DisableAction.DisabledClass, false);
		this._actionItem = this.instantiationService.createInstance(DisableActionItem, this);
		this.disposables.push(this._actionItem);
626 627 628 629 630 631 632 633 634 635

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

	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			this.class = DisableAction.DisabledClass;
			return;
J
Joao Moreno 已提交
636 637
		}

S
Sandeep Somavarapu 已提交
638
		this.enabled = this.extension.type !== LocalExtensionType.System && !this.extension.reload && this.extension.state === ExtensionState.Enabled;
639
		this.class = this.enabled ? DisableAction.EnabledClass : DisableAction.DisabledClass;
J
Joao Moreno 已提交
640 641
	}

S
Sandeep Somavarapu 已提交
642 643 644 645 646
	public run(): TPromise<any> {
		this._actionItem.showMenu();
		return TPromise.wrap(null);
	}

647 648 649 650 651 652 653 654 655 656 657
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class EnableActionItem extends DropDownMenuActionItem {
	constructor(
		action: IAction,
		@IInstantiationService instantiationService: IInstantiationService,
		@IContextMenuService contextMenuService: IContextMenuService) {
S
Sandeep Somavarapu 已提交
658
		super(action, [[instantiationService.createInstance(EnableForWorkspaceAction, EnableForWorkspaceAction.LABEL), instantiationService.createInstance(EnableGloballyAction, EnableGloballyAction.LABEL)]], contextMenuService);
659 660 661 662 663 664 665 666
	}
}

export class DisableActionItem extends DropDownMenuActionItem {
	constructor(
		action: IAction,
		@IInstantiationService instantiationService: IInstantiationService,
		@IContextMenuService contextMenuService: IContextMenuService) {
S
Sandeep Somavarapu 已提交
667
		super(action, [[instantiationService.createInstance(DisableForWorkspaceAction, DisableForWorkspaceAction.LABEL), instantiationService.createInstance(DisableGloballyAction, DisableGloballyAction.LABEL)]], contextMenuService);
J
Joao Moreno 已提交
668
	}
669
}
J
Joao Moreno 已提交
670

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
export class ReloadAction extends Action {

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

	private disposables: IDisposable[] = [];
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }

	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IExtensionsRuntimeService private extensionsRuntimeService: IExtensionsRuntimeService
	) {
		super('extensions.reload', localize('reloadAction', "Reload"), ReloadAction.DisabledClass, false);

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

	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			this.class = ReloadAction.DisabledClass;
			return;
		}

S
Sandeep Somavarapu 已提交
700 701
		const state = this.extension.state;
		this.enabled = state !== ExtensionState.Installing && state !== ExtensionState.Uninstalling && (this.extension.reload || /* Following is needed due to extension is stale */this.extension.state === ExtensionState.Installed);
702
		this.class = this.enabled ? ReloadAction.EnabledClass : ReloadAction.DisabledClass;
S
Sandeep Somavarapu 已提交
703
		this.tooltip = this.getMessage(true);
704 705 706
	}

	run(): TPromise<any> {
S
Sandeep Somavarapu 已提交
707 708 709 710
		if (window.confirm(this.getMessage(false))) {
			this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, localize('reloadNow', "Reload Now")).run();
		}
		return TPromise.wrap(null);
711 712
	}

S
Sandeep Somavarapu 已提交
713 714 715 716 717 718 719 720 721 722 723 724
	private getMessage(tooltip: boolean): string {
		const state = this.extension.state;
		if (state === ExtensionState.Installed) {
			return tooltip ? localize('postInstallTooltip', "Reload to activate") : localize('postInstallMessage', "Reload this window to activate '{0}' extension?", this.extension.displayName);
		}
		if (state === ExtensionState.Disabled) {
			return tooltip ? localize('postEnableTooltip', "Reload to enable") : localize('postEnableMessage', "Reload this window to enable '{0}' extension?", this.extension.displayName);
		}
		if (state === ExtensionState.Enabled) {
			return tooltip ? localize('postDisableTooltip', "Reload to disable") : localize('postDisableMessage', "Reload this window to disable '{0}' extension?", this.extension.displayName);
		}
		return tooltip ? localize('postUninstallTooltip', "Reload to deactivate") : localize('postUninstallMessage', "Reload this window to deactivate '{0}' extension?", this.extension.displayName);
725 726 727
	}
}

J
Joao Moreno 已提交
728 729
export class UpdateAllAction extends Action {

J
Joao Moreno 已提交
730
	static ID = 'workbench.extensions.action.updateAllExtensions';
731 732
	static LABEL = localize('updateAll', "Update All Extensions");

J
Joao Moreno 已提交
733 734 735
	private disposables: IDisposable[] = [];

	constructor(
736 737
		id = UpdateAllAction.ID,
		label = UpdateAllAction.LABEL,
J
Joao Moreno 已提交
738 739
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
740
		super(id, label, '', false);
J
Joao Moreno 已提交
741 742 743 744 745

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

746
	private get outdated(): IExtension[] {
747
		return this.extensionsWorkbenchService.local.filter(e => e.outdated && e.state !== ExtensionState.Installing);
J
Joao Moreno 已提交
748 749 750
	}

	private update(): void {
751
		this.enabled = this.outdated.length > 0;
J
Joao Moreno 已提交
752 753
	}

754 755
	run(): TPromise<any> {
		return TPromise.join(this.outdated.map(e => this.extensionsWorkbenchService.install(e)));
J
Joao Moreno 已提交
756 757 758 759 760 761 762
	}

	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}
763 764 765

export class OpenExtensionsViewletAction extends ToggleViewletAction {

J
Joao Moreno 已提交
766
	static ID = VIEWLET_ID;
767 768 769 770 771 772 773 774 775 776 777 778
	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 已提交
779 780 781 782 783
export class InstallExtensionsAction extends OpenExtensionsViewletAction {
	static ID = 'workbench.extensions.action.installExtensions';
	static LABEL = localize('installExtensions', "Install Extensions");
}

J
Joao Moreno 已提交
784
export class ShowInstalledExtensionsAction extends Action {
785

J
Joao Moreno 已提交
786 787
	static ID = 'workbench.extensions.action.showInstalledExtensions';
	static LABEL = localize('showInstalledExtensions', "Show Installed Extensions");
788 789 790 791

	constructor(
		id: string,
		label: string,
792 793
		@IViewletService private viewletService: IViewletService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
794
	) {
795
		super(id, label, 'clear-extensions', true);
796 797 798 799 800 801
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
802
				viewlet.search('');
803 804 805 806 807
				viewlet.focus();
			});
	}
}

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
export class ShowDisabledExtensionsAction extends Action {

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

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

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

J
Joao Moreno 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
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);
847
		this.enabled = false;
J
Joao Moreno 已提交
848 849 850 851 852 853 854 855 856 857 858 859
		onSearchChange(this.onSearchChange, this, this.disposables);
	}

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

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

J
Joao Moreno 已提交
860
export class ShowOutdatedExtensionsAction extends Action {
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876

	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 => {
877
				viewlet.search('@outdated ');
878 879 880 881 882 883 884 885
				viewlet.focus();
			});
	}

	protected isEnabled(): boolean {
		return true;
	}
}
J
Joao Moreno 已提交
886 887 888 889 890 891 892 893 894 895 896

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
	) {
897
		super(id, label, null, true);
J
Joao Moreno 已提交
898 899 900 901 902 903
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
904
				viewlet.search('@sort:installs ');
J
Joao Moreno 已提交
905 906 907 908 909 910 911 912 913
				viewlet.focus();
			});
	}

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

J
Joao Moreno 已提交
914
export class ShowRecommendedExtensionsAction extends Action {
J
Joao Moreno 已提交
915

J
Joao Moreno 已提交
916
	static ID = 'workbench.extensions.action.showRecommendedExtensions';
J
Joao Moreno 已提交
917
	static LABEL = localize('showRecommendedExtensions', "Show Recommended Extensions");
J
Joao Moreno 已提交
918 919 920 921 922 923

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
924
		super(id, label, null, true);
J
Joao Moreno 已提交
925 926 927 928 929 930
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
931
				viewlet.search('@recommended ');
J
Joao Moreno 已提交
932 933 934 935
				viewlet.focus();
			});
	}

J
Joao Moreno 已提交
936 937 938 939 940
	protected isEnabled(): boolean {
		return true;
	}
}

S
Sandeep Somavarapu 已提交
941 942 943 944 945 946 947 948
export class ShowWorkspaceRecommendedExtensionsAction extends Action {

	static ID = 'workbench.extensions.action.showWorkspaceRecommendedExtensions';
	static LABEL = localize('showWorkspaceRecommendedExtensions', "Show Workspace Recommended Extensions");

	constructor(
		id: string,
		label: string,
S
Sandeep Somavarapu 已提交
949
		@IWorkspaceContextService contextService: IWorkspaceContextService,
S
Sandeep Somavarapu 已提交
950 951
		@IViewletService private viewletService: IViewletService
	) {
S
Sandeep Somavarapu 已提交
952
		super(id, label, null, !!contextService.getWorkspace());
S
Sandeep Somavarapu 已提交
953 954 955 956 957 958
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
959
				viewlet.search('@recommended:workspace ');
S
Sandeep Somavarapu 已提交
960 961 962 963 964 965 966 967 968
				viewlet.focus();
			});
	}

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

J
Joao Moreno 已提交
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
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 已提交
984
		if (sortBy === undefined && sortOrder === undefined) {
J
Joao Moreno 已提交
985 986 987 988
			throw new Error('bad arguments');
		}

		this.query = Query.parse('');
989
		this.enabled = false;
J
Joao Moreno 已提交
990 991 992 993 994 995
		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);
996
		this.enabled = value && this.query.isValid() && !this.query.equals(query);
J
Joao Moreno 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	}

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

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	protected isEnabled(): boolean {
		return true;
	}
}

export class OpenExtensionsFolderAction extends Action {

	static ID = 'workbench.extensions.action.openExtensionsFolder';
	static LABEL = localize('openExtensionsFolder', "Open Extensions Folder");

	constructor(
		id: string,
		label: string,
B
Benjamin Pasero 已提交
1021
		@IWindowService private windowService: IWindowService,
1022 1023 1024 1025 1026 1027 1028
		@IEnvironmentService private environmentService: IEnvironmentService
	) {
		super(id, label, null, true);
	}

	run(): TPromise<any> {
		const extensionsHome = this.environmentService.extensionsPath;
B
Benjamin Pasero 已提交
1029
		this.windowService.getWindow().showItemInFolder(paths.normalize(extensionsHome, true));
1030 1031 1032 1033

		return TPromise.as(true);
	}

J
Joao Moreno 已提交
1034 1035 1036
	protected isEnabled(): boolean {
		return true;
	}
S
Sandeep Somavarapu 已提交
1037 1038
}

S
Sandeep Somavarapu 已提交
1039
export class ConfigureWorkspaceRecommendedExtensionsAction extends Action {
J
Joao Moreno 已提交
1040

S
Sandeep Somavarapu 已提交
1041
	static ID = 'workbench.extensions.action.configureWorkspaceRecommendedExtensions';
1042
	static LABEL = localize('configureWorkspaceRecommendedExtensions', "Configure Recommended Extensions (Workspace)");
S
Sandeep Somavarapu 已提交
1043

J
Joao Moreno 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052
	constructor(
		id: string,
		label: string,
		@IFileService private fileService: IFileService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@IExtensionsWorkbenchService private extensionsService: IExtensionsWorkbenchService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IMessageService private messageService: IMessageService
	) {
S
Sandeep Somavarapu 已提交
1053
		super(id, label, null, !!contextService.getWorkspace());
S
Sandeep Somavarapu 已提交
1054 1055 1056
	}

	public run(event: any): TPromise<any> {
J
Joao Moreno 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		return this.openExtensionsFile();
	}

	private openExtensionsFile(): TPromise<any> {
		if (!this.contextService.getWorkspace()) {
			this.messageService.show(severity.Info, localize('ConfigureWorkspaceRecommendations.noWorkspace', 'Recommendations are only available on a workspace folder.'));
			return TPromise.as(undefined);
		}

		return this.getOrCreateExtensionsFile().then(value => {
			return this.editorService.openEditor({
				resource: value.extensionsFileResource,
				options: {
					forceOpen: true,
					pinned: value.created
				},
			});
		}, (error) => TPromise.wrapError(new Error(localize('OpenExtensionsFile.failed', "Unable to create 'extensions.json' file inside the '.vscode' folder ({0}).", error))));
	}

	private getOrCreateExtensionsFile(): TPromise<{ created: boolean, extensionsFileResource: URI }> {
J
Johannes Rieken 已提交
1078
		const extensionsFileResource = URI.file(paths.join(this.contextService.getWorkspace().resource.fsPath, '.vscode', `${ConfigurationKey}.json`));
J
Joao Moreno 已提交
1079 1080 1081 1082

		return this.fileService.resolveContent(extensionsFileResource).then(content => {
			return { created: false, extensionsFileResource };
		}, err => {
1083
			return this.fileService.updateContent(extensionsFileResource, ExtensionsConfigurationInitialContent).then(() => {
J
Joao Moreno 已提交
1084 1085 1086
				return { created: true, extensionsFileResource };
			});
		});
S
Sandeep Somavarapu 已提交
1087
	}
J
Joao Moreno 已提交
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
}

export class InstallVSIXAction extends Action {

	static ID = 'workbench.extensions.action.installVSIX';
	static LABEL = localize('installVSIX', "Install from VSIX...");

	constructor(
		id = InstallVSIXAction.ID,
		label = InstallVSIXAction.LABEL,
1098 1099 1100
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService
J
Joao Moreno 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	) {
		super(id, label, 'extension-action install-vsix', true);
	}

	run(): TPromise<any> {
		const result = dialog.showOpenDialog(remote.getCurrentWindow(), {
			filters: [{ name: 'VSIX Extensions', extensions: ['vsix'] }],
			properties: ['openFile']
		});

		if (!result) {
			return TPromise.as(null);
		}

1115 1116 1117 1118 1119 1120 1121 1122 1123
		return TPromise.join(result.map(vsix => this.extensionsWorkbenchService.install(vsix))).then(() => {
			this.messageService.show(
				severity.Info,
				{
					message: localize('InstallVSIXAction.success', "Successfully installed the extension. Restart to enable it."),
					actions: [this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, localize('InstallVSIXAction.reloadNow', "Reload Now"))]
				}
			);
		});
J
Joao Moreno 已提交
1124
	}
J
Joao Moreno 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
}

export class BuiltinStatusLabelAction extends Action {

	private static Class = 'extension-action built-in-status';

	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }

	constructor() {
		super('extensions.install', localize('builtin', "Built-in"), '', false);
	}

	private update(): void {
		if (this.extension && this.extension.type === LocalExtensionType.System) {
J
Johannes Rieken 已提交
1141
			this.class = `${BuiltinStatusLabelAction.Class} system`;
J
Joao Moreno 已提交
1142
		} else {
J
Johannes Rieken 已提交
1143
			this.class = `${BuiltinStatusLabelAction.Class} user`;
J
Joao Moreno 已提交
1144 1145 1146 1147 1148 1149 1150
		}
	}

	run(): TPromise<any> {
		return TPromise.as(null);
	}
}
1151 1152 1153 1154

export class DisableAllAction extends Action {

	static ID = 'workbench.extensions.action.disableAll';
J
Joao Moreno 已提交
1155
	static LABEL = localize('disableAll', "Disable All Installed Extensions");
1156 1157 1158 1159 1160

	private disposables: IDisposable[] = [];

	constructor(
		id: string = DisableAllAction.ID, label: string = DisableAllAction.LABEL,
1161 1162
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IExtensionsRuntimeService private extensionRuntimeService: IExtensionsRuntimeService
1163
	) {
1164 1165
		super(id, label);
		this.update();
1166 1167 1168 1169
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
	}

	private update(): void {
1170
		this.enabled = this.extensionsWorkbenchService.local.some(e => e.type === LocalExtensionType.User && !this.extensionRuntimeService.isDisabled(e.identifier));
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	}

	run(): TPromise<any> {
		return TPromise.join(this.extensionsWorkbenchService.local.map(e => this.extensionsWorkbenchService.setEnablement(e, false)));
	}

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

export class DisableAllWorkpsaceAction extends Action {

	static ID = 'workbench.extensions.action.disableAllWorkspace';
J
Joao Moreno 已提交
1186
	static LABEL = localize('disableAllWorkspace', "Disable All Installed Extensions for this Workspace");
1187 1188 1189 1190 1191 1192

	private disposables: IDisposable[] = [];

	constructor(
		id: string = DisableAllWorkpsaceAction.ID, label: string = DisableAllWorkpsaceAction.LABEL,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
1193 1194
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IExtensionsRuntimeService private extensionRuntimeService: IExtensionsRuntimeService
1195
	) {
1196 1197
		super(id, label);
		this.update();
1198 1199 1200 1201
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
	}

	private update(): void {
1202
		this.enabled = !!this.workspaceContextService.getWorkspace() && this.extensionsWorkbenchService.local.some(e => e.type === LocalExtensionType.User && !this.extensionRuntimeService.isDisabled(e.identifier));
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
	}

	run(): TPromise<any> {
		return TPromise.join(this.extensionsWorkbenchService.local.map(e => this.extensionsWorkbenchService.setEnablement(e, false, true)));
	}

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

export class EnableAllAction extends Action {

	static ID = 'workbench.extensions.action.enableAll';
J
Joao Moreno 已提交
1218
	static LABEL = localize('enableAll', "Enable All Installed Extensions");
1219 1220 1221 1222 1223

	private disposables: IDisposable[] = [];

	constructor(
		id: string = EnableAllAction.ID, label: string = EnableAllAction.LABEL,
1224 1225
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IExtensionsRuntimeService private extensionRuntimeService: IExtensionsRuntimeService
1226
	) {
1227 1228
		super(id, label);
		this.update();
1229 1230 1231 1232
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
	}

	private update(): void {
1233
		this.enabled = this.extensionsWorkbenchService.local.some(e => this.extensionRuntimeService.canEnable(e.identifier));
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	}

	run(): TPromise<any> {
		return TPromise.join(this.extensionsWorkbenchService.local.map(e => this.extensionsWorkbenchService.setEnablement(e, true)));
	}

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

export class EnableAllWorkpsaceAction extends Action {

	static ID = 'workbench.extensions.action.enableAllWorkspace';
J
Joao Moreno 已提交
1249
	static LABEL = localize('enableAllWorkspace', "Enable All Installed Extensions for this Workspace");
1250 1251 1252 1253 1254 1255

	private disposables: IDisposable[] = [];

	constructor(
		id: string = EnableAllWorkpsaceAction.ID, label: string = EnableAllWorkpsaceAction.LABEL,
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
1256 1257
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IExtensionsRuntimeService private extensionRuntimeService: IExtensionsRuntimeService
1258
	) {
1259 1260
		super(id, label);
		this.update();
1261 1262 1263 1264
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
	}

	private update(): void {
1265
		this.enabled = !!this.workspaceContextService.getWorkspace() && this.extensionsWorkbenchService.local.some(e => this.extensionRuntimeService.canEnable(e.identifier) && !this.extensionRuntimeService.isDisabledAlways(e.identifier));
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	}

	run(): TPromise<any> {
		return TPromise.join(this.extensionsWorkbenchService.local.map(e => this.extensionsWorkbenchService.setEnablement(e, true, true)));
	}

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