Created
April 15, 2020 03:50
-
-
Save stvhay/3098ef32403227d289ea6658e2dde2e0 to your computer and use it in GitHub Desktop.
upgrade all local pythons and rubies using asdf
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 | |
#set -x | |
function find_latest { | |
# find latest version | |
local PROGRAM=$1 | |
local VERSION=$2 | |
asdf list all "${PROGRAM}" | \ | |
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | \ | |
grep -e '[0-9]*\.[0-9]*\.[0-9]*$' | \ | |
grep -e "^${VERSION}" | sort -Vr | head -n1 | |
} | |
function find_installed { | |
# find latest version | |
local PROGRAM=$1 | |
local VERSION=$2 | |
asdf list "${PROGRAM}" | \ | |
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | \ | |
grep -e '[0-9]*\.[0-9]*\.[0-9]*$' | grep -e "^${VERSION}" | |
} | |
function upgrade_helper { | |
# upgrade | |
local PROGRAM=$1 | |
local VERSION=$2 | |
local LATEST=$(find_latest $PROGRAM $VERSION) | |
local installed=0 | |
for version in $(find_installed $PROGRAM $VERSION); do | |
if [[ $version == $LATEST ]]; then | |
installed=1 | |
else | |
asdf uninstall $PROGRAM $version | |
fi | |
done | |
if [ $installed -eq 0 ]; then | |
asdf install $PROGRAM $LATEST | |
fi | |
} | |
function upgrade { | |
# escape the . | |
upgrade_helper $1 $(sed -e 's/\./\\./g' <<< $2) | |
} | |
function main { | |
for v in 3.6 3.7 3.8; do | |
upgrade "python" $v | |
done | |
for v in 2.5 2.6 2.7; do | |
upgrade "ruby" $v | |
done | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment