Files
zeal/tools/run-clang-format.ps1
T
2026-05-23 19:25:09 +03:00

55 lines
1.2 KiB
PowerShell

#
# run-clang-format.ps1 - Check or fix code formatting using clang-format
#
# SPDX-FileCopyrightText: Oleg Shparber, et al. <https://zealdocs.org>
# SPDX-License-Identifier: MIT
#
# By default, runs in check mode (dry-run).
# Use -Fix to apply changes.
# Use -Staged to only process files staged for commit.
# Use -Verbose to print each file being processed.
#
[CmdletBinding()]
param(
[switch]$Fix,
[switch]$Staged
)
$srcDir = Join-Path -Path $PSScriptRoot -ChildPath "..\src"
if ($Staged) {
$files = git diff --cached --name-only --diff-filter=ACMR -- "*.cpp" "*.h" |
ForEach-Object { Join-Path -Path $PSScriptRoot -ChildPath ".." -AdditionalChildPath $_ } |
Where-Object { Test-Path $_ }
} else {
$files = Get-ChildItem -Recurse $srcDir -Include *.cpp, *.h |
Select-Object -ExpandProperty FullName
}
if (-not $files) {
Write-Information "No files to format."
exit 0
}
$formatArgs = @("--dry-run", "--Werror")
if ($Fix) {
$formatArgs = @("-i")
}
$failed = $false
$total = @($files).Count
$i = 0
foreach ($file in $files) {
$i++
Write-Verbose "[$i/$total] Processing $file"
& clang-format @formatArgs $file
if ($LASTEXITCODE -ne 0) {
$failed = $true
}
}
if ($failed) {
exit 1
}