dotfiles/bin/update

560 lines
16 KiB
Plaintext
Raw Normal View History

2019-05-03 08:45:06 +01:00
#!/usr/bin/env bash
2019-02-11 16:40:33 +00:00
running=0
_check_running() {
if [ $running -ne 0 ]; then
_unlock
exit $running
fi
}
_onkill() {
running=1
}
2019-06-03 11:00:20 +01:00
_lock() {
if [ -f ~/.config/dotfiles/.data/update_lock ]; then
return 1
fi
touch ~/.config/dotfiles/.data/update_lock
}
_lock_or_error_message() {
_lock
local value=$?
if [ $value -ne 0 ]; then
echo -e "\033[31;1merror:\033[0m update seems to be already running"
echo -e "\033[1minfo:\033[0m if you're sure it's not running, run \`update force-unlock\`"
return $value
fi
}
_unlock() {
rm -rf ~/.config/dotfiles/.data/update_lock
}
2019-02-11 16:40:33 +00:00
_check_date_if_format() {
local format=`_date_format`
if [ "$format" != "" ]; then
_check_date $format $1
2019-02-11 16:40:33 +00:00
fi
}
_date_format() {
case $UPDATE_CHECK in
"daily") echo +%d/%m/%Y;;
"weekly") echo +%V/%Y;;
"monthly") echo +%m/%Y;;
esac
}
_check_date_file() {
mkdir -p ~/.config/dotfiles/.data
touch ~/.config/dotfiles/.data/update_date
}
_sentence() {
2019-06-07 14:49:58 +01:00
local subject0=("Your" "The" "This")
local subject1=("system" "machine" "pc" "computer")
local adjective=("awesome" "incredible" "amazing" "brave" "hard working" "loyal" "nice" "polite" "powerful" "pro-active" "reliable" "fabulous" "fantastic" "incredible" "outstanding" "remarkable" "spectacular" "splendid" "super" "happy" "cheerful")
local verb=("is" "seems" "looks" "appears to be")
local no_verb=("is not" "doesn't seem" "doesn't look" "appears not to be")
local adverb=("up-to-date" "ready" "updated")
local dot=("." "!" "...")
local n_subject0=`shuf -i0-"$((${#subject0[@]}-1))" -n1`
local n_subject1=`shuf -i0-"$((${#subject1[@]}-1))" -n1`
local n_adjective=`shuf -i0-"$((${#adjective[@]}-1))" -n1`
local n_adverb=`shuf -i0-"$((${#adverb[@]}-1))" -n1`
local n_dot=`shuf -i0-"$((${#dot[@]}-1))" -n1`
if [ $1 = "updated" ]; then
2019-06-07 14:49:58 +01:00
local color="32"
local n_verb=`shuf -i0-"$((${#verb[@]}-1))" -n1`
local s_verb=${verb[$n_verb]}
local end=""
elif [ $1 = "not_updated" ]; then
2019-06-07 14:49:58 +01:00
local color="31"
local n_verb=`shuf -i0-"$((${#no_verb[@]}-1))" -n1`
local s_verb=${no_verb[$n_verb]}
local end="Run \`update\` to update your system."
fi
2019-05-03 10:32:03 +01:00
echo -e "\033[$color;1m${subject0[$n_subject0]} ${adjective[$n_adjective]} ${subject1[$n_subject1]} $s_verb ${adverb[$n_adverb]}${dot[$n_dot]} ${end}\033[0m"
}
_check_date() {
_check_date_file
2019-06-07 14:49:58 +01:00
local old_date=`cat ~/.config/dotfiles/.data/update_date`
local new_date=`date $1`
2019-06-11 08:29:25 +01:00
if [ "$new_date" != "$old_date" ] && [ ! -f ~/.config/dotfiles/.data/update_lock ]; then
_sentence not_updated
elif [ "$UPDATE_CHECK_ALWAYS" = "true" ] || [ "$2" = "force" ]; then
_sentence updated
fi
}
2019-07-01 10:15:20 +01:00
# 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
}
2019-01-07 13:25:56 +00:00
update-system() {
2019-01-08 10:16:43 +00:00
2019-05-03 09:38:58 +01:00
if [ -f ~/.config/dotfiles/.data/noroot ]; then
# If this file exists, it means root is not available: skip the system update
echo -e "\033[33;1m=== You don't have root, skipping system update ===\033[0m"
2019-05-03 09:39:45 +01:00
return
2019-05-03 09:38:58 +01:00
fi
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Starting system update, please enter your password ===\033[0m"
2019-04-22 09:03:25 +01:00
sudo true
if [ $? -ne 0 ]; then
echo "Could not get sudo..."
2019-01-08 10:16:43 +00:00
return 1
fi
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Updating system ===\033[0m"
2019-06-07 14:49:58 +01:00
local start=`date +%s`
2018-09-26 16:12:04 +01:00
# Debian based systems
command -v apt > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo apt update -y
if [ $? -eq 0 ]; then
sudo apt upgrade -y
if [ $? -eq 0 ]; then
sudo apt autoremove -y
fi
fi
fi
# Archlinux based systems
2019-03-13 09:46:24 +00:00
command -v yay > /dev/null 2>&1
2018-09-26 16:12:04 +01:00
if [ $? -eq 0 ]; then
2019-04-16 13:48:47 +01:00
yay -Syu --noconfirm
2019-03-15 19:42:03 +00:00
yay -Syua --noconfirm
2018-09-26 16:12:04 +01:00
else
2019-03-13 09:46:24 +00:00
command -v pacman > /dev/null 2>&1
2018-09-26 16:12:04 +01:00
if [ $? -eq 0 ]; then
2019-03-15 19:42:03 +00:00
sudo pacman -Syu --noconfirm
2018-09-26 16:12:04 +01:00
fi
fi
# Fedora based systems
command -v dnf > /dev/null 2>&1
if [ $? -eq 0 ]; then
2018-09-26 17:30:54 +01:00
sudo dnf upgrade
2018-09-26 16:12:04 +01:00
fi
2019-06-07 14:49:58 +01:00
local seconds=$((`date +%s` - $start))
local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== System updated in $formatted ===\033[0m"
2019-01-07 13:25:56 +00:00
}
2018-11-19 08:59:36 +00:00
2019-01-07 13:25:56 +00:00
update-rust() {
# Update rust if installed
command -v rustup > /dev/null 2>&1
2019-01-09 10:02:32 +00:00
if [ $? -ne 0 ]; then
return
fi
2019-01-07 13:25:56 +00:00
2019-06-07 14:49:58 +01:00
local start=`date +%s`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Updating rustup ===\033[0m"
2019-01-09 10:02:32 +00:00
rustup self update
2018-09-26 16:12:04 +01:00
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Updating rust ===\033[0m"
2019-01-09 10:02:32 +00:00
rustup update
2018-09-26 16:12:04 +01:00
2019-01-09 10:02:32 +00:00
cargo install-update --help > /dev/null 2>&1
2018-09-26 16:12:04 +01:00
2019-05-03 09:38:58 +01:00
# Program to update cargo programs is not installed
2019-01-09 10:02:32 +00:00
if [ $? -ne 0 ]; then
2018-09-26 17:30:54 +01:00
2019-05-03 09:38:58 +01:00
# This program requires openssl, so check that it is installed
2019-01-09 10:02:32 +00:00
pkg-config --libs --cflags openssl > /dev/null 2>&1
2019-06-07 14:49:58 +01:00
local installed_openssl=$?
2019-01-08 10:16:43 +00:00
2019-01-09 10:02:32 +00:00
# We need to install openssl
2019-05-03 09:38:58 +01:00
if [ $installed_openssl -ne 0 ]; then
2018-09-26 17:30:54 +01:00
2019-05-03 09:38:58 +01:00
# If the user has root power
if [ ! -f ~/.config/dotfiles/.data/noroot ]; then
2018-09-26 17:30:54 +01:00
2019-05-03 09:38:58 +01:00
echo -e "\033[32;1m=== libssl-dev is needed to update rust packages ===\033[0m"
sudo true
2018-09-26 17:30:54 +01:00
2019-01-09 10:02:32 +00:00
if [ $? -eq 0 ]; then
2019-05-03 09:38:58 +01:00
# For ubuntu
command -v apt > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo apt install -y libssl-dev
installed_openssl=0
fi
# For fedora
command -v dnf > /dev/null 2>&1
if [ $? -eq 0 ]; then
sudo dnf install openssl-devel
installed_openssl=0
fi
2019-01-09 10:02:32 +00:00
fi
2018-09-26 17:30:54 +01:00
2019-05-03 09:38:58 +01:00
fi
2018-09-26 16:12:04 +01:00
fi
2018-09-26 17:11:42 +01:00
2019-05-03 09:38:58 +01:00
# Only try to install if openssl is installed
if [ $installed_openssl -eq 0 ]; then
echo -e "\033[32;1m=== Installing rust packages updater ===\033[0m"
cargo install cargo-update
fi
2018-09-26 16:12:04 +01:00
fi
2019-05-03 09:38:58 +01:00
# If the user has no root, and if he doesn't have openssl, we won't install cargo update.
# Before running cargo update, we check again that it is installed.
cargo install-update --help > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo -e "\033[32;1m=== Updating rust packages ===\033[0m"
cargo install-update -ag
fi
2019-01-09 10:02:32 +00:00
2019-06-07 14:49:58 +01:00
local seconds=$((`date +%s` - $start))
local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Rust updated in $formatted ===\033[0m"
2018-11-19 08:59:36 +00:00
2019-01-07 13:25:56 +00:00
}
2019-06-03 17:01:33 +01:00
update-wasm() {
command -v wasmer > /dev/null 2>&1
if [ $? -ne 0 ]; then
return
fi
2019-06-07 14:49:58 +01:00
local start=`date +%s`
2019-07-02 15:38:29 +01:00
echo -e "\033[32;1m=== Updating wasmer ===\033[0m"
2019-06-03 17:01:33 +01:00
local old_path=$PWD
2019-07-01 10:15:20 +01:00
# Protect dotfiles from being modified by wasmer self-update
cd ~/.config/dotfiles
git stash > /dev/null 2>&1
2019-06-03 17:01:33 +01:00
wasmer self-update
git checkout zshrc > /dev/null 2>&1
git stash pop > /dev/null 2>&1
cd $old_path
2019-06-03 17:01:33 +01:00
2019-07-01 10:15:20 +01:00
command -v wapm > /dev/null 2>&1
if [ $? -ne 0 ]; then
return
fi
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
2019-07-02 15:38:29 +01:00
echo "No packages need updating."
2019-07-01 10:15:20 +01:00
fi
2019-07-02 15:38:29 +01:00
local update_packages=0
2019-07-01 10:15:20 +01:00
for package in $(echo "$output" | tail -n +2 | grep 'Yes' | cut -d ' ' -f 1); do
echo Updating $package
wapm install -g $package
2019-07-02 15:38:29 +01:00
update_packages=$(($update_packages + 1))
2019-07-01 10:15:20 +01:00
done
2019-07-02 15:38:29 +01:00
if [ $update_packages -gt 1 ]; then
echo "1 package updated."
elif [ $update_packages -gt 0 ]; then
echo "$update_packages packages updated."
fi
2019-07-01 10:15:20 +01:00
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"
2019-06-03 17:01:33 +01:00
}
2019-03-15 14:32:54 +00:00
update-npm() {
# Update node packages if installed
command -v npm > /dev/null 2>&1
if [ $? -ne 0 ]; then
return
fi
2019-03-21 16:56:49 +00:00
if [ ! -d ~/.npmbin ]; then
return
fi
2019-06-07 14:49:58 +01:00
local start=`date +%s`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Updating node packages ===\033[0m"
2019-06-05 08:52:50 +01:00
npm update -g
2019-03-15 14:32:54 +00:00
2019-06-07 14:49:58 +01:00
local seconds=$((`date +%s` - $start))
local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Node packages updated in $formatted ===\033[0m"
2019-03-15 14:32:54 +00:00
}
2019-01-07 13:25:56 +00:00
update-dotfiles() {
2019-06-07 14:49:58 +01:00
local start=`date +%s`
2019-01-07 13:25:56 +00:00
2019-06-07 14:49:58 +01:00
local current_dir=$PWD
2019-01-07 13:25:56 +00:00
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Updating dotfiles ===\033[0m"
2019-01-07 13:25:56 +00:00
cd ~/.config/dotfiles && git pull
if [ -d ~/.config/oh-my-zsh ]; then
echo -e "\033[32;1m=== Updating oh-my-zsh ===\033[0m"
cd ~/.config/oh-my-zsh && git pull
fi
2019-01-07 13:25:56 +00:00
if [ -d ~/.config/awesome/.git ]; then
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Updating awesome config ===\033[0m"
2019-01-07 13:25:56 +00:00
cd ~/.config/awesome/ && git pull
fi
cd $current_dir
2018-09-26 16:12:04 +01:00
2019-06-07 14:49:58 +01:00
local seconds=$((`date +%s` - $start))
local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Dotfiles updated in $formatted ===\033[0m"
2019-01-07 13:25:56 +00:00
}
2019-04-03 17:26:51 +01:00
update-neovim() {
2019-04-03 17:32:56 +01:00
command -v nvim > /dev/null 2>&1
2019-04-03 17:26:51 +01:00
if [ $? -ne 0 ]; then
return
fi
2019-06-07 14:49:58 +01:00
local start=`date +%s`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Updating neovim packages ===\033[0m"
2019-04-03 17:26:51 +01:00
2019-06-19 08:49:53 +01:00
curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim 2>/dev/null
2019-04-03 17:26:51 +01:00
nvim +PlugUpdate +qall
2019-06-07 14:49:58 +01:00
local seconds=$((`date +%s` - $start))
local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Neovim updated in $formatted ===\033[0m"
2019-04-03 17:26:51 +01:00
}
2019-05-28 06:32:11 +01:00
update-postpone() {
local format=`_date_format`
if [ $? -eq 0 ]; then
_check_date_file
date $format > ~/.config/dotfiles/.data/update_date
fi
}
_print_help() {
2019-06-03 16:25:28 +01:00
echo -e "\033[32mupdate\033[0m"
echo -e "Thomas Forgione <thomas@forgione.fr>"
echo -e "A script that automatically updates your system"
echo
2019-06-03 16:25:28 +01:00
_print_usage
}
_print_usage() {
echo -e "\033[33mUSAGE:\033[0m"
2019-06-05 09:03:03 +01:00
echo -e " \033[32mupdate\033[0m Auto-detect what to update and perform th update"
echo -e " \033[32mupdate <subcommand>\033[0m Calls the update subcommand"
2019-06-03 16:25:28 +01:00
echo
echo -e "\033[33mSUBCOMMANDS:\033[0m"
2019-06-05 09:03:03 +01:00
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)"
2019-07-01 10:15:20 +01:00
echo -e " \033[32mwasm\033[0m Updates wasmer and wapm packages"
2019-06-05 09:03:03 +01:00
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"
echo -e " \033[32mcheck\033[0m Checks whether the system has been recently updated"
echo -e " \033[32mstartup\033[0m Same as \`update check\` but is silent in case of success"
echo -e " \033[32mpostpone\033[0m Skip the current update (disable check warnings)"
echo -e " \033[32mforce-unlock\033[0m Deletes the lock file"
}
2019-04-17 15:46:15 +01:00
partial-test() {
2019-05-03 08:45:06 +01:00
case $1 in
2019-04-17 15:46:15 +01:00
"system") return 0;;
"rust") return 0;;
2019-06-03 17:01:33 +01:00
"wasm") return 0;;
2019-04-17 15:46:15 +01:00
"npm") return 0;;
"dotfiles") return 0;;
"neovim") return 0;;
"check") return 0;;
"startup") return 0;;
2019-05-28 06:32:11 +01:00
"postpone") return 0;;
2019-06-03 11:00:20 +01:00
"force-unlock") return 0;;
*) return 1;;
esac
}
partial-requires-lock() {
case $1 in
"system") return 0;;
"rust") return 0;;
2019-06-03 17:01:33 +01:00
"wasm") return 0;;
2019-06-03 11:00:20 +01:00
"npm") return 0;;
"dotfiles") return 0;;
"neovim") return 0;;
"check") return 1;;
"startup") return 1;;
"postpone") return 1;;
"force-unlock") return 1;;
2019-04-17 15:46:15 +01:00
*) return 1;;
esac
}
partial-update() {
2019-05-03 08:45:06 +01:00
case $1 in
2019-04-17 15:46:15 +01:00
"system") update-system;;
"rust") update-rust;;
2019-06-03 17:01:33 +01:00
"wasm") update-wasm;;
2019-04-17 15:46:15 +01:00
"npm") update-npm;;
"dotfiles") update-dotfiles;;
"neovim") update-neovim;;
"check") _check_date_if_format force;;
"startup") _check_date_if_format;;
2019-05-28 06:32:11 +01:00
"postpone") update-postpone;;
2019-06-03 11:00:20 +01:00
"force-unlock") _unlock;;
2019-04-17 15:46:15 +01:00
*) return 1
esac
}
2019-02-11 16:40:33 +00:00
main() {
2019-01-07 13:25:56 +00:00
2019-02-11 16:40:33 +00:00
if [ $# -eq 0 ]; then
2019-06-03 11:00:20 +01:00
_lock_or_error_message
2019-06-11 14:01:41 +01:00
local error_code=$?
if [ $error_code -ne 0 ]; then
exit $error_code
2019-06-03 11:00:20 +01:00
fi
2019-01-07 13:25:56 +00:00
2019-06-07 14:49:58 +01:00
local start=`date +%s`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Starting full update ===\033[0m"
2019-01-07 13:25:56 +00:00
2019-02-11 16:40:33 +00:00
# Update the system
_check_running
2019-02-11 16:40:33 +00:00
update-system
2019-01-07 13:25:56 +00:00
2019-02-11 16:40:33 +00:00
# Update rust and rust packages
_check_running
2019-02-11 16:40:33 +00:00
update-rust
2018-11-19 08:59:36 +00:00
2019-06-03 17:01:33 +01:00
# Update wasmer
_check_running
update-wasm
2019-03-15 14:32:54 +00:00
# Update npm and npm packages
_check_running
2019-03-15 14:32:54 +00:00
update-npm
2019-02-11 16:40:33 +00:00
# Update the dotfiles
_check_running
2019-02-11 16:40:33 +00:00
update-dotfiles
2019-01-07 13:25:56 +00:00
2019-04-03 17:26:51 +01:00
# Update the neovim packages
_check_running
2019-04-03 17:26:51 +01:00
update-neovim
_check_running
2019-05-28 06:32:11 +01:00
update-postpone
_check_running
2019-06-07 14:49:58 +01:00
local seconds=$((`date +%s` - $start ))
local formatted=`date -ud "@$seconds" +'%H hours %M minutes %S seconds'`
2019-05-03 08:45:06 +01:00
echo -e "\033[32;1m=== Update finished in $formatted ===\033[0m"
2019-02-11 16:40:33 +00:00
_check_running
_check_date_if_format force
_check_running
2019-06-03 11:00:20 +01:00
_unlock
2019-02-11 16:40:33 +00:00
else
2019-06-03 11:00:20 +01:00
local lock_required=1
2019-04-17 15:46:15 +01:00
for part in $@; do
2019-06-03 16:25:28 +01:00
if [ $part == "-h" ] || [ $part == "--help" ]; then
_print_help
exit 0
fi
2019-04-17 15:46:15 +01:00
partial-test $part
if [ $? -ne 0 ]; then
2019-05-03 08:45:06 +01:00
echo -e "\033[1;31merror:\033[0m unrocognized update command \"$part\""
2019-06-03 16:25:28 +01:00
_print_usage
2019-04-17 15:46:15 +01:00
return 1
fi
2019-06-03 16:25:28 +01:00
2019-06-03 11:00:20 +01:00
partial-requires-lock $part
lock_required=$(($lock_required * $?))
2019-04-17 15:46:15 +01:00
done
2019-02-11 16:40:33 +00:00
2019-06-03 11:00:20 +01:00
if [ $lock_required -eq 0 ]; then
_lock_or_error_message
2019-06-11 14:01:41 +01:00
local error_code=$?
if [ $error_code -ne 0 ]; then
exit $error_code
2019-06-03 11:00:20 +01:00
fi
fi
2019-04-17 15:46:15 +01:00
for part in $@; do
_check_running
2019-04-17 15:46:15 +01:00
partial-update $part
done
2019-06-03 11:00:20 +01:00
if [ $lock_required -eq 0 ]; then
_unlock
fi
fi
2018-09-26 16:12:04 +01:00
}
2019-02-11 16:40:33 +00:00
trap _onkill 2 3
2019-02-11 16:40:33 +00:00
main $@
2019-06-03 11:00:20 +01:00