Skip to content

Commit 192dc38

Browse files
author
James Brundage
committed
Update-PipeScript: Adding -RegexReplacement (Fixes #178)
1 parent 1f012be commit 192dc38

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

Update-PipeScript.ps1

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ function Update-PipeScript {
5252
})]
5353
[Collections.IDictionary]
5454
$AstReplacement = [Ordered]@{},
55+
56+
# If provided, will replace regular expression matches.
57+
[Collections.IDictionary]
58+
$RegexReplacement = [Ordered]@{},
5559

5660
# If provided, will remove one or more parameters from a ScriptBlock.
5761
[string[]]
@@ -254,14 +258,26 @@ function Update-PipeScript {
254258
}
255259
) -join ''
256260

257-
if ($ScriptBlock) {
258-
if ($Transpile) {
259-
[ScriptBlock]::Create($newScript) | .>Pipescript
260-
} else {
261-
[ScriptBlock]::Create($newScript)
261+
$text = $newScript
262+
if ($RegexReplacement.Count) {
263+
foreach ($repl in $RegexReplacement.GetEnumerator()) {
264+
$replRegex = if ($repl.Key -is [Regex]) {
265+
$repl.Key
266+
} else {
267+
[Regex]::new($repl.Key,'IgnoreCase,IgnorePatternWhitespace','00:00:05')
262268
}
269+
$newScript = $text = $replRegex.Replace($text, $repl.Value)
270+
}
271+
}
272+
273+
if ($ScriptBlock) {
274+
if ($Transpile) {
275+
[ScriptBlock]::Create($newScript) | .>Pipescript
263276
} else {
264-
$newScript
277+
[ScriptBlock]::Create($newScript)
265278
}
279+
} else {
280+
$newScript
281+
}
266282
}
267283
}

0 commit comments

Comments
 (0)