environmentService.ts 7.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.
 *--------------------------------------------------------------------------------------------*/

6
import { IEnvironmentService, ParsedArgs, IDebugParams, IExtensionHostDebugParams } 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
Joao 已提交
11
import * as fs from 'fs';
12
import URI from 'vs/base/common/uri';
J
Joao Moreno 已提交
13
import { generateUuid, isUUID } from 'vs/base/common/uuid';
14
import { memoize } from 'vs/base/common/decorators';
15 16
import pkg from 'vs/platform/node/package';
import product from 'vs/platform/node/product';
J
Joao Moreno 已提交
17

J
Joao Moreno 已提交
18 19 20 21
// Read this before there's any chance it is overwritten
// Related to https://github.com/Microsoft/vscode/issues/30624
const xdgRuntimeDir = process.env['XDG_RUNTIME_DIR'];

J
Joao Moreno 已提交
22
function getNixIPCHandle(userDataPath: string, type: string): string {
J
Joao Moreno 已提交
23 24 25 26
	if (xdgRuntimeDir) {
		return path.join(xdgRuntimeDir, `${pkg.name}-${pkg.version}-${type}.sock`);
	}

J
Joao Moreno 已提交
27 28
	return path.join(userDataPath, `${pkg.version}-${type}.sock`);
}
29

30 31 32
function getWin32IPCHandle(userDataPath: string, type: string): string {
	const scope = crypto.createHash('md5').update(userDataPath).digest('hex');
	return `\\\\.\\pipe\\${scope}-${pkg.version}-${type}-sock`;
J
Joao Moreno 已提交
33
}
34

J
Joao Moreno 已提交
35
function getIPCHandle(userDataPath: string, type: string): string {
36
	if (process.platform === 'win32') {
37
		return getWin32IPCHandle(userDataPath, type);
38
	} else {
J
Joao Moreno 已提交
39
		return getNixIPCHandle(userDataPath, type);
40 41 42
	}
}

J
Joao Moreno 已提交
43 44
export class EnvironmentService implements IEnvironmentService {

45
	_serviceBrand: any;
J
Joao Moreno 已提交
46

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

49 50
	@memoize
	get appRoot(): string { return path.dirname(URI.parse(require.toUrl('')).fsPath); }
J
Joao Moreno 已提交
51 52

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

J
Joao Moreno 已提交
54 55
	readonly logsPath: string;

56
	@memoize
57 58
	get userHome(): string { return os.homedir(); }

59
	@memoize
60
	get userDataPath(): string { return parseUserDataDir(this._args, process); }
J
Joao Moreno 已提交
61

62 63 64 65
	get appNameLong(): string { return product.nameLong; }

	get appQuality(): string { return product.quality; }

66 67
	@memoize
	get appSettingsHome(): string { return path.join(this.userDataPath, 'User'); }
68

69 70
	@memoize
	get appSettingsPath(): string { return path.join(this.appSettingsHome, 'settings.json'); }
71

72 73 74
	@memoize
	get settingsSearchBuildId(): number { return product.settingsSearchBuildId; }

75 76 77
	@memoize
	get settingsSearchUrl(): string { return product.settingsSearchUrl; }

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

81
	@memoize
82
	get isExtensionDevelopment(): boolean { return !!this._args.extensionDevelopmentPath; }
83

84 85 86 87 88 89
	@memoize
	get backupHome(): string { return path.join(this.userDataPath, 'Backups'); }

	@memoize
	get backupWorkspacesPath(): string { return path.join(this.backupHome, 'workspaces.json'); }

90 91 92
	@memoize
	get workspacesHome(): string { return path.join(this.userDataPath, 'Workspaces'); }

93 94 95
	@memoize
	get installSourcePath(): string { return path.join(this.userDataPath, 'installSource'); }

96
	@memoize
97
	get extensionsPath(): string { return parsePathArg(this._args['extensions-dir'], process) || process.env['VSCODE_EXTENSIONS'] || path.join(this.userHome, product.dataFolderName, 'extensions'); }
J
Joao Moreno 已提交
98

99 100 101 102 103
	@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; }
104

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

107
	get skipGettingStarted(): boolean { return this._args['skip-getting-started']; }
108

109 110
	get skipAddToRecentlyOpened(): boolean { return this._args['skip-add-to-recently-opened']; }

111
	@memoize
112 113 114 115
	get debugExtensionHost(): IExtensionHostDebugParams { return parseExtensionHostPort(this._args, this.isBuilt); }

	@memoize
	get debugSearch(): IDebugParams { return parseSearchPort(this._args, this.isBuilt); }
116

117
	get isBuilt(): boolean { return !process.env['VSCODE_DEV']; }
J
Joao Moreno 已提交
118
	get verbose(): boolean { return this._args.verbose; }
119
	get wait(): boolean { return this._args.wait; }
J
Joao Moreno 已提交
120
	get logExtensionHostCommunication(): boolean { return this._args.logExtensionHostCommunication; }
121

122 123
	get performance(): boolean { return this._args.performance; }

124
	@memoize
J
Joao Moreno 已提交
125
	get mainIPCHandle(): string { return getIPCHandle(this.userDataPath, 'main'); }
126 127

	@memoize
J
Joao Moreno 已提交
128
	get sharedIPCHandle(): string { return getIPCHandle(this.userDataPath, 'shared'); }
129

130
	@memoize
131
	get nodeCachedDataDir(): string { return this.isBuilt ? path.join(this.userDataPath, 'CachedData', product.commit || new Array(41).join('0')) : undefined; }
132

J
Joao Moreno 已提交
133
	get disableUpdates(): boolean { return !!this._args['disable-updates']; }
134
	get disableCrashReporter(): boolean { return !!this._args['disable-crash-reporter']; }
J
Joao Moreno 已提交
135

J
Joao 已提交
136 137 138
	readonly machineUUID: string;

	constructor(private _args: ParsedArgs, private _execPath: string) {
J
Joao Moreno 已提交
139 140 141 142 143 144 145
		if (!process.env['VSCODE_LOGS']) {
			const key = new Date().toISOString().replace(/-|:|\.\d+Z$/g, '');
			process.env['VSCODE_LOGS'] = path.join(this.userDataPath, 'logs', key);
		}

		this.logsPath = process.env['VSCODE_LOGS'];

J
Joao 已提交
146 147 148 149
		const machineIdPath = path.join(this.userDataPath, 'machineid');

		try {
			this.machineUUID = fs.readFileSync(machineIdPath, 'utf8');
J
Joao Moreno 已提交
150 151 152 153

			if (!isUUID(this.machineUUID)) {
				throw new Error('Not a UUID');
			}
J
Joao 已提交
154 155 156 157
		} catch (err) {
			this.machineUUID = generateUuid();

			try {
J
Joao Moreno 已提交
158
				fs.writeFileSync(machineIdPath, this.machineUUID, 'utf8');
J
Joao 已提交
159
			} catch (err) {
J
Joao Moreno 已提交
160
				// noop
J
Joao 已提交
161 162 163
			}
		}
	}
164 165
}

166 167 168 169 170 171 172 173 174 175 176 177 178
export function parseExtensionHostPort(args: ParsedArgs, isBuild: boolean): IExtensionHostDebugParams {
	return parseDebugPort(args.debugPluginHost, args.debugBrkPluginHost, 5870, isBuild, args.debugId);
}

export function parseSearchPort(args: ParsedArgs, isBuild: boolean): IDebugParams {
	return parseDebugPort(args.debugSearch, args.debugBrkSearch, 5876, isBuild);
}

export function parseDebugPort(debugArg: string, debugBrkArg: string, defaultBuildPort: number, isBuild: boolean, debugId?: string): IExtensionHostDebugParams {
	const portStr = debugBrkArg || debugArg;
	const port = Number(portStr) || (!isBuild ? defaultBuildPort : null);
	const brk = port ? Boolean(!!debugBrkArg) : false;
	return { port, break: brk, debugId };
179 180
}

J
João Moreno 已提交
181 182
function parsePathArg(arg: string, process: NodeJS.Process): string {
	if (!arg) {
M
Matt Bierner 已提交
183
		return undefined;
184
	}
J
João Moreno 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198

	// Determine if the arg is relative or absolute, if relative use the original CWD
	// (VSCODE_CWD), not the potentially overridden one (process.cwd()).
	const resolved = path.resolve(arg);

	if (path.normalize(arg) === resolved) {
		return resolved;
	} else {
		return path.resolve(process.env['VSCODE_CWD'] || process.cwd(), arg);
	}
}

export function parseUserDataDir(args: ParsedArgs, process: NodeJS.Process): string {
	return parsePathArg(args['user-data-dir'], process) || path.resolve(paths.getDefaultUserDataPath(process.platform));
199
}