Skip to content

Commit 33fc570

Browse files
committed
🔧 [build] Improve Script Analyzer integration for better error handling
- Enhanced error handling in Script Analyzer to manage known issues with custom settings. - Refactored code to collect and display all findings from analyzed module files. Signed-off-by: Nick2bad4u <20943337+Nick2bad4u@users.noreply.github.com>
1 parent 35948cb commit 33fc570

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

.github/workflows/publish.yml

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,50 @@ jobs:
6868
- name: Run Script Analyzer
6969
run: |
7070
Import-Module PSScriptAnalyzer
71-
$results = Invoke-ScriptAnalyzer -Path ./ColorScripts-Enhanced -Recurse -Settings ./PSScriptAnalyzerSettings.psd1 -Severity 'Error','Warning' -ReportSummary
72-
if ($results) {
73-
$results | Format-Table -AutoSize
74-
throw 'ScriptAnalyzer reported findings.'
71+
$settingsPath = Join-Path (Get-Location) 'PSScriptAnalyzerSettings.psd1'
72+
$moduleRoot = Join-Path (Get-Location) 'ColorScripts-Enhanced'
73+
$moduleFiles = Get-ChildItem -Path $moduleRoot -File -Recurse -Include *.ps1, *.psm1, *.psd1
74+
75+
$allFindings = New-Object 'System.Collections.Generic.List[psobject]'
76+
77+
foreach ($file in $moduleFiles) {
78+
$parameters = @{
79+
Path = $file.FullName
80+
Severity = 'Error', 'Warning'
81+
ErrorAction = 'Stop'
82+
}
83+
84+
if (Test-Path $settingsPath) {
85+
$parameters.Settings = $settingsPath
86+
}
87+
88+
try {
89+
$diagnostics = Invoke-ScriptAnalyzer @parameters
90+
}
91+
catch {
92+
$exception = $_.Exception
93+
$isNullReference = $exception -is [System.NullReferenceException] -or ($exception -and $exception.Message -like 'Object reference*')
94+
95+
if ($isNullReference -and $parameters.ContainsKey('Settings')) {
96+
Write-Warning "ScriptAnalyzer hit a known issue on '$($file.FullName)' with custom settings. Retrying without settings."
97+
$parameters.Remove('Settings')
98+
$diagnostics = Invoke-ScriptAnalyzer @parameters
99+
}
100+
else {
101+
throw
102+
}
103+
}
104+
105+
if ($diagnostics) {
106+
foreach ($entry in $diagnostics) {
107+
$allFindings.Add($entry) | Out-Null
108+
}
109+
}
110+
}
111+
112+
if ($allFindings.Count -gt 0) {
113+
$allFindings | Format-Table -AutoSize
114+
throw 'ScriptAnalyzer reported findings.'
75115
}
76116
shell: pwsh
77117

0 commit comments

Comments
 (0)