Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Created September 6, 2020 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithrbennett/da3d44c7f644385caad666e80ed8cab2 to your computer and use it in GitHub Desktop.
Save keithrbennett/da3d44c7f644385caad666e80ed8cab2 to your computer and use it in GitHub Desktop.
Modification of @abachman's helpful groutes utility to add branch support
#!/bin/bash
# groutes greps routes
#
# the first time you run routes it runs bin/rails routes and caches it and
# greps that. if you update config/routes.rb, groutes _knows_ and rebuilds
# the cache.
#
# usage:
# $ groutes username
# username GET /:username(.:format) dashboards#show
# username_feeds GET /:username/feeds(.:format) feeds#index
# username_group_feeds GET /:username/groups/:group/feeds(.:format) groups#feeds
# username_feed GET /:username/feeds/:id(.:format) feeds#show
# GET /:username/dashboards/:dashboard_id(.:format) dashboards#show
# username_dashboard GET /:username/:dashboard_id(.:format) dashboards#show
#
# if you ctrl-c, you'll probably have to delete the cache files and start over
# $ rm .rails_routes*
#
# add the two cache files to .gitignore
# $ echo .rails_routes* >> .gitignore
export BRANCH=$(git branch --show-current)
export CACHE_FILESPEC=".rails_routes_$BRANCH~"
export ROUTES_RB_REFERENCE_FILESPEC=".rails_routes_rb_$BRANCH~"
if [ ! -f Rakefile -o ! -f config/routes.rb ]; then
echo "groutes will only work in rails project directories :("
exit 1
fi
if [ ! -f $CACHE_FILESPEC ]; then
echo "generate routes because cache doesn't exist"
bundle exec rake routes > $CACHE_FILESPEC
fi
if [ ! -f $ROUTES_RB_REFERENCE_FILESPEC ]; then
echo "regenerate routes because config/routes.rb is newer than cache"
cp config/routes.rb $ROUTES_RB_REFERENCE_FILESPEC
bundle exec rake routes > $CACHE_FILESPEC
else
diff -q config/routes.rb $ROUTES_RB_REFERENCE_FILESPEC
diff_status=$?
if [ "$diff_status" -ne 0 ]; then
echo "regenerate routes because config/routes.rb doesn't match previous version"
cp config/routes.rb $ROUTES_RB_REFERENCE_FILESPEC
bundle exec rake routes > $CACHE_FILESPEC
fi
fi
cat $CACHE_FILESPEC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment