-
Notifications
You must be signed in to change notification settings - Fork 31k
/
Copy pathvariableResolver.ts
392 lines (332 loc) · 17.1 KB
/
variableResolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as paths from 'vs/base/common/path';
import * as process from 'vs/base/common/process';
import * as types from 'vs/base/common/types';
import * as objects from 'vs/base/common/objects';
import { IStringDictionary } from 'vs/base/common/collections';
import { IProcessEnvironment, isWindows, isMacintosh, isLinux } from 'vs/base/common/platform';
import { normalizeDriveLetter } from 'vs/base/common/labels';
import { localize } from 'vs/nls';
import { URI as uri } from 'vs/base/common/uri';
import { IConfigurationResolverService, VariableError, VariableKind } from 'vs/workbench/services/configurationResolver/common/configurationResolver';
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { ILabelService } from 'vs/platform/label/common/label';
import { replaceAsync } from 'vs/base/common/strings';
interface IVariableResolveContext {
getFolderUri(folderName: string): uri | undefined;
getWorkspaceFolderCount(): number;
getConfigurationValue(folderUri: uri | undefined, section: string): string | undefined;
getAppRoot(): string | undefined;
getExecPath(): string | undefined;
getFilePath(): string | undefined;
getWorkspaceFolderPathForFile?(): string | undefined;
getSelectedText(): string | undefined;
getLineNumber(): string | undefined;
getExtension(id: string): Promise<{ readonly extensionLocation: uri } | undefined>;
}
type Environment = { env: IProcessEnvironment | undefined; userHome: string | undefined };
export class AbstractVariableResolverService implements IConfigurationResolverService {
static readonly VARIABLE_LHS = '${';
static readonly VARIABLE_REGEXP = /\$\{(.*?)\}/g;
declare readonly _serviceBrand: undefined;
private _context: IVariableResolveContext;
private _labelService?: ILabelService;
private _envVariablesPromise?: Promise<IProcessEnvironment>;
private _userHomePromise?: Promise<string>;
protected _contributedVariables: Map<string, () => Promise<string | undefined>> = new Map();
constructor(_context: IVariableResolveContext, _labelService?: ILabelService, _userHomePromise?: Promise<string>, _envVariablesPromise?: Promise<IProcessEnvironment>) {
this._context = _context;
this._labelService = _labelService;
this._userHomePromise = _userHomePromise;
if (_envVariablesPromise) {
this._envVariablesPromise = _envVariablesPromise.then(envVariables => {
return this.prepareEnv(envVariables);
});
}
}
private prepareEnv(envVariables: IProcessEnvironment): IProcessEnvironment {
// windows env variables are case insensitive
if (isWindows) {
const ev: IProcessEnvironment = Object.create(null);
Object.keys(envVariables).forEach(key => {
ev[key.toLowerCase()] = envVariables[key];
});
return ev;
}
return envVariables;
}
public resolveWithEnvironment(environment: IProcessEnvironment, root: IWorkspaceFolder | undefined, value: string): Promise<string> {
return this.recursiveResolve({ env: this.prepareEnv(environment), userHome: undefined }, root ? root.uri : undefined, value);
}
public async resolveAsync(root: IWorkspaceFolder | undefined, value: string): Promise<string>;
public async resolveAsync(root: IWorkspaceFolder | undefined, value: string[]): Promise<string[]>;
public async resolveAsync(root: IWorkspaceFolder | undefined, value: IStringDictionary<string>): Promise<IStringDictionary<string>>;
public async resolveAsync(root: IWorkspaceFolder | undefined, value: any): Promise<any> {
const environment: Environment = {
env: await this._envVariablesPromise,
userHome: await this._userHomePromise
};
return this.recursiveResolve(environment, root ? root.uri : undefined, value);
}
private async resolveAnyBase(workspaceFolder: IWorkspaceFolder | undefined, config: any, commandValueMapping?: IStringDictionary<string>, resolvedVariables?: Map<string, string>): Promise<any> {
const result = objects.deepClone(config) as any;
// hoist platform specific attributes to top level
if (isWindows && result.windows) {
Object.keys(result.windows).forEach(key => result[key] = result.windows[key]);
} else if (isMacintosh && result.osx) {
Object.keys(result.osx).forEach(key => result[key] = result.osx[key]);
} else if (isLinux && result.linux) {
Object.keys(result.linux).forEach(key => result[key] = result.linux[key]);
}
// delete all platform specific sections
delete result.windows;
delete result.osx;
delete result.linux;
// substitute all variables recursively in string values
const environmentPromises: Environment = {
env: await this._envVariablesPromise,
userHome: await this._userHomePromise
};
return this.recursiveResolve(environmentPromises, workspaceFolder ? workspaceFolder.uri : undefined, result, commandValueMapping, resolvedVariables);
}
public async resolveAnyAsync(workspaceFolder: IWorkspaceFolder | undefined, config: any, commandValueMapping?: IStringDictionary<string>): Promise<any> {
return this.resolveAnyBase(workspaceFolder, config, commandValueMapping);
}
public async resolveAnyMap(workspaceFolder: IWorkspaceFolder | undefined, config: any, commandValueMapping?: IStringDictionary<string>): Promise<{ newConfig: any; resolvedVariables: Map<string, string> }> {
const resolvedVariables = new Map<string, string>();
const newConfig = await this.resolveAnyBase(workspaceFolder, config, commandValueMapping, resolvedVariables);
return { newConfig, resolvedVariables };
}
public resolveWithInteractionReplace(folder: IWorkspaceFolder | undefined, config: any, section?: string, variables?: IStringDictionary<string>): Promise<any> {
throw new Error('resolveWithInteractionReplace not implemented.');
}
public resolveWithInteraction(folder: IWorkspaceFolder | undefined, config: any, section?: string, variables?: IStringDictionary<string>): Promise<Map<string, string> | undefined> {
throw new Error('resolveWithInteraction not implemented.');
}
public contributeVariable(variable: string, resolution: () => Promise<string | undefined>): void {
if (this._contributedVariables.has(variable)) {
throw new Error('Variable ' + variable + ' is contributed twice.');
} else {
this._contributedVariables.set(variable, resolution);
}
}
private async recursiveResolve(environment: Environment, folderUri: uri | undefined, value: any, commandValueMapping?: IStringDictionary<string>, resolvedVariables?: Map<string, string>): Promise<any> {
if (types.isString(value)) {
return this.resolveString(environment, folderUri, value, commandValueMapping, resolvedVariables);
} else if (Array.isArray(value)) {
return Promise.all(value.map(s => this.recursiveResolve(environment, folderUri, s, commandValueMapping, resolvedVariables)));
} else if (types.isObject(value)) {
const result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
const replaced = await Promise.all(Object.keys(value).map(async key => {
const replaced = await this.resolveString(environment, folderUri, key, commandValueMapping, resolvedVariables);
return [replaced, await this.recursiveResolve(environment, folderUri, value[key], commandValueMapping, resolvedVariables)] as const;
}));
// two step process to preserve object key order
for (const [key, value] of replaced) {
result[key] = value;
}
return result;
}
return value;
}
private resolveString(environment: Environment, folderUri: uri | undefined, value: string, commandValueMapping: IStringDictionary<string> | undefined, resolvedVariables?: Map<string, string>): Promise<string> {
// loop through all variables occurrences in 'value'
return replaceAsync(value, AbstractVariableResolverService.VARIABLE_REGEXP, async (match: string, variable: string) => {
// disallow attempted nesting, see #77289. This doesn't exclude variables that resolve to other variables.
if (variable.includes(AbstractVariableResolverService.VARIABLE_LHS)) {
return match;
}
let resolvedValue = await this.evaluateSingleVariable(environment, match, variable, folderUri, commandValueMapping);
resolvedVariables?.set(variable, resolvedValue);
if ((resolvedValue !== match) && types.isString(resolvedValue) && resolvedValue.match(AbstractVariableResolverService.VARIABLE_REGEXP)) {
resolvedValue = await this.resolveString(environment, folderUri, resolvedValue, commandValueMapping, resolvedVariables);
}
return resolvedValue;
});
}
private fsPath(displayUri: uri): string {
return this._labelService ? this._labelService.getUriLabel(displayUri, { noPrefix: true }) : displayUri.fsPath;
}
private async evaluateSingleVariable(environment: Environment, match: string, variable: string, folderUri: uri | undefined, commandValueMapping: IStringDictionary<string> | undefined): Promise<string> {
// try to separate variable arguments from variable name
let argument: string | undefined;
const parts = variable.split(':');
if (parts.length > 1) {
variable = parts[0];
argument = parts[1];
}
// common error handling for all variables that require an open editor
const getFilePath = (variableKind: VariableKind): string => {
const filePath = this._context.getFilePath();
if (filePath) {
return filePath;
}
throw new VariableError(variableKind, (localize('canNotResolveFile', "Variable {0} can not be resolved. Please open an editor.", match)));
};
// common error handling for all variables that require an open editor
const getFolderPathForFile = (variableKind: VariableKind): string => {
const filePath = getFilePath(variableKind); // throws error if no editor open
if (this._context.getWorkspaceFolderPathForFile) {
const folderPath = this._context.getWorkspaceFolderPathForFile();
if (folderPath) {
return folderPath;
}
}
throw new VariableError(variableKind, localize('canNotResolveFolderForFile', "Variable {0}: can not find workspace folder of '{1}'.", match, paths.basename(filePath)));
};
// common error handling for all variables that require an open folder and accept a folder name argument
const getFolderUri = (variableKind: VariableKind): uri => {
if (argument) {
const folder = this._context.getFolderUri(argument);
if (folder) {
return folder;
}
throw new VariableError(variableKind, localize('canNotFindFolder', "Variable {0} can not be resolved. No such folder '{1}'.", match, argument));
}
if (folderUri) {
return folderUri;
}
if (this._context.getWorkspaceFolderCount() > 1) {
throw new VariableError(variableKind, localize('canNotResolveWorkspaceFolderMultiRoot', "Variable {0} can not be resolved in a multi folder workspace. Scope this variable using ':' and a workspace folder name.", match));
}
throw new VariableError(variableKind, localize('canNotResolveWorkspaceFolder', "Variable {0} can not be resolved. Please open a folder.", match));
};
switch (variable) {
case 'env':
if (argument) {
if (environment.env) {
// Depending on the source of the environment, on Windows, the values may all be lowercase.
const env = environment.env[isWindows ? argument.toLowerCase() : argument];
if (types.isString(env)) {
return env;
}
}
// For `env` we should do the same as a normal shell does - evaluates undefined envs to an empty string #46436
return '';
}
throw new VariableError(VariableKind.Env, localize('missingEnvVarName', "Variable {0} can not be resolved because no environment variable name is given.", match));
case 'config':
if (argument) {
const config = this._context.getConfigurationValue(folderUri, argument);
if (types.isUndefinedOrNull(config)) {
throw new VariableError(VariableKind.Config, localize('configNotFound', "Variable {0} can not be resolved because setting '{1}' not found.", match, argument));
}
if (types.isObject(config)) {
throw new VariableError(VariableKind.Config, localize('configNoString', "Variable {0} can not be resolved because '{1}' is a structured value.", match, argument));
}
return config;
}
throw new VariableError(VariableKind.Config, localize('missingConfigName', "Variable {0} can not be resolved because no settings name is given.", match));
case 'command':
return this.resolveFromMap(VariableKind.Command, match, argument, commandValueMapping, 'command');
case 'input':
return this.resolveFromMap(VariableKind.Input, match, argument, commandValueMapping, 'input');
case 'extensionInstallFolder':
if (argument) {
const ext = await this._context.getExtension(argument);
if (!ext) {
throw new VariableError(VariableKind.ExtensionInstallFolder, localize('extensionNotInstalled', "Variable {0} can not be resolved because the extension {1} is not installed.", match, argument));
}
return this.fsPath(ext.extensionLocation);
}
throw new VariableError(VariableKind.ExtensionInstallFolder, localize('missingExtensionName', "Variable {0} can not be resolved because no extension name is given.", match));
default: {
switch (variable) {
case 'workspaceRoot':
case 'workspaceFolder':
return normalizeDriveLetter(this.fsPath(getFolderUri(VariableKind.WorkspaceFolder)));
case 'cwd':
return ((folderUri || argument) ? normalizeDriveLetter(this.fsPath(getFolderUri(VariableKind.Cwd))) : process.cwd());
case 'workspaceRootFolderName':
case 'workspaceFolderBasename':
return paths.basename(this.fsPath(getFolderUri(VariableKind.WorkspaceFolderBasename)));
case 'userHome': {
if (environment.userHome) {
return environment.userHome;
}
throw new VariableError(VariableKind.UserHome, localize('canNotResolveUserHome', "Variable {0} can not be resolved. UserHome path is not defined", match));
}
case 'lineNumber': {
const lineNumber = this._context.getLineNumber();
if (lineNumber) {
return lineNumber;
}
throw new VariableError(VariableKind.LineNumber, localize('canNotResolveLineNumber', "Variable {0} can not be resolved. Make sure to have a line selected in the active editor.", match));
}
case 'selectedText': {
const selectedText = this._context.getSelectedText();
if (selectedText) {
return selectedText;
}
throw new VariableError(VariableKind.SelectedText, localize('canNotResolveSelectedText', "Variable {0} can not be resolved. Make sure to have some text selected in the active editor.", match));
}
case 'file':
return getFilePath(VariableKind.File);
case 'fileWorkspaceFolder':
return getFolderPathForFile(VariableKind.FileWorkspaceFolder);
case 'relativeFile':
if (folderUri || argument) {
return paths.relative(this.fsPath(getFolderUri(VariableKind.RelativeFile)), getFilePath(VariableKind.RelativeFile));
}
return getFilePath(VariableKind.RelativeFile);
case 'relativeFileDirname': {
const dirname = paths.dirname(getFilePath(VariableKind.RelativeFileDirname));
if (folderUri || argument) {
const relative = paths.relative(this.fsPath(getFolderUri(VariableKind.RelativeFileDirname)), dirname);
return relative.length === 0 ? '.' : relative;
}
return dirname;
}
case 'fileDirname':
return paths.dirname(getFilePath(VariableKind.FileDirname));
case 'fileExtname':
return paths.extname(getFilePath(VariableKind.FileExtname));
case 'fileBasename':
return paths.basename(getFilePath(VariableKind.FileBasename));
case 'fileBasenameNoExtension': {
const basename = paths.basename(getFilePath(VariableKind.FileBasenameNoExtension));
return (basename.slice(0, basename.length - paths.extname(basename).length));
}
case 'fileDirnameBasename':
return paths.basename(paths.dirname(getFilePath(VariableKind.FileDirnameBasename)));
case 'execPath': {
const ep = this._context.getExecPath();
if (ep) {
return ep;
}
return match;
}
case 'execInstallFolder': {
const ar = this._context.getAppRoot();
if (ar) {
return ar;
}
return match;
}
case 'pathSeparator':
return paths.sep;
default:
try {
const key = argument ? `${variable}:${argument}` : variable;
return this.resolveFromMap(VariableKind.Unknown, match, key, commandValueMapping, undefined);
} catch (error) {
return match;
}
}
}
}
}
private resolveFromMap(variableKind: VariableKind, match: string, argument: string | undefined, commandValueMapping: IStringDictionary<string> | undefined, prefix: string | undefined): string {
if (argument && commandValueMapping) {
const v = (prefix === undefined) ? commandValueMapping[argument] : commandValueMapping[prefix + ':' + argument];
if (typeof v === 'string') {
return v;
}
throw new VariableError(variableKind, localize('noValueForCommand', "Variable {0} can not be resolved because the command has no value.", match));
}
return match;
}
}