microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/linting/Invoke-PythonLint.ps1
135lines · modecode
| 1 | #!/usr/bin/env pwsh |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | # |
| 5 | # Invoke-PythonLint.ps1 |
| 6 | # |
| 7 | # Purpose: Dynamically discovers and lints Python skills using ruff |
| 8 | # Author: HVE Core Team |
| 9 | |
| 10 | #Requires -Version 7.0 |
| 11 | |
| 12 | [CmdletBinding()] |
| 13 | param( |
| 14 | [Parameter(Mandatory = $false)] |
| 15 | [string]$RepoRoot = (Get-Location).Path, |
| 16 | |
| 17 | [Parameter(Mandatory = $false)] |
| 18 | [string]$OutputPath |
| 19 | ) |
| 20 | |
| 21 | $ErrorActionPreference = 'Stop' |
| 22 | |
| 23 | #region Functions |
| 24 | |
| 25 | function Invoke-PythonLint { |
| 26 | [CmdletBinding()] |
| 27 | param( |
| 28 | [Parameter(Mandatory = $true)] |
| 29 | [string]$RepoRoot, |
| 30 | |
| 31 | [Parameter(Mandatory = $false)] |
| 32 | [string]$OutputPath |
| 33 | ) |
| 34 | |
| 35 | Push-Location $RepoRoot |
| 36 | try { |
| 37 | # Find all directories with pyproject.toml |
| 38 | $pythonSkills = Get-ChildItem -Path . -Filter 'pyproject.toml' -Recurse -Force -File | |
| 39 | Where-Object { $_.FullName -notmatch 'node_modules' } | |
| 40 | ForEach-Object { $_.Directory.FullName } |
| 41 | |
| 42 | if (-not $pythonSkills) { |
| 43 | Write-Host 'No Python skills found (no pyproject.toml files detected)' -ForegroundColor Yellow |
| 44 | return @{ success = $true; skillsChecked = 0; errors = @() } |
| 45 | } |
| 46 | |
| 47 | Write-Host "Found $($pythonSkills.Count) Python skill(s):" -ForegroundColor Cyan |
| 48 | $pythonSkills | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray } |
| 49 | |
| 50 | # Check if ruff is available (once, before loop) |
| 51 | $ruffAvailable = Get-Command ruff -ErrorAction SilentlyContinue |
| 52 | if (-not $ruffAvailable) { |
| 53 | Write-Host '❌ ruff is not installed. Run "uv sync" in a skill directory or install with "uv pip install ruff".' -ForegroundColor Red |
| 54 | return @{ success = $false; skillsChecked = 0; errors = @('ruff not installed') } |
| 55 | } |
| 56 | |
| 57 | $results = @{ |
| 58 | success = $true |
| 59 | skillsChecked = 0 |
| 60 | errors = @() |
| 61 | details = @() |
| 62 | } |
| 63 | |
| 64 | foreach ($skillPath in $pythonSkills) { |
| 65 | Write-Host "`nRunning ruff in $skillPath..." -ForegroundColor Cyan |
| 66 | |
| 67 | Push-Location $skillPath |
| 68 | try { |
| 69 | $output = ruff check . 2>&1 |
| 70 | $exitCode = $LASTEXITCODE |
| 71 | |
| 72 | $result = @{ |
| 73 | path = $skillPath |
| 74 | passed = ($exitCode -eq 0) |
| 75 | output = $output | Out-String |
| 76 | } |
| 77 | |
| 78 | $results.details += $result |
| 79 | $results.skillsChecked++ |
| 80 | |
| 81 | if ($exitCode -ne 0) { |
| 82 | Write-Host "$output" -ForegroundColor Red |
| 83 | Write-Host '❌ Linting issues found' -ForegroundColor Red |
| 84 | $results.success = $false |
| 85 | $results.errors += $skillPath |
| 86 | } else { |
| 87 | if ($output) { |
| 88 | Write-Host "$output" |
| 89 | } |
| 90 | Write-Host '✓ No linting issues' -ForegroundColor Green |
| 91 | } |
| 92 | } catch { |
| 93 | Write-Host "Error running ruff: $_" -ForegroundColor Red |
| 94 | $results.success = $false |
| 95 | $results.errors += "$skillPath - error: $_" |
| 96 | } finally { |
| 97 | Pop-Location |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | # Default to logs directory when no OutputPath specified |
| 102 | if (-not $OutputPath) { |
| 103 | $logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs' |
| 104 | if (-not (Test-Path $logsDir)) { |
| 105 | New-Item -ItemType Directory -Path $logsDir -Force | Out-Null |
| 106 | } |
| 107 | $OutputPath = Join-Path -Path $logsDir -ChildPath 'python-lint-results.json' |
| 108 | } |
| 109 | $results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8 |
| 110 | Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan |
| 111 | |
| 112 | return $results |
| 113 | } finally { |
| 114 | Pop-Location |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | #endregion |
| 119 | |
| 120 | #region Main Execution |
| 121 | |
| 122 | # Don't run main logic if dot-sourced for testing |
| 123 | if ($MyInvocation.InvocationName -ne '.') { |
| 124 | $result = Invoke-PythonLint -RepoRoot $RepoRoot -OutputPath $OutputPath |
| 125 | |
| 126 | if ($result.success) { |
| 127 | Write-Host "`n✅ All Python skills passed linting" -ForegroundColor Green |
| 128 | exit 0 |
| 129 | } else { |
| 130 | Write-Host "`n❌ Linting completed with errors" -ForegroundColor Red |
| 131 | exit 1 |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | #endregion |
| 136 | |