Skip to content

Commit e05b821

Browse files
author
James Brundage
committed
Update-PipeScript: Adding -InsertBefore/After (Fixes #309). Improving aliasing (Fixes #310)
1 parent eda542d commit e05b821

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

Update-PipeScript.ps1

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
# Replaces sections within text. -TextReplacement is a dictionary of replacements.
3939
# Keys in the dictionary must be a string describing a character range, in the form start,end.
40-
[Alias('ScriptReplacements','TextReplacements','ScriptReplacement')]
40+
[Alias('ScriptReplacements','TextReplacements','ScriptReplacement', 'ReplaceText')]
4141
[ValidateScript({
4242
if (-not $_.Count) { return $true }
4343
$badKeys = $_.Keys -notmatch '^\d+,\d+$'
@@ -50,7 +50,7 @@
5050
$TextReplacement,
5151

5252
# If set, will replace items based off of the abstract syntax tree.
53-
[Alias('AstReplacements')]
53+
[Alias('AstReplacements', 'ReplaceAST')]
5454
[ValidateScript({
5555
$badKeys =
5656
foreach ($k in $_.Keys) {
@@ -67,17 +67,27 @@
6767
$AstReplacement = [Ordered]@{},
6868

6969
# If provided, will replace regular expression matches.
70+
[Alias('ReplaceRegex', 'RegexReplacements')]
7071
[Collections.IDictionary]
7172
$RegexReplacement = [Ordered]@{},
7273

7374
# If provided, will replace regions.
75+
[Alias('ReplaceRegion')]
7476
[Collections.IDictionary]
7577
$RegionReplacement,
7678

7779
# If provided, will remove one or more parameters from a ScriptBlock.
7880
[string[]]
7981
$RemoveParameter,
8082

83+
# If provided, will insert text before any regular epxression match.
84+
[Collections.IDictionary]
85+
$InsertBefore,
86+
87+
# If provided, will insert text after any regular expression match.
88+
[Collections.IDictionary]
89+
$InsertAfter,
90+
8191
# A collection of variables to rename.
8292
[Collections.IDictionary]
8393
[Alias('RenameParameter')]
@@ -311,7 +321,7 @@
311321
#endregion Replace Text Spans
312322

313323
# Update $text before we continue
314-
$text = $updatedText
324+
$text = $updatedText
315325

316326
#region Replace Regions
317327
if ($RegionReplacement.Count) {
@@ -340,6 +350,39 @@
340350
}
341351
#endregion Replace Regions
342352

353+
#region Insert Before/After
354+
355+
# If we have any -InsertBefore
356+
if ($InsertBefore.Count) {
357+
# Walk thru each insertion
358+
foreach ($kv in @($InsertBefore.GetEnumerator())) {
359+
if (
360+
$kv.Value -is [string] -and # If the value was a string and
361+
-not $kv.Value.Contains('$&') # it did not include the substitution.
362+
) {
363+
$kv.Value += '$&' # subsitute the match at the end.
364+
}
365+
# Add our insertion to the regular expression replacements.
366+
$RegexReplacement[$kv.Key] = $kv.Value
367+
}
368+
}
369+
370+
# If we had any -InsertAfter
371+
if ($InsertAfter.Count) {
372+
# Walk thru each insertion
373+
foreach ($kv in @($InsertAfter.GetEnumerator())) {
374+
if (
375+
$kv.Value -is [string] -and # If the value was a string and
376+
-not $kv.Value.Contains('$&') # it did not include the substitution.
377+
) {
378+
$kv.Value = '$&' + $kv.Value # subsitute the match at the start.
379+
}
380+
# Add our insertion to the regular expression replacements.
381+
$RegexReplacement[$kv.Key] = $kv.Value
382+
}
383+
}
384+
#endregion Insert Before/After
385+
343386
#region Replace Regex
344387
if ($RegexReplacement.Count) {
345388
foreach ($repl in $RegexReplacement.GetEnumerator()) {
@@ -354,10 +397,11 @@
354397
#endregion Replace Regex
355398

356399
if ($ScriptBlock) {
400+
$updatedScriptBlock = [ScriptBlock]::Create($updatedText)
357401
if ($Transpile) {
358-
[ScriptBlock]::Create($updatedText) | .>Pipescript
402+
$updatedScriptBlock | .>Pipescript
359403
} else {
360-
[ScriptBlock]::Create($updatedText)
404+
$updatedScriptBlock
361405
}
362406
} else {
363407
$updatedText

0 commit comments

Comments
 (0)