Add scripts that can be used to ensure format

This commit is contained in:
luisangelsm
2026-03-06 12:01:36 +01:00
parent 09ae34cb10
commit 9024ff7d0c
4 changed files with 156 additions and 0 deletions

44
scripts/clang-format-linux.sh Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
TEST_MODE=0
if [[ $# -gt 1 ]]; then
echo "Unknown arguments. Supported flags: --test, -t" >&2
exit 2
fi
if [[ $# -eq 1 ]]; then
if [[ "${1:-}" == "--test" || "${1:-}" == "-t" ]]; then
TEST_MODE=1
else
echo "Unknown argument: ${1:-}. Supported flags: --test, -t" >&2
exit 2
fi
fi
cd "${REPO_ROOT}"
if ! VERSION_OUTPUT="$(clang-format --version 2>/dev/null)"; then
echo "Unable to run clang-format --version. Make sure clang-format is in PATH." >&2
exit 127
fi
if [[ "${VERSION_OUTPUT}" =~ clang-format[[:space:]]+version[[:space:]]+([0-9]+(\.[0-9]+)*) ]]; then
VERSION="${BASH_REMATCH[1]}"
echo "Running using clang-format ${VERSION}"
else
echo "Running using ${VERSION_OUTPUT}"
fi
find . \( -name '*.h' -or -name '*.cpp' -or -name '*.c' -or -name '*.mm' -or -name '*.m' \) -print0 | xargs -0 clang-format -style=file -i
if [[ "${TEST_MODE}" -eq 1 ]]; then
BASE_SHA="$(git rev-parse HEAD)"
git diff "${BASE_SHA}"
if [[ "$(git diff "${BASE_SHA}")" != "" ]]; then
exit 1
fi
fi

44
scripts/clang-format-macos.sh Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
TEST_MODE=0
if [[ $# -gt 1 ]]; then
echo "Unknown arguments. Supported flags: --test, -t" >&2
exit 2
fi
if [[ $# -eq 1 ]]; then
if [[ "${1:-}" == "--test" || "${1:-}" == "-t" ]]; then
TEST_MODE=1
else
echo "Unknown argument: ${1:-}. Supported flags: --test, -t" >&2
exit 2
fi
fi
cd "${REPO_ROOT}"
if ! VERSION_OUTPUT="$(clang-format --version 2>/dev/null)"; then
echo "Unable to run clang-format --version. Make sure clang-format is in PATH." >&2
exit 127
fi
if [[ "${VERSION_OUTPUT}" =~ clang-format[[:space:]]+version[[:space:]]+([0-9]+(\.[0-9]+)*) ]]; then
VERSION="${BASH_REMATCH[1]}"
echo "Running using clang-format ${VERSION}"
else
echo "Running using ${VERSION_OUTPUT}"
fi
find . \( -name '*.h' -or -name '*.cpp' -or -name '*.c' -or -name '*.mm' -or -name '*.m' \) -print0 | xargs -0 clang-format -style=file -i
if [[ "${TEST_MODE}" -eq 1 ]]; then
BASE_SHA="$(git rev-parse HEAD)"
git diff "${BASE_SHA}"
if [[ "$(git diff "${BASE_SHA}")" != "" ]]; then
exit 1
fi
fi

View File

@ -0,0 +1,6 @@
@echo off
setlocal
set "SCRIPT_DIR=%~dp0"
powershell -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT_DIR%clang-format-windows.ps1" %*
exit /b %ERRORLEVEL%

View File

@ -0,0 +1,62 @@
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$test = $false
foreach ($arg in $args) {
switch ($arg) {
"--test" { $test = $true; continue }
"-test" { $test = $true; continue }
"-t" { $test = $true; continue }
default {
Write-Error "Unknown argument: $arg. Supported flags: --test, -test, -t"
exit 2
}
}
}
$repoRoot = (Resolve-Path "$PSScriptRoot/..").Path
Set-Location $repoRoot
$clangFormatCommand = Get-Command clang-format -ErrorAction SilentlyContinue
if (-not $clangFormatCommand) {
Write-Error "Unable to find clang-format in PATH."
exit 127
}
$clangFormat = $clangFormatCommand.Source
$versionOutput = & $clangFormat --version
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($versionOutput)) {
Write-Error "Unable to run clang-format --version. Make sure clang-format is in PATH."
exit 127
}
$versionMatch = [regex]::Match($versionOutput, "clang-format version ([0-9]+(\.[0-9]+)*)")
if ($versionMatch.Success) {
Write-Host "Running using clang-format $($versionMatch.Groups[1].Value)"
}
else {
Write-Host "Running using $versionOutput"
}
$extensions = @("*.h", "*.cpp", "*.c", "*.mm", "*.m")
$files = Get-ChildItem -Path . -Recurse -File | Where-Object {
$name = $_.Name
foreach ($ext in $extensions) {
if ($name -clike $ext) {
return $true
}
}
return $false
}
foreach ($file in $files) {
& $clangFormat -style=file -i $file.FullName
}
if ($test) {
$baseSha = "$(git rev-parse HEAD)".Trim()
git diff $baseSha
if ("$(git diff $baseSha)" -ne "") {
exit 1
}
}