env.ts 7.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';

B
Benjamin Pasero 已提交
8
import * as fs from 'original-fs';
J
Joao Moreno 已提交
9
import * as path from 'path';
J
Joao Moreno 已提交
10
import * as os from 'os';
J
Joao Moreno 已提交
11
import { app } from 'electron';
J
Joao Moreno 已提交
12
import { mkdirp } from 'vs/base/node/pfs';
J
Joao Moreno 已提交
13 14 15
import * as arrays from 'vs/base/common/arrays';
import * as strings from 'vs/base/common/strings';
import * as paths from 'vs/base/common/paths';
J
Joao Moreno 已提交
16
import { TPromise } from 'vs/base/common/winjs.base';
J
Joao Moreno 已提交
17 18 19
import * as platform from 'vs/base/common/platform';
import URI from 'vs/base/common/uri';
import * as types from 'vs/base/common/types';
20
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
J
Joao Moreno 已提交
21
import product, { IProductConfiguration } from 'vs/platform/product';
22
import { parseArgs, ParsedArgs } from 'vs/code/node/argv';
J
Joao Moreno 已提交
23 24 25 26 27

export interface IProcessEnvironment {
	[key: string]: string;
}

28
export interface ICommandLineArguments extends ParsedArgs {
B
Benjamin Pasero 已提交
29
	paths?: string[];
J
Joao Moreno 已提交
30 31
}

J
Joao Moreno 已提交
32
export const IEnvService = createDecorator<IEnvService>('mainEnvironmentService');
J
Joao Moreno 已提交
33

J
Joao Moreno 已提交
34
export interface IEnvService {
35
	_serviceBrand: any;
J
Joao Moreno 已提交
36 37 38
	cliArgs: ICommandLineArguments;
	userExtensionsHome: string;
	isTestingFromCli: boolean;
J
Joao Moreno 已提交
39 40 41 42 43
	isBuilt: boolean;
	product: IProductConfiguration;
	updateUrl: string;
	quality: string;
	userHome: string;
J
Joao Moreno 已提交
44 45 46 47 48 49
	appRoot: string;
	currentWorkingDirectory: string;
	appHome: string;
	appSettingsHome: string;
	appSettingsPath: string;
	appKeybindingsPath: string;
J
Joao Moreno 已提交
50 51

	createPaths(): TPromise<void>;
J
Joao Moreno 已提交
52 53
}

J
Joao Moreno 已提交
54
export class EnvService implements IEnvService {
J
Joao Moreno 已提交
55

56
	_serviceBrand: any;
J
Joao Moreno 已提交
57

J
Joao Moreno 已提交
58
	private _cliArgs: ICommandLineArguments;
J
Joao Moreno 已提交
59 60
	get cliArgs(): ICommandLineArguments { return this._cliArgs; }

J
Joao Moreno 已提交
61
	private _userExtensionsHome: string;
J
Joao Moreno 已提交
62 63
	get userExtensionsHome(): string { return this._userExtensionsHome; }

J
Joao Moreno 已提交
64
	private _isTestingFromCli: boolean;
J
Joao Moreno 已提交
65 66 67 68
	get isTestingFromCli(): boolean { return this._isTestingFromCli; }

	get isBuilt(): boolean { return !process.env['VSCODE_DEV']; }

J
Joao Moreno 已提交
69 70 71
	get product(): IProductConfiguration { return product; }
	get updateUrl(): string { return product.updateUrl; }
	get quality(): string { return product.quality; }
J
Joao Moreno 已提交
72 73 74 75

	private _userHome: string;
	get userHome(): string { return this._userHome; }

J
Joao Moreno 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	private _appRoot: string;
	get appRoot(): string { return this._appRoot; }

	private _currentWorkingDirectory: string;
	get currentWorkingDirectory(): string { return this._currentWorkingDirectory; }

	private _appHome: string;
	get appHome(): string { return this._appHome; }

	private _appSettingsHome: string;
	get appSettingsHome(): string { return this._appSettingsHome; }

	private _appSettingsPath: string;
	get appSettingsPath(): string { return this._appSettingsPath; }

	private _appKeybindingsPath: string;
	get appKeybindingsPath(): string { return this._appKeybindingsPath; }

J
Joao Moreno 已提交
94
	constructor() {
J
Joao Moreno 已提交
95
		this._appRoot = path.dirname(URI.parse(require.toUrl('')).fsPath);
J
Joao Moreno 已提交
96 97 98 99 100 101
		this._currentWorkingDirectory = process.env['VSCODE_CWD'] || process.cwd();
		this._appHome = app.getPath('userData');
		this._appSettingsHome = path.join(this._appHome, 'User');
		this._appSettingsPath = path.join(this._appSettingsHome, 'settings.json');
		this._appKeybindingsPath = path.join(this._appSettingsHome, 'keybindings.json');

J
Joao Moreno 已提交
102 103 104
		// Remove the Electron executable
		let [, ...args] = process.argv;

J
Joao Moreno 已提交
105
		// If dev, remove the first non-option argument: it's the app location
J
Joao Moreno 已提交
106
		if (!this.isBuilt) {
J
Joao Moreno 已提交
107 108 109 110 111
			const index = arrays.firstIndex(args, a => !/^-/.test(a));

			if (index > -1) {
				args.splice(index, 1);
			}
J
Joao Moreno 已提交
112 113
		}

J
Joao Moreno 已提交
114
		const argv = parseArgs(args);
B
Benjamin Pasero 已提交
115
		const paths = parsePathArguments(this._currentWorkingDirectory, argv._, argv.goto);
J
Joao Moreno 已提交
116 117

		this._cliArgs = Object.freeze({
118
			_: [],
B
Benjamin Pasero 已提交
119
			paths,
B
Benjamin Pasero 已提交
120
			performance: argv.performance,
B
Benjamin Pasero 已提交
121
			verbose: argv.verbose,
122 123
			debugPluginHost: argv.debugPluginHost,
			debugBrkPluginHost: argv.debugBrkPluginHost,
J
Joao Moreno 已提交
124
			logExtensionHostCommunication: argv.logExtensionHostCommunication,
125 126 127
			'new-window': argv['new-window'],
			'reuse-window': argv['reuse-window'],
			goto: argv.goto,
B
Benjamin Pasero 已提交
128
			diff: argv.diff && paths.length === 2,
B
Benjamin Pasero 已提交
129
			extensionHomePath: normalizePath(argv.extensionHomePath),
J
Joao Moreno 已提交
130 131
			extensionDevelopmentPath: normalizePath(argv.extensionDevelopmentPath),
			extensionTestsPath: normalizePath(argv.extensionTestsPath),
B
Benjamin Pasero 已提交
132
			'disable-extensions': argv['disable-extensions'],
J
Joao Moreno 已提交
133
			locale: argv.locale,
134
			wait: argv.wait
J
Joao Moreno 已提交
135 136
		});

137
		this._isTestingFromCli = this.cliArgs.extensionTestsPath && !this.cliArgs.debugBrkPluginHost;
J
Joao Moreno 已提交
138
		this._userHome = path.join(os.homedir(), product.dataFolderName);
B
Benjamin Pasero 已提交
139
		this._userExtensionsHome = this.cliArgs.extensionHomePath || path.join(this._userHome, 'extensions');
J
Joao Moreno 已提交
140
	}
J
Joao Moreno 已提交
141 142 143 144 145 146 147

	createPaths(): TPromise<void> {
		const promises = [this.appSettingsHome, this.userHome, this.userExtensionsHome]
			.map(p => mkdirp(p));

		return TPromise.join(promises) as TPromise<any>;
	}
J
Joao Moreno 已提交
148
}
E
Erich Gamma 已提交
149

J
Joao Moreno 已提交
150 151
function parsePathArguments(cwd: string, args: string[], gotoLineMode?: boolean): string[] {
	const result = args.map(arg => {
J
Joao Moreno 已提交
152
		let pathCandidate = String(arg);
J
Joao Moreno 已提交
153 154 155

		let parsedPath: IParsedPath;
		if (gotoLineMode) {
J
oops  
Joao Moreno 已提交
156
			parsedPath = parseLineAndColumnAware(pathCandidate);
J
Joao Moreno 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
			pathCandidate = parsedPath.path;
		}

		if (pathCandidate) {
			pathCandidate = preparePath(cwd, pathCandidate);
		}

		let realPath: string;
		try {
			realPath = fs.realpathSync(pathCandidate);
		} catch (error) {
			// in case of an error, assume the user wants to create this file
			// if the path is relative, we join it to the cwd
			realPath = path.normalize(path.isAbsolute(pathCandidate) ? pathCandidate : path.join(cwd, pathCandidate));
		}

173 174
		const basename = path.basename(realPath);
		if (basename /* can be empty if code is opened on root */ && !paths.isValidBasename(basename)) {
J
Joao Moreno 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
			return null; // do not allow invalid file names
		}

		if (gotoLineMode) {
			parsedPath.path = realPath;
			return toLineAndColumnPath(parsedPath);
		}

		return realPath;
	});

	const caseInsensitive = platform.isWindows || platform.isMacintosh;
	const distinct = arrays.distinct(result, e => e && caseInsensitive ? e.toLowerCase() : e);

	return arrays.coalesce(distinct);
190
}
E
Erich Gamma 已提交
191

J
Joao Moreno 已提交
192
function preparePath(cwd: string, p: string): string {
B
polish  
Benjamin Pasero 已提交
193 194

	// Trim trailing quotes
195
	if (platform.isWindows) {
B
polish  
Benjamin Pasero 已提交
196
		p = strings.rtrim(p, '"'); // https://github.com/Microsoft/vscode/issues/1498
197
	}
198

199
	// Trim whitespaces
B
polish  
Benjamin Pasero 已提交
200
	p = strings.trim(strings.trim(p, ' '), '\t');
E
Erich Gamma 已提交
201

202
	if (platform.isWindows) {
B
polish  
Benjamin Pasero 已提交
203 204

		// Resolve the path against cwd if it is relative
J
Joao Moreno 已提交
205
		p = path.resolve(cwd, p);
B
polish  
Benjamin Pasero 已提交
206 207 208

		// Trim trailing '.' chars on Windows to prevent invalid file names
		p = strings.rtrim(p, '.');
209 210
	}

B
polish  
Benjamin Pasero 已提交
211
	return p;
E
Erich Gamma 已提交
212 213 214 215 216 217 218 219 220 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
}

function normalizePath(p?: string): string {
	return p ? path.normalize(p) : p;
}

export interface IParsedPath {
	path: string;
	line?: number;
	column?: number;
}

export function parseLineAndColumnAware(rawPath: string): IParsedPath {
	let segments = rawPath.split(':'); // C:\file.txt:<line>:<column>

	let path: string;
	let line: number = null;
	let column: number = null;

	segments.forEach(segment => {
		let segmentAsNumber = Number(segment);
		if (!types.isNumber(segmentAsNumber)) {
			path = !!path ? [path, segment].join(':') : segment; // a colon can well be part of a path (e.g. C:\...)
		} else if (line === null) {
			line = segmentAsNumber;
		} else if (column === null) {
			column = segmentAsNumber;
		}
	});

	return {
		path: path,
		line: line !== null ? line : void 0,
		column: column !== null ? column : line !== null ? 1 : void 0 // if we have a line, make sure column is also set
B
Benjamin Pasero 已提交
246
	};
E
Erich Gamma 已提交
247 248
}

249
function toLineAndColumnPath(parsedPath: IParsedPath): string {
E
Erich Gamma 已提交
250 251 252 253 254 255 256 257 258 259 260 261
	let segments = [parsedPath.path];

	if (types.isNumber(parsedPath.line)) {
		segments.push(String(parsedPath.line));
	}

	if (types.isNumber(parsedPath.column)) {
		segments.push(String(parsedPath.column));
	}

	return segments.join(':');
}