check_for_upgrade.sh 1.1 KB

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