Skip to content

Instantly share code, notes, and snippets.

@kosh30
Created December 4, 2023 21:53
Show Gist options
  • Save kosh30/2938621bf605e9bd7e509fc5fc545f6f to your computer and use it in GitHub Desktop.
Save kosh30/2938621bf605e9bd7e509fc5fc545f6f to your computer and use it in GitHub Desktop.
Install or update all asdf plugins defined in ~/.tool-versions files.
#!/usr/bin/env bash
#export GITHUB_API_TOKEN=""
function log() {
printf "%s %s\n" "->" "$1"
}
CHECK_SYMBOL='\u2713'
X_SYMBOL='\u2A2F'
function execute_and_wait() {
# stolen from: https://gitlab.com/-/snippets/1936196
local __resultvar=$3
eval $1 >/tmp/execute-and-wait.log 2>&1 &
pid=$!
delay=0.05
frames=('\u280B' '\u2819' '\u2839' '\u2838' '\u283C' '\u2834' '\u2826' '\u2827' '\u2807' '\u280F')
echo "$pid" >"/tmp/.spinner.pid"
# Hide the cursor, it looks ugly :D
tput civis
index=0
framesCount=${#frames[@]}
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
printf "${YELLOW}${frames[$index]}${NC} ${GREEN}$2${NC}"
let index=index+1
if [ "$index" -ge "$framesCount" ]; then
index=0
fi
printf "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
sleep $delay
done
#
# Wait the command to be finished, this is needed to capture its exit status
#
wait $!
exitCode=$?
if [ "$exitCode" -eq "0" ]; then
printf "${CHECK_SYMBOL} ${2} \b\n"
else
printf "${X_SYMBOL} ${2} \b\n"
fi
# Restore the cursor
tput cnorm
}
log "Updating all asdf-plugin remotes..."
plugin_list=$(cat ~/.tool-versions | awk '{print $1}')
use_sleep=""
plugins_count=$(echo $plugin_list | wc -w)
if (($plugins_count > 20)); then
log "There are more than 20 plugins, inject sleep 1 to avoid github restrictions?"
use_sleep="&& sleep 1"
fi
for plugin in $plugin_list; do
if asdf plugin list | grep -q $plugin; then
execute_and_wait "asdf plugin update $plugin ${use_sleep}" "Updating $plugin..."
else
execute_and_wait "asdf plugin add $plugin ${use_sleep}" "Adding $plugin..."
fi
done
log "Updating each plugin reference to the latest revision..."
rm -rf ~/.tool-versions.new
for plugin in $plugin_list; do
execute_and_wait "echo \"${plugin} $(asdf latest $plugin)\" >> ~/.tool-versions.new ${use_sleep}" "Check last version for $plugin..."
done
if cmp --silent ~/.tool-versions ~/.tool-versions.new; then
log "No changes found"
else
diff -y ~/.tool-versions ~/.tool-versions.new
while true; do
read -p "Do you wish to install all new revisions?" yn
case $yn in
[YyJj]*)
mv ~/.tool-versions ~/.tool-versions.bk
mv ~/.tool-versions.new ~/.tool-versions
asdf install
break
;;
[Nn]*)
log "Ok, I will not install the new versions"
break
;;
*) log "Please answer Yes or No." ;;
esac
done
fi
log "Done, bye! 👋"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment