run-flake8.ps1 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env pwsh
  2. <#
  3. .SYNOPSIS
  4. Runs flake8 to analyze Python source code for errors and best practices.
  5. #>
  6. # Get path.
  7. $scriptDir = Split-Path $PSCommandPath -Parent
  8. $projectDir = (Get-Item $scriptDir).Parent
  9. # Set working directory to root of project.
  10. Push-Location $projectDir
  11. # Get the diff for the current branch.
  12. $ExitCode = 0
  13. $GitDiff = git diff origin/master
  14. # If there is no diff between master, then flake8 everything.
  15. if ( $null -eq $GitDiff ) {
  16. flake8 .
  17. if ($LastExitCode -ne 0) { $ExitCode = $LastExitCode }
  18. }
  19. # Else flake8 just the diff.
  20. else {
  21. Write-Output $GitDiff | flake8 --diff
  22. if ($LastExitCode -ne 0) { $ExitCode = $LastExitCode }
  23. # If the project_template changed, then flake8 the testproject too.
  24. $GitDiffTempl = Write-Output $GitDiff | Select-String -Pattern "^diff .*/project_template/.*"
  25. if ( $null -ne $GitDiffTempl ) {
  26. flake8 testproject
  27. if ($LastExitCode -ne 0) { $ExitCode = $LastExitCode }
  28. }
  29. }
  30. # Write output for humans.
  31. if ($ExitCode -eq 0) {
  32. Write-Host -ForegroundColor Green "[✔] Flake8 passed with no errors"
  33. }
  34. else {
  35. Write-Host -ForegroundColor Red "[X] Flake8 exited with errors. Please resolve issues above."
  36. }
  37. # Unset working directory and exit with flake8's exit code.
  38. Pop-Location
  39. exit $ExitCode