check_for_upgrade.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_CACHE_DIR}/.zsh-update
  8. }
  9. function _upgrade_zsh() {
  10. env ZSH=$ZSH 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. # Cancel upgrade if git is unavailable on the system
  23. whence git >/dev/null || return 0
  24. if mkdir "$ZSH/log/update.lock" 2>/dev/null; then
  25. if [ -f ${ZSH_CACHE_DIR}/.zsh-update ]; then
  26. . ${ZSH_CACHE_DIR}/.zsh-update
  27. if [[ -z "$LAST_EPOCH" ]]; then
  28. _update_zsh_update
  29. rmdir $ZSH/log/update.lock # TODO: fix later
  30. return 0
  31. fi
  32. epoch_diff=$(($(_current_epoch) - $LAST_EPOCH))
  33. if [ $epoch_diff -gt $epoch_target ]; then
  34. if [ "$DISABLE_UPDATE_PROMPT" = "true" ]; then
  35. _upgrade_zsh
  36. else
  37. echo "[Oh My Zsh] Would you like to update? [Y/n]: \c"
  38. read line
  39. if [[ "$line" == Y* ]] || [[ "$line" == y* ]] || [ -z "$line" ]; 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
  50. rmdir $ZSH/log/update.lock
  51. fi