main.ts 6.4 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 12
import { TPromise } from 'vs/base/common/winjs.base';
import { WorkbenchShell } from 'vs/workbench/electron-browser/shell';
import { IOptions } from 'vs/workbench/common/options';
import { domContentLoaded } from 'vs/base/browser/dom';
E
Erich Gamma 已提交
13 14 15 16 17 18
import errors = require('vs/base/common/errors');
import platform = require('vs/base/common/platform');
import paths = require('vs/base/common/paths');
import timer = require('vs/base/common/timer');
import uri from 'vs/base/common/uri';
import strings = require('vs/base/common/strings');
J
Johannes Rieken 已提交
19 20 21 22 23 24 25 26
import { IResourceInput } from 'vs/platform/editor/common/editor';
import { EventService } from 'vs/platform/event/common/eventService';
import { WorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWorkspace } from 'vs/platform/workspace/common/workspace';
import { WorkspaceConfigurationService } from 'vs/workbench/services/configuration/node/configurationService';
import { ParsedArgs } from 'vs/platform/environment/node/argv';
import { realpath } from 'vs/base/node/pfs';
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
E
Erich Gamma 已提交
27 28 29
import path = require('path');
import fs = require('fs');
import gracefulFs = require('graceful-fs');
J
Johannes Rieken 已提交
30
import { IPath, IOpenFileRequest } from 'vs/workbench/electron-browser/common';
B
Benjamin Pasero 已提交
31 32

gracefulFs.gracefulify(fs); // enable gracefulFs
E
Erich Gamma 已提交
33

34
const timers = (<any>window).MonacoEnvironment.timers;
A
Alex Dima 已提交
35

36
export interface IWindowConfiguration extends ParsedArgs, IOpenFileRequest {
37 38 39
	appRoot: string;
	execPath: string;

40
	userEnv: any; /* vs/code/electron-main/env/IProcessEnvironment*/
41

E
Erich Gamma 已提交
42 43 44
	workspacePath?: string;
}

45
export function startup(configuration: IWindowConfiguration): TPromise<void> {
46

E
Erich Gamma 已提交
47
	// Shell Options
B
polish  
Benjamin Pasero 已提交
48 49 50
	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;
51 52
	const filesToRestore = configuration.filesToRestore && configuration.filesToRestore.length ? toInputs(configuration.filesToRestore) : null;
	const untitledToRestore = configuration.untitledToRestore && configuration.untitledToRestore.length ? toInputs(configuration.untitledToRestore) : null;
B
Benjamin Pasero 已提交
53
	const shellOptions: IOptions = {
54 55
		filesToOpen,
		filesToCreate,
56 57 58
		filesToDiff,
		filesToRestore,
		untitledToRestore
E
Erich Gamma 已提交
59 60
	};

61
	if (configuration.performance) {
E
Erich Gamma 已提交
62 63 64
		timer.ENABLE_TIMER = true;
	}

65 66 67 68 69 70
	// Resolve workspace
	return getWorkspace(configuration.workspacePath).then(workspace => {

		// Open workbench
		return openWorkbench(configuration, workspace, shellOptions);
	});
E
Erich Gamma 已提交
71 72 73 74
}

function toInputs(paths: IPath[]): IResourceInput[] {
	return paths.map(p => {
B
Benjamin Pasero 已提交
75
		const input = <IResourceInput>{
E
Erich Gamma 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
			resource: uri.file(p.filePath)
		};

		if (p.lineNumber) {
			input.options = {
				selection: {
					startLineNumber: p.lineNumber,
					startColumn: p.columnNumber
				}
			};
		}

		return input;
	});
}

92
function getWorkspace(workspacePath: string): TPromise<IWorkspace> {
B
polish  
Benjamin Pasero 已提交
93
	if (!workspacePath) {
94
		return TPromise.as(null);
E
Erich Gamma 已提交
95 96
	}

97 98
	return realpath(workspacePath).then(realWorkspacePath => {

E
Erich Gamma 已提交
99 100 101 102
		// 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
103 104 105
		if (paths.isUNC(realWorkspacePath) && strings.endsWith(realWorkspacePath, paths.nativeSep)) {
			realWorkspacePath = strings.rtrim(realWorkspacePath, paths.nativeSep);
		}
E
Erich Gamma 已提交
106

107 108 109
		const workspaceResource = uri.file(realWorkspacePath);
		const folderName = path.basename(realWorkspacePath) || realWorkspacePath;
		const folderStat = fs.statSync(realWorkspacePath);
E
Erich Gamma 已提交
110

111 112 113 114 115 116 117 118 119 120
		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!
		};
	}, (error) => {
		errors.onUnexpectedError(error);

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

123
function openWorkbench(environment: IWindowConfiguration, workspace: IWorkspace, options: IOptions): TPromise<void> {
B
Benjamin Pasero 已提交
124
	const eventService = new EventService();
J
Joao Moreno 已提交
125
	const environmentService = new EnvironmentService(environment, environment.execPath);
126
	const contextService = new WorkspaceContextService(workspace);
B
Benjamin Pasero 已提交
127
	const configurationService = new WorkspaceConfigurationService(contextService, eventService, environmentService);
128 129 130 131 132 133

	// 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(() => {
		timers.beforeReady = new Date();

A
Alex Dima 已提交
134
		return domContentLoaded().then(() => {
135 136 137
			timers.afterReady = new Date();

			// Open Shell
B
Benjamin Pasero 已提交
138 139
			const beforeOpen = new Date();
			const shell = new WorkbenchShell(document.body, workspace, {
140 141
				configurationService,
				eventService,
142 143
				contextService,
				environmentService
144
			}, options);
145 146 147
			shell.open();

			shell.joinCreation().then(() => {
148
				timer.start(timer.Topic.STARTUP, 'Open Shell, Viewlet & Editor', beforeOpen, 'Workbench has opened after this event with viewlet and editor restored').stop();
149 150 151 152 153 154
			});

			// Inform user about loading issues from the loader
			(<any>self).require.config({
				onError: (err: any) => {
					if (err.errorCode === 'load') {
155
						shell.onUnexpectedError(loaderError(err));
156
					}
E
Erich Gamma 已提交
157
				}
158
			});
A
Alex Dima 已提交
159
		});
160
	});
161 162 163 164 165 166 167 168
}

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)));
E
Erich Gamma 已提交
169
}