-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathvariableSubstitution.ts
More file actions
194 lines (162 loc) · 6.56 KB
/
variableSubstitution.ts
File metadata and controls
194 lines (162 loc) · 6.56 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as crypto from 'crypto';
import { ContainerError } from './errors';
import { URI } from 'vscode-uri';
export interface SubstitutionContext {
platform: NodeJS.Platform;
configFile?: URI;
localWorkspaceFolder?: string;
containerWorkspaceFolder?: string;
env: NodeJS.ProcessEnv;
}
export function substitute<T extends object>(context: SubstitutionContext, value: T): T {
let env: NodeJS.ProcessEnv | undefined;
const isWindows = context.platform === 'win32';
const updatedContext = {
...context,
get env() {
return env || (env = normalizeEnv(isWindows, context.env));
}
};
const replace = replaceWithContext.bind(undefined, isWindows, updatedContext);
if (context.containerWorkspaceFolder) {
updatedContext.containerWorkspaceFolder = resolveString(replace, context.containerWorkspaceFolder);
}
return substitute0(replace, value);
}
export function beforeContainerSubstitute<T extends object>(idLabels: Record<string, string> | undefined, value: T): T {
let devcontainerId: string | undefined;
return substitute0(replaceDevContainerId.bind(undefined, () => devcontainerId || (idLabels && (devcontainerId = devcontainerIdForLabels(idLabels)))), value);
}
export function containerSubstitute<T extends object>(platform: NodeJS.Platform, configFile: URI | undefined, containerEnv: NodeJS.ProcessEnv, value: T): T {
const isWindows = platform === 'win32';
return substitute0(replaceContainerEnv.bind(undefined, isWindows, configFile, normalizeEnv(isWindows, containerEnv)), value);
}
type Replace = (match: string, variable: string, args: string[]) => string;
function substitute0(replace: Replace, value: any): any {
if (typeof value === 'string') {
return resolveString(replace, value);
} else if (Array.isArray(value)) {
return value.map(s => substitute0(replace, s));
} else if (value && typeof value === 'object' && !URI.isUri(value)) {
const result: any = Object.create(null);
Object.keys(value).forEach(key => {
result[key] = substitute0(replace, value[key]);
});
return result;
}
return value;
}
const VARIABLE_REGEXP = /\$\{(.*?)\}/g;
function normalizeEnv(isWindows: boolean, originalEnv: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
if (isWindows) {
const env = Object.create(null);
Object.keys(originalEnv).forEach(key => {
env[key.toLowerCase()] = originalEnv[key];
});
return env;
}
return originalEnv;
}
function resolveString(replace: Replace, value: string): string {
// loop through all variables occurrences in 'value'
return value.replace(VARIABLE_REGEXP, evaluateSingleVariable.bind(undefined, replace));
}
function evaluateSingleVariable(replace: Replace, match: string, variable: string): string {
// try to separate variable arguments from variable name
let args: string[] = [];
const parts = variable.split(':');
if (parts.length > 1) {
variable = parts[0];
args = parts.slice(1);
}
return replace(match, variable, args);
}
function replaceWithContext(isWindows: boolean, context: SubstitutionContext, match: string, variable: string, args: string[]) {
switch (variable) {
case 'env':
case 'localEnv':
return lookupValue(isWindows, context.env, args, match, context.configFile);
case 'localHomeFolder':
return lookupHomeFolder(isWindows, context.env, args, match, context.configFile);
case 'localWorkspaceFolder':
return context.localWorkspaceFolder !== undefined ? context.localWorkspaceFolder : match;
case 'localWorkspaceFolderBasename':
return context.localWorkspaceFolder !== undefined ? (isWindows ? path.win32 : path.posix).basename(context.localWorkspaceFolder) : match;
case 'containerWorkspaceFolder':
return context.containerWorkspaceFolder !== undefined ? context.containerWorkspaceFolder : match;
case 'containerWorkspaceFolderBasename':
return context.containerWorkspaceFolder !== undefined ? path.posix.basename(context.containerWorkspaceFolder) : match;
default:
return match;
}
}
function replaceContainerEnv(isWindows: boolean, configFile: URI | undefined, containerEnvObj: NodeJS.ProcessEnv, match: string, variable: string, args: string[]) {
switch (variable) {
case 'containerEnv':
return lookupValue(isWindows, containerEnvObj, args, match, configFile);
default:
return match;
}
}
function replaceDevContainerId(getDevContainerId: () => string | undefined, match: string, variable: string) {
switch (variable) {
case 'devcontainerId':
return getDevContainerId() || match;
default:
return match;
}
}
function lookupHomeFolder(isWindows: boolean, envObj: NodeJS.ProcessEnv, args: string[], match: string, configFile: URI | undefined) {
if (args.length != 0) {
let envVariableName = "HOME";
if (isWindows) {
envVariableName = "userprofile";
}
const env = envObj[envVariableName];
if (typeof env === 'string') {
return env;
}
// For `env` we should do the same as a normal shell does - evaluates missing envs to an empty string #46436
return '';
}
throw new ContainerError({
description: `'${match}'${configFile ? ` in ${path.posix.basename(configFile.path)}` : ''} localHomeFolder cannot have any arguments.`
});
}
function lookupValue(isWindows: boolean, envObj: NodeJS.ProcessEnv, args: string[], match: string, configFile: URI | undefined) {
if (args.length > 0) {
let envVariableName = args[0];
if (isWindows) {
envVariableName = envVariableName.toLowerCase();
}
const env = envObj[envVariableName];
if (typeof env === 'string') {
return env;
}
if (args.length > 1) {
const defaultValue = args[1];
return defaultValue;
}
// For `env` we should do the same as a normal shell does - evaluates missing envs to an empty string #46436
return '';
}
throw new ContainerError({
description: `'${match}'${configFile ? ` in ${path.posix.basename(configFile.path)}` : ''} can not be resolved because no environment variable name is given.`
});
}
function devcontainerIdForLabels(idLabels: Record<string, string>): string {
const stringInput = JSON.stringify(idLabels, Object.keys(idLabels).sort()); // sort properties
const bufferInput = Buffer.from(stringInput, 'utf-8');
const hash = crypto.createHash('sha256')
.update(bufferInput)
.digest();
const uniqueId = BigInt(`0x${hash.toString('hex')}`)
.toString(32)
.padStart(52, '0');
return uniqueId;
}