forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemVariables.ts
More file actions
151 lines (131 loc) · 4.97 KB
/
systemVariables.ts
File metadata and controls
151 lines (131 loc) · 4.97 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
import * as Types from './types';
import * as vscode from 'vscode';
/**
* An interface for a JavaScript object that
* acts a dictionary. The keys are strings.
*/
export interface IStringDictionary<V> {
[name: string]: V;
}
export abstract class Parser {
protected log(message: string): void {
}
protected is(value: any, func: (value: any) => boolean, wrongTypeState?: any, wrongTypeMessage?: string, undefinedState?: any, undefinedMessage?: string): boolean {
if (Types.isUndefined(value)) {
return false;
}
if (!func(value)) {
return false;
}
return true;
}
protected static merge<T>(destination: T, source: T, overwrite: boolean): void {
Object.keys(source).forEach((key) => {
let destValue = destination[key];
let sourceValue = source[key];
if (Types.isUndefined(sourceValue)) {
return;
}
if (Types.isUndefined(destValue)) {
destination[key] = sourceValue;
} else {
if (overwrite) {
if (Types.isObject(destValue) && Types.isObject(sourceValue)) {
this.merge(destValue, sourceValue, overwrite);
} else {
destination[key] = sourceValue;
}
}
}
});
}
}
export interface ISystemVariables {
resolve(value: string): string;
resolve(value: string[]): string[];
resolve(value: IStringDictionary<string>): IStringDictionary<string>;
resolve(value: IStringDictionary<string[]>): IStringDictionary<string[]>;
resolve(value: IStringDictionary<IStringDictionary<string>>): IStringDictionary<IStringDictionary<string>>;
resolveAny<T>(value: T): T;
[key: string]: any;
}
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>>;
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;
}
resolveAny<T>(value: T): T;
resolveAny<T>(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 {
let regexp = /\$\{(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => {
let newValue = (<any>this)[name];
if (Types.isString(newValue)) {
return newValue;
} else {
return match && match.indexOf('env.') > 0 ? '' : match;
}
});
}
private __resolveLiteral(values: IStringDictionary<string | IStringDictionary<string> | string[]>): IStringDictionary<string | IStringDictionary<string> | string[]> {
let result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
Object.keys(values).forEach(key => {
let value = values[key];
result[key] = <any>this.resolve(<any>value);
});
return result;
}
private __resolveAnyLiteral<T>(values: T): T;
private __resolveAnyLiteral<T>(values: any): any {
let result: IStringDictionary<string | IStringDictionary<string> | string[]> = Object.create(null);
Object.keys(values).forEach(key => {
let 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 _workspaceRoot: string;
private _execPath: string;
constructor() {
super();
this._workspaceRoot = vscode.workspace.rootPath;
Object.keys(process.env).forEach(key => {
this[`env.${key}`] = process.env[key];
});
}
public get cwd(): string {
return this.workspaceRoot;
}
public get workspaceRoot(): string {
return this._workspaceRoot;
}
}