Skip to content

Instantly share code, notes, and snippets.

@jeanlescure
Last active June 1, 2016 21:19
Show Gist options
  • Save jeanlescure/3cfd8a819a1e2f7defa1bcac44e3f194 to your computer and use it in GitHub Desktop.
Save jeanlescure/3cfd8a819a1e2f7defa1bcac44e3f194 to your computer and use it in GitHub Desktop.
Get ruby version from a project regardless of version manager used
#!/bin/bash
echo 'Checking for ruby version in $RBENV_VERSION env var...'
if [ -n "$RBENV_VERSION" ]; then
VERSION=$RBENV_VERSION
fi
if [ -z ${VERSION+x} ]; then
echo 'Checking for ruby version in .ruby-version file...'
if [ -f ./.ruby-version ]; then
VERSION=`cat .ruby-version | tr -d '\n'`
fi
fi
if [ -z ${VERSION+x} ]; then
echo 'Checking for ruby version in .rvmrc file...'
if [ -f ./.rvmrc ]; then
[[ `cat .rvmrc | sed -e 's/\s*#[^!].*$//'` =~ environment_id\=[\'\"]([^\'\"]*)[\'\"] ]] && VERSION=${BASH_REMATCH[1]}
fi
fi
if [ -z ${VERSION+x} ]; then
echo 'Checking for ruby version in Gemfile...'
if [ -f ./Gemfile ]; then
[[ `cat Gemfile | sed -e 's/\s*#[^!].*$//'` =~ [^#][[:space:]]*ruby[[:space:]]*[\'\"]([^\'\"]*)[\'\"] ]] && VERSION=${BASH_REMATCH[1]}
fi
fi
if [ -z ${VERSION+x} ]; then
echo 'NO RUBY VERSION FOUND!'
exit
fi
echo "$VERSION found!"
## Un-comment to use RVM
#
# rvm install $VERSION
# rvm use $VERSION
## Un-comment to use rbenv
#
# rbenv install $VERSION
# rbenv global $VERSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment