Skip to content

Commit f86f601

Browse files
committed
fix powershell Expand-LineVariables
Signed-off-by: Tony Germano <tony@germano.name>
1 parent 9e3d277 commit f86f601

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

server/basedir-includes/oieserver.ps1

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,33 @@ function Expand-LineVariables {
100100
param(
101101
[string]$Line
102102
)
103-
$resultBuilder = [System.Text.StringBuilder]::new()
104-
$remainingLine = $Line
105-
106-
# This loop consumes the line from left to right, preventing recursive expansion.
107-
while ($remainingLine -match '\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}') {
108-
# Append the text before the match to our result.
109-
$prefix = $remainingLine.Substring(0, $matches[0].Index)
110-
$resultBuilder.Append($prefix) | Out-Null
103+
104+
# Define a "match evaluator" script block. This block will be called
105+
# for every match the regex finds.
106+
$evaluator = {
107+
param($match)
108+
109+
# The variable name is in the first capture group.
110+
$varName = $match.Groups[1].Value
111111

112-
# Get the variable name and its value.
113-
$varName = $matches[1]
112+
# Look for a PowerShell variable first.
114113
$varValue = (Get-Variable -Name $varName -Scope "global" -ErrorAction SilentlyContinue).Value
114+
# If not found, look for an environment variable.
115115
if ($null -eq $varValue) {
116116
$varValue = (Get-Variable -Name "env:$varName" -ErrorAction SilentlyContinue).Value
117117
}
118-
$resultBuilder.Append($varValue) | Out-Null
119118

120-
# Update the line to be only the portion *after* the match.
121-
$remainingLine = $remainingLine.Substring($matches[0].Index + $matches[0].Length)
119+
# Return the found value. This will be the replacement for the match.
120+
return $varValue
122121
}
123122

124-
# Append any remaining part of the line after the last match and return.
125-
$resultBuilder.Append($remainingLine) | Out-Null
126-
return $resultBuilder.ToString()
123+
# Define the regex pattern to find ${...} variables.
124+
$regex = '\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}'
125+
126+
# Use the static Replace method, passing the input line, the regex, and our evaluator.
127+
$expandedLine = [regex]::Replace($Line, $regex, $evaluator)
128+
129+
return $expandedLine
127130
}
128131

129132
# --- Function to validate Java version ---

0 commit comments

Comments
 (0)