environmentService.ts 4.1 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
Johannes Rieken 已提交
6
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
7
import * as crypto from 'crypto';
J
Joao Moreno 已提交
8 9 10
import * as paths from 'vs/base/node/paths';
import * as os from 'os';
import * as path from 'path';
J
Johannes Rieken 已提交
11
import { ParsedArgs } from 'vs/platform/environment/node/argv';
12
import URI from 'vs/base/common/uri';
13
import { memoize } from 'vs/base/common/decorators';
14 15
import pkg from 'vs/platform/package';
import product from 'vs/platform/product';
J
Joao Moreno 已提交
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
function getUniqueUserId(): string {
	let username: string;
	if (process.platform === 'win32') {
		username = process.env.USERNAME;
	} else {
		username = process.env.USER;
	}

	if (!username) {
		return ''; // fail gracefully if there is no user name
	}

	// use sha256 to ensure the userid value can be used in filenames and are unique
	return crypto.createHash('sha256').update(username).digest('hex').substr(0, 6);
}

function getIPCHandleBaseName(): string {
	let name = pkg.name;

	// Support to run VS Code multiple times as different user
	// by making the socket unique over the logged in user
	let userId = getUniqueUserId();
	if (userId) {
J
Johannes Rieken 已提交
40
		name += `-${userId}`;
41 42 43
	}

	if (process.platform === 'win32') {
J
Johannes Rieken 已提交
44
		return `\\\\.\\pipe\\${name}`;
45 46 47 48 49 50 51 52
	}

	return path.join(os.tmpdir(), name);
}

const IPCHandlePrefix = getIPCHandleBaseName();
const IPCHandleSuffix = process.platform === 'win32' ? '-sock' : '.sock';

J
Joao Moreno 已提交
53 54
export class EnvironmentService implements IEnvironmentService {

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

J
Joao Moreno 已提交
57 58
	get args(): ParsedArgs { return this._args; }

59 60
	@memoize
	get appRoot(): string { return path.dirname(URI.parse(require.toUrl('')).fsPath); }
J
Joao Moreno 已提交
61 62

	get execPath(): string { return this._execPath; }
63

64 65
	@memoize
	get userHome(): string { return path.join(os.homedir(), product.dataFolderName); }
J
Joao Moreno 已提交
66

67
	@memoize
68
	get userDataPath(): string { return path.resolve(this._args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform)); }
J
Joao Moreno 已提交
69

70 71
	@memoize
	get appSettingsHome(): string { return path.join(this.userDataPath, 'User'); }
72

73 74
	@memoize
	get appSettingsPath(): string { return path.join(this.appSettingsHome, 'settings.json'); }
75

76 77
	@memoize
	get appKeybindingsPath(): string { return path.join(this.appSettingsHome, 'keybindings.json'); }
78

79
	@memoize
J
Joao Moreno 已提交
80
	get extensionsPath(): string { return path.normalize(this._args.extensionHomePath || path.join(this.userHome, 'extensions')); }
J
Joao Moreno 已提交
81

82 83 84 85 86
	@memoize
	get extensionDevelopmentPath(): string { return this._args.extensionDevelopmentPath ? path.normalize(this._args.extensionDevelopmentPath) : this._args.extensionDevelopmentPath; }

	@memoize
	get extensionTestsPath(): string { return this._args.extensionTestsPath ? path.normalize(this._args.extensionTestsPath) : this._args.extensionTestsPath; }
87

J
Johannes Rieken 已提交
88
	get disableExtensions(): boolean { return this._args['disable-extensions']; }
B
Benjamin Pasero 已提交
89

90
	@memoize
J
Joao Moreno 已提交
91
	get debugExtensionHost(): { port: number; break: boolean; } { return parseExtensionHostPort(this._args, this.isBuilt); }
92

93
	get isBuilt(): boolean { return !process.env['VSCODE_DEV']; }
J
Joao Moreno 已提交
94
	get verbose(): boolean { return this._args.verbose; }
95
	get wait(): boolean { return this._args.wait; }
J
Joao Moreno 已提交
96 97
	get performance(): boolean { return this._args.performance; }
	get logExtensionHostCommunication(): boolean { return this._args.logExtensionHostCommunication; }
98

99
	@memoize
J
Johannes Rieken 已提交
100
	get mainIPCHandle(): string { return `${IPCHandlePrefix}-${pkg.version}${IPCHandleSuffix}`; }
101 102

	@memoize
J
Johannes Rieken 已提交
103
	get sharedIPCHandle(): string { return `${IPCHandlePrefix}-${pkg.version}-shared${IPCHandleSuffix}`; }
104

J
Johannes Rieken 已提交
105
	constructor(private _args: ParsedArgs, private _execPath: string) { }
106 107
}

J
Joao Moreno 已提交
108
export function parseExtensionHostPort(args: ParsedArgs, isBuild: boolean): { port: number; break: boolean; } {
109 110 111
	const portStr = args.debugBrkPluginHost || args.debugPluginHost;
	const port = Number(portStr) || (!isBuild ? 5870 : null);
	const brk = port ? Boolean(!!args.debugBrkPluginHost) : false;
J
Joao Moreno 已提交
112
	return { port, break: brk };
J
Joao Moreno 已提交
113
}