check_for_upgrade.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Migrate .zsh-update file to $ZSH_CACHE_DIR
  2. if [[ -f ~/.zsh-update && ! -f "${ZSH_CACHE_DIR}/.zsh-update" ]]; then
  3. mv ~/.zsh-update "${ZSH_CACHE_DIR}/.zsh-update"
  4. fi
  5. # Cancel update if:
  6. # - the automatic update is disabled.
  7. # - the current user doesn't have write permissions nor owns the $ZSH directory.
  8. # - git is unavailable on the system.
  9. if [[ "$DISABLE_AUTO_UPDATE" = true ]] \
  10. || [[ ! -w "$ZSH" || ! -O "$ZSH" ]] \
  11. || ! command -v git &>/dev/null; then
  12. return
  13. fi
  14. function current_epoch() {
  15. zmodload zsh/datetime
  16. echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
  17. }
  18. function update_last_updated_file() {
  19. echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update"
  20. }
  21. function update_ohmyzsh() {
  22. ZSH="$ZSH" zsh -f "$ZSH/tools/upgrade.sh" --interactive
  23. update_last_updated_file
  24. }
  25. () {
  26. emulate -L zsh
  27. local epoch_target mtime option LAST_EPOCH
  28. # Remove lock directory if older than a day
  29. zmodload zsh/datetime
  30. zmodload -F zsh/stat b:zstat
  31. if mtime=$(zstat +mtime "$ZSH/log/update.lock" 2>/dev/null); then
  32. if (( (mtime + 3600 * 24) < EPOCHSECONDS )); then
  33. command rm -rf "$ZSH/log/update.lock"
  34. fi
  35. fi
  36. # Check for lock directory
  37. if ! command mkdir "$ZSH/log/update.lock" 2>/dev/null; then
  38. return
  39. fi
  40. # Remove lock directory on exit. `return 1` is important for when trapping a SIGINT:
  41. # The return status from the function is handled specially. If it is zero, the signal is
  42. # assumed to have been handled, and execution continues normally. Otherwise, the shell
  43. # will behave as interrupted except that the return status of the trap is retained.
  44. trap "
  45. unset -f current_epoch update_last_updated_file update_ohmyzsh
  46. command rm -rf '$ZSH/log/update.lock'
  47. return 1
  48. " EXIT INT QUIT
  49. # Create or update .zsh-update file if missing or malformed
  50. if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
  51. update_last_updated_file
  52. return
  53. fi
  54. # Number of days before trying to update again
  55. epoch_target=${UPDATE_ZSH_DAYS:-13}
  56. # Test if enough time has passed until the next update
  57. if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then
  58. return
  59. fi
  60. # Ask for confirmation before updating unless disabled
  61. if [[ "$DISABLE_UPDATE_PROMPT" = true ]]; then
  62. update_ohmyzsh
  63. else
  64. # input sink to swallow all characters typed before the prompt
  65. # and add a newline if there wasn't one after characters typed
  66. while read -t -k 1 option; do true; done
  67. [[ "$option" != ($'\n'|"") ]] && echo
  68. echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
  69. read -r -k 1 option
  70. [[ "$option" != $'\n' ]] && echo
  71. case "$option" in
  72. [yY$'\n']) update_ohmyzsh ;;
  73. [nN]) update_last_updated_file ;;
  74. esac
  75. fi
  76. }