From f5cb1a9426555e58a3309077bf841d5d2b91f472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Musia=C5=82?= <111433005+SpectraL519@users.noreply.github.com> Date: Sat, 17 Feb 2024 16:40:08 +0100 Subject: [PATCH] improved formatting scripts --- .github/workflows/format.yaml | 2 +- README.md | 2 ++ change_log.md | 8 ------- scripts/format/unix.sh | 35 +++++++++++++++++++++++++++++-- scripts/format/windows.ps1 | 39 +++++++++++++++++++++++++++++++++-- 5 files changed, 73 insertions(+), 13 deletions(-) diff --git a/.github/workflows/format.yaml b/.github/workflows/format.yaml index 6407abd..a5b3585 100644 --- a/.github/workflows/format.yaml +++ b/.github/workflows/format.yaml @@ -25,4 +25,4 @@ jobs: - name: Test formatting shell: bash run: | - find . -type f -name "*.hpp" -o -name "*.cpp" -exec clang-format-17 --dry-run --Werror {} \; + bash ./scripts/format/unix.sh --check diff --git a/README.md b/README.md index f6096e5..308b287 100644 --- a/README.md +++ b/README.md @@ -501,6 +501,8 @@ To format the code use run the following: ./scripts/format/windows.ps1 ``` +To run a forrmat check use the scripts mentioned above with a `--check` flag. +

diff --git a/change_log.md b/change_log.md index c8ca4f4..9e9ce2f 100644 --- a/change_log.md +++ b/change_log.md @@ -24,11 +24,3 @@ * Aligned the `.clang-format` configuration file and moved formatting scripts to a new directory `/scripts` * Added the `install_clang_format_17.sh` env script * Added the `format` workflow - -TODO: -* Split dev notes into sections and include them in table of contents -* Explicit formatting instructions in dev notes -* Add format checking workflow -* Explicit clang compiler flags setting in CMake -* Add `CMakeLists.txt` in project root -* Add CMake integration instructions in dev notes diff --git a/scripts/format/unix.sh b/scripts/format/unix.sh index a1bafb3..a3b1e8a 100644 --- a/scripts/format/unix.sh +++ b/scripts/format/unix.sh @@ -1,4 +1,35 @@ #!/bin/bash -# Recursively find .hpp files and format them with clang-format -find . -type f -name "*.hpp" -o -name "*.cpp" -exec clang-format -i {} \; +format_check=false +if [[ "$1" == "--check" ]]; then + format_check=true +fi + +run_clang_format() { + local file="$1" + if [[ "$format_check" == true ]]; then + clang-format-17 --dry-run --Werror "$file" + else + clang-format-17 -i "$file" + fi +} + +# Count the number of files to format +file_count=$(find . -type f \( -name "*.hpp" -o -name "*.cpp" \) | wc -l) +if [[ "$format_check" == true ]]; then + echo "Files to check: $file_count" +else + echo "Files to format: $file_count" +fi +echo + +# Iterate over the files and run format/check +file_number=0 +find . -type f \( -name "*.hpp" -o -name "*.cpp" \) -print0 | while IFS= read -r -d '' file; do + ((file_number++)) + echo "[$file_number/$file_count] $file" + run_clang_format "$file" +done + +echo +echo "Done!" diff --git a/scripts/format/windows.ps1 b/scripts/format/windows.ps1 index e8e365d..607488b 100644 --- a/scripts/format/windows.ps1 +++ b/scripts/format/windows.ps1 @@ -1,2 +1,37 @@ -# Recursively find .hpp files and format them with clang-format -Get-ChildItem -Recurse -Include *.hpp, *.cpp | ForEach-Object { clang-format -i $_.FullName } +$formatCheck = $false +if ($args[0] -eq "--check") { + $formatCheck = $true +} + +function Run-Clang-Format { + param ( + [string]$filePath, + [bool]$check + ) + + if ($check) { + clang-format --dry-run --Werror $filePath + } + else { + clang-format -i $filePath + } +} + +# Get the list of files to format +$files = Get-ChildItem -Recurse -Include *.hpp, *.cpp + +# Count the number of files to format +$fileCount = $files.Count +Write-Host "Total files to format: $fileCount" +Write-Host + +# Iterate over the files and run format/check +$fileNumber = 0 +foreach ($file in $files) { + $fileNumber++ + Write-Host "[$fileNumber/$fileCount] $($file.FullName)" + Run-Clang-Format -filePath $file.FullName -check $formatCheck +} + +Write-Host +Write-Host "Done!"