|
37 | 37 |
|
38 | 38 | # Replaces sections within text. -TextReplacement is a dictionary of replacements. |
39 | 39 | # 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')] |
41 | 41 | [ValidateScript({ |
42 | 42 | if (-not $_.Count) { return $true } |
43 | 43 | $badKeys = $_.Keys -notmatch '^\d+,\d+$' |
|
50 | 50 | $TextReplacement, |
51 | 51 |
|
52 | 52 | # If set, will replace items based off of the abstract syntax tree. |
53 | | - [Alias('AstReplacements')] |
| 53 | + [Alias('AstReplacements', 'ReplaceAST')] |
54 | 54 | [ValidateScript({ |
55 | 55 | $badKeys = |
56 | 56 | foreach ($k in $_.Keys) { |
|
67 | 67 | $AstReplacement = [Ordered]@{}, |
68 | 68 |
|
69 | 69 | # If provided, will replace regular expression matches. |
| 70 | + [Alias('ReplaceRegex', 'RegexReplacements')] |
70 | 71 | [Collections.IDictionary] |
71 | 72 | $RegexReplacement = [Ordered]@{}, |
72 | 73 |
|
73 | 74 | # If provided, will replace regions. |
| 75 | + [Alias('ReplaceRegion')] |
74 | 76 | [Collections.IDictionary] |
75 | 77 | $RegionReplacement, |
76 | 78 |
|
77 | 79 | # If provided, will remove one or more parameters from a ScriptBlock. |
78 | 80 | [string[]] |
79 | 81 | $RemoveParameter, |
80 | 82 |
|
| 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 | + |
81 | 91 | # A collection of variables to rename. |
82 | 92 | [Collections.IDictionary] |
83 | 93 | [Alias('RenameParameter')] |
|
311 | 321 | #endregion Replace Text Spans |
312 | 322 |
|
313 | 323 | # Update $text before we continue |
314 | | - $text = $updatedText |
| 324 | + $text = $updatedText |
315 | 325 |
|
316 | 326 | #region Replace Regions |
317 | 327 | if ($RegionReplacement.Count) { |
|
340 | 350 | } |
341 | 351 | #endregion Replace Regions |
342 | 352 |
|
| 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 | + |
343 | 386 | #region Replace Regex |
344 | 387 | if ($RegexReplacement.Count) { |
345 | 388 | foreach ($repl in $RegexReplacement.GetEnumerator()) { |
|
354 | 397 | #endregion Replace Regex |
355 | 398 |
|
356 | 399 | if ($ScriptBlock) { |
| 400 | + $updatedScriptBlock = [ScriptBlock]::Create($updatedText) |
357 | 401 | if ($Transpile) { |
358 | | - [ScriptBlock]::Create($updatedText) | .>Pipescript |
| 402 | + $updatedScriptBlock | .>Pipescript |
359 | 403 | } else { |
360 | | - [ScriptBlock]::Create($updatedText) |
| 404 | + $updatedScriptBlock |
361 | 405 | } |
362 | 406 | } else { |
363 | 407 | $updatedText |
|
0 commit comments