Last active
January 12, 2022 19:49
-
-
Save privatmamtora/333e55cdc64fcbce4ff5 to your computer and use it in GitHub Desktop.
Use rbenv to automate updating to latest ruby version
This file contains 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
#!/bin/bash | |
# Name: updateRuby | |
# Purpose: Upgrade ruby | |
# Author: Privat Krish Mamtora Atmaram | |
# ----------------------------------------------------------------------------------- | |
usage () | |
{ | |
cat <<EOF | |
Update ruby version (using rbenv) | |
USAGE: updateRuby | |
EOF | |
exit 1; | |
} | |
if [ "$#" -ge 1 ] | |
then | |
usage | |
fi | |
command -v rbenv >/dev/null 2>&1 || { echo >&2 "rbenv is not installed. Aborting."; exit 1; } | |
command -v ruby-build >/dev/null 2>&1 || { echo >&2 "ruby-build is not installed. Aborting."; exit 1; } | |
current=$(rbenv version | xargs) | |
if [[ "$current" =~ ^\(.*\)$ ]] ; | |
then | |
# This situation happens when something was uninstalled and global is set to a non-existant package | |
current=""; | |
else | |
current=$(echo "$current" | cut -d " " -f 1); | |
fi | |
# Handel situation for when using system version | |
if [ "$current" == "system" ] ; | |
then | |
current="" | |
fi | |
available="$(rbenv install -l | tail -n +2 | awk '{$1=$1};1')"; | |
if [[ ${current:0:1} =~ [[:alpha:]] ]] ; | |
then | |
type="$(echo "$current" | xargs | cut -d "-" -f 1)"; | |
ver="$(echo "$available" | grep "^[^0-9].*" | grep "^$type.*" | cut -d "-" -f 2 | grep -v "[^\.0-9]" | tail -1)"; | |
latest="$(echo "$type-$ver")"; | |
else | |
latest="$(echo "$available" | grep "^[^a-zA-Z].*" | grep -v "[-]\+" | tail -1)"; | |
fi | |
installed=$(rbenv versions | sed "/$latest/! d" | xargs | cut -d " " -f 2) | |
echo "Current: $current"; | |
echo "Latest: $latest"; | |
echo "Installed: $installed"; | |
#Situations | |
#If no current > Get latest | |
#If not using latest and it is already installed > switch to latest | |
#If latest not installed and not current > install latest, switch to it, remove current | |
if [ "$latest" == "$current" ] | |
then | |
echo "Already have latest" | |
elif [ "$latest" != "$current" ] && [ "$latest" == "$installed" ] | |
then | |
echo "Have latest, switching to $latest" | |
eval "rbenv global $latest" | |
elif [ "$latest" != "$current" ] && [ "$latest" != "$installed" ] | |
then | |
if [ "$current" == "" ] ; | |
then | |
echo "Don't have latest, installing $latest" | |
eval "rbenv install $latest" && eval "rbenv global $latest" | |
else | |
echo "Dont have latest, installing $latest, removing $current" | |
eval "rbenv install $latest" && eval "rbenv global $latest" && eval "rbenv uninstall $current" <<< "y" | |
fi | |
fi |
Can you update this to reinstall all existing gems from the current to the latest? Seems like reinstalling is preferred over copying
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Manage when current version is
system