Automatic updates

By default, this should prompt to update once every 14 days.
If user answers anything other than y/Y, wait until next cycle
If user answers y/Y, pull down the current branch's dotfiles
(ensure that your machine is on the correct branch if you have multiple)
This commit is contained in:
David Todd (c0de) 2018-02-10 21:36:07 -06:00
parent 46f08706bf
commit 81301e3ccc
5 changed files with 110 additions and 1 deletions

60
check_for_upgrade.sh Normal file
View File

@ -0,0 +1,60 @@
#!/usr/bin/env zsh
zmodload zsh/datetime
function _current_epoch() {
echo $(( $EPOCHSECONDS / 60 / 60 / 24 ))
}
function _update_dotfiles_update() {
echo "LAST_EPOCH=$(_current_epoch)" >! ~/.dotfiles-update
}
function _upgrade_dotfiles() {
env DOTFILES=$DOTFILES sh $DOTFILES/upgrade.sh
# update the zsh file
_update_zsh_update
}
epoch_target=$UPDATE_ZSH_DAYS
if [[ -z "$epoch_target" ]]; then
# Default to old behavior
epoch_target=13
fi
# Cancel upgrade if the current user doesn't have write permissions for the
# dotfiles directory.
[[ -w "$DOTFILES" ]] || return 0
# Cancel upgrade if git is unavailable on the system
whence git >/dev/null || return 0
if mkdir "$DOTFILES/log/update.lock" 2>/dev/null; then
if [ -f ~/.dotfiles-update ]; then
. ~/.dotfiles-update
if [[ -z "$LAST_EPOCH" ]]; then
_update_zsh_update && return 0;
fi
epoch_diff=$(($(_current_epoch) - $LAST_EPOCH))
if [ $epoch_diff -gt $epoch_target ]; then
if [ "$DISABLE_UPDATE_PROMPT" = "true" ]; then
_upgrade_dotfiles
else
echo "[Dotfiles] Would you like to check for updates? [Y/n]: \c"
read line
if [[ "$line" == Y* ]] || [[ "$line" == y* ]] || [ -z "$line" ]; then
_upgrade_dotfiles
else
_update_dotfiles_update
fi
fi
fi
else
# create the zsh file
_update_dotfiles_update
fi
rmdir $DOTFILES/log/update.lock
fi

View File

@ -6,9 +6,14 @@ export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8 export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8 export LANGUAGE=en_US.UTF-8
export DOTFILES = "${HOME}/dotfiles"
# Paths # Paths
export PATH=$PATH:${HOME}/bin:/usr/lib/wine/bin:/sbin:/usr/sbin:/usr/local/bin export PATH=$PATH:${HOME}/bin:/usr/lib/wine/bin:/sbin:/usr/sbin:/usr/local/bin
# Allow automatic updates
DISABLE_DOTFILES_AUTO_UPDATE = "FALSE"
# Automatically attach tmux session "C0DE" upon entering a shell # Automatically attach tmux session "C0DE" upon entering a shell
if [[ -z "$TMUX" ]]; then if [[ -z "$TMUX" ]]; then
tmux attach -t C0DE || tmux new -s C0DE tmux attach -t C0DE || tmux new -s C0DE

View File

@ -20,7 +20,7 @@ extract () { # Extracts most common compressed files
fi fi
} }
# Takes 1st argument and makes a .tar.gz archive # Takes 1st argument and makes a .tar.gz archive
maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; } maketgz() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
# Takes 1st argument and makes a .zip archive # Takes 1st argument and makes a .zip archive
makezip() { zip -r "${1%%/}.zip" "$1" ; } makezip() { zip -r "${1%%/}.zip" "$1" ; }
# Generate password - 1st argument is the number of characters, defaults to 20 # Generate password - 1st argument is the number of characters, defaults to 20

View File

@ -39,6 +39,12 @@ function insert_sudo {
zle -N insert-sudo insert_sudo zle -N insert-sudo insert_sudo
bindkey "^[s" insert-sudo bindkey "^[s" insert-sudo
# Check for updates...
# Stolen and modified Oh-My-ZSH's update system
if [ "$DISABLE_DOTFILES_AUTO_UPDATE" != "true" ]; then
env ZSH=$ZSH DOTFILES=$DOTFILES DISABLE_UPDATE_PROMPT=$DISABLE_UPDATE_PROMPT zsh -f $DOTFILES/check_for_upgrade.sh
fi
# Oh-My-ZSH Options below this line # Oh-My-ZSH Options below this line
# Define Oh-My-ZSH root # Define Oh-My-ZSH root
@ -64,6 +70,7 @@ COMPLETION_WAITING_DOTS="true"
# Example format: plugins=(rails git textmate ruby lighthouse) # Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup. # Add wisely, as too many plugins slow down shell startup.
plugins=(git \ plugins=(git \
per-directory-history \
virtualenvwrapper \ virtualenvwrapper \
wp-cli \ wp-cli \
nyan) nyan)

37
upgrade.sh Normal file
View File

@ -0,0 +1,37 @@
# Use colors, but only if connected to a terminal, and that terminal
# supports them.
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
BOLD=""
NORMAL=""
fi
printf "${BLUE}%s${NORMAL}\n" "Updating your dotfiles"
cd "$DOTFILES"
branch=$(git name-rev --name-only HEAD)
if git pull --rebase --stat origin $branch
then
printf '%s' "$GREEN"
printf '%s\n' ' _____ ______ ______ ______ __ __ ______ ______ '
printf '%s\n' '/\ __-. /\ __ \ /\__ _\ /\ ___\ /\ \ /\ \ /\ ___\ /\ ___\ '
printf '%s\n' '\ \ \/\ \ \ \ \/\ \ \/_/\ \/ \ \ __\ \ \ \ \ \ \____ \ \ __\ \ \___ \ '
printf '%s\n' ' \ \____- \ \_____\ \ \_\ \ \_\ \ \_\ \ \_____\ \ \_____\ \/\_____\ '
printf '%s\n' ' \/____/ \/_____/ \/_/ \/_/ \/_/ \/_____/ \/_____/ \/_____/ '
printf "${BLUE}%s\n" "Hooray! Your dotfiles have been updated and/or is at the current version."
else
printf "${RED}%s${NORMAL}\n" 'There was an error updating. Try again later?'
fi