chore(tools): add pwsh linter/formatter

This commit is contained in:
Oleg Shparber
2026-04-11 18:35:07 +03:00
parent b68f3c4dd6
commit a400ceab0d
2 changed files with 69 additions and 6 deletions
+3 -6
View File
@@ -22,8 +22,7 @@ if ($Version) {
# Verify the release exists.
try {
Invoke-RestMethod -Uri "https://api.github.com/repos/zealdocs/zeal/releases/tags/v$Version" | Out-Null
}
catch {
} catch {
throw "Release v$Version not found on GitHub."
}
@@ -60,8 +59,7 @@ if ($Version) {
throw "Portable checksum mismatch! Expected: $portableDigest, Got: $portableChecksum"
}
Write-Information "Checksums verified."
}
finally {
} finally {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
@@ -113,8 +111,7 @@ if ($Pack) {
if ($LASTEXITCODE -ne 0) { throw "choco pack failed for zeal.portable" }
choco pack zeal/zeal.nuspec
if ($LASTEXITCODE -ne 0) { throw "choco pack failed for zeal" }
}
finally {
} finally {
Pop-Location
}
}
+66
View File
@@ -0,0 +1,66 @@
#
# run-pwsh-analyzer.ps1 - Check or fix PowerShell script issues using PSScriptAnalyzer
#
# SPDX-FileCopyrightText: Oleg Shparber, et al. <https://zealdocs.org>
# SPDX-License-Identifier: MIT
#
# Prerequisites:
# Install-Module -Name PSScriptAnalyzer -Scope CurrentUser
#
# By default, runs in check mode.
# Use -Fix to apply automatic formatting fixes.
# Use -Staged to only process files staged for commit.
#
[CmdletBinding()]
param(
[switch]$Fix,
[switch]$Staged
)
if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) {
Write-Error "PSScriptAnalyzer is not installed. Run: Install-Module -Name PSScriptAnalyzer -Scope CurrentUser"
}
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..")
if ($Staged) {
$files = git diff --cached --name-only --diff-filter=ACMR -- "*.ps1" |
ForEach-Object { Join-Path $repoRoot $_ } |
Where-Object { Test-Path $_ }
} else {
$files = Get-ChildItem -Recurse $repoRoot -Include *.ps1 -File |
Where-Object { $_.FullName -notmatch '\\(build|\.git)\\' } |
Select-Object -ExpandProperty FullName
}
if (-not $files) {
Write-Information "No files to analyze."
exit 0
}
$failed = $false
foreach ($file in $files) {
Write-Verbose "Processing $file"
if ($Fix) {
$original = Get-Content -Raw $file
$formatted = Invoke-Formatter -ScriptDefinition $original -Settings CodeFormattingOTBS
if ($original -ne $formatted) {
Set-Content -Path $file -Value $formatted -NoNewline
Write-Output "Formatted: $file"
}
}
$results = Invoke-ScriptAnalyzer -Path $file
if ($results) {
$failed = $true
foreach ($r in $results) {
Write-Output "$($r.ScriptName):$($r.Line):$($r.Column) [$($r.Severity)] $($r.RuleName): $($r.Message)"
}
}
}
if ($failed) {
exit 1
}