Skip to content

Commit 6a04eba

Browse files
committed
Move using statements even if the module's broken
1 parent 0b1e851 commit 6a04eba

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

Source/Private/GetBuildInfo.ps1

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ function GetBuildInfo {
6565
}
6666
# BuildInfo.SourcePath should point to a module manifest
6767
if ($BuildInfo.SourcePath -and $BuildInfo.SourcePath -ne $BuildManifest) {
68+
Write-Debug " Updating: SourcePath"
69+
Write-Debug " To: $($BuildInfo.SourcePath)"
6870
$ParameterValues["SourcePath"] = $BuildInfo.SourcePath
6971
}
7072
# If SourcePath point to build.psd1, we should clear it
7173
if ($ParameterValues["SourcePath"] -eq $BuildManifest) {
74+
Write-Debug " Removing: SourcePath"
7275
$ParameterValues.Remove("SourcePath")
7376
}
7477
Write-Debug "Finished parsing Build Manifest $BuildManifest"
@@ -80,25 +83,31 @@ function GetBuildInfo {
8083
}
8184

8285
if ((-not $BuildInfo.SourcePath) -and $ParameterValues["SourcePath"] -notmatch '\.psd1') {
86+
Write-Debug " Searching: SourcePath"
8387
# Find a module manifest (or maybe several)
8488
$ModuleInfo = Get-ChildItem $BuildManifestParent -Recurse -Filter *.psd1 -ErrorAction SilentlyContinue |
8589
ImportModuleManifest -ErrorAction SilentlyContinue
8690
# If we found more than one module info, the only way we have of picking just one is if it matches a folder name
8791
if (@($ModuleInfo).Count -gt 1) {
88-
# Resolve Build Manifest's parent folder to find the Absolute path
89-
$ModuleName = Split-Path -Leaf $BuildManifestParent
90-
# If we're in a "well known" source folder, look higher for a name
91-
if ($ModuleName -in 'Source', 'src') {
92-
$ModuleName = Split-Path (Split-Path -Parent $BuildManifestParent) -Leaf
92+
Write-Debug (@(@(" Found $(@($ModuleInfo).Count):") + @($ModuleInfo.Path)) -join "`n ")
93+
# It can't be a module that needs building unless it has either:
94+
$ModuleInfo = $ModuleInfo.Where{
95+
$Root = Split-Path $_.Path
96+
@(
97+
# - A build.psd1 next to it
98+
Test-Path (Join-Path $Root "build.ps1") -PathType Leaf
99+
# - A Public (or Private) folder with source scripts in it
100+
Test-Path (Join-Path $Root "Public") -PathType Container
101+
Test-Path (Join-Path $Root "Private") -PathType Container
102+
) -contains $true
93103
}
94-
$ModuleInfo = @($ModuleInfo).Where{ $_.Name -eq $ModuleName }
104+
Write-Debug (@(@(" Filtered $(@($ModuleInfo).Count):") + @($ModuleInfo.Path)) -join "`n ")
95105
}
96106
if (@($ModuleInfo).Count -eq 1) {
97107
Write-Debug "Updating BuildInfo SourcePath to $($ModuleInfo.Path)"
98108
$ParameterValues["SourcePath"] = $ModuleInfo.Path
99-
}
100-
if (-Not $ModuleInfo) {
101-
throw "Can't find a module manifest in $BuildManifestParent"
109+
} else {
110+
throw "Can't determine the module manifest in $BuildManifestParent"
102111
}
103112
}
104113

Source/Private/MoveUsingStatements.ps1

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ function MoveUsingStatements {
88
99
This function uses AST to comment out those statements (to preserver line numbering)
1010
and insert them (conserving order) at the top of the script.
11-
12-
Should the merged RootModule already have errors not related to the Using statements,
13-
or no errors caused by misplaced Using statements, this steps is skipped.
14-
15-
If moving (comment & copy) the Using statements introduce parsing errors to the script,
16-
those changes won't be applied to the file.
1711
#>
1812
[CmdletBinding()]
1913
param(
@@ -52,15 +46,14 @@ function MoveUsingStatements {
5246
# Edit the Script content by commenting out existing statements (conserving line numbering)
5347
$ScriptText = $AST.Extent.Text
5448
$InsertedCharOffset = 0
55-
$StatementsToCopy = New-Object System.Collections.ArrayList
49+
$StatementsToCopy = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
50+
5651
foreach ($UsingSatement in $UsingStatementExtents) {
5752
$ScriptText = $ScriptText.Insert($UsingSatement.StartOffset + $InsertedCharOffset, '#')
5853
$InsertedCharOffset++
5954

6055
# 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-
}
56+
$null = $StatementsToCopy.Add($UsingSatement.Text)
6457
}
6558

6659
$ScriptText = $ScriptText.Insert(0, ($StatementsToCopy -join "`r`n") + "`r`n")

Tests/Private/MoveUsingStatements.Tests.ps1

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Describe "MoveUsingStatements" {
2929

3030
$TestCases = @(
3131
@{
32-
TestCaseName = 'Move all using statements in `n terminated files to the top'
32+
TestCaseName = 'Moves all using statements in `n terminated files to the top'
3333
PSM1File = "function x {`n}`n" +
3434
"using namespace System.IO`n`n" + #UsingMustBeAtStartOfScript
3535
"function y {`n}`n" +
@@ -38,7 +38,7 @@ Describe "MoveUsingStatements" {
3838
ErrorAfter = 0
3939
},
4040
@{
41-
TestCaseName = 'Move all using statements in `r`n terminated files to the top'
41+
TestCaseName = 'Moves all using statements in`r`n terminated files to the top'
4242
PSM1File = "function x {`r`n}`r`n" +
4343
"USING namespace System.IO`r`n`r`n" + #UsingMustBeAtStartOfScript
4444
"function y {`r`n}`r`n" +
@@ -47,16 +47,32 @@ Describe "MoveUsingStatements" {
4747
ErrorAfter = 0
4848
},
4949
@{
50-
TestCaseName = 'Not change the content again if there are no out-of-place using statements'
50+
TestCaseName = 'Prevents duplicate using statements'
51+
PSM1File = "using namespace System.IO`r`n" + #UsingMustBeAtStartOfScript
52+
"function x {`r`n}`r`n`r`n" +
53+
"using namespace System.IO`r`n" + #UsingMustBeAtStartOfScript
54+
"function y {`r`n}`r`n" +
55+
"USING namespace System.IO" #UsingMustBeAtStartOfScript
56+
ExpectedResult = "using namespace System.IO`r`n" +
57+
"#using namespace System.IO`r`n" +
58+
"function x {`r`n}`r`n`r`n" +
59+
"#using namespace System.IO`r`n" +
60+
"function y {`r`n}`r`n" +
61+
"#USING namespace System.IO"
62+
ErrorBefore = 2
63+
ErrorAfter = 0
64+
},
65+
@{
66+
TestCaseName = 'Does not change the content again if there are no out-of-place using statements'
5167
PSM1File = "using namespace System.IO`r`n`r`n" +
5268
"using namespace System.Drawing`r`n" +
53-
"function x { `r`n}`r`n" +
54-
"function y { `r`n}`r`n"
69+
"function x {`r`n}`r`n" +
70+
"function y {`r`n}`r`n"
5571
ErrorBefore = 0
5672
ErrorAfter = 0
5773
},
5874
@{
59-
TestCaseName = 'Move using statements even if types are used'
75+
TestCaseName = 'Moves using statements even if types are used'
6076
PSM1File = "function x {`r`n}`r`n" +
6177
"using namespace System.IO`r`n`r`n" + #UsingMustBeAtStartOfScript
6278
"function y {`r`n}`r`n" +
@@ -66,18 +82,18 @@ Describe "MoveUsingStatements" {
6682
ErrorAfter = 0
6783
},
6884
@{
69-
TestCaseName = 'Move using statements even when there are (other) parse errors'
85+
TestCaseName = 'Moves using statements even when there are (other) parse errors'
7086
PSM1File = "using namespace System.IO`r`n`r`n" +
71-
"function x { `r`n}`r`n" +
87+
"function x {`r`n}`r`n" +
7288
"using namespace System.Drawing`r`n" + # UsingMustBeAtStartOfScript
73-
"function y { `r`n}`r`n}" # Extra } at the end
89+
"function y {`r`n}`r`n}" # Extra } at the end
7490
ErrorBefore = 2
7591
ErrorAfter = 1
7692
}
7793
)
7894

79-
It 'It should <TestCaseName>' -TestCases $TestCases {
80-
param($TestCaseName, $PSM1File, $ErrorBefore, $ErrorAfter)
95+
It '<TestCaseName>' -TestCases $TestCases {
96+
param($TestCaseName, $PSM1File, $ErrorBefore, $ErrorAfter, $ExpectedResult)
8197

8298
$testModuleFile = "$TestDrive/MyModule.psm1"
8399
Set-Content $testModuleFile -value $PSM1File -Encoding UTF8
@@ -99,6 +115,10 @@ Describe "MoveUsingStatements" {
99115
[ref]$ErrorFound
100116
)
101117
$ErrorFound.Count | Should -Be $ErrorAfter
118+
if ($ExpectedResult) {
119+
$ActualResult = Get-Content $testModuleFile -Raw
120+
$ActualResult.Trim() | Should -Be $ExpectedResult -Because "there should be no duplicate using statements in:`n$ActualResult"
121+
}
102122
}
103123
}
104124

0 commit comments

Comments
 (0)