Skip to content

Commit

Permalink
Resolve environment variables in tasks (#4202)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwateratmsft authored Jan 11, 2024
1 parent ae75aa5 commit 5fa4733
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/utils/resolveVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { cloneObject } from '../utils/cloneObject';

const variableMatcher: RegExp = /\$\{[a-z.\-_:]+\}/ig;
const configVariableMatcher: RegExp = /\$\{config:([a-z.\-_]+)\}/i;
const envVariableMatcher: RegExp = /\$\{env:([\w\d]+)\}/i;

export function resolveVariables<T>(target: T, folder?: WorkspaceFolder, additionalVariables?: { [key: string]: string }): T {
if (!target) {
Expand Down Expand Up @@ -50,14 +51,14 @@ function resolveSingleVariable(variable: string, folder?: WorkspaceFolder, addit
}
}

// Replace additional variables
// Replace additional variables as specified by the caller
const variableNameOnly = variable.replace(/[${}]/ig, '');
const replacement = additionalVariables?.[variable] ?? additionalVariables?.[variableNameOnly];
if (replacement !== undefined) {
return replacement;
}

// Replace config variables
// Replace config variables, e.g. ${config:foo.bar}
const configMatch = configVariableMatcher.exec(variable);
if (configMatch && configMatch.length > 1) {
const configName: string = configMatch[1]; // Index 1 is the "something.something" group of "${config:something.something}"
Expand All @@ -72,6 +73,17 @@ function resolveSingleVariable(variable: string, folder?: WorkspaceFolder, addit
}
}

// Replace environment variables, e.g. ${env:FOO}
const envMatch = envVariableMatcher.exec(variable);
if (envMatch && envMatch.length > 1) {
const envVarName: string = envMatch[1]; // Index 1 is the "FOO" group of "${env:FOO}"
const envVarValue = process.env[envVarName];

if (envVarValue) {
return envVarValue;
}
}

// Replace other variables
switch (variable) {
case '${file}':
Expand Down

0 comments on commit 5fa4733

Please sign in to comment.