mirror of
https://github.com/zealdocs/zeal.git
synced 2026-08-29 08:34:50 +08:00
chore(tools): add pwsh linter/formatter
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user