microsoft/hve-core

Public

mirrored from https://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
hve-core-v3.2.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/linting/Invoke-PythonLint.ps1

135lines · modeblame

0a90f573Jason3 months ago1#!/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()]
13param(
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
25function Invoke-PythonLint {
26[CmdletBinding()]
27param(
28[Parameter(Mandatory = $true)]
29[string]$RepoRoot,
30
31[Parameter(Mandatory = $false)]
32[string]$OutputPath
33)
34
35Push-Location $RepoRoot
36try {
37# Find all directories with pyproject.toml
17bbe7a3Bill Berry3 months ago38$pythonSkills = Get-ChildItem -Path . -Filter 'pyproject.toml' -Recurse -Force -File |
0a90f573Jason3 months ago39Where-Object { $_.FullName -notmatch 'node_modules' } |
40ForEach-Object { $_.Directory.FullName }
41
42if (-not $pythonSkills) {
43Write-Host 'No Python skills found (no pyproject.toml files detected)' -ForegroundColor Yellow
44return @{ success = $true; skillsChecked = 0; errors = @() }
45}
46
47Write-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
52if (-not $ruffAvailable) {
53Write-Host '❌ ruff is not installed. Run "uv sync" in a skill directory or install with "uv pip install ruff".' -ForegroundColor Red
54return @{ success = $false; skillsChecked = 0; errors = @('ruff not installed') }
55}
56
57$results = @{
58success = $true
59skillsChecked = 0
60errors = @()
61details = @()
62}
63
64foreach ($skillPath in $pythonSkills) {
65Write-Host "`nRunning ruff in $skillPath..." -ForegroundColor Cyan
66
67Push-Location $skillPath
68try {
69$output = ruff check . 2>&1
70$exitCode = $LASTEXITCODE
71
72$result = @{
73path = $skillPath
74passed = ($exitCode -eq 0)
75output = $output | Out-String
76}
77
78$results.details += $result
79$results.skillsChecked++
80
81if ($exitCode -ne 0) {
82Write-Host "$output" -ForegroundColor Red
83Write-Host '❌ Linting issues found' -ForegroundColor Red
84$results.success = $false
85$results.errors += $skillPath
86} else {
87if ($output) {
88Write-Host "$output"
89}
90Write-Host '✓ No linting issues' -ForegroundColor Green
91}
92} catch {
93Write-Host "Error running ruff: $_" -ForegroundColor Red
94$results.success = $false
95$results.errors += "$skillPath - error: $_"
96} finally {
97Pop-Location
98}
99}
100
d337e1d2Bill Berry3 months ago101# Default to logs directory when no OutputPath specified
102if (-not $OutputPath) {
103$logsDir = Join-Path -Path $RepoRoot -ChildPath 'logs'
104if (-not (Test-Path $logsDir)) {
105New-Item -ItemType Directory -Path $logsDir -Force | Out-Null
106}
107$OutputPath = Join-Path -Path $logsDir -ChildPath 'python-lint-results.json'
0a90f573Jason3 months ago108}
d337e1d2Bill Berry3 months ago109$results | ConvertTo-Json -Depth 3 | Out-File $OutputPath -Encoding UTF8
110Write-Host "📊 Results written to: $OutputPath" -ForegroundColor Cyan
0a90f573Jason3 months ago111
112return $results
113} finally {
114Pop-Location
115}
116}
117
118#endregion
119
120#region Main Execution
121
122# Don't run main logic if dot-sourced for testing
123if ($MyInvocation.InvocationName -ne '.') {
124$result = Invoke-PythonLint -RepoRoot $RepoRoot -OutputPath $OutputPath
125
126if ($result.success) {
127Write-Host "`n✅ All Python skills passed linting" -ForegroundColor Green
128exit 0
129} else {
130Write-Host "`n❌ Linting completed with errors" -ForegroundColor Red
131exit 1
132}
133}
134
135#endregion