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

'use strict';

8
import { app, dialog } from 'electron';
J
Joao Moreno 已提交
9 10
import { assign } from 'vs/base/common/objects';
import * as platform from 'vs/base/common/platform';
11
import product from 'vs/platform/node/product';
J
Joao Moreno 已提交
12
import { parseMainProcessArgv } from 'vs/platform/environment/node/argv';
J
Joao Moreno 已提交
13
import { mkdirp } from 'vs/base/node/pfs';
B
Benjamin Pasero 已提交
14
import { validatePaths } from 'vs/code/node/paths';
15
import { LifecycleService, ILifecycleService } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
J
Joao Moreno 已提交
16 17
import { Server, serve, connect } from 'vs/base/parts/ipc/node/ipc.net';
import { TPromise } from 'vs/base/common/winjs.base';
18
import { ILaunchChannel, LaunchChannelClient } from './launch';
J
Joao Moreno 已提交
19 20 21 22
import { ServicesAccessor, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
23
import { ILogService, LogMainService } from 'vs/platform/log/common/log';
24 25
import { StorageMainService } from 'vs/platform/storage2/node/storageMainService';
import { IStorageMainService } from 'vs/platform/storage2/common/storage';
D
Daniel Imms 已提交
26
import { IBackupMainService } from 'vs/platform/backup/common/backup';
D
Daniel Imms 已提交
27
import { BackupMainService } from 'vs/platform/backup/electron-main/backupMainService';
J
Joao Moreno 已提交
28
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
J
Joao Moreno 已提交
29 30
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
31
import { ConfigurationService } from 'vs/platform/configuration/node/configurationService';
J
Joao Moreno 已提交
32
import { IRequestService } from 'vs/platform/request/node/request';
J
Joao Moreno 已提交
33
import { RequestService } from 'vs/platform/request/electron-main/requestService';
J
Joao Moreno 已提交
34
import { IURLService } from 'vs/platform/url/common/url';
J
Joao Moreno 已提交
35
import { URLService } from 'vs/platform/url/electron-main/urlService';
B
Benjamin Pasero 已提交
36
import * as fs from 'original-fs';
37 38
import { CodeApplication } from 'vs/code/electron-main/app';
import { HistoryMainService } from 'vs/platform/history/electron-main/historyMainService';
B
Benjamin Pasero 已提交
39
import { IHistoryMainService } from 'vs/platform/history/common/history';
40
import { WorkspacesMainService } from 'vs/platform/workspaces/electron-main/workspacesMainService';
B
Benjamin Pasero 已提交
41
import { IWorkspacesMainService } from 'vs/platform/workspaces/common/workspaces';
42 43
import { localize } from 'vs/nls';
import { mnemonicButtonLabel } from 'vs/base/common/labels';
B
Benjamin Pasero 已提交
44

B
Benjamin Pasero 已提交
45 46 47 48
function createServices(args: ParsedArgs): IInstantiationService {
	const services = new ServiceCollection();

	services.set(IEnvironmentService, new SyncDescriptor(EnvironmentService, args, process.execPath));
49
	services.set(ILogService, new SyncDescriptor(LogMainService));
50
	services.set(IWorkspacesMainService, new SyncDescriptor(WorkspacesMainService));
51
	services.set(IHistoryMainService, new SyncDescriptor(HistoryMainService));
B
Benjamin Pasero 已提交
52
	services.set(ILifecycleService, new SyncDescriptor(LifecycleService));
53
	services.set(IStorageMainService, new SyncDescriptor(StorageMainService));
B
Benjamin Pasero 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
	services.set(IRequestService, new SyncDescriptor(RequestService));
	services.set(IURLService, new SyncDescriptor(URLService, args['open-url']));
	services.set(IBackupMainService, new SyncDescriptor(BackupMainService));

	return new InstantiationService(services, true);
}

function createPaths(environmentService: IEnvironmentService): TPromise<any> {
	const paths = [
		environmentService.appSettingsHome,
		environmentService.extensionsPath,
		environmentService.nodeCachedDataDir
	];
	return TPromise.join(paths.map(p => p && mkdirp(p))) as TPromise<any>;
}

J
Joao Moreno 已提交
71 72 73 74
class ExpectedError extends Error {
	public readonly isExpected = true;
}

J
Joao Moreno 已提交
75 76
function setupIPC(accessor: ServicesAccessor): TPromise<Server> {
	const logService = accessor.get(ILogService);
77
	const environmentService = accessor.get(IEnvironmentService);
J
Joao Moreno 已提交
78

B
Benjamin Pasero 已提交
79
	function allowSetForegroundWindow(service: LaunchChannelClient): TPromise<void> {
80
		let promise = TPromise.wrap<void>(void 0);
B
Benjamin Pasero 已提交
81 82 83 84
		if (platform.isWindows) {
			promise = service.getMainProcessId()
				.then(processId => {
					logService.log('Sending some foreground love to the running instance:', processId);
J
Johannes Rieken 已提交
85

B
Benjamin Pasero 已提交
86 87 88 89 90 91 92 93 94 95 96 97
					try {
						const { allowSetForegroundWindow } = <any>require.__$__nodeRequire('windows-foreground-love');
						allowSetForegroundWindow(processId);
					} catch (e) {
						// noop
					}
				});
		}

		return promise;
	}

E
Erich Gamma 已提交
98
	function setup(retry: boolean): TPromise<Server> {
99
		return serve(environmentService.mainIPCHandle).then(server => {
100 101 102 103 104 105
			if (platform.isMacintosh) {
				app.dock.show(); // dock might be hidden at this case due to a retry
			}

			return server;
		}, err => {
E
Erich Gamma 已提交
106
			if (err.code !== 'EADDRINUSE') {
107
				return TPromise.wrapError<Server>(err);
E
Erich Gamma 已提交
108 109
			}

110 111 112 113
			// Since we are the second instance, we do not want to show the dock
			if (platform.isMacintosh) {
				app.dock.hide();
			}
B
Benjamin Pasero 已提交
114

115
			// there's a running instance, let's connect to it
116
			return connect(environmentService.mainIPCHandle, 'main').then(
117
				client => {
J
Joao Moreno 已提交
118

B
Benjamin Pasero 已提交
119
					// Tests from CLI require to be the only instance currently
J
Joao Moreno 已提交
120
					if (environmentService.extensionTestsPath && !environmentService.debugExtensionHost.break) {
J
Joao Moreno 已提交
121
						const msg = 'Running extension tests from the command line is currently only supported if no other instance of Code is running.';
B
Benjamin Pasero 已提交
122
						logService.error(msg);
J
Joao Moreno 已提交
123
						client.dispose();
B
Benjamin Pasero 已提交
124

125
						return TPromise.wrapError<Server>(new Error(msg));
J
Joao Moreno 已提交
126 127
					}

J
Joao Moreno 已提交
128
					logService.log('Sending env to running instance...');
E
Erich Gamma 已提交
129

130 131 132 133 134 135 136 137 138 139 140 141
					// Show a warning dialog after some timeout if it takes long to talk to the other instance
					// Skip this if we are running with --wait where it is expected that we wait for a while
					let startupWarningDialogHandle: number;
					if (!environmentService.wait) {
						startupWarningDialogHandle = setTimeout(() => {
							showStartupWarningDialog(
								localize('secondInstanceNoResponse', "Another instance of {0} is running but not responding", product.nameShort),
								localize('secondInstanceNoResponseDetail', "Please close all other instances and try again.")
							);
						}, 10000);
					}

J
Joao Moreno 已提交
142 143
					const channel = client.getChannel<ILaunchChannel>('launch');
					const service = new LaunchChannelClient(channel);
E
Erich Gamma 已提交
144

B
Benjamin Pasero 已提交
145
					return allowSetForegroundWindow(service)
146
						.then(() => service.start(environmentService.args, process.env))
E
Erich Gamma 已提交
147
						.then(() => client.dispose())
148 149 150 151 152 153 154 155 156
						.then(() => {

							// Now that we started, make sure the warning dialog is prevented
							if (startupWarningDialogHandle) {
								clearTimeout(startupWarningDialogHandle);
							}

							return TPromise.wrapError(new ExpectedError('Sent env to running instance. Terminating...'));
						});
E
Erich Gamma 已提交
157 158 159
				},
				err => {
					if (!retry || platform.isWindows || err.code !== 'ECONNREFUSED') {
160 161 162 163 164 165 166
						if (err.code === 'EPERM') {
							showStartupWarningDialog(
								localize('secondInstanceAdmin', "A second instance of {0} is already running as administrator.", product.nameShort),
								localize('secondInstanceAdminDetail', "Please close the other instance and try again.")
							);
						}

167
						return TPromise.wrapError<Server>(err);
E
Erich Gamma 已提交
168 169 170 171
					}

					// it happens on Linux and OS X that the pipe is left behind
					// let's delete it, since we can't connect to it
S
Shreya Dahal 已提交
172
					// and then retry the whole thing
E
Erich Gamma 已提交
173
					try {
174
						fs.unlinkSync(environmentService.mainIPCHandle);
E
Erich Gamma 已提交
175
					} catch (e) {
J
Joao Moreno 已提交
176
						logService.log('Fatal error deleting obsolete instance handle', e);
177
						return TPromise.wrapError<Server>(e);
E
Erich Gamma 已提交
178 179 180 181 182 183 184 185 186 187 188
					}

					return setup(false);
				}
			);
		});
	}

	return setup(true);
}

189 190 191 192 193 194 195 196 197 198 199
function showStartupWarningDialog(message: string, detail: string): void {
	dialog.showMessageBox(null, {
		title: product.nameLong,
		type: 'warning',
		buttons: [mnemonicButtonLabel(localize({ key: 'close', comment: ['&& denotes a mnemonic'] }, "&&Close"))],
		message,
		detail,
		noLink: true
	});
}

B
Benjamin Pasero 已提交
200
function quit(accessor: ServicesAccessor, reason?: ExpectedError | Error): void {
201 202
	const logService = accessor.get(ILogService);
	const lifecycleService = accessor.get(ILifecycleService);
203

204
	let exitCode = 0;
J
Joao Moreno 已提交
205

B
Benjamin Pasero 已提交
206 207 208
	if (reason) {
		if ((reason as ExpectedError).isExpected) {
			logService.log(reason.message);
209
		} else {
J
Joao Moreno 已提交
210 211
			exitCode = 1; // signal error to the outside

B
Benjamin Pasero 已提交
212 213
			if (reason.stack) {
				console.error(reason.stack);
J
Joao Moreno 已提交
214
			} else {
B
Benjamin Pasero 已提交
215
				console.error(`Startup error: ${reason.toString()}`);
J
Joao Moreno 已提交
216
			}
217
		}
J
Joao Moreno 已提交
218 219
	}

220
	lifecycleService.kill(exitCode);
J
Joao Moreno 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
}

function main() {
	let args: ParsedArgs;

	try {
		args = parseMainProcessArgv(process.argv);
		args = validatePaths(args);
	} catch (err) {
		console.error(err.message);
		app.exit(1);

		return;
	}

	const instantiationService = createServices(args);

	return instantiationService.invokeFunction(accessor => {

		// Patch `process.env` with the instance's environment
		const environmentService = accessor.get(IEnvironmentService);
		const instanceEnv: typeof process.env = {
			VSCODE_PID: String(process.pid),
			VSCODE_IPC_HOOK: environmentService.mainIPCHandle,
			VSCODE_NLS_CONFIG: process.env['VSCODE_NLS_CONFIG']
		};
		assign(process.env, instanceEnv);

		// Startup
		return instantiationService.invokeFunction(a => createPaths(a.get(IEnvironmentService)))
			.then(() => instantiationService.invokeFunction(setupIPC))
			.then(mainIpcServer => {
B
Benjamin Pasero 已提交
253
				const app = instantiationService.createInstance(CodeApplication, mainIpcServer, instanceEnv);
J
Joao Moreno 已提交
254 255 256 257 258
				app.startup();
			});
	}).done(null, err => instantiationService.invokeFunction(quit, err));
}

S
Shreya Dahal 已提交
259
main();