check_for_upgrade.sh 2.9 KB

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