forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystemVariables.ts
More file actions
200 lines (167 loc) · 7.1 KB
/
systemVariables.ts
File metadata and controls
200 lines (167 loc) · 7.1 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as Path from 'path';
import { Range, Uri } from 'vscode';
import { IDocumentManager, IWorkspaceService } from '../application/types';
import { WorkspaceService } from '../application/workspace';
import * as Types from '../utils/sysTypes';
import { IStringDictionary, ISystemVariables } from './types';
abstract class AbstractSystemVariables implements ISystemVariables {
public resolve(value: string): string;
public resolve(value: string[]): string[];
public resolve(value: IStringDictionary<string>): IStringDictionary<string>;
public resolve(value: IStringDictionary<string[]>): IStringDictionary<string[]>;
public resolve(value: IStringDictionary<IStringDictionary<string>>): IStringDictionary<IStringDictionary<string>>;
public resolve(value: any): any {
if (Types.isString(value)) {
return this.__resolveString(value);
} else if (Types.isArray(value)) {
return this.__resolveArray(value);
} else if (Types.isObject(value)) {
return this.__resolveLiteral(value);
}
return value;
}
public resolveAny<T>(value: T): T;
public resolveAny(value: any): any {
if (Types.isString(value)) {
return this.__resolveString(value);
} else if (Types.isArray(value)) {
return this.__resolveAnyArray(value);
} else if (Types.isObject(value)) {
return this.__resolveAnyLiteral(value);
}
return value;
}
private __resolveString(value: string): string {
const regexp = /\$\{(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => {
const newValue = (<any>this)[name];
if (Types.isString(newValue)) {
return newValue;
} else {
return match && (match.indexOf('env.') > 0 || match.indexOf('env:') > 0) ? '' : match;
}
});
}
private __resolveLiteral(
values: IStringDictionary<string | IStringDictionary<string> | string[]>,
): IStringDictionary<string | IStringDictionary<string> | string[]> {
const result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
Object.keys(values).forEach((key) => {
const value = values[key];
result[key] = <any>this.resolve(<any>value);
});
return result;
}
private __resolveAnyLiteral<T>(values: T): T;
private __resolveAnyLiteral(values: any): any {
const result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
Object.keys(values).forEach((key) => {
const value = values[key];
result[key] = <any>this.resolveAny(<any>value);
});
return result;
}
private __resolveArray(value: string[]): string[] {
return value.map((s) => this.__resolveString(s));
}
private __resolveAnyArray<T>(value: T[]): T[];
private __resolveAnyArray(value: any[]): any[] {
return value.map((s) => this.resolveAny(s));
}
}
export class SystemVariables extends AbstractSystemVariables {
private _workspaceFolder: string;
private _workspaceFolderName: string;
private _filePath: string | undefined;
private _lineNumber: number | undefined;
private _selectedText: string | undefined;
private _execPath: string;
constructor(
file: Uri | undefined,
rootFolder: string | undefined,
workspace?: IWorkspaceService,
documentManager?: IDocumentManager,
) {
super();
const workspaceFolder = workspace && file ? workspace.getWorkspaceFolder(file) : undefined;
this._workspaceFolder = workspaceFolder ? workspaceFolder.uri.fsPath : rootFolder || __dirname;
this._workspaceFolderName = Path.basename(this._workspaceFolder);
this._filePath = file ? file.fsPath : undefined;
if (documentManager && documentManager.activeTextEditor) {
this._lineNumber = documentManager.activeTextEditor.selection.anchor.line + 1;
this._selectedText = documentManager.activeTextEditor.document.getText(
new Range(
documentManager.activeTextEditor.selection.start,
documentManager.activeTextEditor.selection.end,
),
);
}
this._execPath = process.execPath;
Object.keys(process.env).forEach((key) => {
(this as any as Record<string, string | undefined>)[`env:${key}`] = (
this as any as Record<string, string | undefined>
)[`env.${key}`] = process.env[key];
});
workspace = workspace ?? new WorkspaceService();
try {
workspace.workspaceFolders?.forEach((folder) => {
const basename = Path.basename(folder.uri.fsPath);
(this as any as Record<string, string | undefined>)[`workspaceFolder:${basename}`] = folder.uri.fsPath;
(this as any as Record<string, string | undefined>)[`workspaceFolder:${folder.name}`] =
folder.uri.fsPath;
});
} catch {
// This try...catch block is here to support pre-existing tests, ignore error.
}
}
public get cwd(): string {
return this.workspaceFolder;
}
public get workspaceRoot(): string {
return this._workspaceFolder;
}
public get workspaceFolder(): string {
return this._workspaceFolder;
}
public get workspaceRootFolderName(): string {
return this._workspaceFolderName;
}
public get workspaceFolderBasename(): string {
return this._workspaceFolderName;
}
public get file(): string | undefined {
return this._filePath;
}
public get relativeFile(): string | undefined {
return this.file ? Path.relative(this._workspaceFolder, this.file) : undefined;
}
public get relativeFileDirname(): string | undefined {
return this.relativeFile ? Path.dirname(this.relativeFile) : undefined;
}
public get fileBasename(): string | undefined {
return this.file ? Path.basename(this.file) : undefined;
}
public get fileBasenameNoExtension(): string | undefined {
return this.file ? Path.parse(this.file).name : undefined;
}
public get fileDirname(): string | undefined {
return this.file ? Path.dirname(this.file) : undefined;
}
public get fileExtname(): string | undefined {
return this.file ? Path.extname(this.file) : undefined;
}
public get lineNumber(): number | undefined {
return this._lineNumber;
}
public get selectedText(): string | undefined {
return this._selectedText;
}
public get execPath(): string {
return this._execPath;
}
}