Created
June 23, 2025 21:45
-
-
Save roberttravispierce/80e0039f010f075633f23c7d4750c22a to your computer and use it in GitHub Desktop.
Check andFformat files for Rails project (for Claude Code, etc.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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