extensionsActions.ts 12.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import nls = require('vs/nls');
J
Joao Moreno 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
8
import { Action } from 'vs/base/common/actions';
J
Joao Moreno 已提交
9 10 11 12 13 14 15 16 17 18
// import { assign } from 'vs/base/common/objects';
// import Severity from 'vs/base/common/severity';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
// import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
// import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
// import { IMessageService } from 'vs/platform/message/common/message';
// import { ReloadWindowAction } from 'vs/workbench/electron-browser/actions';
import { IExtension, ExtensionsModel, ExtensionState } from './extensionsModel';
// import { extensionEquals, getTelemetryData } from 'vs/platform/extensionManagement/node/extensionManagementUtil';
// import { IQuickOpenService } from 'vs/workbench/services/quickopen/common/quickOpenService';
J
Joao Moreno 已提交
19
import * as semver from 'semver';
J
Joao Moreno 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

// const CloseAction = new Action('action.close', nls.localize('close', "Close"));

// export class ListExtensionsAction extends Action {

// 	static ID = 'workbench.extensions.action.listExtensions';
// 	static LABEL = nls.localize('showInstalledExtensions', "Show Installed Extensions");

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext ');
// 	}

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

// export class InstallExtensionAction extends Action {

// 	static ID = 'workbench.extensions.action.installExtension';
// 	static LABEL = nls.localize('installExtension', "Install Extension");

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext install ');
// 	}

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

// export class ListOutdatedExtensionsAction extends Action {

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

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext update ');
// 	}

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

// export class ListSuggestedExtensionsAction extends Action {

// 	static ID = 'workbench.extensions.action.listSuggestedExtensions';
// 	static LABEL = nls.localize('showExtensionRecommendations', "Show Extension Recommendations");

// 	constructor(
// 		id: string,
// 		label: string,
// 		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
// 		@IQuickOpenService private quickOpenService: IQuickOpenService
// 	) {
// 		super(id, label, null, true);
// 	}

// 	run(): Promise {
// 		return this.quickOpenService.show('ext recommend ');
// 	}

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

115 116
export class InstallAction extends Action {

J
Joao Moreno 已提交
117
	private disposables: IDisposable[] = [];
118

J
Joao Moreno 已提交
119 120
	constructor(private model: ExtensionsModel, private extension: IExtension) {
		super('extensions.install', nls.localize('installAction', "Install"), 'octicon octicon-cloud-download', false);
121

J
Joao Moreno 已提交
122 123
		this.disposables.push(this.model.onChange(() => this.updateEnablement()));
		this.updateEnablement();
124 125
	}

J
Joao Moreno 已提交
126
	private updateEnablement(): void {
J
Joao Moreno 已提交
127
		this.enabled = this.model.canInstall(this.extension) && this.extension.state === ExtensionState.Uninstalled;
128 129
	}

J
Joao Moreno 已提交
130 131
	run(): TPromise<any> {
		return this.model.install(this.extension);
J
Joao Moreno 已提交
132

J
Joao Moreno 已提交
133
		// this.enabled = false;
134

J
Joao Moreno 已提交
135 136 137 138 139 140 141 142 143
		// return this.extensionManagementService.getInstalled()
		// 	.then(installed => installed.some(({ manifest }) => extensionEquals(manifest, extension)))
		// 	.then(isUpdate => {
		// 		return this.extensionManagementService
		// 			.install(extension)
		// 			.then(() => this.onSuccess(extension, isUpdate), err => this.onError(err, extension, isUpdate))
		// 			.then(() => this.enabled = true)
		// 			.then(() => null);
		// 	});
144 145
	}

J
Joao Moreno 已提交
146 147 148 149 150 151 152 153 154 155
	// private onSuccess(extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, true);
	// 	this.messageService.show(Severity.Info, {
	// 		message: nls.localize('success-installed', "'{0}' was successfully installed. Restart to enable it.", extension.displayName || extension.name),
	// 		actions: [
	// 			CloseAction,
	// 			this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, nls.localize('restartNow', "Restart Now"))
	// 		]
	// 	});
	// }
156

J
Joao Moreno 已提交
157 158 159 160
	// private onError(err: Error, extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, false);
	// 	this.messageService.show(Severity.Error, err);
	// }
161

J
Joao Moreno 已提交
162 163 164
	// private reportTelemetry(extension: IGalleryExtension, isUpdate: boolean, success: boolean) {
	// 	const event = isUpdate ? 'extensionGallery:update' : 'extensionGallery:install';
	// 	const data = assign(getTelemetryData(extension), { success });
165

J
Joao Moreno 已提交
166 167
	// 	this.telemetryService.publicLog(event, data);
	// }
J
Joao Moreno 已提交
168

J
Joao Moreno 已提交
169 170 171
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
172 173
	}
}
J
Joao Moreno 已提交
174

J
Joao Moreno 已提交
175
export class UninstallAction extends Action {
J
Joao Moreno 已提交
176

J
Joao Moreno 已提交
177
	private disposables: IDisposable[] = [];
J
Joao Moreno 已提交
178

J
Joao Moreno 已提交
179 180
	constructor(private model: ExtensionsModel, private extension: IExtension) {
		super('extensions.uninstall', nls.localize('uninstall', "Uninstall"), 'octicon octicon-x', false);
J
Joao Moreno 已提交
181

J
Joao Moreno 已提交
182 183 184
		this.disposables.push(this.model.onChange(() => this.updateEnablement()));
		this.updateEnablement();
	}
J
Joao Moreno 已提交
185

J
Joao Moreno 已提交
186 187 188
	private updateEnablement(): void {
		this.enabled = this.extension.state === ExtensionState.Installed;
	}
J
Joao Moreno 已提交
189

J
Joao Moreno 已提交
190 191
	run(): TPromise<any> {
		// const name = extension.manifest.displayName || extension.manifest.name;
J
Joao Moreno 已提交
192

J
Joao Moreno 已提交
193 194 195
		if (!window.confirm(nls.localize('deleteSure', "Are you sure you want to uninstall '{0}'?", this.extension.displayName))) {
			return TPromise.as(null);
		}
J
Joao Moreno 已提交
196

J
Joao Moreno 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

		return this.model.uninstall(this.extension);

		// this.enabled = false;

		// return this.extensionManagementService.getInstalled().then(localExtensions => {
		// 	const [local] = localExtensions.filter(local => extensionEquals(local.manifest, extension.manifest));

		// 	if (!local) {
		// 		return TPromise.wrapError(nls.localize('notFound', "Extension '{0}' not installed.", name));
		// 	}

		// 	return this.extensionManagementService.uninstall(local)
		// 		.then(() => this.onSuccess(local), err => this.onError(err, local))
		// 		.then(() => this.enabled = true)
		// 		.then(() => null);
		// });
	}
J
Joao Moreno 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

// 	private onSuccess(extension: ILocalExtension) {
// 		const name = extension.manifest.displayName || extension.manifest.name;
// 		this.reportTelemetry(extension, true);

// 		this.messageService.show(Severity.Info, {
// 			message: nls.localize('success-uninstalled', "'{0}' was successfully uninstalled. Restart to deactivate it.", name),
// 			actions: [
// 				CloseAction,
// 				this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, nls.localize('restartNow2', "Restart Now"))
// 			]
// 		});
// 	}

// 	private onError(err: Error, extension: ILocalExtension) {
// 		this.reportTelemetry(extension, false);
// 		this.messageService.show(Severity.Error, err);
// 	}

// 	private reportTelemetry(extension: ILocalExtension, success: boolean) {
// 		const data = assign(getTelemetryData(extension), { success });

// 		this.telemetryService.publicLog('extensionGallery:uninstall', data);
// 	}
J
Joao Moreno 已提交
239 240 241 242 243 244

	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}
J
Joao Moreno 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

export class CombinedInstallAction extends Action {

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

	constructor(private model: ExtensionsModel, private extension: IExtension) {
		super('extensions.combinedInstall', '', '', false);

		this.installAction = new InstallAction(model, extension);
		this.uninstallAction = new UninstallAction(model, extension);
		this.disposables.push(this.installAction, this.uninstallAction);

		this.disposables.push(this.installAction.addListener2(Action.ENABLED, () => this.update()));
		this.disposables.push(this.uninstallAction.addListener2(Action.ENABLED, () => this.update()));
		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 已提交
273 274 275 276
		} else if (this.extension.state === ExtensionState.Installing) {
			this.enabled = false;
			this.label = this.installAction.label;
			this.class = this.installAction.class;
J
Joao Moreno 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
		} 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 已提交
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 357 358 359 360 361
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}

export class UpdateAction extends Action {

	private static EnabledClass = 'extension-update-action octicon octicon-cloud-download';
	private static DisabledClass = `${ UpdateAction.EnabledClass } disabled`;

	private disposables: IDisposable[] = [];

	constructor(private model: ExtensionsModel, private extension: IExtension) {
		super('extensions.install', nls.localize('installAction', "Install"), UpdateAction.DisabledClass, false);

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

	private updateEnablement(): void {
		const canInstall = this.model.canInstall(this.extension);
		const isInstalled = this.extension.state === ExtensionState.Installed;
		const canUpdate = semver.gt(this.extension.latestVersion, this.extension.version);

		console.log(this.extension.latestVersion, this.extension.version);

		this.enabled = canInstall && isInstalled && canUpdate;
		this.class = this.enabled ? UpdateAction.EnabledClass : UpdateAction.DisabledClass;
	}

	run(): TPromise<any> {
		return this.model.install(this.extension);

		// this.enabled = false;

		// return this.extensionManagementService.getInstalled()
		// 	.then(installed => installed.some(({ manifest }) => extensionEquals(manifest, extension)))
		// 	.then(isUpdate => {
		// 		return this.extensionManagementService
		// 			.install(extension)
		// 			.then(() => this.onSuccess(extension, isUpdate), err => this.onError(err, extension, isUpdate))
		// 			.then(() => this.enabled = true)
		// 			.then(() => null);
		// 	});
	}

	// private onSuccess(extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, true);
	// 	this.messageService.show(Severity.Info, {
	// 		message: nls.localize('success-installed', "'{0}' was successfully installed. Restart to enable it.", extension.displayName || extension.name),
	// 		actions: [
	// 			CloseAction,
	// 			this.instantiationService.createInstance(ReloadWindowAction, ReloadWindowAction.ID, nls.localize('restartNow', "Restart Now"))
	// 		]
	// 	});
	// }

	// private onError(err: Error, extension: IGalleryExtension, isUpdate: boolean) {
	// 	this.reportTelemetry(extension, isUpdate, false);
	// 	this.messageService.show(Severity.Error, err);
	// }

	// private reportTelemetry(extension: IGalleryExtension, isUpdate: boolean, success: boolean) {
	// 	const event = isUpdate ? 'extensionGallery:update' : 'extensionGallery:install';
	// 	const data = assign(getTelemetryData(extension), { success });

	// 	this.telemetryService.publicLog(event, data);
	// }

J
Joao Moreno 已提交
362 363 364 365 366
	dispose(): void {
		super.dispose();
		this.disposables = dispose(this.disposables);
	}
}