extensionsActions.ts 31.4 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 15
import { ActionItem, IActionItem } from 'vs/base/browser/ui/actionbar/actionbar';
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 './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';
J
Joao Moreno 已提交
27
import { shell, 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';
J
Joao Moreno 已提交
33 34

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

36 37
export class InstallAction extends Action {

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

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

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

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

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

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

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

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

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

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

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

S
Sandeep Somavarapu 已提交
89 90 91 92 93 94
	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 已提交
95
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
96 97 98
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
J
Joao Moreno 已提交
99

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

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

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

117 118
		const state = this.extension.state;

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

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

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

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

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

145
		return this.extensionsWorkbenchService.uninstall(this.extension);
J
Joao Moreno 已提交
146
	}
J
Joao Moreno 已提交
147

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

export class CombinedInstallAction extends Action {

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

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

J
Joao Moreno 已提交
173 174
		this.installAction = instantiationService.createInstance(InstallAction);
		this.uninstallAction = instantiationService.createInstance(UninstallAction);
J
Joao Moreno 已提交
175 176
		this.disposables.push(this.installAction, this.uninstallAction);

177
		this.installAction.onDidChange(this.update, this, this.disposables);
178
		this.uninstallAction.onDidChange(this.update, this, this.disposables);
J
Joao Moreno 已提交
179 180 181 182
		this.update();
	}

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

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

export class UpdateAction extends Action {

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

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

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

J
Joao Moreno 已提交
240 241
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
		this.update();
J
Joao Moreno 已提交
242 243
	}

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

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

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

262
		this.enabled = canInstall && isInstalled && this.extension.outdated;
263 264 265
		this.class = this.enabled ? UpdateAction.EnabledClass : UpdateAction.DisabledClass;
	}

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

J
Joao Moreno 已提交
270 271 272 273 274 275
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

276 277 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
export interface IExtensionAction extends IAction {
	extension: IExtension;
}

export class DropDownMenuActionItem extends ActionItem {

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

	constructor(action: IAction, private menuActions: IExtensionAction[], private contextMenuService: IContextMenuService) {
		super(null, action, { icon: true, label: true });
		this.disposables = [...menuActions];
	}

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

	set extension(extension: IExtension) {
		this._extension = extension;
		for (const menuAction of this.menuActions) {
			menuAction.extension = extension;
		}
	}

	public onClick(event: any): void {
		DOM.EventHelper.stop(event, true);
		let elementPosition = DOM.getDomNodePagePosition(this.builder.getHTMLElement());
		const anchor = { x: elementPosition.left, y: elementPosition.top + elementPosition.height + 10 };
		this.contextMenuService.showContextMenu({
			getAnchor: () => anchor,
			getActions: () => TPromise.wrap(this.menuActions),
		});
	}

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

export class EnableForWorkspaceAction extends Action {

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

	constructor(
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IExtensionsRuntimeService private extensionsRuntimeService: IExtensionsRuntimeService
	) {
		super('extensions.enableForWorkspace', localize('enableForWorkspaceAction', "Workspace"), '', !!workspaceContextService.getWorkspace());
	}

	private update(): void {
		if (!!this.workspaceContextService.getWorkspace()) {
			this.enabled = this.extensionsRuntimeService.getDisabledExtensions(true).indexOf(this.extension.identifier) !== -1;
		}
	}

	run(): TPromise<any> {
		return this.extensionsWorkbenchService.setEnablement(this.extension, true, true);
	}
}

export class EnableGloballyAction extends Action {

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

	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		super('extensions.enableGlobally', localize('enableGloballyAction', "Global"), '', true);
	}

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

J
Joao Moreno 已提交
357 358
export class EnableAction extends Action {

359
	static ID = 'extensions.enable';
J
Joao Moreno 已提交
360
	private static EnabledClass = 'extension-action enable';
J
Johannes Rieken 已提交
361
	private static DisabledClass = `${EnableAction.EnabledClass} disabled`;
J
Joao Moreno 已提交
362 363

	private disposables: IDisposable[] = [];
364 365 366 367

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

J
Joao Moreno 已提交
368 369
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
370 371
	set extension(extension: IExtension) { this._extension = extension; this._actionItem.extension = extension; this.update(); }

J
Joao Moreno 已提交
372 373

	constructor(
374
		@IInstantiationService private instantiationService: IInstantiationService,
375
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
J
Joao Moreno 已提交
376
	) {
377 378 379 380
		super(EnableAction.ID, localize('enableAction', "Enable"), EnableAction.DisabledClass, false);

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

J
Joao Moreno 已提交
382 383
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
		this.update();
J
Joao Moreno 已提交
384 385
	}

J
Joao Moreno 已提交
386 387 388 389 390 391 392
	private update(): void {
		if (!this.extension) {
			this.enabled = false;
			this.class = EnableAction.DisabledClass;
			return;
		}

393
		this.enabled = !this.extension.reload && this.extension.state === ExtensionState.Disabled;
J
Joao Moreno 已提交
394 395 396
		this.class = this.enabled ? EnableAction.EnabledClass : EnableAction.DisabledClass;
	}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}

}

export class DisableForWorkspaceAction extends Action {

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

	constructor(
		@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		super('extensions.disableForWorkspace', localize('disableForWorkspaceAction', "Workspace"), '', !!workspaceContextService.getWorkspace());
	}

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

export class DisableGloballyAction extends Action {

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

	constructor(
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		super('extensions.disableGlobally', localize('disableGloballyAction', "Global"), '', true);
	}

J
Joao Moreno 已提交
434
	run(): TPromise<any> {
435
		return this.extensionsWorkbenchService.setEnablement(this.extension, false, false);
436 437 438 439 440
	}
}

export class DisableAction extends Action {

441 442
	static ID = 'extensions.disable';

443 444 445 446
	private static EnabledClass = 'extension-action disable';
	private static DisabledClass = `${DisableAction.EnabledClass} disabled`;

	private disposables: IDisposable[] = [];
447 448 449
	private _actionItem: DisableActionItem;
	get actionItem(): IActionItem { return this._actionItem; }

450 451
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
452 453
	set extension(extension: IExtension) { this._extension = extension; this._actionItem.extension = extension; this.update(); }

454 455

	constructor(
456
		@IInstantiationService private instantiationService: IInstantiationService,
457 458
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
	) {
459 460 461
		super(DisableAction.ID, localize('disableAction', "Disable"), DisableAction.DisabledClass, false);
		this._actionItem = this.instantiationService.createInstance(DisableActionItem, this);
		this.disposables.push(this._actionItem);
462 463 464 465 466 467 468 469 470 471

		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 已提交
472 473
		}

474
		this.enabled = !this.extension.reload && this.extension.state === ExtensionState.Enabled;
475
		this.class = this.enabled ? DisableAction.EnabledClass : DisableAction.DisabledClass;
J
Joao Moreno 已提交
476 477
	}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class EnableActionItem extends DropDownMenuActionItem {
	constructor(
		action: IAction,
		@IInstantiationService instantiationService: IInstantiationService,
		@IContextMenuService contextMenuService: IContextMenuService) {
		super(action, [instantiationService.createInstance(EnableForWorkspaceAction), instantiationService.createInstance(EnableGloballyAction)], contextMenuService);
	}
}

export class DisableActionItem extends DropDownMenuActionItem {
	constructor(
		action: IAction,
		@IInstantiationService instantiationService: IInstantiationService,
		@IContextMenuService contextMenuService: IContextMenuService) {
		super(action, [instantiationService.createInstance(DisableForWorkspaceAction), instantiationService.createInstance(DisableGloballyAction)], contextMenuService);
J
Joao Moreno 已提交
499
	}
500
}
J
Joao Moreno 已提交
501

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
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 已提交
531 532
		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);
533 534 535 536 537 538 539 540 541 542 543 544 545 546
		this.class = this.enabled ? ReloadAction.EnabledClass : ReloadAction.DisabledClass;
	}

	run(): TPromise<any> {
		return this.getReloadMessage().then(message => {
			if (window.confirm(message)) {
				this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, localize('reloadNow', "Reload Now")).run();
			}
			return null;
		});
	}

	private getReloadMessage(): TPromise<string> {
		return this.extensionsRuntimeService.getExtensions(true).then(extensionDescriptions => {
547 548 549 550 551 552 553 554 555
			const state = this.extension.state;
			if (state === ExtensionState.Installed) {
				return localize('postInstallMessage', "Reload this window to activate '{0}' extension?", this.extension.displayName);
			}
			if (state === ExtensionState.Disabled) {
				return localize('postEnableMessage', "Reload this window to enable '{0}' extension?", this.extension.displayName);
			}
			if (state === ExtensionState.Enabled) {
				return localize('postDisableMessage', "Reload this window to disable '{0}' extension?", this.extension.displayName);
556
			}
557
			return localize('postUninstallMessage', "Reload this window to deactivate '{0}' extension?", this.extension.displayName);
558 559 560 561
		});
	}
}

J
Joao Moreno 已提交
562 563
export class UpdateAllAction extends Action {

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

J
Joao Moreno 已提交
567 568 569
	private disposables: IDisposable[] = [];

	constructor(
570 571
		id = UpdateAllAction.ID,
		label = UpdateAllAction.LABEL,
J
Joao Moreno 已提交
572 573
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
574
		super(id, label, '', false);
J
Joao Moreno 已提交
575 576 577 578 579

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

580 581
	private get outdated(): IExtension[] {
		return this.extensionsWorkbenchService.local.filter(
J
Joao Moreno 已提交
582
			e => this.extensionsWorkbenchService.canInstall(e)
583
				&& e.type === LocalExtensionType.User
584
				&& (e.state === ExtensionState.Enabled || e.state === ExtensionState.Disabled || e.state === ExtensionState.Installed)
585
				&& e.outdated
J
Joao Moreno 已提交
586
		);
J
Joao Moreno 已提交
587 588 589
	}

	private update(): void {
590
		this.enabled = this.outdated.length > 0;
J
Joao Moreno 已提交
591 592
	}

J
Johannes Rieken 已提交
593
	run(promptToInstallDependencies: boolean = true, ): TPromise<any> {
594
		return TPromise.join(this.outdated.map(e => this.extensionsWorkbenchService.install(e, promptToInstallDependencies)));
J
Joao Moreno 已提交
595 596 597 598 599 600 601
	}

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

export class OpenExtensionsViewletAction extends ToggleViewletAction {

J
Joao Moreno 已提交
605
	static ID = VIEWLET_ID;
606 607 608 609 610 611 612 613 614 615 616 617
	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 已提交
618 619 620 621 622
export class InstallExtensionsAction extends OpenExtensionsViewletAction {
	static ID = 'workbench.extensions.action.installExtensions';
	static LABEL = localize('installExtensions', "Install Extensions");
}

J
Joao Moreno 已提交
623
export class ShowInstalledExtensionsAction extends Action {
624

J
Joao Moreno 已提交
625 626
	static ID = 'workbench.extensions.action.showInstalledExtensions';
	static LABEL = localize('showInstalledExtensions', "Show Installed Extensions");
627 628 629 630

	constructor(
		id: string,
		label: string,
631 632
		@IViewletService private viewletService: IViewletService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
633
	) {
634
		super(id, label, 'clear-extensions', true);
635 636 637 638 639 640
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
641
				viewlet.search('');
642 643 644 645 646
				viewlet.focus();
			});
	}
}

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
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 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
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);
686
		this.enabled = false;
J
Joao Moreno 已提交
687 688 689 690 691 692 693 694 695 696 697 698
		onSearchChange(this.onSearchChange, this, this.disposables);
	}

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

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

J
Joao Moreno 已提交
699
export class ShowOutdatedExtensionsAction extends Action {
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

	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 => {
716
				viewlet.search('@outdated ');
717 718 719 720 721 722 723 724
				viewlet.focus();
			});
	}

	protected isEnabled(): boolean {
		return true;
	}
}
J
Joao Moreno 已提交
725 726 727 728 729 730 731 732 733 734 735

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
	) {
736
		super(id, label, null, true);
J
Joao Moreno 已提交
737 738 739 740 741 742
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
743
				viewlet.search('@sort:installs ');
J
Joao Moreno 已提交
744 745 746 747 748 749 750 751 752
				viewlet.focus();
			});
	}

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

J
Joao Moreno 已提交
753
export class ShowRecommendedExtensionsAction extends Action {
J
Joao Moreno 已提交
754

J
Joao Moreno 已提交
755
	static ID = 'workbench.extensions.action.showRecommendedExtensions';
J
Joao Moreno 已提交
756
	static LABEL = localize('showRecommendedExtensions', "Show Recommended Extensions");
J
Joao Moreno 已提交
757 758 759 760 761 762

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
763
		super(id, label, null, true);
J
Joao Moreno 已提交
764 765 766 767 768 769
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
770
				viewlet.search('@recommended ');
J
Joao Moreno 已提交
771 772 773 774
				viewlet.focus();
			});
	}

J
Joao Moreno 已提交
775 776 777 778 779
	protected isEnabled(): boolean {
		return true;
	}
}

S
Sandeep Somavarapu 已提交
780 781 782 783 784 785 786 787
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 已提交
788
		@IWorkspaceContextService contextService: IWorkspaceContextService,
S
Sandeep Somavarapu 已提交
789 790
		@IViewletService private viewletService: IViewletService
	) {
S
Sandeep Somavarapu 已提交
791
		super(id, label, null, !!contextService.getWorkspace());
S
Sandeep Somavarapu 已提交
792 793 794 795 796 797
	}

	run(): TPromise<void> {
		return this.viewletService.openViewlet(VIEWLET_ID, true)
			.then(viewlet => viewlet as IExtensionsViewlet)
			.then(viewlet => {
798
				viewlet.search('@recommended:workspace ');
S
Sandeep Somavarapu 已提交
799 800 801 802 803 804 805 806 807
				viewlet.focus();
			});
	}

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

J
Joao Moreno 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
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 已提交
823
		if (sortBy === undefined && sortOrder === undefined) {
J
Joao Moreno 已提交
824 825 826 827
			throw new Error('bad arguments');
		}

		this.query = Query.parse('');
828
		this.enabled = false;
J
Joao Moreno 已提交
829 830 831 832 833 834
		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);
835
		this.enabled = value && this.query.isValid() && !this.query.equals(query);
J
Joao Moreno 已提交
836 837 838 839 840 841 842 843 844 845 846
	}

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

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
	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,
		@IEnvironmentService private environmentService: IEnvironmentService
	) {
		super(id, label, null, true);
	}

	run(): TPromise<any> {
		const extensionsHome = this.environmentService.extensionsPath;
		shell.showItemInFolder(paths.normalize(extensionsHome, true));

		return TPromise.as(true);
	}

J
Joao Moreno 已提交
872 873 874
	protected isEnabled(): boolean {
		return true;
	}
S
Sandeep Somavarapu 已提交
875 876
}

S
Sandeep Somavarapu 已提交
877
export class ConfigureWorkspaceRecommendedExtensionsAction extends Action {
J
Joao Moreno 已提交
878

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

J
Joao Moreno 已提交
882 883 884 885 886 887 888 889 890
	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 已提交
891
		super(id, label, null, !!contextService.getWorkspace());
S
Sandeep Somavarapu 已提交
892 893 894
	}

	public run(event: any): TPromise<any> {
J
Joao Moreno 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
		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 已提交
916
		const extensionsFileResource = URI.file(paths.join(this.contextService.getWorkspace().resource.fsPath, '.vscode', `${ConfigurationKey}.json`));
J
Joao Moreno 已提交
917 918 919 920

		return this.fileService.resolveContent(extensionsFileResource).then(content => {
			return { created: false, extensionsFileResource };
		}, err => {
921
			return this.fileService.updateContent(extensionsFileResource, ExtensionsConfigurationInitialContent).then(() => {
J
Joao Moreno 已提交
922 923 924
				return { created: true, extensionsFileResource };
			});
		});
S
Sandeep Somavarapu 已提交
925
	}
J
Joao Moreno 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
}

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,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
		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);
		}

		return TPromise.join(result.map(vsix => this.extensionsWorkbenchService.install(vsix)));
	}
J
Joao Moreno 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
}

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 已提交
969
			this.class = `${BuiltinStatusLabelAction.Class} system`;
J
Joao Moreno 已提交
970
		} else {
J
Johannes Rieken 已提交
971
			this.class = `${BuiltinStatusLabelAction.Class} user`;
J
Joao Moreno 已提交
972 973 974 975 976 977 978
		}
	}

	run(): TPromise<any> {
		return TPromise.as(null);
	}
}