|
| 1 | +using namespace System.Management.Automation.Language |
| 2 | + |
| 3 | +function Move-UsingStatement { |
| 4 | + <# |
| 5 | + .SYNOPSIS |
| 6 | + A Script Generator that commetnts out using statements and copies them to the top of the file |
| 7 | + .DESCRIPTION |
| 8 | + Move-UsingStatement supports having using statements repeated in multiple files that are merged by ModuleBuilder. |
| 9 | + When all the files are merged together, the using statements from individual files |
| 10 | + don't necessarily end up at the beginning of the PSM1, which creates Parsing Errors. |
| 11 | +
|
| 12 | + This function uses the AST to generate TextReplacements to: |
| 13 | + 1. Comment out the original using statements (to preserve line numbering) |
| 14 | + 2. Insert the using statements (conserving order, but removing duplicates) at the top of the script |
| 15 | + #> |
| 16 | + [CmdletBinding()] |
| 17 | + [OutputType([TextReplacement])] |
| 18 | + param( |
| 19 | + # The AST of the original script module to refactor |
| 20 | + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] |
| 21 | + [Alias("Ast")] |
| 22 | + [Ast]$InputObject, |
| 23 | + |
| 24 | + # Parser Errors from parsing the original script module |
| 25 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 26 | + [AllowNull()] |
| 27 | + [ParseError[]]$ParseErrors |
| 28 | + ) |
| 29 | + process { |
| 30 | + # Avoid modifying the file if there's no Parsing Error caused by Using Statements or other errors |
| 31 | + if (!$ParseErrors.Where{ $_.ErrorId -eq 'UsingMustBeAtStartOfScript' }) { |
| 32 | + Write-Debug "No using statement errors found." |
| 33 | + return |
| 34 | + } else { |
| 35 | + # as decided https://github.com/PoshCode/ModuleBuilder/issues/96 |
| 36 | + Write-Debug "Parsing errors found. We'll still attempt to Move using statements." |
| 37 | + } |
| 38 | + |
| 39 | + # Find all Using statements including those non erroring (to conserve their order) |
| 40 | + $UsingStatementExtents = $InputObject.FindAll( |
| 41 | + { $Args[0] -is [System.Management.Automation.Language.UsingStatementAst] }, |
| 42 | + $false |
| 43 | + ).Extent |
| 44 | + |
| 45 | + # Edit the Script content by commenting out existing statements (conserving line numbering) |
| 46 | + $StatementsToCopy = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase) |
| 47 | + |
| 48 | + foreach ($UsingSatement in $UsingStatementExtents) { |
| 49 | + [TextReplacement]@{ |
| 50 | + StartOffset = $UsingSatement.StartOffset |
| 51 | + EndOffset = $UsingSatement.EndOffset |
| 52 | + Text = '# ' + $UsingSatement.Text |
| 53 | + } |
| 54 | + # Keep track of unique statements we'll need to insert at the top |
| 55 | + $null = $StatementsToCopy.Add($UsingSatement.Text) |
| 56 | + } |
| 57 | + if ($StatementsToCopy.Count -gt 0) { |
| 58 | + [TextReplacement]@{ |
| 59 | + StartOffset = 0 |
| 60 | + EndOffset = 0 |
| 61 | + Text = ($StatementsToCopy -join "`r`n")+ "`r`n" |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments