diff --git a/check_for_upgrade.sh b/check_for_upgrade.sh new file mode 100644 index 0000000..53451d8 --- /dev/null +++ b/check_for_upgrade.sh @@ -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 diff --git a/shell/env b/shell/env index d5ec83e..1d121dc 100644 --- a/shell/env +++ b/shell/env @@ -6,9 +6,14 @@ export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8 export LANGUAGE=en_US.UTF-8 +export DOTFILES = "${HOME}/dotfiles" + # Paths 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 if [[ -z "$TMUX" ]]; then tmux attach -t C0DE || tmux new -s C0DE diff --git a/shell/functions b/shell/functions index 138b9b0..2f5098d 100644 --- a/shell/functions +++ b/shell/functions @@ -20,7 +20,7 @@ extract () { # Extracts most common compressed files fi } # 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 makezip() { zip -r "${1%%/}.zip" "$1" ; } # Generate password - 1st argument is the number of characters, defaults to 20 diff --git a/shell/zshrc b/shell/zshrc index 3ed321c..4e10408 100644 --- a/shell/zshrc +++ b/shell/zshrc @@ -39,6 +39,12 @@ function insert_sudo { zle -N insert-sudo 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 # Define Oh-My-ZSH root @@ -64,6 +70,7 @@ COMPLETION_WAITING_DOTS="true" # Example format: plugins=(rails git textmate ruby lighthouse) # Add wisely, as too many plugins slow down shell startup. plugins=(git \ + per-directory-history \ virtualenvwrapper \ wp-cli \ nyan) diff --git a/upgrade.sh b/upgrade.sh new file mode 100644 index 0000000..1020a8b --- /dev/null +++ b/upgrade.sh @@ -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