1111 If a Regular Expression can match each section, then the content in each section can be replaced.
1212#>
1313param (
14- # A list of source sections
15- [Parameter (Mandatory , ParameterSetName = ' SourceSections' , Position = 0 , ValueFromPipeline )]
16- [PSObject []]
17- $SourceSection ,
1814
1915# A string containing the text contents of the file
2016[Parameter (Mandatory , ParameterSetName = ' SourceTextAndPattern' )]
@@ -200,138 +196,5 @@ process {
200196
201197 return $ReplacePattern.Replace ($fileText , $ReplacementEvaluator )
202198 }
203-
204- if ($psParameterSet -eq ' SourceTextAndPattern' ) {
205-
206- $fileText = $SourceText
207- $foundSpots = @ ($SourcePattern.Matches ($fileText ))
208-
209- $SourceGeneratorInput = @ (
210- $index = 0
211- for ($spotIndex = 0 ; $spotIndex -lt $foundSpots.Count ; $spotIndex ++ ) {
212-
213- # If there's any distance between the last token and here, output it as a string.
214- if ($foundSpots [$spotIndex ].Index -gt $index ) {
215- $captureLength = $foundSpots [$spotIndex ].Index - $index
216- if ($captureLength -ge 0 ) {
217- $fileText.Substring ($index , $captureLength )
218- }
219- $index = $foundSpots [$spotIndex ].Index + $foundSpots [$spotIndex ].Length
220- }
221-
222- $isLastSpot = $spotIndex -ge ($foundSpots.Length - 1 )
223-
224- if ($foundSpots [$spotIndex ].Groups[" PSStart" ].Length) {
225- $absoluteStart = $foundSpots [$spotIndex ].Groups[" PSStart" ].Index +
226- $foundSpots [$spotIndex ].Groups[" PSStart" ].Length
227-
228- $index = $foundSpots [$spotIndex + 1 ].Index +
229- $foundSpots [$spotIndex + 1 ].Length
230- if (-not $isLastSpot -and $foundSpots [$spotIndex + 1 ].Groups[" PSEnd" ].Length) {
231- # If we find an end block, the next section becomes code
232-
233- $scriptToCreate = @ (
234- if ($Begin ) { $Begin }
235- $AddForeach =
236- $ (
237- if ($ForeachObject ) {
238- ' |' + [Environment ]::NewLine
239- @ (foreach ($foreachStatement in $ForeachObject ) {
240- if ($foreachStatement.Ast.ProcessBlock -or $foreachStatement.Ast.BeginBlock ) {
241- " . {$ForeachStatement }"
242- } elseif ($foreachStatement.Ast.EndBlock.Statements -and
243- $foreachStatement.Ast.EndBlock.Statements [0 ].PipelineElements[0 ].CommandElements -and
244- $foreachStatement.Ast.EndBlock.Statements [0 ].PipelineElements[0 ].CommandElements.Value -in ' Foreach-Object' , ' %' ) {
245- " $ForeachStatement "
246- } else {
247- " Foreach-Object {$ForeachStatement }"
248- }
249- }) -join (' |' + [Environment ]::NewLine)
250- }
251- )
252- $Statement = $fileText.Substring ($absoluteStart ,
253- $index - $absoluteStart - $foundSpots [$spotIndex + 1 ].Groups[" PSEnd" ].Length
254- )
255- if ($AddForeach ) {
256- " @($Statement )" + $AddForeach.Trim ()
257- } else {
258- $Statement
259- }
260-
261- if ($End ) { $end }
262- ) -join [Environment ]::Newline
263- [scriptblock ]::Create($scriptToCreate )
264-
265- $spotIndex ++
266- } else {
267- Write-Error " Start Not Followed By End' $ ( $foundSpots [$spotIndex ].Index) '"
268- }
269- }
270- }
271-
272- if ($index -lt $fileText.Length ) {
273- $fileText.Substring ($index )
274- }
275- )
276-
277- $null = $PSBoundParameters.Remove (" SourceText" )
278- $null = $PSBoundParameters.Remove (" SourcePattern" )
279-
280- & $myInvocation.MyCommand.ScriptBlock @PSBoundParameters - SourceSection $SourceGeneratorInput
281- return
282- }
283-
284- $allSections += @ (
285- foreach ($section in $SourceSection ) {
286-
287- if ($section -is [string ]) {
288- if ($section -match ' [\r\n]' ) {
289- " @'" + [Environment ]::NewLine + $section + [Environment ]::newLine + " '@"
290- } else {
291- " '" + $section.Replace (" '" , " ''" ) + " '"
292- }
293- }
294-
295- if ($section -is [ScriptBlock ]) {
296- if (-not $NoTranspile ) {
297- $section |
298- .> Pipescript
299- } else {
300- $section
301- }
302- }
303-
304- })
305199}
306200
307- end {
308- if ($allSections ) {
309-
310- $combinedSections = @ (for ($sectionIndex = 0 ; $sectionIndex -lt $allSections.Length ; $sectionIndex ++ ) {
311- $section = $allSections [$sectionIndex ]
312- $isLastSection = $sectionIndex -eq $allSections.Length - 1
313- if ($section -is [ScriptBlock ]) {
314- " `$ ($section )"
315- } else {
316- $section
317- }
318- if (-not $isLastSection ) {
319- ' +'
320- }
321- })
322- $combinedFile = $combinedSections -join ' '
323-
324- if ($SourceFile -and $SourceFile -match ' \.ps1{0,1}\.(?<ext>[^.]+$)' ) {
325- $sourceTempFilePath = $SourceFile -replace ' \.ps1{0,1}\.(?<ext>[^.]+$)' , ' .${ext}.source.ps1'
326- $combinedFile | Set-Content $sourceTempFilePath - Force
327- }
328-
329-
330- try {
331- [scriptblock ]::Create($combinedFile )
332- } catch {
333- $ex = $_
334- Write-Error - ErrorRecord $ex
335- }
336- }
337- }
0 commit comments