extensionsActions.ts 11.1 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 { IDisposable, dispose } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
12
import { ReloadWindowAction } from 'vs/workbench/electron-browser/actions';
13
import { IExtension, ExtensionState, IExtensionsWorkbenchService, VIEWLET_ID, IExtensionsViewlet } from './extensions';
14
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
15
import { IMessageService, CloseAction } from 'vs/platform/message/common/message';
16 17 18
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 已提交
19

20 21
export class InstallAction extends Action {

J
Joao Moreno 已提交
22 23
	private static InstallLabel = localize('installAction', "Install");
	private static InstallingLabel = localize('installing', "Installing");
J
Joao Moreno 已提交
24
	private disposables: IDisposable[] = [];
25

26 27 28 29
	constructor(
		private extension: IExtension,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
30
		super('extensions.install', InstallAction.InstallLabel, 'extension-action install', false);
31

32
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.update()));
J
Joao Moreno 已提交
33
		this.update();
34 35
	}

J
Joao Moreno 已提交
36
	private update(): void {
37
		this.enabled = this.extensionsWorkbenchService.canInstall(this.extension) && this.extension.state === ExtensionState.Uninstalled;
J
Joao Moreno 已提交
38
		this.label = this.extension.state === ExtensionState.Installing ? InstallAction.InstallingLabel : InstallAction.InstallLabel;
39 40
	}

J
Joao Moreno 已提交
41
	run(): TPromise<any> {
42
		return this.extensionsWorkbenchService.install(this.extension);
43 44
	}

J
Joao Moreno 已提交
45 46 47
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
48 49
	}
}
J
Joao Moreno 已提交
50

J
Joao Moreno 已提交
51
export class UninstallAction extends Action {
J
Joao Moreno 已提交
52

J
Joao Moreno 已提交
53
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
54

55 56
	constructor(
		private extension: IExtension,
57 58 59
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService,
		@IMessageService private messageService: IMessageService,
		@IInstantiationService private instantiationService: IInstantiationService
60
	) {
J
Joao Moreno 已提交
61
		super('extensions.uninstall', localize('uninstall', "Uninstall"), 'extension-action uninstall', false);
J
Joao Moreno 已提交
62

63
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.updateEnablement()));
J
Joao Moreno 已提交
64 65
		this.updateEnablement();
	}
J
Joao Moreno 已提交
66

J
Joao Moreno 已提交
67
	private updateEnablement(): void {
J
Joao Moreno 已提交
68 69
		this.enabled = this.extension.state === ExtensionState.Installed
			|| this.extension.state === ExtensionState.NeedsRestart;
J
Joao Moreno 已提交
70
	}
J
Joao Moreno 已提交
71

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

77 78 79 80 81 82
		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),
				actions: [CloseAction, this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, localize('restartNow', "Restart Now"))]
			});
		});
J
Joao Moreno 已提交
83
	}
J
Joao Moreno 已提交
84

J
Joao Moreno 已提交
85 86 87 88 89
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}
J
Joao Moreno 已提交
90 91 92 93 94 95 96

export class CombinedInstallAction extends Action {

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

97 98 99 100
	constructor(
		private extension: IExtension,
		@IInstantiationService instantiationService: IInstantiationService
	) {
J
Joao Moreno 已提交
101 102
		super('extensions.combinedInstall', '', '', false);

103 104
		this.installAction = instantiationService.createInstance(InstallAction, extension);
		this.uninstallAction = instantiationService.createInstance(UninstallAction, extension);
J
Joao Moreno 已提交
105 106
		this.disposables.push(this.installAction, this.uninstallAction);

107
		this.installAction.onDidChange(this.update, this, this.disposables);
J
Joao Moreno 已提交
108 109 110 111 112 113 114 115 116 117 118 119
		this.update();
	}

	private update(): void {
		if (this.installAction.enabled) {
			this.enabled = true;
			this.label = this.installAction.label;
			this.class = this.installAction.class;
		} else if (this.uninstallAction.enabled) {
			this.enabled = true;
			this.label = this.uninstallAction.label;
			this.class = this.uninstallAction.class;
J
Joao Moreno 已提交
120 121 122 123
		} else if (this.extension.state === ExtensionState.Installing) {
			this.enabled = false;
			this.label = this.installAction.label;
			this.class = this.installAction.class;
J
Joao Moreno 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		} else {
			this.enabled = false;
		}
	}

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

		return TPromise.as(null);
	}

J
Joao Moreno 已提交
139 140 141 142 143 144 145 146
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class UpdateAction extends Action {

J
Joao Moreno 已提交
147
	private static EnabledClass = 'extension-action update';
J
Joao Moreno 已提交
148 149 150 151
	private static DisabledClass = `${ UpdateAction.EnabledClass } disabled`;

	private disposables: IDisposable[] = [];

152 153 154 155
	constructor(
		private extension: IExtension,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
	) {
J
Joao Moreno 已提交
156
		super('extensions.update', localize('updateAction', "Update"), UpdateAction.DisabledClass, false);
J
Joao Moreno 已提交
157

158
		this.disposables.push(this.extensionsWorkbenchService.onChange(() => this.updateEnablement()));
J
Joao Moreno 已提交
159 160 161 162
		this.updateEnablement();
	}

	private updateEnablement(): void {
163
		const canInstall = this.extensionsWorkbenchService.canInstall(this.extension);
J
Joao Moreno 已提交
164 165
		const isInstalled = this.extension.state === ExtensionState.Installed
			|| this.extension.state === ExtensionState.NeedsRestart;
J
Joao Moreno 已提交
166

J
Joao Moreno 已提交
167
		this.enabled = canInstall && isInstalled && this.extension.outdated;
J
Joao Moreno 已提交
168 169 170 171
		this.class = this.enabled ? UpdateAction.EnabledClass : UpdateAction.DisabledClass;
	}

	run(): TPromise<any> {
172
		return this.extensionsWorkbenchService.install(this.extension);
J
Joao Moreno 已提交
173 174
	}

J
Joao Moreno 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	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[] = [];

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

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

	private updateEnablement(): void {
		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 已提交
213 214 215 216
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
217 218 219 220
}

export class OpenExtensionsViewletAction extends ToggleViewletAction {

J
Joao Moreno 已提交
221
	static ID = VIEWLET_ID;
222 223 224 225 226 227 228 229 230 231 232 233
	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 已提交
234 235 236 237 238
export class InstallExtensionsAction extends OpenExtensionsViewletAction {
	static ID = 'workbench.extensions.action.installExtensions';
	static LABEL = localize('installExtensions', "Install Extensions");
}

239
export class ClearExtensionsInputAction extends Action {
240

241 242
	static ID = 'workbench.extensions.action.clearExtensionsInput';
	static LABEL = localize('clearExtensionsInput', "Clear Extensions Input");
243 244 245 246

	constructor(
		id: string,
		label: string,
247 248
		@IViewletService private viewletService: IViewletService,
		@IExtensionsWorkbenchService private extensionsWorkbenchService: IExtensionsWorkbenchService
249
	) {
250
		super(id, label, 'clear-extensions', true);
251 252 253 254 255 256 257 258 259 260 261 262 263
	}

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

}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
export class ListOutdatedExtensionsAction extends Action {

	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 已提交
291 292 293 294 295 296 297 298 299 300 301

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
	) {
302
		super(id, label, null, true);
J
Joao Moreno 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	}

	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 已提交
319
export class ShowRecommendedExtensionsAction extends Action {
J
Joao Moreno 已提交
320

J
Joao Moreno 已提交
321 322
	static ID = 'workbench.extensions.action.showRecommendedExtensions';
	static LABEL = localize('showRecommendedExtensions', "Show Recommended Extension");
J
Joao Moreno 已提交
323 324 325 326 327 328

	constructor(
		id: string,
		label: string,
		@IViewletService private viewletService: IViewletService
	) {
329
		super(id, label, null, true);
J
Joao Moreno 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	}

	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;
	}
}
345

346 347 348
export class ShowInstalledExtensionsAction extends ClearExtensionsInputAction {
	static ID = 'workbench.extensions.action.showInstalledExtensions';
	static LABEL = localize('showInstalledExtensions', "Show Installed Extensions");
349
}