|
| 1 | +import path = require('path'); |
1 | 2 | import * as vscode from 'vscode';
|
2 | 3 | import { Env } from './client';
|
3 | 4 | import { log } from "./util";
|
@@ -210,3 +211,125 @@ export async function updateConfig(config: vscode.WorkspaceConfiguration) {
|
210 | 211 | }
|
211 | 212 | }
|
212 | 213 | }
|
| 214 | + |
| 215 | +export function substituteVariablesInEnv(env: Env): Env { |
| 216 | + const missingDeps = new Set<string>(); |
| 217 | + // vscode uses `env:ENV_NAME` for env vars resolution, and it's easier |
| 218 | + // to follow the same convention for our dependency tracking |
| 219 | + const definedEnvKeys = new Set(Object.keys(env).map(key => `env:${key}`)); |
| 220 | + const envWithDeps = Object.fromEntries(Object.entries(env).map(([key, value]) => { |
| 221 | + const deps = new Set<string>(); |
| 222 | + const depRe = new RegExp(/\${(?<depName>.+?)}/g); |
| 223 | + let match = undefined; |
| 224 | + while ((match = depRe.exec(value))) { |
| 225 | + const depName = match.groups!.depName; |
| 226 | + deps.add(depName); |
| 227 | + // `depName` at this point can have a form of `expression` or |
| 228 | + // `prefix:expression` |
| 229 | + if (!definedEnvKeys.has(depName)) { |
| 230 | + missingDeps.add(depName); |
| 231 | + } |
| 232 | + } |
| 233 | + return [`env:${key}`, { deps: [...deps], value }]; |
| 234 | + })); |
| 235 | + |
| 236 | + const resolved = new Set<string>(); |
| 237 | + for (const dep of missingDeps) { |
| 238 | + const match = /(?<prefix>.*?):(?<body>.+)/.exec(dep); |
| 239 | + if (match) { |
| 240 | + const { prefix, body } = match.groups!; |
| 241 | + if (prefix === 'env') { |
| 242 | + const envName = body; |
| 243 | + envWithDeps[dep] = { |
| 244 | + value: process.env[envName] ?? '', |
| 245 | + deps: [] |
| 246 | + }; |
| 247 | + resolved.add(dep); |
| 248 | + } else { |
| 249 | + // we can't handle other prefixes at the moment |
| 250 | + // leave values as is, but still mark them as resolved |
| 251 | + envWithDeps[dep] = { |
| 252 | + value: '${' + dep + '}', |
| 253 | + deps: [] |
| 254 | + }; |
| 255 | + resolved.add(dep); |
| 256 | + } |
| 257 | + } else { |
| 258 | + envWithDeps[dep] = { |
| 259 | + value: computeVscodeVar(dep), |
| 260 | + deps: [] |
| 261 | + }; |
| 262 | + } |
| 263 | + } |
| 264 | + const toResolve = new Set(Object.keys(envWithDeps)); |
| 265 | + |
| 266 | + let leftToResolveSize; |
| 267 | + do { |
| 268 | + leftToResolveSize = toResolve.size; |
| 269 | + for (const key of toResolve) { |
| 270 | + if (envWithDeps[key].deps.every(dep => resolved.has(dep))) { |
| 271 | + envWithDeps[key].value = envWithDeps[key].value.replace( |
| 272 | + /\${(?<depName>.+?)}/g, (_wholeMatch, depName) => { |
| 273 | + return envWithDeps[depName].value; |
| 274 | + }); |
| 275 | + resolved.add(key); |
| 276 | + toResolve.delete(key); |
| 277 | + } |
| 278 | + } |
| 279 | + } while (toResolve.size > 0 && toResolve.size < leftToResolveSize); |
| 280 | + |
| 281 | + const resolvedEnv: Env = {}; |
| 282 | + for (const key of Object.keys(env)) { |
| 283 | + resolvedEnv[key] = envWithDeps[`env:${key}`].value; |
| 284 | + } |
| 285 | + return resolvedEnv; |
| 286 | +} |
| 287 | + |
| 288 | +function computeVscodeVar(varName: string): string { |
| 289 | + // https://code.visualstudio.com/docs/editor/variables-reference |
| 290 | + const supportedVariables: { [k: string]: () => string } = { |
| 291 | + workspaceFolder: () => { |
| 292 | + const folders = vscode.workspace.workspaceFolders ?? []; |
| 293 | + if (folders.length === 1) { |
| 294 | + // TODO: support for remote workspaces? |
| 295 | + return folders[0].uri.fsPath; |
| 296 | + } else if (folders.length > 1) { |
| 297 | + // could use currently opened document to detect the correct |
| 298 | + // workspace. However, that would be determined by the document |
| 299 | + // user has opened on Editor startup. Could lead to |
| 300 | + // unpredictable workspace selection in practice. |
| 301 | + // It's better to pick the first one |
| 302 | + return folders[0].uri.fsPath; |
| 303 | + } else { |
| 304 | + // no workspace opened |
| 305 | + return ''; |
| 306 | + } |
| 307 | + }, |
| 308 | + |
| 309 | + workspaceFolderBasename: () => { |
| 310 | + const workspaceFolder = computeVscodeVar('workspaceFolder'); |
| 311 | + if (workspaceFolder) { |
| 312 | + return path.basename(workspaceFolder); |
| 313 | + } else { |
| 314 | + return ''; |
| 315 | + } |
| 316 | + }, |
| 317 | + |
| 318 | + cwd: () => process.cwd(), |
| 319 | + |
| 320 | + // see |
| 321 | + // https://github.com/microsoft/vscode/blob/08ac1bb67ca2459496b272d8f4a908757f24f56f/src/vs/workbench/api/common/extHostVariableResolverService.ts#L81 |
| 322 | + // or |
| 323 | + // https://github.com/microsoft/vscode/blob/29eb316bb9f154b7870eb5204ec7f2e7cf649bec/src/vs/server/node/remoteTerminalChannel.ts#L56 |
| 324 | + execPath: () => process.env.VSCODE_EXEC_PATH ?? process.execPath, |
| 325 | + |
| 326 | + pathSeparator: () => path.sep |
| 327 | + }; |
| 328 | + |
| 329 | + if (varName in supportedVariables) { |
| 330 | + return supportedVariables[varName](); |
| 331 | + } else { |
| 332 | + // can't resolve, keep the expression as is |
| 333 | + return '${' + varName + '}'; |
| 334 | + } |
| 335 | +} |
0 commit comments