Repository navigation
Expand file tree
/
Copy pathsystemVariables.ts
More file actions
125 lines (106 loc) · 4.66 KB
/
Copy pathsystemVariables.ts
File metadata and controls
125 lines (106 loc) · 4.66 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
/*---------------------------------------------------------------------------------------------
* 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 * as Types from '../utils/sysTypes';
import { IStringDictionary, ISystemVariables } from './types';
/* tslint:disable:rule1 no-any no-unnecessary-callback-wrapper jsdoc-format no-for-in prefer-const no-increment-decrement */
export 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>>;
// tslint:disable-next-line:no-any
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;
// tslint:disable-next-line:no-any
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) => {
// tslint:disable-next-line:no-any
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];
// tslint:disable-next-line:no-any
result[key] = <any>this.resolve(<any>value);
});
return result;
}
private __resolveAnyLiteral<T>(values: T): T;
// tslint:disable-next-line:no-any
private __resolveAnyLiteral(values: any): any {
const result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
Object.keys(values).forEach(key => {
const value = values[key];
// tslint:disable-next-line:no-any
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[];
// tslint:disable-next-line:no-any
private __resolveAnyArray(value: any[]): any[] {
return value.map(s => this.resolveAny(s));
}
}
export class SystemVariables extends AbstractSystemVariables {
private _workspaceFolder: string;
private _workspaceFolderName: string;
constructor(workspaceFolder?: string) {
super();
this._workspaceFolder = typeof workspaceFolder === 'string' ? workspaceFolder : __dirname;
this._workspaceFolderName = Path.basename(this._workspaceFolder);
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];
});
}
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;
}
}