diff --git a/Gemfile b/Gemfile index 6f04cf4..c7b900e 100644 --- a/Gemfile +++ b/Gemfile @@ -12,5 +12,5 @@ gem "bake", "~> 0.20" group :development, :test do gem "guard", "~> 2.18" gem "minitest", "~> 6.0" - gem "standard", "~> 1.43" + gem "standard", "~> 1.52" end diff --git a/Gemfile.lock b/Gemfile.lock index 826e45a..3874929 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -174,7 +174,7 @@ DEPENDENCIES minitest (~> 6.0) phlex (~> 2.3) rouge (~> 4.6) - standard (~> 1.43) + standard (~> 1.52) RUBY VERSION ruby 4.0.2 diff --git a/bin/bootstrap b/bin/bootstrap index 0067969..6454c86 100755 --- a/bin/bootstrap +++ b/bin/bootstrap @@ -34,4 +34,8 @@ else bundle install fi +echo "*** installing git hooks" +mkdir -p .git/hooks +ln -sf "../../bin/hooks/pre-commit" .git/hooks/pre-commit + echo "*** done" diff --git a/bin/hooks/pre-commit b/bin/hooks/pre-commit new file mode 100755 index 0000000..4d00e11 --- /dev/null +++ b/bin/hooks/pre-commit @@ -0,0 +1,49 @@ +#!/bin/zsh + +set -euo pipefail + +STAGED_FILES=(${(f)"$(git diff --cached --name-only --diff-filter=ACM)"}) + +if [[ ${#STAGED_FILES[@]} -eq 0 ]]; then + exit 0 +fi + +RUBY_FILES=() +for file in "${STAGED_FILES[@]}"; do + if [[ -f "$file" ]]; then + if [[ "$file" =~ \.rb$ ]] || head -1 "$file" 2>/dev/null | grep -q ruby; then + RUBY_FILES+=("$file") + fi + fi +done + +if [[ ${#RUBY_FILES[@]} -eq 0 ]]; then + exit 0 +fi + +echo "Running StandardRB on staged Ruby files..." +if command -v rbenv &>/dev/null; then + rbenv exec bundle exec standardrb --fix "${RUBY_FILES[@]}" +else + bundle exec standardrb --fix "${RUBY_FILES[@]}" +fi + +MODIFIED=false +for file in "${RUBY_FILES[@]}"; do + if ! git diff --quiet "$file"; then + MODIFIED=true + echo "✗ $file was modified by StandardRB" + fi +done + +if [[ "$MODIFIED" == "true" ]]; then + echo "" + echo "❌ Formatting changes were made. Please review and stage the changes, then commit again." + echo "" + echo "To see the changes: git diff" + echo "To add the changes: git add -u" + exit 1 +fi + +echo "✅ All files are properly formatted" +exit 0