Skip to content

Instantly share code, notes, and snippets.

@duggan
Last active December 16, 2015 21:48
Show Gist options
  • Save duggan/5501889 to your computer and use it in GitHub Desktop.
Save duggan/5501889 to your computer and use it in GitHub Desktop.
Envclean helps nuke and reseed a ruby environment for development purposes using rbenv and bundler. Add it to your PATH.
#!/bin/sh
set -o nounset
set -o errexit
help() {
printf "envclean - managing ruby environments
envclean list list available ruby versions (uses rbenv).
envclean for RUBY_VERSION cleans and initializes a specific ruby version.
"
}
remove_gems() {
gem list | grep -v '[test\-unit|rdoc|rake|psych|minitest|io-console|json|bigdecimal]' | cut -d" " -f1 | xargs gem uninstall -aIx
}
ensure_bundler() {
if [ "x$(gem list |grep bundler)x" == "xx" ] ; then
gem install bundler --no-rdoc --no-ri
fi
}
# Define macros
switch_to() {
local version=$1
printf "Switching to ruby $version\n"
rbenv local $version
remove_gems
ensure_bundler
if [ -d .bundle ] ; then
printf "Rebuilding bundles..."
rm -rf .bundle && bundle
fi
}
list_versions() {
ls ~/.rbenv/versions
}
envclean_for() {
for v in $(list_versions)
do
#SHORTHAND=$(printf $v | cut -d'-' -f 1)
if [ $1 = $v ] ; then
read -p "This will delete bundles, continue? (y/n) " yn
case $yn in
[Yy]* )
switch_to $v
;;
[Nn]* )
exit 0
;;
* )
exit 0
;;
esac
break;
fi
done
}
# Process options
if [ ! -d .git ] ; then
printf "Not a git repository.\n"
exit 1
fi
if [ -z "$*" ] ; then
help
exit 1
fi
if [ $1 = 'list' ] ; then
list_versions
exit 0
fi
if [ $1 = 'for' ] ; then
envclean_for $2
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment