cliProcessMain.ts 9.2 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/node/product';
import pkg from 'vs/platform/node/package';
9
import * as path from 'path';
10
import * as fs from 'fs';
J
Joao Moreno 已提交
11

J
Joao Moreno 已提交
12
import { TPromise } from 'vs/base/common/winjs.base';
13
import { sequence } from 'vs/base/common/async';
J
Joao Moreno 已提交
14
import { IPager } from 'vs/base/common/paging';
J
Joao Moreno 已提交
15 16 17 18
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';
J
Joao Moreno 已提交
19
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
20
import { EnvironmentService, getInstallSourcePath } from 'vs/platform/environment/node/environmentService';
J
Joao Moreno 已提交
21
import { IExtensionManagementService, IExtensionGalleryService, IExtensionManifest, IGalleryExtension, LocalExtensionType } from 'vs/platform/extensionManagement/common/extensionManagement';
J
Joao Moreno 已提交
22 23
import { ExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService';
import { ExtensionGalleryService } from 'vs/platform/extensionManagement/node/extensionGalleryService';
24 25
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { combinedAppender, NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
26 27
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
J
Joao Moreno 已提交
28
import { IRequestService } from 'vs/platform/request/node/request';
J
Joao Moreno 已提交
29
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';
S
Sandeep Somavarapu 已提交
34
import { IChoiceService } from 'vs/platform/message/common/message';
S
Sandeep Somavarapu 已提交
35
import { ChoiceCliService } from 'vs/platform/message/node/messageCli';
J
Joao Moreno 已提交
36

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

G
greams 已提交
41 42
function getId(manifest: IExtensionManifest, withVersion?: boolean): string {
	if (withVersion) {
J
Joao Moreno 已提交
43
		return `${manifest.publisher}.${manifest.name}@${manifest.version}`;
G
greams 已提交
44
	} else {
45
		return `${manifest.publisher}.${manifest.name}`;
G
greams 已提交
46
	}
J
Joao Moreno 已提交
47 48
}

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

J
Joao Moreno 已提交
51 52 53
class Main {

	constructor(
54
		@IEnvironmentService private environmentService: IEnvironmentService,
J
Joao Moreno 已提交
55 56
		@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
		@IExtensionGalleryService private extensionGalleryService: IExtensionGalleryService
J
Johannes Rieken 已提交
57
	) { }
J
Joao Moreno 已提交
58 59

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

62 63 64
		if (argv['install-source']) {
			return this.setInstallSource(argv['install-source']);
		} else if (argv['list-extensions']) {
G
greams 已提交
65
			return this.listExtensions(argv['show-versions']);
J
Joao Moreno 已提交
66
		} else if (argv['install-extension']) {
67
			const arg = argv['install-extension'];
J
Joao Moreno 已提交
68 69
			const args: string[] = typeof arg === 'string' ? [arg] : arg;
			return this.installExtension(args);
J
Joao Moreno 已提交
70
		} else if (argv['uninstall-extension']) {
71 72 73
			const arg = argv['uninstall-extension'];
			const ids: string[] = typeof arg === 'string' ? [arg] : arg;
			return this.uninstallExtension(ids);
J
Joao Moreno 已提交
74
		}
M
Matt Bierner 已提交
75
		return undefined;
J
Joao Moreno 已提交
76
	}
J
Joao Moreno 已提交
77

78 79 80 81 82 83 84
	private setInstallSource(installSource: string): TPromise<any> {
		return new TPromise<void>((c, e) => {
			const path = getInstallSourcePath(this.environmentService.userDataPath);
			fs.writeFile(path, installSource.slice(0, 30), 'utf8', err => err ? e(err) : c(null));
		});
	}

G
greams 已提交
85
	private listExtensions(showVersions: boolean): TPromise<any> {
J
Joao Moreno 已提交
86
		return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(extensions => {
G
greams 已提交
87
			extensions.forEach(e => console.log(getId(e.manifest, showVersions)));
J
Joao Moreno 已提交
88 89 90
		});
	}

91 92 93 94 95
	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);
96

97 98 99 100
				return this.extensionManagementService.install(extension).then(() => {
					console.log(localize('successVsixInstall', "Extension '{0}' was successfully installed!", path.basename(extension)));
				});
			});
J
Joao Moreno 已提交
101

102 103 104
		const galleryTasks: Task[] = extensions
			.filter(e => !/\.vsix$/i.test(e))
			.map(id => () => {
J
Joao Moreno 已提交
105
				return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(installed => {
106 107 108 109 110 111 112 113 114 115 116 117 118 119
					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 已提交
120
									// noop
121
								}
J
Joao Moreno 已提交
122
							}
J
Joao Moreno 已提交
123 124

							return TPromise.wrapError(err);
125 126 127
						})
						.then(result => {
							const [extension] = result.firstPage;
J
Joao Moreno 已提交
128

129
							if (!extension) {
130
								return TPromise.wrapError(new Error(`${notFound(id)}\n${useId}`));
131
							}
J
Joao Moreno 已提交
132

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

S
Sandeep Somavarapu 已提交
136
							return this.extensionManagementService.installFromGallery(extension)
137
								.then(() => console.log(localize('successInstall', "Extension '{0}' v{1} was successfully installed!", id, extension.version)));
J
Joao Moreno 已提交
138
						});
139
				});
140
			});
141 142

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

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

J
Joao Moreno 已提交
150
				if (!extension) {
151
					return TPromise.wrapError(new Error(`${notInstalled(id)}\n${useId}`));
152
				}
J
Joao Moreno 已提交
153

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

156
				return this.extensionManagementService.uninstall(extension, true)
157
					.then(() => console.log(localize('successUninstall', "Extension '{0}' was successfully uninstalled!", id)));
J
Joao Moreno 已提交
158
			});
159
		}));
J
Joao Moreno 已提交
160
	}
J
Joao Moreno 已提交
161 162
}

163 164
const eventPrefix = 'monacoworkbench';

J
Joao Moreno 已提交
165 166
export function main(argv: ParsedArgs): TPromise<void> {
	const services = new ServiceCollection();
J
Joao Moreno 已提交
167
	services.set(IEnvironmentService, new SyncDescriptor(EnvironmentService, argv, process.execPath));
J
Joao Moreno 已提交
168 169

	const instantiationService: IInstantiationService = new InstantiationService(services);
170 171

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

D
Daniel Imms 已提交
174
		return TPromise.join([envService.appSettingsHome, envService.extensionsPath].map(p => mkdirp(p))).then(() => {
175
			const { appRoot, extensionsPath, extensionDevelopmentPath, isBuilt, installSource } = envService;
176

J
Joao Moreno 已提交
177
			const services = new ServiceCollection();
178
			services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
J
Joao Moreno 已提交
179
			services.set(IRequestService, new SyncDescriptor(RequestService));
J
Joao Moreno 已提交
180 181
			services.set(IExtensionManagementService, new SyncDescriptor(ExtensionManagementService));
			services.set(IExtensionGalleryService, new SyncDescriptor(ExtensionGalleryService));
S
Sandeep Somavarapu 已提交
182
			services.set(IChoiceService, new SyncDescriptor(ChoiceCliService));
183

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

J
Joao Moreno 已提交
187 188 189
				if (product.aiConfig && product.aiConfig.asimovKey) {
					appenders.push(new AppInsightsAppender(eventPrefix, null, product.aiConfig.asimovKey));
				}
190

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

J
Joao Moreno 已提交
195 196
				const config: ITelemetryServiceConfig = {
					appender: combinedAppender(...appenders),
197
					commonProperties: resolveCommonProperties(product.commit, pkg.version, installSource),
J
Joao Moreno 已提交
198 199
					piiPaths: [appRoot, extensionsPath]
				};
200

J
Joao Moreno 已提交
201 202 203 204
				services.set(ITelemetryService, new SyncDescriptor(TelemetryService, config));
			} else {
				services.set(ITelemetryService, NullTelemetryService);
			}
205

J
Joao Moreno 已提交
206 207
			const instantiationService2 = instantiationService.createChild(services);
			const main = instantiationService2.createInstance(Main);
208

J
Joao Moreno 已提交
209 210
			return main.run(argv);
		});
211
	});
J
Joao Moreno 已提交
212
}