From 9024ff7d0cd8843dd7d8cc3d2ad15eff43dfe33b Mon Sep 17 00:00:00 2001 From: luisangelsm Date: Fri, 6 Mar 2026 12:01:36 +0100 Subject: [PATCH] Add scripts that can be used to ensure format --- scripts/clang-format-linux.sh | 44 +++++++++++++++++++++++ scripts/clang-format-macos.sh | 44 +++++++++++++++++++++++ scripts/clang-format-windows.cmd | 6 ++++ scripts/clang-format-windows.ps1 | 62 ++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100755 scripts/clang-format-linux.sh create mode 100755 scripts/clang-format-macos.sh create mode 100644 scripts/clang-format-windows.cmd create mode 100644 scripts/clang-format-windows.ps1 diff --git a/scripts/clang-format-linux.sh b/scripts/clang-format-linux.sh new file mode 100755 index 00000000..f400e2ae --- /dev/null +++ b/scripts/clang-format-linux.sh @@ -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 diff --git a/scripts/clang-format-macos.sh b/scripts/clang-format-macos.sh new file mode 100755 index 00000000..f400e2ae --- /dev/null +++ b/scripts/clang-format-macos.sh @@ -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 diff --git a/scripts/clang-format-windows.cmd b/scripts/clang-format-windows.cmd new file mode 100644 index 00000000..9d59fcdd --- /dev/null +++ b/scripts/clang-format-windows.cmd @@ -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% diff --git a/scripts/clang-format-windows.ps1 b/scripts/clang-format-windows.ps1 new file mode 100644 index 00000000..a905ce92 --- /dev/null +++ b/scripts/clang-format-windows.ps1 @@ -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 + } +}