Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roberttravispierce/80e0039f010f075633f23c7d4750c22a to your computer and use it in GitHub Desktop.
Save roberttravispierce/80e0039f010f075633f23c7d4750c22a to your computer and use it in GitHub Desktop.
Check andFformat files for Rails project (for Claude Code, etc.)
# Adapted from Mikkel Malmberg (@mikker) video on Claude Code setup and operations: https://x.com/mikker/status/1937107047209701615
## bin/check
```bash
#!/usr/bin/env bash
set -e
# Progress reporting
GREEN=32
RED=31
BLUE=34
YELLOW=33
announce() { echo -e "\033[0;$2m$1\033[0m"; }
run() {
local SPLIT=$SECONDS
announce "\nRunning $1" $BLUE
if eval "$1"; then
local INTERVAL=$((SECONDS - SPLIT))
announce "βœ“ Completed $1 in $INTERVAL seconds" $GREEN
else
announce "βœ— Failed: $1" $RED
exit 1
fi
}
echo "πŸ” Running checks..."
# Ruby linting with Rubocop
run "bundle exec rubocop --parallel"
# ERB linting
run "bundle exec erb_lint app/views"
# Security check with Brakeman
run "bundle exec brakeman -q --no-summary"
# Run Rails tests
run "bin/rails test"
# Run system tests if requested
if [[ "$1" == "--system" ]]; then
run "bin/rails test:system"
fi
announce "\nβœ… All checks passed!" $GREEN
```
## bin/format
```bash
#!/usr/bin/env bash
set -e
# Progress reporting
GREEN=32
RED=31
BLUE=34
YELLOW=33
announce() { echo -e "\033[0;$2m$1\033[0m"; }
run() {
local SPLIT=$SECONDS
announce "\nRunning $1" $BLUE
if eval "$1"; then
local INTERVAL=$((SECONDS - SPLIT))
announce "βœ“ Completed $1 in $INTERVAL seconds" $GREEN
else
announce "βœ— Failed: $1" $RED
exit 1
fi
}
echo "🎨 Running formatters..."
# Ruby formatting with Rubocop autocorrect
run "bundle exec rubocop --autocorrect-all --parallel"
# ERB formatting with erb_lint
run "bundle exec erb_lint app/views --fix"
# Additional cleanup - remove trailing whitespace
announce "\nRemoving trailing whitespace" $BLUE
find . -type f -name "*.rb" -o -name "*.erb" -o -name "*.yml" -o -name "*.js" -o -name "*.css" | \
grep -v -E "(vendor/|node_modules/|tmp/|log/|.git/|worktrees/)" | \
xargs -I {} sed -i '' 's/[[:space:]]*$//' {} 2>/dev/null || true
announce "βœ“ Trailing whitespace removed" $GREEN
# Ensure all files end with newline
announce "\nEnsuring files end with newline" $BLUE
find . -type f -name "*.rb" -o -name "*.erb" -o -name "*.yml" -o -name "*.js" -o -name "*.css" | \
grep -v -E "(vendor/|node_modules/|tmp/|log/|.git/|worktrees/)" | \
while read -r file; do
if [ -n "$(tail -c 1 "$file")" ]; then
echo >> "$file"
fi
done
announce "βœ“ Newlines added where needed" $GREEN
announce "\nβœ… All formatting completed!" $GREEN
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment