check_for_upgrade.sh 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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" sh "$ZSH/tools/upgrade.sh"
  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 "command rm -rf '$ZSH/log/update.lock'; return 1" EXIT INT QUIT
  45. # Create or update .zsh-update file if missing or malformed
  46. if ! source "${ZSH_CACHE_DIR}/.zsh-update" 2>/dev/null || [[ -z "$LAST_EPOCH" ]]; then
  47. update_last_updated_file
  48. return
  49. fi
  50. # Number of days before trying to update again
  51. epoch_target=${UPDATE_ZSH_DAYS:-13}
  52. # Test if enough time has passed until the next update
  53. if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then
  54. return
  55. fi
  56. # Ask for confirmation before updating unless disabled
  57. if [[ "$DISABLE_UPDATE_PROMPT" = true ]]; then
  58. update_ohmyzsh
  59. else
  60. # input sink to swallow all characters typed before the prompt
  61. # and add a newline if there wasn't one after characters typed
  62. while read -t -k 1 option; do true; done
  63. [[ "$option" != ($'\n'|"") ]] && echo
  64. echo -n "[oh-my-zsh] Would you like to update? [Y/n] "
  65. read -r -k 1 option
  66. [[ "$option" != $'\n' ]] && echo
  67. case "$option" in
  68. [yY$'\n']) update_ohmyzsh ;;
  69. [nN]) update_last_updated_file ;;
  70. esac
  71. fi
  72. }
  73. unset -f current_epoch update_last_updated_file update_ohmyzsh