check_for_upgrade.sh 2.8 KB

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