@@ -16,7 +16,7 @@ function MoveUsingStatements {
16
16
those changes won't be applied to the file.
17
17
#>
18
18
[CmdletBinding ()]
19
- Param (
19
+ param (
20
20
# Path to the PSM1 file to amend
21
21
[Parameter (Mandatory , ValueFromPipelineByPropertyName , ValueFromPipeline )]
22
22
[System.Management.Automation.Language.Ast ]$AST ,
@@ -27,54 +27,58 @@ function MoveUsingStatements {
27
27
28
28
# The encoding defaults to UTF8 (or UTF8NoBom on Core)
29
29
[Parameter (DontShow)]
30
- [string ]$Encoding = $ (if ($IsCoreCLR ) { " UTF8NoBom" } else { " UTF8" })
30
+ [string ]$Encoding = $ (if ($IsCoreCLR ) {
31
+ " UTF8NoBom"
32
+ } else {
33
+ " UTF8"
34
+ })
31
35
)
36
+ process {
37
+ # Avoid modifying the file if there's no Parsing Error caused by Using Statements or other errors
38
+ if (! $ParseErrors.Where { $_.ErrorId -eq ' UsingMustBeAtStartOfScript' }) {
39
+ Write-Debug " No using statement errors found."
40
+ return
41
+ } else {
42
+ # as decided https://github.com/PoshCode/ModuleBuilder/issues/96
43
+ Write-Debug " Parsing errors found. We'll still attempt to Move using statements."
44
+ }
32
45
33
- # Avoid modifying the file if there's no Parsing Error caused by Using Statements or other errors
34
- if (! $ParseErrors.Where {$_.ErrorId -eq ' UsingMustBeAtStartOfScript' }) {
35
- Write-Debug " No using statement errors found."
36
- return
37
- }
38
-
39
- # as per issue https://github.com/PoshCode/ModuleBuilder/issues/96
40
- if ($ParseErrors.Where {$_.ErrorId -ne ' UsingMustBeAtStartOfScript' }) {
41
- Write-Verbose " Parsing errors found. We'll still attempt to Move using statements."
42
- }
43
-
44
- # Find all Using statements including those non erroring (to conserve their order)
45
- $UsingStatementExtents = $AST.FindAll (
46
- {$Args [0 ] -is [System.Management.Automation.Language.UsingStatementAst ]},
47
- $false
48
- ).Extent
46
+ # Find all Using statements including those non erroring (to conserve their order)
47
+ $UsingStatementExtents = $AST.FindAll (
48
+ { $Args [0 ] -is [System.Management.Automation.Language.UsingStatementAst ] },
49
+ $false
50
+ ).Extent
49
51
50
- # Edit the Script content by commenting out existing statements (conserving line numbering)
51
- $ScriptText = $AST.Extent.Text
52
- $InsertedCharOffset = 0
53
- $StatementsToCopy = New-Object System.Collections.ArrayList
54
- foreach ($UsingSatement in $UsingStatementExtents ) {
55
- $ScriptText = $ScriptText.Insert ($UsingSatement.StartOffset + $InsertedCharOffset , ' #' )
56
- $InsertedCharOffset ++
52
+ # Edit the Script content by commenting out existing statements (conserving line numbering)
53
+ $ScriptText = $AST.Extent.Text
54
+ $InsertedCharOffset = 0
55
+ $StatementsToCopy = New-Object System.Collections.ArrayList
56
+ foreach ($UsingSatement in $UsingStatementExtents ) {
57
+ $ScriptText = $ScriptText.Insert ($UsingSatement.StartOffset + $InsertedCharOffset , ' #' )
58
+ $InsertedCharOffset ++
57
59
58
- # Keep track of unique statements we'll need to insert at the top
59
- if (! $StatementsToCopy.Contains ($UsingSatement.Text )) {
60
- $null = $StatementsToCopy.Add ($UsingSatement.Text )
60
+ # Keep track of unique statements we'll need to insert at the top
61
+ if (! $StatementsToCopy.Contains ($UsingSatement.Text )) {
62
+ $null = $StatementsToCopy.Add ($UsingSatement.Text )
63
+ }
61
64
}
62
- }
63
65
64
- $ScriptText = $ScriptText.Insert (0 , ($StatementsToCopy -join " `r`n " ) + " `r`n " )
65
-
66
- $ParseErrorsAfterMovingUsings = [System.Management.Automation.Language.ParseError []]::new(0 )
66
+ $ScriptText = $ScriptText.Insert (0 , ($StatementsToCopy -join " `r`n " ) + " `r`n " )
67
+ $null = Set-Content - Value $ScriptText - Path $RootModule - Encoding $Encoding
67
68
68
- # Verify we haven't introduced new Parsing errors
69
- $null = [System.Management.Automation.Language.Parser ]::ParseInput (
70
- $ScriptText ,
71
- [ref ]$null ,
72
- [ref ]$ParseErrorsAfterMovingUsings
73
- )
69
+ # Verify we haven't introduced new Parsing errors
70
+ $null = [System.Management.Automation.Language.Parser ]::ParseFile (
71
+ $RootModule ,
72
+ [ref ]$null ,
73
+ [ref ]$ParseErrors
74
+ )
74
75
75
- if ($ParseErrorsAfterMovingUsings.count -gt $ParseErrors.Count ) {
76
- Write-Warning " We introduced parsing error(s) while attempting to move using statements. Cancelling changes."
77
- } else {
78
- $null = Set-Content - Value $ScriptText - Path $RootModule - Encoding $Encoding
76
+ if ($ParseErrors.Count ) {
77
+ $Message = $ParseErrors |
78
+ Format-Table - Auto @ {n = " File" ; expr = { $_.Extent.File | Split-Path - Leaf }},
79
+ @ {n = " Line" ; expr = { $_.Extent.StartLineNumber }},
80
+ Extent, ErrorId, Message | Out-String
81
+ Write-Warning " Parse errors in build output:`n $Message "
82
+ }
79
83
}
80
84
}
0 commit comments