check_for_upgrade.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env zsh
  2. zmodload zsh/datetime
  3. function _current_epoch() {
  4. echo $EPOCHSECONDS
  5. }
  6. function _update_zsh_update() {
  7. echo "LAST_EPOCH=$(_current_epoch)" >! ~/.zsh-update
  8. }
  9. function _upgrade_zsh() {
  10. /usr/bin/env ZSH=$ZSH /bin/sh $ZSH/tools/upgrade.sh
  11. # update the zsh file
  12. _update_zsh_update
  13. }
  14. epoch_target=$UPDATE_ZSH_DAYS
  15. if [[ -z "$epoch_target" ]]; then
  16. # Default to old behavior
  17. epoch_target=13
  18. fi
  19. epoch_target_seconds=$((epoch_target * 86400))
  20. [ -f ~/.profile ] && source ~/.profile
  21. # Cancel upgrade if the current user doesn't have write permissions for the
  22. # oh-my-zsh directory.
  23. [[ -w "$ZSH" ]] || return 0
  24. if [ -f ~/.zsh-update ]
  25. then
  26. . ~/.zsh-update
  27. if [[ -z "$LAST_EPOCH" ]]; then
  28. _update_zsh_update && return 0;
  29. fi
  30. epoch_diff=$(($(_current_epoch) - $LAST_EPOCH))
  31. if [ $epoch_diff -gt $epoch_target_seconds ]
  32. then
  33. if [ "$DISABLE_UPDATE_PROMPT" = "true" ]
  34. then
  35. _upgrade_zsh
  36. else
  37. echo "[Oh My Zsh] Would you like to check for updates?"
  38. echo "Type Y to update oh-my-zsh: \c"
  39. read line
  40. if [ "$line" = Y ] || [ "$line" = y ]; then
  41. _upgrade_zsh
  42. else
  43. _update_zsh_update
  44. fi
  45. fi
  46. fi
  47. else
  48. # create the zsh file
  49. _update_zsh_update
  50. fi