3c9743313cc916a6cede71e333eecef4b07ce3ce

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(updater): change auto-update settings to use `zstyle`

Diff

 1diff --git a/templates/zshrc.zsh-template b/templates/zshrc.zsh-template
 2index 4a998c2fcfb1954002dc933b81436d3592a5a630..7cb87078c126adf999f858fbbafac71268a94c34 100644
 3--- a/templates/zshrc.zsh-template
 4+++ b/templates/zshrc.zsh-template
 5@@ -23,14 +23,12 @@ ZSH_THEME="robbyrussell"
 6 # Case-sensitive completion must be off. _ and - will be interchangeable.
 7 # HYPHEN_INSENSITIVE="true"
 8 
 9-# Uncomment the following line to disable bi-weekly auto-update checks.
10-# DISABLE_AUTO_UPDATE="true"
11-
12-# Uncomment the following line to automatically update without prompting.
13-# DISABLE_UPDATE_PROMPT="true"
14+# Uncomment the following lines to change the auto-update behavior
15+# zstyle ':omz:update' mode disabled  # disable automatic updates
16+# zstyle ':omz:update' mode auto      # update automatically without asking
17 
18 # Uncomment the following line to change how often to auto-update (in days).
19-# export UPDATE_ZSH_DAYS=13
20+# zstyle ':omz:update' frequency 13
21 
22 # Uncomment the following line if pasting URLs and other text is messed up.
23 # DISABLE_MAGIC_FUNCTIONS="true"
24diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh
25index e87da3487634c3cc6f32ce83405e49d5b22875c7..fa832ffac187fddb8d8073f894e63edef51a512e 100644
26--- a/tools/check_for_upgrade.sh
27+++ b/tools/check_for_upgrade.sh
28@@ -3,17 +3,30 @@ if [[ -f ~/.zsh-update && ! -f "${ZSH_CACHE_DIR}/.zsh-update" ]]; then
29   mv ~/.zsh-update "${ZSH_CACHE_DIR}/.zsh-update"
30 fi
31 
32+# Get user's update preferences
33+#
34+# Supported update modes:
35+# - prompt (default): the user is asked before updating when it's time to update
36+# - auto: the update is performed automatically when it's time
37+# - reminder: a reminder is shown to the user when it's time to update
38+# - disabled: automatic update is turned off
39+zstyle -s ':omz:update' mode update_mode || update_mode=prompt
40+
41+# Support old-style settings
42+[[ "$DISABLE_UPDATE_PROMPT" != true ]] || update_mode=auto
43+[[ "$DISABLE_AUTO_UPDATE" != true ]] || update_mode=disabled
44+
45 # Cancel update if:
46 # - the automatic update is disabled.
47 # - the current user doesn't have write permissions nor owns the $ZSH directory.
48 # - git is unavailable on the system.
49-if [[ "$DISABLE_AUTO_UPDATE" = true ]] \
50+if [[ "$update_mode" = disabled ]] \
51    || [[ ! -w "$ZSH" || ! -O "$ZSH" ]] \
52    || ! command -v git &>/dev/null; then
53+  unset update_mode
54   return
55 fi
56 
57-
58 function current_epoch() {
59   zmodload zsh/datetime
60   echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
61@@ -56,7 +69,6 @@ function update_ohmyzsh() {
62   #  the shell actually exits what it's running.
63   trap "
64     ret=\$?
65-    unset -f current_epoch update_last_updated_file update_ohmyzsh 2>/dev/null
66     command rm -rf '$ZSH/log/update.lock'
67     return \$ret
68   " EXIT INT QUIT
69@@ -68,14 +80,14 @@ function update_ohmyzsh() {
70   fi
71 
72   # Number of days before trying to update again
73-  epoch_target=${UPDATE_ZSH_DAYS:-13}
74+  zstyle -s ':omz:update' frequency epoch_target || epoch_target=${UPDATE_ZSH_DAYS:-13}
75   # Test if enough time has passed until the next update
76   if (( ( $(current_epoch) - $LAST_EPOCH ) < $epoch_target )); then
77     return
78   fi
79 
80-  # Ask for confirmation before updating unless disabled
81-  if [[ "$DISABLE_UPDATE_PROMPT" = true ]]; then
82+  # Ask for confirmation before updating unless in auto mode
83+  if [[ "$update_mode" = auto ]]; then
84     update_ohmyzsh
85   else
86     # input sink to swallow all characters typed before the prompt
87@@ -92,3 +104,6 @@ function update_ohmyzsh() {
88     esac
89   fi
90 }
91+
92+unset update_mode
93+unset -f current_epoch update_last_updated_file update_ohmyzsh