microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/linting/Invoke-PSScriptAnalyzer.ps1
206lines · modeblame
66be1367Bill Berry7 months ago | 1 | #!/usr/bin/env pwsh |
d19c9b3alittleKitchen5 months ago | 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT | |
66be1367Bill Berry7 months ago | 4 | # |
| 5 | # Invoke-PSScriptAnalyzer.ps1 | |
| 6 | # | |
| 7 | # Purpose: Wrapper for PSScriptAnalyzer with GitHub Actions integration | |
| 8 | # Author: HVE Core Team | |
| 9 | | |
6e262826little Kitchen5 months ago | 10 | #Requires -Version 7.0 |
| 11 | | |
66be1367Bill Berry7 months ago | 12 | [CmdletBinding()] |
| 13 | param( | |
| 14 | [Parameter(Mandatory = $false)] | |
| 15 | [switch]$ChangedFilesOnly, | |
| 16 | | |
| 17 | [Parameter(Mandatory = $false)] | |
| 18 | [string]$BaseBranch = "origin/main", | |
| 19 | | |
| 20 | [Parameter(Mandatory = $false)] | |
| 21 | [string]$ConfigPath = (Join-Path $PSScriptRoot "PSScriptAnalyzer.psd1"), | |
| 22 | | |
| 23 | [Parameter(Mandatory = $false)] | |
| 24 | [string]$OutputPath = "logs/psscriptanalyzer-results.json" | |
| 25 | ) | |
| 26 | | |
6e262826little Kitchen5 months ago | 27 | $ErrorActionPreference = 'Stop' |
| 28 | | |
66be1367Bill Berry7 months ago | 29 | # Import shared helpers |
| 30 | Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force | |
fdd75ed7Bill Berry5 months ago | 31 | Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force |
66be1367Bill Berry7 months ago | 32 | |
6b84a8e4Bill Berry4 months ago | 33 | #region Functions |
66be1367Bill Berry7 months ago | 34 | |
6b84a8e4Bill Berry4 months ago | 35 | function Invoke-PSScriptAnalyzerCore { |
| 36 | [CmdletBinding()] | |
| 37 | [OutputType([void])] | |
| 38 | param( | |
| 39 | [Parameter(Mandatory = $false)] | |
| 40 | [switch]$ChangedFilesOnly, | |
66be1367Bill Berry7 months ago | 41 | |
6b84a8e4Bill Berry4 months ago | 42 | [Parameter(Mandatory = $false)] |
| 43 | [string]$BaseBranch = "origin/main", | |
66be1367Bill Berry7 months ago | 44 | |
6b84a8e4Bill Berry4 months ago | 45 | [Parameter(Mandatory = $false)] |
| 46 | [string]$ConfigPath = (Join-Path $PSScriptRoot "PSScriptAnalyzer.psd1"), | |
66be1367Bill Berry7 months ago | 47 | |
6b84a8e4Bill Berry4 months ago | 48 | [Parameter(Mandatory = $false)] |
| 49 | [string]$OutputPath = "logs/psscriptanalyzer-results.json" | |
| 50 | ) | |
66be1367Bill Berry7 months ago | 51 | |
6b84a8e4Bill Berry4 months ago | 52 | Write-Host "🔍 Running PSScriptAnalyzer..." -ForegroundColor Cyan |
66be1367Bill Berry7 months ago | 53 | |
6b84a8e4Bill Berry4 months ago | 54 | # Ensure PSScriptAnalyzer is available |
| 55 | if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { | |
| 56 | Write-Host "Installing PSScriptAnalyzer module..." -ForegroundColor Yellow | |
| 57 | Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser -Repository PSGallery | |
| 58 | } | |
| 59 | | |
| 60 | Import-Module PSScriptAnalyzer | |
| 61 | | |
| 62 | # Get files to analyze | |
| 63 | $filesToAnalyze = @() | |
| 64 | | |
| 65 | if ($ChangedFilesOnly) { | |
| 66 | Write-Host "Detecting changed PowerShell files..." -ForegroundColor Cyan | |
| 67 | $filesToAnalyze = @(Get-ChangedFilesFromGit -BaseBranch $BaseBranch -FileExtensions @('*.ps1', '*.psm1', '*.psd1')) | |
| 68 | } | |
| 69 | else { | |
| 70 | Write-Host "Analyzing all PowerShell files..." -ForegroundColor Cyan | |
f120b93bBill Berry4 months ago | 71 | $filesToAnalyze = @(Get-FilesRecursive -Path "." -Include @('*.ps1', '*.psm1', '*.psd1')) |
6b84a8e4Bill Berry4 months ago | 72 | } |
| 73 | | |
| 74 | if (@($filesToAnalyze).Count -eq 0) { | |
| 75 | Write-Host "✅ No PowerShell files to analyze" -ForegroundColor Green | |
| 76 | Set-CIOutput -Name "count" -Value "0" | |
| 77 | Set-CIOutput -Name "issues" -Value "0" | |
| 78 | return | |
| 79 | } | |
| 80 | | |
| 81 | Write-Host "Analyzing $($filesToAnalyze.Count) PowerShell files..." -ForegroundColor Cyan | |
| 82 | Set-CIOutput -Name "count" -Value $filesToAnalyze.Count | |
66be1367Bill Berry7 months ago | 83 | |
35c44178o75 months ago | 84 | # Run PSScriptAnalyzer |
| 85 | $allResults = @() | |
| 86 | $hasErrors = $false | |
| 87 | | |
| 88 | foreach ($file in $filesToAnalyze) { | |
| 89 | $filePath = if ($file -is [System.IO.FileInfo]) { $file.FullName } else { $file } | |
| 90 | Write-Host "`n📄 Analyzing: $filePath" -ForegroundColor Cyan | |
66be1367Bill Berry7 months ago | 91 | |
35c44178o75 months ago | 92 | $results = Invoke-ScriptAnalyzer -Path $filePath -Settings $ConfigPath |
| 93 | | |
| 94 | if ($results) { | |
| 95 | $allResults += $results | |
66be1367Bill Berry7 months ago | 96 | |
35c44178o75 months ago | 97 | foreach ($result in $results) { |
3587e6abBill Berry5 months ago | 98 | $annotationLevel = switch ($result.Severity) { |
| 99 | 'Error' { 'Error' } | |
| 100 | 'Warning' { 'Warning' } | |
| 101 | 'Information' { 'Notice' } | |
| 102 | default { 'Notice' } | |
| 103 | } | |
| 104 | | |
| 105 | Write-CIAnnotation ` | |
35c44178o75 months ago | 106 | -Message "$($result.RuleName): $($result.Message)" ` |
3587e6abBill Berry5 months ago | 107 | -Level $annotationLevel ` |
35c44178o75 months ago | 108 | -File $filePath ` |
| 109 | -Line $result.Line ` | |
| 110 | -Column $result.Column | |
| 111 | | |
| 112 | $icon = switch ($result.Severity) { | |
| 113 | 'Error' { '❌'; $hasErrors = $true } | |
| 114 | 'Warning' { '⚠️' } | |
| 115 | default { 'ℹ️' } | |
| 116 | } | |
| 117 | | |
| 118 | Write-Host " $icon [$($result.Severity)] $($result.RuleName): $($result.Message) (Line $($result.Line))" -ForegroundColor $( | |
| 119 | if ($result.Severity -eq 'Error') { 'Red' } | |
| 120 | elseif ($result.Severity -eq 'Warning') { 'Yellow' } | |
| 121 | else { 'Cyan' } | |
| 122 | ) | |
66be1367Bill Berry7 months ago | 123 | } |
35c44178o75 months ago | 124 | } |
| 125 | else { | |
| 126 | Write-Host " ✅ No issues found" -ForegroundColor Green | |
66be1367Bill Berry7 months ago | 127 | } |
| 128 | } | |
| 129 | | |
35c44178o75 months ago | 130 | # Export results |
| 131 | $summary = @{ | |
de43e73eKatrien De Graeve5 months ago | 132 | TotalFiles = @($filesToAnalyze).Count |
| 133 | TotalIssues = @($allResults).Count | |
| 134 | Errors = @($allResults | Where-Object Severity -eq 'Error').Count | |
| 135 | Warnings = @($allResults | Where-Object Severity -eq 'Warning').Count | |
| 136 | Information = @($allResults | Where-Object Severity -eq 'Information').Count | |
35c44178o75 months ago | 137 | HasErrors = $hasErrors |
| 138 | } | |
66be1367Bill Berry7 months ago | 139 | |
67585f5aAndrew Vineyard4 months ago | 140 | # Ensure logs directory exists |
| 141 | $logsDir = Split-Path $OutputPath -Parent | |
| 142 | if (-not (Test-Path $logsDir)) { | |
| 143 | New-Item -ItemType Directory -Force -Path $logsDir | Out-Null | |
| 144 | } | |
| 145 | | |
35c44178o75 months ago | 146 | $allResults | ConvertTo-Json -Depth 5 | Out-File $OutputPath |
67585f5aAndrew Vineyard4 months ago | 147 | $summary | ConvertTo-Json | Out-File (Join-Path $logsDir "psscriptanalyzer-summary.json") |
66be1367Bill Berry7 months ago | 148 | |
35c44178o75 months ago | 149 | # Set outputs |
3587e6abBill Berry5 months ago | 150 | Set-CIOutput -Name "issues" -Value $summary.TotalIssues |
| 151 | Set-CIOutput -Name "errors" -Value $summary.Errors | |
| 152 | Set-CIOutput -Name "warnings" -Value $summary.Warnings | |
66be1367Bill Berry7 months ago | 153 | |
35c44178o75 months ago | 154 | if ($hasErrors) { |
3587e6abBill Berry5 months ago | 155 | Set-CIEnv -Name "PSSCRIPTANALYZER_FAILED" -Value "true" |
35c44178o75 months ago | 156 | } |
66be1367Bill Berry7 months ago | 157 | |
35c44178o75 months ago | 158 | # Write summary |
3587e6abBill Berry5 months ago | 159 | Write-CIStepSummary -Content "## PSScriptAnalyzer Results`n" |
66be1367Bill Berry7 months ago | 160 | |
35c44178o75 months ago | 161 | if ($summary.TotalIssues -eq 0) { |
3587e6abBill Berry5 months ago | 162 | Write-CIStepSummary -Content "✅ **Status**: Passed`n`nAll $($summary.TotalFiles) PowerShell files passed linting checks." |
35c44178o75 months ago | 163 | Write-Host "`n✅ All PowerShell files passed PSScriptAnalyzer checks!" -ForegroundColor Green |
6b84a8e4Bill Berry4 months ago | 164 | return |
35c44178o75 months ago | 165 | } |
| 166 | else { | |
3587e6abBill Berry5 months ago | 167 | Write-CIStepSummary -Content @" |
66be1367Bill Berry7 months ago | 168 | ❌ **Status**: Failed |
| 169 | | |
| 170 | | Metric | Count | | |
| 171 | |--------|-------| | |
| 172 | | Files Analyzed | $($summary.TotalFiles) | | |
| 173 | | Total Issues | $($summary.TotalIssues) | | |
| 174 | | Errors | $($summary.Errors) | | |
| 175 | | Warnings | $($summary.Warnings) | | |
| 176 | | Information | $($summary.Information) | | |
| 177 | "@ | |
| 178 | | |
35c44178o75 months ago | 179 | Write-Host "`n❌ PSScriptAnalyzer found $($summary.TotalIssues) issue(s)" -ForegroundColor Red |
6b84a8e4Bill Berry4 months ago | 180 | throw "PSScriptAnalyzer found $($summary.TotalIssues) issue(s)" |
35c44178o75 months ago | 181 | } |
| 182 | } | |
6b84a8e4Bill Berry4 months ago | 183 | |
| 184 | #endregion Functions | |
| 185 | | |
| 186 | #region Main Execution | |
| 187 | | |
| 188 | if ($MyInvocation.InvocationName -ne '.') { | |
f120b93bBill Berry4 months ago | 189 | # Strip /mnt/* paths from PATH to avoid slow 9P cross-filesystem |
| 190 | # lookups in WSL. PSScriptAnalyzer resolves commands by scanning every | |
| 191 | # PATH directory per file; Windows mount points add ~40s per file. | |
| 192 | $env:PATH = ($env:PATH -split [System.IO.Path]::PathSeparator | | |
| 193 | Where-Object { $_ -notlike '/mnt/*' }) -join [System.IO.Path]::PathSeparator | |
| 194 | | |
6b84a8e4Bill Berry4 months ago | 195 | try { |
| 196 | Invoke-PSScriptAnalyzerCore -ChangedFilesOnly:$ChangedFilesOnly -BaseBranch $BaseBranch -ConfigPath $ConfigPath -OutputPath $OutputPath | |
| 197 | exit 0 | |
| 198 | } | |
| 199 | catch { | |
| 200 | Write-Error -ErrorAction Continue "PSScriptAnalyzer failed: $($_.Exception.Message)" | |
| 201 | Write-CIAnnotation -Message $_.Exception.Message -Level Error | |
| 202 | exit 1 | |
| 203 | } | |
66be1367Bill Berry7 months ago | 204 | } |
6b84a8e4Bill Berry4 months ago | 205 | |
| 206 | #endregion Main Execution |