diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh old mode 100644 new mode 100755 index e1e4eb99f..4193f7a83 --- a/tools/check_for_upgrade.sh +++ b/tools/check_for_upgrade.sh @@ -1,5 +1,7 @@ #!/bin/sh +source ./common + function _current_epoch() { echo $(($(date +%s) / 60 / 60 / 24)) } @@ -19,15 +21,17 @@ then epoch_diff=$(($(_current_epoch) - $LAST_EPOCH)) if [ $epoch_diff -gt 6 ] then - echo "[Oh My Zsh] Would you like to check for updates?" - echo "Type Y to update oh-my-zsh: \c" + note '[Oh My Zsh] Would you like to check for updates?' + query 'Type Y to update oh-my-zsh:' read line if [ "$line" = Y ] || [ "$line" = y ] then - /bin/sh $ZSH/tools/upgrade.sh + ./upgrade.sh # update the zsh file _update_zsh_update fi + else + proclaim 'Updated recently.' fi else # create the zsh file diff --git a/tools/common b/tools/common new file mode 100644 index 000000000..69a048adb --- /dev/null +++ b/tools/common @@ -0,0 +1,30 @@ +COLOR_DEFAULT='\033[0m' +COLOR_LIGHTRED='\033[1;31m' +COLOR_GREEN='\033[0;32m' +COLOR_LIGHTGREEN='\033[1;32m' +COLOR_BROWN='\033[0;33m' +COLOR_PINK='\033[1;35m' +COLOR_YELLOW='\033[1;33m' +COLOR_WHITE='\033[1;37m' + +function echo_color { + for i in "${@:3}" + do + eval printf "\$COLOR_${1}" + printf "$2$i\n" + done + printf $COLOR_DEFAULT +} + +function query { echo_color 'BROWN' ' ' "$@"; printf ' > '; } +function text { echo_color 'WHITE' '' "$@"; } +function proclaim { echo_color 'PINK' \ "$@"; } +function info { echo_color 'BROWN' \ "$@"; } +function note { echo_color 'YELLOW' \ "$@"; } +function shell_example { + printf '\n'; + echo_color 'LIGHTGREEN' "\t>${COLOR_GREEN} " "$@" + printf '\n' +} +function warn { echo_color 'LIGHTRED' \ "$@"; } + diff --git a/tools/install.sh b/tools/install.sh index aedb706da..da837d24c 100755 --- a/tools/install.sh +++ b/tools/install.sh @@ -1,36 +1,47 @@ + + ################ + # install.sh # + ################ + +source ./common + if [ -d ~/.oh-my-zsh ] then - echo "\033[0;33mYou already have Oh My Zsh installed.\033[0m You'll need to remove ~/.oh-my-zsh if you want to install" - exit + proclaim 'You already have Oh My Zsh installed' + note 'You\47ll need to remove ~/.oh-my-zsh if you want to install' + exit 1 fi -echo "\033[0;34mCloning Oh My Zsh...\033[0m" -/usr/bin/env git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh +proclaim 'Installing Oh-My-Zsh' +info 'Cloning Oh My Zsh...' +/usr/bin/env git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh \ + || ( warn 'Couldn\47t clone repository.'; exit 2) -echo "\033[0;34mLooking for an existing zsh config...\033[0m" +info 'Looking for an existing zsh config...' if [ -f ~/.zshrc ] || [ -h ~/.zshrc ] then - echo "\033[0;33mFound ~/.zshrc.\033[0m \033[0;32]Backing up to ~/.zshrc.pre-oh-my-zsh\033[0m"; - cp ~/.zshrc ~/.zshrc.pre-oh-my-zsh; - rm ~/.zshrc; + info 'Found ~/.zshrc.' + info 'Backing up to ~/.zshrc.pre-oh-my-zsh' + cp -n ~/.zshrc ~/.zshrc.pre-oh-my-zsh && rm ~/.zshrc \ + || ( warn 'Couldn\47t backup .zshrc!'; exit 3) fi -echo "\033[0;34mUsing the Oh My Zsh template file and adding it to ~/.zshrc\033[0m" -cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc +info 'Using the Oh My Zsh template file and adding it to ~/.zshrc' +cp -n ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc || exit 4 -echo "\033[0;34mCopying your current PATH and adding it to the end of ~/.zshrc for you.\033[0m" +info 'Copying your current PATH and adding it to the end of ~/.zshrc for you.' echo "export PATH=$PATH" >> ~/.zshrc -echo "\033[0;34mTime to change your default shell to zsh!\033[0m" -chsh -s `which zsh` +note 'You might need to change your default shell to zsh:' +shell_example 'chsh -s $(which zsh)' -echo "\033[0;32m"' __ __ '"\033[0m" -echo "\033[0;32m"' ____ / /_ ____ ___ __ __ ____ _____/ /_ '"\033[0m" -echo "\033[0;32m"' / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ '"\033[0m" -echo "\033[0;32m"'/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / '"\033[0m" -echo "\033[0;32m"'\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ '"\033[0m" -echo "\033[0;32m"' /____/ '"\033[0m" +proclaim ' __ __ ' +proclaim ' ____ / /_ ____ ___ __ __ ____ _____/ /_ ' +proclaim ' / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ ' +proclaim '/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / ' +proclaim '\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ ' +proclaim ' /____/ ' +proclaim ' is now installed.' + +/usr/bin/env zsh && source ~/.zshrc; -echo "\n\n \033[0;32m....is now installed.\033[0m" -/usr/bin/env zsh -source ~/.zshrc diff --git a/tools/uninstall.sh b/tools/uninstall.sh old mode 100644 new mode 100755 index 8ff583322..3164335b7 --- a/tools/uninstall.sh +++ b/tools/uninstall.sh @@ -1,20 +1,41 @@ -echo "Removing ~/.oh-my-zsh" -if [[ -d ~/.oh-my-zsh ]] -then - rm -rf ~/.oh-my-zsh + + ################## + # uninstall.sh # + ################## + +source ./common +proclaim 'Uninstalling Oh-My-Zsh' + +if [[ -d ~/.oh-my-zsh ]]; then + changes=`diff --unchanged-group-format='' --suppress-common-lines ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc` + changes=`echo "$changes" | grep -v "^export PATH=$PATH$"` + if [ ! -z "$changes" ]; then + info 'Appending changes to ~/.zshrc to ~/.zshrc.changes:' + text "$changes" + echo "$changes" >> ~/.zshrc.changes \ + || ( warn 'Cannot append to ~/.zshrc.changes!'; exit 1 ) + fi + info 'Removing ~/.zshrc' + rm ~/.zshrc + info 'Removing ~/.oh-my-zsh' + rm -rf ~/.oh-my-zsh; +else + warn 'Cannot find ~/.oh-my-zsh' + exit 2 fi -echo "Looking for an existing zsh config..." if [ -f ~/.zshrc.pre-oh-my-zsh ] || [ -h ~/.zshrc.pre-oh-my-zsh ] then - echo "Found ~/.zshrc. Backing up to ~/.zshrc.pre-oh-my-zsh"; - rm ~/.zshrc; - cp ~/.zshrc.pre-oh-my-zsh ~/.zshrc; + info 'Found ~/.zshrc.pre-oh-my-zsh, Restoring to ~/.zshrc' + mv ~/.zshrc.pre-oh-my-zsh ~/.zshrc \ + || ( warn 'Cannot restore ~/.zshrc!'; exit 3 ) source ~/.zshrc; else - echo "Switching back to bash" - chsh -s /bin/bash - source /etc/profile + note 'You might want to switch back to bash:' + shell_example \ + 'chsh -s /bin/bash' \ + 'source /etc/profile' fi -echo "Thanks for trying out Oh My Zsh. It's been uninstalled." \ No newline at end of file +proclaim 'Thanks for trying out \47Oh My Zsh\47, It is no longer installed' + diff --git a/tools/upgrade.sh b/tools/upgrade.sh old mode 100644 new mode 100755 index 52a8cc4da..cf3400426 --- a/tools/upgrade.sh +++ b/tools/upgrade.sh @@ -1,12 +1,30 @@ + +source ./common + +if [ ! -d ~/.oh-my-zsh ]; then + warn 'Cannot find ~/.oh-my-zsh' + exit 1 +fi +proclaim 'Upgrading Oh My Zsh' + +# I think pushd/popd might be cleaner, +# but more of a risk if they are over-ridden? current_path=`pwd` -echo -e "\033[0;34mUpgrading Oh My Zsh\033[0m" -( cd $ZSH && git pull origin master ) -echo -e "\033[0;32m"' __ __ '"\033[0m" -echo -e "\033[0;32m"' ____ / /_ ____ ___ __ __ ____ _____/ /_ '"\033[0m" -echo -e "\033[0;32m"' / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ '"\033[0m" -echo -e "\033[0;32m"'/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / '"\033[0m" -echo -e "\033[0;32m"'\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ '"\033[0m" -echo -e "\033[0;32m"' /____/ '"\033[0m" -echo -e "\033[0;34mHooray! Oh My Zsh has been updated and/or is at the current version.\033[0m" -echo -e "\033[0;34mTo keep up on the latest, be sure to follow Oh My Zsh on twitter: \033[1mhttp://twitter.com/ohmyzsh\033[0m" + +# Is there a better way to ensure $ZSH is passed? +cd "${ZSH:-$HOME/.oh-my-zsh}" \ + && git pull origin master \ + || ( warn 'Cannot upgrade Zsh!'; cd "$current_path"; exit 1 ) + +proclaim ' __ __ ' +proclaim ' ____ / /_ ____ ___ __ __ ____ _____/ /_ ' +proclaim ' / __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \ ' +proclaim '/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / / ' +proclaim '\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/ ' +proclaim ' /____/ ' +proclaim 'Hooray! Oh My Zsh has been updated and/or is at the current version.' +proclaim 'To keep up on the latest, be sure to follow Oh My Zsh on twitter:' +proclaim "${COLOR_WHITE}http://twitter.com/ohmyzsh" + cd "$current_path" +