cliProcessMain.ts 8.5 KB
Newer Older
J
Joao Moreno 已提交
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 { localize } from 'vs/nls';
7 8
import product from 'vs/platform/product';
import pkg from 'vs/platform/package';
9
import * as path from 'path';
B
Benjamin Pasero 已提交
10
import { ParsedArgs } from 'vs/platform/environment/node/argv';
J
Joao Moreno 已提交
11
import { TPromise } from 'vs/base/common/winjs.base';
12
import { sequence } from 'vs/base/common/async';
J
Joao Moreno 已提交
13
import { IPager } from 'vs/base/common/paging';
J
Joao Moreno 已提交
14 15 16 17 18 19 20 21
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { IEventService } from 'vs/platform/event/common/event';
import { EventService } from 'vs/platform/event/common/eventService';
J
Joao Moreno 已提交
22
import { IExtensionManagementService, IExtensionGalleryService, IExtensionManifest, IGalleryExtension, LocalExtensionType } from 'vs/platform/extensionManagement/common/extensionManagement';
J
Joao Moreno 已提交
23 24
import { ExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService';
import { ExtensionGalleryService } from 'vs/platform/extensionManagement/node/extensionGalleryService';
25 26 27
import { ITelemetryService, combinedAppender, NullTelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
J
Joao Moreno 已提交
28 29
import { IRequestService } from 'vs/platform/request/common/request';
import { RequestService } from 'vs/platform/request/node/requestService';
J
Joao Moreno 已提交
30
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
31
import { ConfigurationService } from 'vs/platform/configuration/node/configurationService';
32
import { AppInsightsAppender } from 'vs/platform/telemetry/node/appInsightsAppender';
J
Johannes Rieken 已提交
33
import { mkdirp } from 'vs/base/node/pfs';
J
Joao Moreno 已提交
34

J
Joao Moreno 已提交
35
const notFound = id => localize('notFound', "Extension '{0}' not found.", id);
J
Joao Moreno 已提交
36
const notInstalled = id => localize('notInstalled', "Extension '{0}' is not installed.", id);
J
Joao Moreno 已提交
37
const useId = localize('useId', "Make sure you use the full extension ID, including the publisher, eg: {0}", 'ms-vscode.csharp');
J
Joao Moreno 已提交
38

J
Joao Moreno 已提交
39
function getId(manifest: IExtensionManifest): string {
J
Johannes Rieken 已提交
40
	return `${manifest.publisher}.${manifest.name}`;
J
Joao Moreno 已提交
41 42
}

J
Johannes Rieken 已提交
43
type Task = { (): TPromise<void> };
44

J
Joao Moreno 已提交
45 46 47 48 49
class Main {

	constructor(
		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
		@IExtensionGalleryService private extensionGalleryService: IExtensionGalleryService
J
Johannes Rieken 已提交
50
	) { }
J
Joao Moreno 已提交
51 52

	run(argv: ParsedArgs): TPromise<any> {
J
Joao Moreno 已提交
53 54
		// TODO@joao - make this contributable

J
Joao Moreno 已提交
55
		if (argv['list-extensions']) {
J
Joao Moreno 已提交
56 57
			return this.listExtensions();
		} else if (argv['install-extension']) {
58
			const arg = argv['install-extension'];
J
Joao Moreno 已提交
59 60
			const args: string[] = typeof arg === 'string' ? [arg] : arg;
			return this.installExtension(args);
J
Joao Moreno 已提交
61
		} else if (argv['uninstall-extension']) {
62 63 64
			const arg = argv['uninstall-extension'];
			const ids: string[] = typeof arg === 'string' ? [arg] : arg;
			return this.uninstallExtension(ids);
J
Joao Moreno 已提交
65 66
		}
	}
J
Joao Moreno 已提交
67 68

	private listExtensions(): TPromise<any> {
J
Joao Moreno 已提交
69
		return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(extensions => {
J
Joao Moreno 已提交
70
			extensions.forEach(e => console.log(getId(e.manifest)));
J
Joao Moreno 已提交
71 72 73
		});
	}

74 75 76 77 78
	private installExtension(extensions: string[]): TPromise<any> {
		const vsixTasks: Task[] = extensions
			.filter(e => /\.vsix$/i.test(e))
			.map(id => () => {
				const extension = path.isAbsolute(id) ? id : path.join(process.cwd(), id);
79

80 81 82 83
				return this.extensionManagementService.install(extension).then(() => {
					console.log(localize('successVsixInstall', "Extension '{0}' was successfully installed!", path.basename(extension)));
				});
			});
J
Joao Moreno 已提交
84

85 86 87
		const galleryTasks: Task[] = extensions
			.filter(e => !/\.vsix$/i.test(e))
			.map(id => () => {
J
Joao Moreno 已提交
88
				return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(installed => {
89 90 91 92 93 94 95 96 97 98 99 100 101 102
					const isInstalled = installed.some(e => getId(e.manifest) === id);

					if (isInstalled) {
						console.log(localize('alreadyInstalled', "Extension '{0}' is already installed.", id));
						return TPromise.as(null);
					}

					return this.extensionGalleryService.query({ names: [id] })
						.then<IPager<IGalleryExtension>>(null, err => {
							if (err.responseText) {
								try {
									const response = JSON.parse(err.responseText);
									return TPromise.wrapError(response.message);
								} catch (e) {
J
Joao Moreno 已提交
103
									// noop
104
								}
J
Joao Moreno 已提交
105
							}
J
Joao Moreno 已提交
106 107

							return TPromise.wrapError(err);
108 109 110
						})
						.then(result => {
							const [extension] = result.firstPage;
J
Joao Moreno 已提交
111

112
							if (!extension) {
J
Johannes Rieken 已提交
113
								return TPromise.wrapError(`${notFound(id)}\n${useId}`);
114
							}
J
Joao Moreno 已提交
115

116 117
							console.log(localize('foundExtension', "Found '{0}' in the marketplace.", id));
							console.log(localize('installing', "Installing..."));
J
Joao Moreno 已提交
118

119 120
							return this.extensionManagementService.installFromGallery(extension)
								.then(() => console.log(localize('successInstall', "Extension '{0}' v{1} was successfully installed!", id, extension.version)));
J
Joao Moreno 已提交
121
						});
122
				});
123
			});
124 125

		return sequence([...vsixTasks, ...galleryTasks]);
J
Joao Moreno 已提交
126
	}
J
Joao Moreno 已提交
127

128 129
	private uninstallExtension(ids: string[]): TPromise<any> {
		return sequence(ids.map(id => () => {
J
Joao Moreno 已提交
130
			return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(installed => {
J
Joao Moreno 已提交
131
				const [extension] = installed.filter(e => getId(e.manifest) === id);
J
Joao Moreno 已提交
132

J
Joao Moreno 已提交
133
				if (!extension) {
J
Johannes Rieken 已提交
134
					return TPromise.wrapError(`${notInstalled(id)}\n${useId}`);
135
				}
J
Joao Moreno 已提交
136

137
				console.log(localize('uninstalling', "Uninstalling {0}...", id));
J
Joao Moreno 已提交
138

J
Joao Moreno 已提交
139
				return this.extensionManagementService.uninstall(extension)
140
					.then(() => console.log(localize('successUninstall', "Extension '{0}' was successfully uninstalled!", id)));
J
Joao Moreno 已提交
141
			});
142
		}));
J
Joao Moreno 已提交
143
	}
J
Joao Moreno 已提交
144 145
}

146 147
const eventPrefix = 'monacoworkbench';

J
Joao Moreno 已提交
148 149
export function main(argv: ParsedArgs): TPromise<void> {
	const services = new ServiceCollection();
J
Joao Moreno 已提交
150
	services.set(IEnvironmentService, new SyncDescriptor(EnvironmentService, argv, process.execPath));
J
Joao Moreno 已提交
151 152

	const instantiationService: IInstantiationService = new InstantiationService(services);
153 154

	return instantiationService.invokeFunction(accessor => {
J
Joao Moreno 已提交
155
		const envService = accessor.get(IEnvironmentService);
156

J
Joao Moreno 已提交
157
		return TPromise.join([envService.appSettingsHome, envService.userHome, envService.extensionsPath].map(p => mkdirp(p))).then(() => {
J
Joao Moreno 已提交
158
			const { appRoot, extensionsPath, extensionDevelopmentPath, isBuilt } = envService;
159

J
Joao Moreno 已提交
160 161
			const services = new ServiceCollection();
			services.set(IEventService, new SyncDescriptor(EventService));
162
			services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
J
Joao Moreno 已提交
163
			services.set(IRequestService, new SyncDescriptor(RequestService));
J
Joao Moreno 已提交
164 165
			services.set(IExtensionManagementService, new SyncDescriptor(ExtensionManagementService));
			services.set(IExtensionGalleryService, new SyncDescriptor(ExtensionGalleryService));
166

J
Joao Moreno 已提交
167 168
			if (isBuilt && !extensionDevelopmentPath && product.enableTelemetry) {
				const appenders: AppInsightsAppender[] = [];
169

J
Joao Moreno 已提交
170 171 172 173 174 175 176
				if (product.aiConfig && product.aiConfig.key) {
					appenders.push(new AppInsightsAppender(eventPrefix, null, product.aiConfig.key));
				}

				if (product.aiConfig && product.aiConfig.asimovKey) {
					appenders.push(new AppInsightsAppender(eventPrefix, null, product.aiConfig.asimovKey));
				}
177

J
Joao Moreno 已提交
178 179 180
				// It is important to dispose the AI adapter properly because
				// only then they flush remaining data.
				process.once('exit', () => appenders.forEach(a => a.dispose()));
181

J
Joao Moreno 已提交
182 183 184 185 186
				const config: ITelemetryServiceConfig = {
					appender: combinedAppender(...appenders),
					commonProperties: resolveCommonProperties(product.commit, pkg.version),
					piiPaths: [appRoot, extensionsPath]
				};
187

J
Joao Moreno 已提交
188 189 190 191
				services.set(ITelemetryService, new SyncDescriptor(TelemetryService, config));
			} else {
				services.set(ITelemetryService, NullTelemetryService);
			}
192

J
Joao Moreno 已提交
193 194
			const instantiationService2 = instantiationService.createChild(services);
			const main = instantiationService2.createInstance(Main);
195

J
Joao Moreno 已提交
196 197
			return main.run(argv);
		});
198
	});
J
Joao Moreno 已提交
199
}