extensionsActions.ts 13.0 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

21 22
export class InstallAction extends Action {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

93 94 95
		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),
96
				actions: [LaterAction, this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, localize('restartNow', "Restart Now"))]
97 98
			});
		});
J
Joao Moreno 已提交
99
	}
J
Joao Moreno 已提交
100

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

export class CombinedInstallAction extends Action {

J
Joao Moreno 已提交
109
	private static NoExtensionClass = 'extension-action install no-extension';
J
Joao Moreno 已提交
110 111 112
	private installAction: InstallAction;
	private uninstallAction: UninstallAction;
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
113 114 115 116 117 118 119
	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 已提交
120

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

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

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

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

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

export class UpdateAction extends Action {

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

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

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

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

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

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

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

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

J
Joao Moreno 已提交
212 213 214 215 216 217 218 219 220 221 222 223
	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 已提交
224 225 226
	private _extension: IExtension;
	get extension(): IExtension { return this._extension; }
	set extension(extension: IExtension) { this._extension = extension; this.update(); }
J
Joao Moreno 已提交
227 228 229 230 231 232 233

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

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

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

J
Joao Moreno 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257
		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 已提交
258 259 260 261
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
262 263 264 265
}

export class OpenExtensionsViewletAction extends ToggleViewletAction {

J
Joao Moreno 已提交
266
	static ID = VIEWLET_ID;
267 268 269 270 271 272 273 274 275 276 277 278
	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 已提交
279 280 281 282 283
export class InstallExtensionsAction extends OpenExtensionsViewletAction {
	static ID = 'workbench.extensions.action.installExtensions';
	static LABEL = localize('installExtensions', "Install Extensions");
}

J
Joao Moreno 已提交
284
export class ShowInstalledExtensionsAction extends Action {
285

J
Joao Moreno 已提交
286 287
	static ID = 'workbench.extensions.action.showInstalledExtensions';
	static LABEL = localize('showInstalledExtensions', "Show Installed Extensions");
288 289 290 291

	constructor(
		id: string,
		label: string,
292 293
		@IViewletService private viewletService: IViewletService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
294
	) {
295
		super(id, label, 'clear-extensions', true);
296 297 298 299 300 301 302 303 304 305 306 307
	}

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

J
Joao Moreno 已提交
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
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);
		onSearchChange(this.onSearchChange, this, this.disposables);
	}

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

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

J
Joao Moreno 已提交
335
export class ShowOutdatedExtensionsAction extends Action {
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

	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 => {
				viewlet.search('@outdated', true);
				viewlet.focus();
			});
	}

	protected isEnabled(): boolean {
		return true;
	}
}
J
Joao Moreno 已提交
362 363 364 365 366 367 368 369 370 371 372

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
	) {
373
		super(id, label, null, true);
J
Joao Moreno 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	}

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

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

J
Joao Moreno 已提交
390
export class ShowRecommendedExtensionsAction extends Action {
J
Joao Moreno 已提交
391

J
Joao Moreno 已提交
392
	static ID = 'workbench.extensions.action.showRecommendedExtensions';
J
Joao Moreno 已提交
393
	static LABEL = localize('showRecommendedExtensions', "Show Recommended Extensions");
J
Joao Moreno 已提交
394 395 396 397 398 399

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
400
		super(id, label, null, true);
J
Joao Moreno 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414
	}

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

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