microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/linting/Invoke-PSScriptAnalyzer.ps1
166lines Ā· modecode
| 1 | #!/usr/bin/env pwsh |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | # |
| 5 | # Invoke-PSScriptAnalyzer.ps1 |
| 6 | # |
| 7 | # Purpose: Wrapper for PSScriptAnalyzer with GitHub Actions integration |
| 8 | # Author: HVE Core Team |
| 9 | |
| 10 | #Requires -Version 7.0 |
| 11 | |
| 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 | |
| 27 | $ErrorActionPreference = 'Stop' |
| 28 | |
| 29 | # Import shared helpers |
| 30 | Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force |
| 31 | Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force |
| 32 | |
| 33 | Write-Host "š Running PSScriptAnalyzer..." -ForegroundColor Cyan |
| 34 | |
| 35 | # Ensure PSScriptAnalyzer is available |
| 36 | if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { |
| 37 | Write-Host "Installing PSScriptAnalyzer module..." -ForegroundColor Yellow |
| 38 | Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser -Repository PSGallery |
| 39 | } |
| 40 | |
| 41 | Import-Module PSScriptAnalyzer |
| 42 | |
| 43 | # Get files to analyze |
| 44 | $filesToAnalyze = @() |
| 45 | |
| 46 | if ($ChangedFilesOnly) { |
| 47 | Write-Host "Detecting changed PowerShell files..." -ForegroundColor Cyan |
| 48 | $filesToAnalyze = @(Get-ChangedFilesFromGit -BaseBranch $BaseBranch -FileExtensions @('*.ps1', '*.psm1', '*.psd1')) |
| 49 | } |
| 50 | else { |
| 51 | Write-Host "Analyzing all PowerShell files..." -ForegroundColor Cyan |
| 52 | $gitignorePath = Join-Path (git rev-parse --show-toplevel 2>$null) ".gitignore" |
| 53 | $filesToAnalyze = @(Get-FilesRecursive -Path "." -Include @('*.ps1', '*.psm1', '*.psd1') -GitIgnorePath $gitignorePath) |
| 54 | } |
| 55 | |
| 56 | if (@($filesToAnalyze).Count -eq 0) { |
| 57 | Write-Host "ā
No PowerShell files to analyze" -ForegroundColor Green |
| 58 | Set-CIOutput -Name "count" -Value "0" |
| 59 | Set-CIOutput -Name "issues" -Value "0" |
| 60 | exit 0 |
| 61 | } |
| 62 | |
| 63 | Write-Host "Analyzing $($filesToAnalyze.Count) PowerShell files..." -ForegroundColor Cyan |
| 64 | Set-CIOutput -Name "count" -Value $filesToAnalyze.Count |
| 65 | |
| 66 | #region Main Execution |
| 67 | try { |
| 68 | # Run PSScriptAnalyzer |
| 69 | $allResults = @() |
| 70 | $hasErrors = $false |
| 71 | |
| 72 | foreach ($file in $filesToAnalyze) { |
| 73 | $filePath = if ($file -is [System.IO.FileInfo]) { $file.FullName } else { $file } |
| 74 | Write-Host "`nš Analyzing: $filePath" -ForegroundColor Cyan |
| 75 | |
| 76 | $results = Invoke-ScriptAnalyzer -Path $filePath -Settings $ConfigPath |
| 77 | |
| 78 | if ($results) { |
| 79 | $allResults += $results |
| 80 | |
| 81 | foreach ($result in $results) { |
| 82 | $annotationLevel = switch ($result.Severity) { |
| 83 | 'Error' { 'Error' } |
| 84 | 'Warning' { 'Warning' } |
| 85 | 'Information' { 'Notice' } |
| 86 | default { 'Notice' } |
| 87 | } |
| 88 | |
| 89 | Write-CIAnnotation ` |
| 90 | -Message "$($result.RuleName): $($result.Message)" ` |
| 91 | -Level $annotationLevel ` |
| 92 | -File $filePath ` |
| 93 | -Line $result.Line ` |
| 94 | -Column $result.Column |
| 95 | |
| 96 | $icon = switch ($result.Severity) { |
| 97 | 'Error' { 'ā'; $hasErrors = $true } |
| 98 | 'Warning' { 'ā ļø' } |
| 99 | default { 'ā¹ļø' } |
| 100 | } |
| 101 | |
| 102 | Write-Host " $icon [$($result.Severity)] $($result.RuleName): $($result.Message) (Line $($result.Line))" -ForegroundColor $( |
| 103 | if ($result.Severity -eq 'Error') { 'Red' } |
| 104 | elseif ($result.Severity -eq 'Warning') { 'Yellow' } |
| 105 | else { 'Cyan' } |
| 106 | ) |
| 107 | } |
| 108 | } |
| 109 | else { |
| 110 | Write-Host " ā
No issues found" -ForegroundColor Green |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | # Export results |
| 115 | $summary = @{ |
| 116 | TotalFiles = @($filesToAnalyze).Count |
| 117 | TotalIssues = @($allResults).Count |
| 118 | Errors = @($allResults | Where-Object Severity -eq 'Error').Count |
| 119 | Warnings = @($allResults | Where-Object Severity -eq 'Warning').Count |
| 120 | Information = @($allResults | Where-Object Severity -eq 'Information').Count |
| 121 | HasErrors = $hasErrors |
| 122 | } |
| 123 | |
| 124 | $allResults | ConvertTo-Json -Depth 5 | Out-File $OutputPath |
| 125 | $summary | ConvertTo-Json | Out-File "logs/psscriptanalyzer-summary.json" |
| 126 | |
| 127 | # Set outputs |
| 128 | Set-CIOutput -Name "issues" -Value $summary.TotalIssues |
| 129 | Set-CIOutput -Name "errors" -Value $summary.Errors |
| 130 | Set-CIOutput -Name "warnings" -Value $summary.Warnings |
| 131 | |
| 132 | if ($hasErrors) { |
| 133 | Set-CIEnv -Name "PSSCRIPTANALYZER_FAILED" -Value "true" |
| 134 | } |
| 135 | |
| 136 | # Write summary |
| 137 | Write-CIStepSummary -Content "## PSScriptAnalyzer Results`n" |
| 138 | |
| 139 | if ($summary.TotalIssues -eq 0) { |
| 140 | Write-CIStepSummary -Content "ā
**Status**: Passed`n`nAll $($summary.TotalFiles) PowerShell files passed linting checks." |
| 141 | Write-Host "`nā
All PowerShell files passed PSScriptAnalyzer checks!" -ForegroundColor Green |
| 142 | exit 0 |
| 143 | } |
| 144 | else { |
| 145 | Write-CIStepSummary -Content @" |
| 146 | ā **Status**: Failed |
| 147 | |
| 148 | | Metric | Count | |
| 149 | |--------|-------| |
| 150 | | Files Analyzed | $($summary.TotalFiles) | |
| 151 | | Total Issues | $($summary.TotalIssues) | |
| 152 | | Errors | $($summary.Errors) | |
| 153 | | Warnings | $($summary.Warnings) | |
| 154 | | Information | $($summary.Information) | |
| 155 | "@ |
| 156 | |
| 157 | Write-Host "`nā PSScriptAnalyzer found $($summary.TotalIssues) issue(s)" -ForegroundColor Red |
| 158 | exit 1 |
| 159 | } |
| 160 | } |
| 161 | catch { |
| 162 | Write-Error -ErrorAction Continue "PSScriptAnalyzer failed: $($_.Exception.Message)" |
| 163 | Write-CIAnnotation -Message "PSScriptAnalyzer failed: $($_.Exception.Message)" -Level Error |
| 164 | exit 1 |
| 165 | } |
| 166 | #endregion |
| 167 | |