main.ts 6.9 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 nls = require('vs/nls');
J
Johannes Rieken 已提交
9 10 11
import { TPromise } from 'vs/base/common/winjs.base';
import { WorkbenchShell } from 'vs/workbench/electron-browser/shell';
import { IOptions } from 'vs/workbench/common/options';
12
import * as browser from 'vs/base/browser/browser';
J
Johannes Rieken 已提交
13
import { domContentLoaded } from 'vs/base/browser/dom';
E
Erich Gamma 已提交
14
import errors = require('vs/base/common/errors');
15
import comparer = require('vs/base/common/comparers');
E
Erich Gamma 已提交
16 17 18 19
import platform = require('vs/base/common/platform');
import paths = require('vs/base/common/paths');
import uri from 'vs/base/common/uri';
import strings = require('vs/base/common/strings');
J
Johannes Rieken 已提交
20
import { IResourceInput } from 'vs/platform/editor/common/editor';
J
Joao Moreno 已提交
21
import { IWorkspace, WorkspaceContextService } from 'vs/platform/workspace/common/workspace';
J
Johannes Rieken 已提交
22
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configurationService';
J
Joao Moreno 已提交
23
import { ParsedArgs } from 'vs/platform/environment/common/environment';
B
Benjamin Pasero 已提交
24
import { realpath, stat } from 'vs/base/node/pfs';
J
Johannes Rieken 已提交
25
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
E
Erich Gamma 已提交
26 27
import path = require('path');
import gracefulFs = require('graceful-fs');
J
Johannes Rieken 已提交
28
import { IPath, IOpenFileRequest } from 'vs/workbench/electron-browser/common';
B
Benjamin Pasero 已提交
29 30
import { IInitData } from 'vs/workbench/services/timer/common/timerService';
import { TimerService } from 'vs/workbench/services/timer/node/timerService';
A
Alex Dima 已提交
31
import { KeyboardMapperFactory } from "vs/workbench/services/keybinding/electron-browser/keybindingService";
B
Benjamin Pasero 已提交
32

33 34
import { webFrame } from 'electron';

B
Benjamin Pasero 已提交
35
import fs = require('fs');
B
Benjamin Pasero 已提交
36
gracefulFs.gracefulify(fs); // enable gracefulFs
E
Erich Gamma 已提交
37

38
export interface IWindowConfiguration extends ParsedArgs, IOpenFileRequest {
A
Alex Dima 已提交
39 40 41 42 43 44

	/**
	 * The physical keyboard is of ISO type (on OSX)
	 */
	isISOKeyboard?: boolean;

45 46 47
	appRoot: string;
	execPath: string;

48
	userEnv: any; /* vs/code/electron-main/env/IProcessEnvironment*/
49

E
Erich Gamma 已提交
50
	workspacePath?: string;
51 52 53

	zoomLevel?: number;
	fullscreen?: boolean;
E
Erich Gamma 已提交
54 55
}

56
export function startup(configuration: IWindowConfiguration): TPromise<void> {
57

58 59
	// Ensure others can listen to zoom level changes
	browser.setZoomFactor(webFrame.getZoomFactor());
60
	browser.setZoomLevel(webFrame.getZoomLevel());
61 62
	browser.setFullscreen(!!configuration.fullscreen);

A
Alex Dima 已提交
63 64
	KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged(configuration.isISOKeyboard);

65 66 67
	// Setup Intl
	comparer.setFileNameComparer(new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }));

E
Erich Gamma 已提交
68
	// Shell Options
B
polish  
Benjamin Pasero 已提交
69 70 71
	const filesToOpen = configuration.filesToOpen && configuration.filesToOpen.length ? toInputs(configuration.filesToOpen) : null;
	const filesToCreate = configuration.filesToCreate && configuration.filesToCreate.length ? toInputs(configuration.filesToCreate) : null;
	const filesToDiff = configuration.filesToDiff && configuration.filesToDiff.length ? toInputs(configuration.filesToDiff) : null;
B
Benjamin Pasero 已提交
72
	const shellOptions: IOptions = {
73 74
		filesToOpen,
		filesToCreate,
B
Benjamin Pasero 已提交
75
		filesToDiff
E
Erich Gamma 已提交
76 77
	};

78 79 80 81 82 83
	// Resolve workspace
	return getWorkspace(configuration.workspacePath).then(workspace => {

		// Open workbench
		return openWorkbench(configuration, workspace, shellOptions);
	});
E
Erich Gamma 已提交
84 85
}

86
function toInputs(paths: IPath[], isUntitledFile?: boolean): IResourceInput[] {
E
Erich Gamma 已提交
87
	return paths.map(p => {
88 89 90 91 92 93 94
		const input = <IResourceInput>{};

		if (isUntitledFile) {
			input.resource = uri.from({ scheme: 'untitled', path: p.filePath });
		} else {
			input.resource = uri.file(p.filePath);
		}
E
Erich Gamma 已提交
95

96 97 98 99
		input.options = {
			pinned: true // opening on startup is always pinned and not preview
		};

E
Erich Gamma 已提交
100
		if (p.lineNumber) {
101 102 103
			input.options.selection = {
				startLineNumber: p.lineNumber,
				startColumn: p.columnNumber
E
Erich Gamma 已提交
104 105 106 107 108 109 110
			};
		}

		return input;
	});
}

111
function getWorkspace(workspacePath: string): TPromise<IWorkspace> {
B
polish  
Benjamin Pasero 已提交
112
	if (!workspacePath) {
113
		return TPromise.as(null);
E
Erich Gamma 已提交
114 115
	}

116 117
	return realpath(workspacePath).then(realWorkspacePath => {

E
Erich Gamma 已提交
118 119 120 121
		// for some weird reason, node adds a trailing slash to UNC paths
		// we never ever want trailing slashes as our workspace path unless
		// someone opens root ("/").
		// See also https://github.com/nodejs/io.js/issues/1765
122 123 124
		if (paths.isUNC(realWorkspacePath) && strings.endsWith(realWorkspacePath, paths.nativeSep)) {
			realWorkspacePath = strings.rtrim(realWorkspacePath, paths.nativeSep);
		}
E
Erich Gamma 已提交
125

126 127
		const workspaceResource = uri.file(realWorkspacePath);
		const folderName = path.basename(realWorkspacePath) || realWorkspacePath;
E
Erich Gamma 已提交
128

B
Benjamin Pasero 已提交
129 130 131 132 133 134 135
		return stat(realWorkspacePath).then(folderStat => {
			return <IWorkspace>{
				'resource': workspaceResource,
				'name': folderName,
				'uid': platform.isLinux ? folderStat.ino : folderStat.birthtime.getTime() // On Linux, birthtime is ctime, so we cannot use it! We use the ino instead!
			};
		});
136 137 138 139 140
	}, (error) => {
		errors.onUnexpectedError(error);

		return null; // treat invalid paths as empty workspace
	});
E
Erich Gamma 已提交
141 142
}

143
function openWorkbench(environment: IWindowConfiguration, workspace: IWorkspace, options: IOptions): TPromise<void> {
144
	const environmentService = new EnvironmentService(environment, environment.execPath);
145
	const contextService = new WorkspaceContextService(workspace);
146
	const configurationService = new WorkspaceConfigurationService(contextService, environmentService);
B
Benjamin Pasero 已提交
147
	const timerService = new TimerService((<any>window).MonacoEnvironment.timers as IInitData, !contextService.hasWorkspace());
148 149 150 151

	// Since the configuration service is one of the core services that is used in so many places, we initialize it
	// right before startup of the workbench shell to have its data ready for consumers
	return configurationService.initialize().then(() => {
152
		timerService.beforeDOMContentLoaded = Date.now();
153

A
Alex Dima 已提交
154
		return domContentLoaded().then(() => {
155
			timerService.afterDOMContentLoaded = Date.now();
156 157

			// Open Shell
158
			timerService.beforeWorkbenchOpen = Date.now();
159
			const shell = new WorkbenchShell(document.body, {
160
				configurationService,
161
				contextService,
B
Benjamin Pasero 已提交
162 163
				environmentService,
				timerService
164
			}, options);
165 166 167 168 169 170
			shell.open();

			// Inform user about loading issues from the loader
			(<any>self).require.config({
				onError: (err: any) => {
					if (err.errorCode === 'load') {
171
						shell.onUnexpectedError(loaderError(err));
172
					}
E
Erich Gamma 已提交
173
				}
174
			});
A
Alex Dima 已提交
175
		});
176
	});
177 178 179 180 181 182 183 184
}

function loaderError(err: Error): Error {
	if (platform.isWeb) {
		return new Error(nls.localize('loaderError', "Failed to load a required file. Either you are no longer connected to the internet or the server you are connected to is offline. Please refresh the browser to try again."));
	}

	return new Error(nls.localize('loaderErrorNative', "Failed to load a required file. Please restart the application to try again. Details: {0}", JSON.stringify(err)));
185
}