Skip to content

Commit

Permalink
add ci to check cmake formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
expipiplus1 committed Oct 24, 2024
1 parent ee709cf commit dbb7551
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/check-formatting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Check Formatting

on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
check-formatting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pip3 install gersemi
- run: ./extras/formatting.sh --check-only
66 changes: 66 additions & 0 deletions extras/formatting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
cd "$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)" || exit 1

check_only=0

while [[ "$#" -gt 0 ]]; do
case $1 in
-h | --help) help=1 ;;
--check-only) check_only=1 ;;
esac
shift
done

if [ "$help" ]; then
me=$(basename "$0")
cat <<EOF
$me: Format or check formatting of files in this repo
Usage: $me [--check-only]
Options:
--check-only Check formatting without modifying files
EOF
exit 0
fi

require_bin() {
if ! command -v "$1" &>/dev/null; then
echo "This script needs $1, but it isn't in \$PATH"
missing_bin=1
fi
}

require_bin "git"
require_bin "gersemi"

if [ "$missing_bin" ]; then
exit 1
fi

exit_code=0

cmake_formatting() {
readarray -t files < <(git ls-files '*.cmake' '**/CMakeLists.txt')

common_args=(
# turn on warning when this is fixed https://github.com/BlankSpruce/gersemi/issues/39
--no-warn-about-unknown-commands
--definitions "${files[@]}"
)

if [ "$check_only" -eq 1 ]; then
gersemi "${common_args[@]}" --diff --color "${files[@]}"
gersemi "${common_args[@]}" --check "${files[@]}" || exit_code=1
else
gersemi "${common_args[@]}" --in-place "${files[@]}"
fi
}

cmake_formatting

exit $exit_code

0 comments on commit dbb7551

Please sign in to comment.