environmentService.ts 8.9 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';
11
import { memoize } from 'vs/base/common/decorators';
12 13
import pkg from 'vs/platform/node/package';
import product from 'vs/platform/node/product';
14
import { toLocalISOString } from 'vs/base/common/date';
15
import { isWindows, isLinux } from 'vs/base/common/platform';
16
import { getPathFromAmdModule } from 'vs/base/common/amd';
17
import { URI } from 'vs/base/common/uri';
J
Joao Moreno 已提交
18

J
Joao Moreno 已提交
19 20 21 22
// 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 已提交
23
function getNixIPCHandle(userDataPath: string, type: string): string {
J
Joao Moreno 已提交
24
	if (xdgRuntimeDir) {
J
Joao Moreno 已提交
25 26
		const scope = crypto.createHash('md5').update(userDataPath).digest('hex').substr(0, 8);
		return path.join(xdgRuntimeDir, `vscode-${scope}-${pkg.version}-${type}.sock`);
J
Joao Moreno 已提交
27 28
	}

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

32 33
function getWin32IPCHandle(userDataPath: string, type: string): string {
	const scope = crypto.createHash('md5').update(userDataPath).digest('hex');
34

35
	return `\\\\.\\pipe\\${scope}-${pkg.version}-${type}-sock`;
J
Joao Moreno 已提交
36
}
37

J
Joao Moreno 已提交
38
function getIPCHandle(userDataPath: string, type: string): string {
39
	if (isWindows) {
40
		return getWin32IPCHandle(userDataPath, type);
41
	}
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

	return getNixIPCHandle(userDataPath, type);
}

function getCLIPath(execPath: string, appRoot: string, isBuilt: boolean): string {

	// Windows
	if (isWindows) {
		if (isBuilt) {
			return path.join(path.dirname(execPath), 'bin', `${product.applicationName}.cmd`);
		}

		return path.join(appRoot, 'scripts', 'code-cli.bat');
	}

	// Linux
	if (isLinux) {
		if (isBuilt) {
			return path.join(path.dirname(execPath), 'bin', `${product.applicationName}`);
		}

		return path.join(appRoot, 'scripts', 'code-cli.sh');
	}

	// macOS
	if (isBuilt) {
		return path.join(appRoot, 'bin', 'code');
	}

	return path.join(appRoot, 'scripts', 'code-cli.sh');
72 73
}

J
Joao Moreno 已提交
74 75
export class EnvironmentService implements IEnvironmentService {

76
	_serviceBrand: any;
J
Joao Moreno 已提交
77

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

80
	@memoize
81
	get appRoot(): string { return path.dirname(getPathFromAmdModule(require, '')); }
J
Joao Moreno 已提交
82 83

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

85 86 87
	@memoize
	get cliPath(): string { return getCLIPath(this.execPath, this.appRoot, this.isBuilt); }

J
Joao Moreno 已提交
88 89
	readonly logsPath: string;

90
	@memoize
91 92
	get userHome(): string { return os.homedir(); }

J
Joao Moreno 已提交
93 94
	@memoize
	get userDataPath(): string {
J
Joao Moreno 已提交
95 96
		if (process.env['VSCODE_PORTABLE']) {
			return path.join(process.env['VSCODE_PORTABLE'], 'user-data');
J
Joao Moreno 已提交
97 98 99 100
		}

		return parseUserDataDir(this._args, process);
	}
J
Joao Moreno 已提交
101

102 103 104 105
	get appNameLong(): string { return product.nameLong; }

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

106 107
	@memoize
	get appSettingsHome(): string { return path.join(this.userDataPath, 'User'); }
108

109 110
	@memoize
	get appSettingsPath(): string { return path.join(this.appSettingsHome, 'settings.json'); }
111

112 113 114
	@memoize
	get settingsSearchBuildId(): number { return product.settingsSearchBuildId; }

115 116 117
	@memoize
	get settingsSearchUrl(): string { return product.settingsSearchUrl; }

118 119
	@memoize
	get appKeybindingsPath(): string { return path.join(this.appSettingsHome, 'keybindings.json'); }
120

121
	@memoize
122
	get isExtensionDevelopment(): boolean { return !!this._args.extensionDevelopmentPath; }
123

124 125 126
	@memoize
	get backupHome(): string { return path.join(this.userDataPath, 'Backups'); }

127 128 129
	@memoize
	get storageHome(): string { return path.join(this.userDataPath, 'Storage'); }

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

133 134 135
	@memoize
	get workspacesHome(): string { return path.join(this.userDataPath, 'Workspaces'); }

136 137 138
	@memoize
	get installSourcePath(): string { return path.join(this.userDataPath, 'installSource'); }

139 140 141 142 143 144 145 146 147 148
	@memoize
	get builtinExtensionsPath(): string {
		const fromArgs = parsePathArg(this._args['builtin-extensions-dir'], process);
		if (fromArgs) {
			return fromArgs;
		} else {
			return path.normalize(path.join(getPathFromAmdModule(require, ''), '..', 'extensions'));
		}
	}

149
	@memoize
J
Joao Moreno 已提交
150 151 152 153 154 155 156
	get extensionsPath(): string {
		const fromArgs = parsePathArg(this._args['extensions-dir'], process);

		if (fromArgs) {
			return fromArgs;
		} else if (process.env['VSCODE_EXTENSIONS']) {
			return process.env['VSCODE_EXTENSIONS'];
J
Joao Moreno 已提交
157 158
		} else if (process.env['VSCODE_PORTABLE']) {
			return path.join(process.env['VSCODE_PORTABLE'], 'extensions');
J
Joao Moreno 已提交
159 160 161 162
		} else {
			return path.join(this.userHome, product.dataFolderName, 'extensions');
		}
	}
J
Joao Moreno 已提交
163

164
	@memoize
165 166 167 168 169 170 171 172 173 174
	get extensionDevelopmentLocationURI(): URI {
		const s = this._args.extensionDevelopmentPath;
		if (s) {
			if (/^[^:/?#]+?:\/\//.test(s)) {
				return URI.parse(s);
			}
			return URI.file(path.normalize(s));
		}
		return void 0;
	}
175 176 177

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

S
Sandeep Somavarapu 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	get disableExtensions(): boolean | string[] {
		if (this._args['disable-extensions']) {
			return true;
		}
		const disableExtensions: string | string[] = this._args['disable-extension'];
		if (disableExtensions) {
			if (typeof disableExtensions === 'string') {
				return [disableExtensions];
			}
			if (Array.isArray(disableExtensions) && disableExtensions.length > 0) {
				return disableExtensions;
			}
		}
		return false;
	}
B
Benjamin Pasero 已提交
194

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

197 198
	get skipReleaseNotes(): boolean { return this._args['skip-release-notes']; }

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

201
	@memoize
202 203 204 205
	get debugExtensionHost(): IExtensionHostDebugParams { return parseExtensionHostPort(this._args, this.isBuilt); }

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

207
	get isBuilt(): boolean { return !process.env['VSCODE_DEV']; }
J
Joao Moreno 已提交
208
	get verbose(): boolean { return this._args.verbose; }
209
	get log(): string { return this._args.log; }
210

211
	get wait(): boolean { return this._args.wait; }
J
Joao Moreno 已提交
212
	get logExtensionHostCommunication(): boolean { return this._args.logExtensionHostCommunication; }
213

214
	get performance(): boolean { return this._args.performance; }
B
Benjamin Pasero 已提交
215
	get status(): boolean { return this._args.status; }
216

217
	@memoize
J
Joao Moreno 已提交
218
	get mainIPCHandle(): string { return getIPCHandle(this.userDataPath, 'main'); }
219 220

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

223
	@memoize
224
	get nodeCachedDataDir(): string { return process.env['VSCODE_NODE_CACHED_DATA_DIR'] || undefined; }
225

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

J
Joao Moreno 已提交
229
	get driverHandle(): string { return this._args['driver']; }
230
	get driverVerbose(): boolean { return this._args['driver-verbose']; }
J
Joao Moreno 已提交
231

J
Joao 已提交
232
	constructor(private _args: ParsedArgs, private _execPath: string) {
J
Joao Moreno 已提交
233
		if (!process.env['VSCODE_LOGS']) {
234
			const key = toLocalISOString(new Date()).replace(/-|:|\.\d+Z$/g, '');
J
Joao Moreno 已提交
235 236 237 238
			process.env['VSCODE_LOGS'] = path.join(this.userDataPath, 'logs', key);
		}

		this.logsPath = process.env['VSCODE_LOGS'];
J
Joao 已提交
239
	}
240 241
}

242
export function parseExtensionHostPort(args: ParsedArgs, isBuild: boolean): IExtensionHostDebugParams {
243
	return parseDebugPort(args.debugPluginHost, args.debugBrkPluginHost, 5870, isBuild, args.debugId);
244 245 246
}

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

250
export function parseDebugPort(debugArg: string, debugBrkArg: string, defaultBuildPort: number, isBuild: boolean, debugId?: string): IExtensionHostDebugParams {
251
	const portStr = debugBrkArg || debugArg;
252
	const port = Number(portStr) || (!isBuild ? defaultBuildPort : null);
253 254
	const brk = port ? Boolean(!!debugBrkArg) : false;
	return { port, break: brk, debugId };
255 256
}

J
João Moreno 已提交
257 258
function parsePathArg(arg: string, process: NodeJS.Process): string {
	if (!arg) {
M
Matt Bierner 已提交
259
		return undefined;
260
	}
J
João Moreno 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274

	// 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));
275
}