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

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

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

60 61 62 63
	get appNameLong(): string { return product.nameLong; }

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

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

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

70 71 72
	@memoize
	get settingsSearchBuildId(): number { return product.settingsSearchBuildId; }

73 74 75
	@memoize
	get settingsSearchUrl(): string { return product.settingsSearchUrl; }

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

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

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

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

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

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

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

97 98 99 100 101
	@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; }
102

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

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

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

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

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

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

120 121
	get performance(): boolean { return this._args.performance; }

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

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

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

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

J
Joao 已提交
134 135 136 137 138 139 140
	readonly machineUUID: string;

	constructor(private _args: ParsedArgs, private _execPath: string) {
		const machineIdPath = path.join(this.userDataPath, 'machineid');

		try {
			this.machineUUID = fs.readFileSync(machineIdPath, 'utf8');
J
Joao Moreno 已提交
141 142 143 144

			if (!isUUID(this.machineUUID)) {
				throw new Error('Not a UUID');
			}
J
Joao 已提交
145 146 147 148
		} catch (err) {
			this.machineUUID = generateUuid();

			try {
J
Joao Moreno 已提交
149
				fs.writeFileSync(machineIdPath, this.machineUUID, 'utf8');
J
Joao 已提交
150
			} catch (err) {
J
Joao Moreno 已提交
151
				// noop
J
Joao 已提交
152 153 154
			}
		}
	}
155 156
}

157 158 159 160 161 162 163 164 165 166 167 168 169
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 };
170 171
}

J
João Moreno 已提交
172 173
function parsePathArg(arg: string, process: NodeJS.Process): string {
	if (!arg) {
M
Matt Bierner 已提交
174
		return undefined;
175
	}
J
João Moreno 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189

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