|
| 1 | +class MergeBlocksAspect : ModuleBuilderAspect { |
| 2 | + [System.Management.Automation.HiddenAttribute()] |
| 3 | + [NamedBlockAst]$BeginBlockTemplate |
| 4 | + |
| 5 | + [System.Management.Automation.HiddenAttribute()] |
| 6 | + [NamedBlockAst]$ProcessBlockTemplate |
| 7 | + |
| 8 | + [System.Management.Automation.HiddenAttribute()] |
| 9 | + [NamedBlockAst]$EndBlockTemplate |
| 10 | + |
| 11 | + [List[TextReplace]]Generate([Ast]$ast) { |
| 12 | + if (!($this.BeginBlockTemplate = $this.Aspect.Find({ $args[0] -is [NamedBlockAst] -and $args[0].BlockKind -eq "Begin" }, $false))) { |
| 13 | + Write-Debug "No Aspect for BeginBlock" |
| 14 | + } else { |
| 15 | + Write-Debug "BeginBlock Aspect: $($this.BeginBlockTemplate)" |
| 16 | + } |
| 17 | + if (!($this.ProcessBlockTemplate = $this.Aspect.Find({ $args[0] -is [NamedBlockAst] -and $args[0].BlockKind -eq "Process" }, $false))) { |
| 18 | + Write-Debug "No Aspect for ProcessBlock" |
| 19 | + } else { |
| 20 | + Write-Debug "ProcessBlock Aspect: $($this.ProcessBlockTemplate)" |
| 21 | + } |
| 22 | + if (!($this.EndBlockTemplate = $this.Aspect.Find({ $args[0] -is [NamedBlockAst] -and $args[0].BlockKind -eq "End" }, $false))) { |
| 23 | + Write-Debug "No Aspect for EndBlock" |
| 24 | + } else { |
| 25 | + Write-Debug "EndBlock Aspect: $($this.EndBlockTemplate)" |
| 26 | + } |
| 27 | + |
| 28 | + $ast.Visit($this) |
| 29 | + return $this.Replacements |
| 30 | + } |
| 31 | + |
| 32 | + # The [Alias(...)] attribute on functions matters, but we can't export aliases that are defined inside a function |
| 33 | + [AstVisitAction] VisitFunctionDefinition([FunctionDefinitionAst]$ast) { |
| 34 | + if (!$ast.Where($this.Where)) { |
| 35 | + return [AstVisitAction]::SkipChildren |
| 36 | + } |
| 37 | + |
| 38 | + if ($this.BeginBlockTemplate) { |
| 39 | + if ($ast.Body.BeginBlock) { |
| 40 | + $BeginExtent = $ast.Body.BeginBlock.Extent |
| 41 | + $BeginBlockText = ($BeginExtent.Text -replace "^begin[\s\r\n]*{|}[\s\r\n]*$", "`n").Trim("`r`n").TrimEnd("`r`n ") |
| 42 | + |
| 43 | + $Replacement = [TextReplace]@{ |
| 44 | + StartOffset = $BeginExtent.StartOffset |
| 45 | + EndOffset = $BeginExtent.EndOffset |
| 46 | + Text = $this.BeginBlockTemplate.Extent.Text.Replace("existingcode", $BeginBlockText) |
| 47 | + } |
| 48 | + |
| 49 | + $this.Replacements.Add( $Replacement ) |
| 50 | + } else { |
| 51 | + Write-Debug "$($ast.Name) Missing BeginBlock" |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if ($this.ProcessBlockTemplate) { |
| 56 | + if ($ast.Body.ProcessBlock) { |
| 57 | + # In a "filter" function, the process block may contain the param block |
| 58 | + $ProcessBlockExtent = $ast.Body.ProcessBlock.Extent |
| 59 | + |
| 60 | + if ($ast.Body.ProcessBlock.UnNamed -and $ast.Body.ParamBlock.Extent.Text) { |
| 61 | + # Trim the paramBlock out of the end block |
| 62 | + $ProcessBlockText = $ProcessBlockExtent.Text.Remove( |
| 63 | + $ast.Body.ParamBlock.Extent.StartOffset - $ProcessBlockExtent.StartOffset, |
| 64 | + $ast.Body.ParamBlock.Extent.EndOffset - $ast.Body.ParamBlock.Extent.StartOffset) |
| 65 | + $StartOffset = $ast.Body.ParamBlock.Extent.EndOffset |
| 66 | + } else { |
| 67 | + # Trim the `process {` ... `}` because we're inserting it into the template process |
| 68 | + $ProcessBlockText = ($ProcessBlockExtent.Text -replace "^process[\s\r\n]*{|}[\s\r\n]*$", "`n").Trim("`r`n").TrimEnd("`r`n ") |
| 69 | + $StartOffset = $ProcessBlockExtent.StartOffset |
| 70 | + } |
| 71 | + |
| 72 | + $Replacement = [TextReplace]@{ |
| 73 | + StartOffset = $StartOffset |
| 74 | + EndOffset = $ProcessBlockExtent.EndOffset |
| 75 | + Text = $this.ProcessBlockTemplate.Extent.Text.Replace("existingcode", $ProcessBlockText) |
| 76 | + } |
| 77 | + |
| 78 | + $this.Replacements.Add( $Replacement ) |
| 79 | + } else { |
| 80 | + Write-Debug "$($ast.Name) Missing ProcessBlock" |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if ($this.EndBlockTemplate) { |
| 85 | + if ($ast.Body.EndBlock) { |
| 86 | + # The end block is a problem because it frequently contains the param block, which must be left alone |
| 87 | + $EndBlockExtent = $ast.Body.EndBlock.Extent |
| 88 | + |
| 89 | + $EndBlockText = $EndBlockExtent.Text |
| 90 | + $StartOffset = $EndBlockExtent.StartOffset |
| 91 | + if ($ast.Body.EndBlock.UnNamed -and $ast.Body.ParamBlock.Extent.Text) { |
| 92 | + # Trim the paramBlock out of the end block |
| 93 | + $EndBlockText = $EndBlockExtent.Text.Remove( |
| 94 | + $ast.Body.ParamBlock.Extent.StartOffset - $EndBlockExtent.StartOffset, |
| 95 | + $ast.Body.ParamBlock.Extent.EndOffset - $ast.Body.ParamBlock.Extent.StartOffset) |
| 96 | + $StartOffset = $ast.Body.ParamBlock.Extent.EndOffset |
| 97 | + } else { |
| 98 | + # Trim the `end {` ... `}` because we're inserting it into the template end |
| 99 | + $EndBlockText = ($EndBlockExtent.Text -replace "^end[\s\r\n]*{|}[\s\r\n]*$", "`n").Trim("`r`n").TrimEnd("`r`n ") |
| 100 | + } |
| 101 | + |
| 102 | + $Replacement = [TextReplace]@{ |
| 103 | + StartOffset = $StartOffset |
| 104 | + EndOffset = $EndBlockExtent.EndOffset |
| 105 | + Text = $this.EndBlockTemplate.Extent.Text.Replace("existingcode", $EndBlockText) |
| 106 | + } |
| 107 | + |
| 108 | + $this.Replacements.Add( $Replacement ) |
| 109 | + } else { |
| 110 | + Write-Debug "$($ast.Name) Missing EndBlock" |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return [AstVisitAction]::SkipChildren |
| 115 | + } |
| 116 | +} |
0 commit comments