Skip to content

Commit

Permalink
improved formatting scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectraL519 committed Feb 28, 2024
1 parent 9674639 commit f5cb1a9
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<br />
<br />
Expand Down
8 changes: 0 additions & 8 deletions change_log.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,3 @@
* Aligned the `.clang-format` configuration file and moved formatting scripts to a new directory `<project-root>/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
35 changes: 33 additions & 2 deletions scripts/format/unix.sh
Original file line number Diff line number Diff line change
@@ -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!"
39 changes: 37 additions & 2 deletions scripts/format/windows.ps1
Original file line number Diff line number Diff line change
@@ -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!"

0 comments on commit f5cb1a9

Please sign in to comment.