@@ -23,6 +23,7 @@ $SourceSection,
2323
2424# A string containing the text contents of the file
2525[Parameter (Mandatory , ParameterSetName = ' SourceTextAndPattern' )]
26+ [Parameter (Mandatory , ParameterSetName = ' SourceTextReplace' )]
2627[string ]
2728$SourceText ,
2829
@@ -31,6 +32,25 @@ $SourceText,
3132[regex ]
3233$SourcePattern ,
3334
35+
36+ [Parameter (Mandatory , ParameterSetName = ' SourceTextReplace' )]
37+ [Alias (' Replace' )]
38+ [ValidateScript ({
39+ if ($_.GetGroupNames () -notcontains ' PS' -and
40+ $_.GetGroupNames () -notcontains ' PipeScript'
41+ ) {
42+ throw " Group Name PS or PipeScript required"
43+ }
44+ return $true
45+ })]
46+ [regex ]
47+ $ReplacePattern ,
48+
49+ [Parameter (ParameterSetName = ' SourceTextReplace' )]
50+ [Alias (' Replacer' )]
51+ [ScriptBlock ]
52+ $ReplacementEvaluator ,
53+
3454# If set, will not transpile script blocks.
3555[Parameter (ParameterSetName = ' SourceTextAndPattern' )]
3656[Parameter (ParameterSetName = ' SourceSections' )]
@@ -40,25 +60,29 @@ $NoTranspile,
4060# The path to the source file.
4161[Parameter (ParameterSetName = ' SourceTextAndPattern' )]
4262[Parameter (ParameterSetName = ' SourceSections' )]
63+ [Parameter (ParameterSetName = ' SourceTextReplace' )]
4364[string ]
4465$SourceFile ,
4566
4667# A Script Block that will be injected before each inline is run.
4768[Parameter (ParameterSetName = ' SourceTextAndPattern' )]
4869[Parameter (ParameterSetName = ' SourceSections' )]
70+ [Parameter (ParameterSetName = ' SourceTextReplace' )]
4971[ScriptBlock ]
5072$Begin ,
5173
5274# A Script Block that will be piped to after each output.
5375[Parameter (ParameterSetName = ' SourceTextAndPattern' )]
5476[Parameter (ParameterSetName = ' SourceSections' )]
77+ [Parameter (ParameterSetName = ' SourceTextReplace' )]
5578[Alias (' Process' )]
5679[ScriptBlock ]
5780$ForeachObject ,
5881
5982# A Script Block that will be injected after each inline script is run.
6083[Parameter (ParameterSetName = ' SourceTextAndPattern' )]
6184[Parameter (ParameterSetName = ' SourceSections' )]
85+ [Parameter (ParameterSetName = ' SourceTextReplace' )]
6286[ScriptBlock ]
6387$End
6488)
@@ -68,6 +92,82 @@ begin {
6892}
6993
7094process {
95+ if ($psCmdlet.ParameterSetName -eq ' SourceTextReplace' ) {
96+ $fileText = $SourceText
97+ if (-not $PSBoundParameters [" ReplacementEvaluator" ]) {
98+ $ReplacementEvaluator = {
99+ param ($match )
100+
101+ $pipeScriptText =
102+ if ($Match.Groups [" PipeScript" ].Value) {
103+ $Match.Groups [" PipeScript" ].Value
104+ } elseif ($match.Groups [" PS" ].Value) {
105+ $Match.Groups [" PS" ].Value
106+ }
107+
108+ if (-not $pipeScriptText ) {
109+ return
110+ }
111+
112+ $InlineScriptBlock = [scriptblock ]::Create($pipeScriptText )
113+ if (-not $InlineScriptBlock ) {
114+ return
115+ }
116+
117+ if (-not $NoTranspile ) {
118+ $TranspiledOutput = $InlineScriptBlock | .> Pipescript
119+ if ($TranspiledOutput -is [ScriptBlock ]) {
120+ $InlineScriptBlock = $TranspiledOutput
121+ }
122+ }
123+
124+ $inlineAstString = $InlineScriptBlock.Ast.Extent.ToString ()
125+ if ($InlineScriptBlock.ParamBlock ) {
126+ $inlineAstString = $inlineAstString.Replace ($InlineScriptBlock.ParamBlock.Extent.ToString (), ' ' )
127+ }
128+ $inlineAstString = $inlineAstString
129+ $AddForeach =
130+ $ (
131+ if ($ForeachObject ) {
132+ ' |' + [Environment ]::NewLine
133+ @ (foreach ($foreachStatement in $ForeachObject ) {
134+ if ($foreachStatement.Ast.ProcessBlock -or $foreachStatement.Ast.BeginBlock ) {
135+ " . {$ForeachStatement }"
136+ } elseif ($foreachStatement.Ast.EndBlock.Statements -and
137+ $foreachStatement.Ast.EndBlock.Statements [0 ].PipelineElements[0 ].CommandElements -and
138+ $foreachStatement.Ast.EndBlock.Statements [0 ].PipelineElements[0 ].CommandElements.Value -in ' Foreach-Object' , ' %' ) {
139+ " $ForeachStatement "
140+ } else {
141+ " Foreach-Object {$ForeachStatement }"
142+ }
143+ }) -join (' |' + [Environment ]::NewLine)
144+ }
145+ )
146+
147+
148+ $statements = @ (
149+ if ($begin ) {
150+ " $begin "
151+ }
152+ if ($AddForeach ) {
153+ " @($inlineAstString )" + $AddForeach.Trim ()
154+ } else {
155+ $inlineAstString
156+ }
157+ if ($end ) {
158+ " $end "
159+ }
160+ )
161+
162+ $codeToRun = [ScriptBlock ]::Create($statements -join [Environment ]::Newline)
163+
164+ " $ ( & $codeToRun ) "
165+ }
166+ }
167+
168+ return $ReplacePattern.Replace ($fileText , $ReplacementEvaluator )
169+ }
170+
71171 if ($psCmdlet.ParameterSetName -eq ' SourceTextAndPattern' ) {
72172
73173 $fileText = $SourceText
0 commit comments