check_for_upgrade.sh 1.2 KB

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