From 1ff3b890285c7bfbe25aee40cb10bacd4b62e94b Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 1 Jul 2019 11:15:20 +0200 Subject: [PATCH] Updates wapm packages --- bin/update | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/bin/update b/bin/update index 733e6db..274aefa 100755 --- a/bin/update +++ b/bin/update @@ -103,6 +103,26 @@ _check_date() { fi } +# Returns 0 if $1 > $2 +_semver_greater() { + local major_1=$(echo $1 | cut -d '.' -f 1) + local minor_1=$(echo $1 | cut -d '.' -f 2) + local patch_1=$(echo $1 | cut -d '.' -f 3) + local major_2=$(echo $2 | cut -d '.' -f 1) + local minor_2=$(echo $2 | cut -d '.' -f 2) + local patch_2=$(echo $2 | cut -d '.' -f 3) + + if [ $major_1 -gt $major_2 ]; then + return 0 + elif [ $major_1 -eq $major_2 ] && [ $minor_1 -gt $minor_2 ]; then + return 0 + elif [ $major_1 -eq $major_2 ] && [ $minor_1 -eq $minor_2 ] && [ $patch_1 -gt $patch_2 ]; then + return 0 + else + return 1 + fi +} + update-system() { if [ -f ~/.config/dotfiles/.data/noroot ]; then @@ -244,6 +264,7 @@ update-wasm() { local old_path=$PWD + # Protect dotfiles from being modified by wasmer self-update cd ~/.config/dotfiles git stash > /dev/null 2>&1 wasmer self-update @@ -254,6 +275,41 @@ update-wasm() { local seconds=$((`date +%s` - $start)) local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'` echo -e "\033[32;1m=== Wasm updated in $formatted ===\033[0m" + + command -v wapm > /dev/null 2>&1 + if [ $? -ne 0 ]; then + return + fi + + start=`date +%s` + echo -e "\033[32;1m=== Updating wapm packages ===\033[0m" + + local output=$(wapm list -g | grep '^ _/' | cut -d '|' -f 1 | cut -d '/' -f 2 | while read package; do + local package_trimmed=$(echo $package | tr -d '[:space:]') + local current_version=$(wapm list -g | grep "^ _/$package_trimmed " | cut -d '|' -f 2 | tr -d '[:space:]') + local last_version=$(wapm search $package_trimmed | grep $package_trimmed | cut -d '|' -f 4 | tr -d '[:space:']) + local needs_update="No" + if _semver_greater $last_version $current_version; then + needs_update="Yes" + fi + echo $package_trimmed@$current_version@$last_version@$needs_update + done | column -t -s "@" -N "Package,Installed,Latest,Needs update") + + if [ "$output" != "" ]; then + echo -e "$output\n" + else + echo "No packages to update" + fi + + for package in $(echo "$output" | tail -n +2 | grep 'Yes' | cut -d ' ' -f 1); do + echo Updating $package + wapm install -g $package + done + + local seconds=$((`date +%s` - $start)) + local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'` + echo -e "\033[32;1m=== Wapm packages updated in $formatted ===\033[0m" + } update-npm() { @@ -346,7 +402,7 @@ _print_usage() { echo -e "\033[33mSUBCOMMANDS:\033[0m" echo -e " \033[32msystem\033[0m Updates the system (Debian, Archlinux, Fedora)" echo -e " \033[32mrust\033[0m Updates rust and rust packages (requires rustup)" - echo -e " \033[32mwasm\033[0m Updates wasmer" + echo -e " \033[32mwasm\033[0m Updates wasmer and wapm packages" echo -e " \033[32mnpm\033[0m Updates npm packages (in ~/.config/npmbin)" echo -e " \033[32mdotfiles\033[0m Updates the dotfiles" echo -e " \033[32mneovim\033[0m Updates the neovim packages"