lint.ps1 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. $ErrorActionPreference = "Stop"
  2. # Get the root directory and third_party directory
  3. $ROOT_DIR = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -Parent | Split-Path -Parent -Resolve
  4. # Include the common functions
  5. . "$ROOT_DIR/hack/lib/windows/init.ps1"
  6. function Get-PSScriptAnalyzer {
  7. $module = Get-Module -ListAvailable -Name PSScriptAnalyzer
  8. if (-not $module) {
  9. Install-Module -Name PSScriptAnalyzer -Scope CurrentUser -Force -SkipPublisherCheck -AllowClobber
  10. }
  11. }
  12. function Lint {
  13. param (
  14. [string]$path
  15. )
  16. GPUStack.Log.Info "linting $path"
  17. $result = Invoke-ScriptAnalyzer -Path $ROOT_DIR -Recurse -EnableExit -ExcludeRule PSUseBOMForUnicodeEncodedFile,PSAvoidUsingPlainTextForPassword,PSAvoidUsingInvokeExpression, PSReviewUnusedParameter, PSUseApprovedVerbs, PSAvoidGlobalVars, PSUseShouldProcessForStateChangingFunctions, PSAvoidUsingWriteHost
  18. $result | Format-Table -AutoSize
  19. if ($result.Length -ne 0) {
  20. GPUStack.Log.Fatal "failed with Invoke-ScriptAnalyzer lint."
  21. }
  22. uv run pre-commit run flake8 --all-files
  23. if ($LASTEXITCODE -ne 0) {
  24. GPUStack.Log.Fatal "failed with flake8 lint."
  25. }
  26. uv run pre-commit run black --all-files
  27. if ($LASTEXITCODE -ne 0) {
  28. GPUStack.Log.Fatal "failed with black lint."
  29. }
  30. uv run pre-commit run check-yaml --all-files
  31. if ($LASTEXITCODE -ne 0) {
  32. GPUStack.Log.Fatal "failed with check-yaml lint."
  33. }
  34. uv run pre-commit run debug-statements --all-files
  35. if ($LASTEXITCODE -ne 0) {
  36. GPUStack.Log.Fatal "failed with debug-statements lint."
  37. }
  38. uv run pre-commit run end-of-file-fixer --all-files
  39. if ($LASTEXITCODE -ne 0) {
  40. GPUStack.Log.Fatal "failed with end-of-file-fixer lint."
  41. }
  42. }
  43. function Remove-BOM {
  44. $filePath = Join-Path $ROOT_DIR -ChildPath "install.ps1"
  45. $bytes = [System.IO.File]::ReadAllBytes($filePath)
  46. if ($bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {
  47. [System.IO.File]::WriteAllBytes($filePath, $bytes[3..($bytes.Length - 1)])
  48. }
  49. Write-Host "BOM removed from $filePath"
  50. }
  51. #
  52. # main
  53. #
  54. GPUStack.Log.Info "+++ LINT +++"
  55. try {
  56. Get-PSScriptAnalyzer
  57. Lint "gpustack"
  58. Remove-BOM
  59. }
  60. catch {
  61. GPUStack.Log.Fatal "failed to lint: $($_.Exception.Message)"
  62. }
  63. GPUStack.Log.Info "--- LINT ---"