]> arthur.barton.de Git - ax-zsh.git/blob - ax.zsh
Simplify error checking on cache file creation
[ax-zsh.git] / ax.zsh
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # Copyright (c) 2015-2020 Alexander Barton <alex@barton.de>
3
4 script_name="${${(%):-%N}:t}"
5 script_type="$script_name[2,-1]"
6
7 # Handle "initialization stage", load all plugins of that stage, either from an
8 # existing cache file or individually, optionally creating the cache.
9 # - $1: Script name
10 # - $2: Stage name (ax-io, zprofile, zshrc, zlogin, zlogout)
11 function axzsh_handle_stage {
12         local name="$1"
13         local type="$2"
14
15         [[ -n "$AXZSH_DEBUG" ]] && echo "ยป $name ($type):"
16
17         # Look for some 3rd-party integrations ...
18
19         # --- Powerlevel10k ---
20         # Read in Powerlevel10k configuration file, if not already read:
21         [[ -z "$POWERLEVEL9K_CONFIG_FILE" && -r ~/.p10k.zsh ]] && source ~/.p10k.zsh
22         # Enable instant prompt. Should stay close to the top of ~/.zshrc.
23         # Initialization code that may require console input (password prompts,
24         # [y/n] confirmations, etc.) must be executed before this, so all ax-zsh
25         # plugings should do output in their "ax-io" stage only!
26         if [[ "$type" == "zprofile" ]]; then
27                 p10k_instant_prompt="${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
28                 [[ -r "$p10k_instant_prompt" ]] && source "$p10k_instant_prompt"
29         fi
30
31         # Initialize cache
32         [[ -d "$AXZSH/cache" ]] || mkdir -p "$AXZSH/cache"
33         local cache_file="$AXZSH/cache/$type.cache"
34
35         local cat_cmd=${commands[cat]:-cat}
36
37         if [[ -r "$cache_file" ]]; then
38                 # Cache file exists, use it!
39                 # But when in the "zshrc" stage, make sure that the "zprofile" stage
40                 # has already been handled (this uses the "01_zprofile" plugin which
41                 # is used in the "zshrc.cache" as well, but can't be used successfully
42                 # there because it becomes sourced inside of a ZSH function; so we have
43                 # to source it here in the global context manually ...):
44                 [[ -z "$AXZSH_ZPROFILE_READ" && "$type" = "zshrc" ]] \
45                         && source "$AXZSH/core/01_zprofile/01_zprofile.zshrc"
46                 [[ -n "$AXZSH_DEBUG" ]] \
47                         && echo "   - Reading cache file \"$cache_file\" ..."
48                 source "$cache_file"
49                 unfunction axzsh_plugin_init
50         else
51                 # No cache file available.
52                 local new_cache_file="$cache_file.NEW"
53
54                 # Setup list of plugins to load:
55                 local plugin_list
56                 typeset -U plugin_list
57                 plugin_list=(
58                         "$AXZSH/core/"[0-4]*
59                         "$AXZSH/active_plugins/"*(N)
60                         "$AXZSH/core/"[5-9]*
61                 )
62
63                 # Create new cache file:
64                 [[ -n "$AXZSH_DEBUG" ]] \
65                         && echo "   (Writing new cache file to \"$new_cache_file\" ...)"
66                 if ! printf "# %s\n\n" "$(LC_ALL=C date)" >"$new_cache_file"; then
67                         unset new_cache_file
68                 fi
69
70                 # Read in all the plugins for the current "type":
71                 for plugin ($plugin_list); do
72                         # Read the "theme file", if any and in "zshrc" stage.
73                         # This must be done before 99_cleanup is run!
74                         if [[ "$plugin:t" == "99_cleanup" && "$type" = "zshrc" ]]; then
75                                 if [[ -r "$AXZSH_THEME" ]]; then
76                                         source "$AXZSH_THEME"
77                                         if [[ -n "$new_cache_file" ]]; then
78                                                 # Source the theme in the new cache file:
79                                                 echo "# BEGIN Theme" >>"$new_cache_file"
80                                                 echo 'source "$AXZSH_THEME"' >>"$new_cache_file"
81                                                 echo "# END Theme" >>"$new_cache_file"
82                                         fi
83                                 fi
84                         fi
85                         axzsh_load_plugin "$plugin" "$type" "$new_cache_file"
86                 done
87
88                 if [[ -n "$cache_file" && -n "$new_cache_file" && -r "$new_cache_file" ]]; then
89                         # Move newly created cache file in place:
90                         mv "$new_cache_file" "$cache_file"
91                 fi
92         fi
93 }
94
95 # Load plugin code of a given type.
96 # - $1: plugin name
97 # - $2: plugin type (optional; defaults to "zshrc")
98 # - $3: cache file (optional)
99 function axzsh_load_plugin {
100         local dname="$1:A"
101         local plugin="$dname:t"
102         [[ -z "$2" ]] && local type="zshrc" || local type="$2"
103         local fname="$dname/$plugin.$type"
104         local cache_file="$3"
105
106         # Strip repository prefix (like "alexbarton#test-plugin"):
107         [[ "$plugin" =~ "#" ]] && plugin=$(echo $plugin | cut -d'#' -f2-)
108
109         # "short plugin name": strip ".zsh" suffix:
110         plugin_short=${plugin%.zsh}
111
112         if [[ ! -d "$dname" ]]; then
113                 # Plugin not found!
114                 if [[ -n "$AXZSH_DEBUG" ]]; then
115                         # Show error message for all stages in "debug mode":
116                         echo "AX-ZSH plugin \"$plugin\" not found (type \"$type\")!" >&2
117                 elif [[ "$type" == "zshrc" ]]; then
118                         # Show error message for the "zshrc" stage:
119                         echo "AX-ZSH plugin \"$plugin\" not found, skipped!" >&2
120                 fi
121                 return 1
122         fi
123
124         if [[ ! -r "$fname" && "$type" == "zshrc" ]]; then
125                 zsh_themes=("$dname/"*.zsh-theme(NY1))
126                 if [[ -r "$dname/$plugin.ax-io" || -r "$dname/$plugin.zprofile" || -r "$dname/$plugin.zlogout" ]]; then
127                         # Native AX-ZSH plugin, but for different stage. Skip it!
128                         :
129                 elif [[ -r "$dname/${plugin_short}.plugin.zsh" ]]; then
130                         # Oh My ZSH plugin
131                         type="plugin.zsh"
132                         fname="$dname/${plugin_short}.plugin.zsh"
133                 elif [[ -r "$dname/${plugin_short##zsh-}.plugin.zsh" ]]; then
134                         # Oh My ZSH plugin with "zsh-" prefix stripped
135                         type="plugin.zsh"
136                         fname="$dname/${plugin_short##zsh-}.plugin.zsh"
137                 elif [[ -r "$dname/${plugin%.plugin.zsh}.plugin.zsh" ]]; then
138                         # Oh My ZSH plugin with ".plugin.zsh" suffix in its name
139                         type="plugin.zsh"
140                         fname="$dname/${plugin}"
141                 elif [[ -r "$dname/init.zsh" ]]; then
142                         # Prezto module
143                         type="init.zsh"
144                         fname="$dname/init.zsh"
145                 elif [[ ${#zsh_themes} -gt 0 ]]; then
146                         # ZSH "theme plugin", ignore here!
147                         :
148                 else
149                         echo "AX-ZSH plugin type of \"$plugin\" unknown, skipped!" >&2
150                         echo "Contents of \"$dname\":" >&2
151                         ls -lh "$dname/" >&2
152                         return 0
153                 fi
154         fi
155
156         if [[ "$type" == "zprofile" && -d "$dname/functions" ]]; then
157                 # Add plugin function path when folder exists
158                 [[ -n "$AXZSH_DEBUG" ]] \
159                         && echo "   - $plugin ($type): functions ..."
160                 axzsh_fpath+=("$dname/functions")
161
162                 # Add function path to cache file.
163                 [[ -n "$cache_file" ]] \
164                         && echo "axzsh_fpath+=('$dname/functions')" >>$cache_file
165         fi
166
167         if [[ -r "$fname" ]]; then
168                 # Read plugin ...
169                 [[ -n "$AXZSH_DEBUG" ]] \
170                         && echo "   - $plugin ($type) ..."
171
172                 # Note for "external" ("repo/*") plugins and unusual ("not so
173                 # modern") terminals: These (modern?) plugins most probably
174                 # don't expect such a terminal configuration and don't behave
175                 # well (echo color sequences, for example). Therefore we DON'T
176                 # load any external plugins at all in that case: this results in
177                 # reduced/disabled functionality, but hopefully in readable
178                 # output ...
179
180                 case "$fname" in
181                         *"/repos/"*)
182                                 axzsh_is_modern_terminal && source "$fname"
183                                 ;;
184                         *)
185                                 source "$fname"
186                 esac
187
188                 if [[ -n "$cache_file" ]]; then
189                         # Add plugin data to cache
190                         printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$fname" >>"$cache_file"
191                         case "$fname" in
192                                 *"/repos/"*)
193                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type): \"$fname\" ...'" >>$cache_file
194                                         echo "axzsh_is_modern_terminal && source '$fname'" >>$cache_file
195                                         ;;
196                                 *)
197                                         echo "[[ -n \"\$AXZSH_DEBUG\" ]] && echo '     - $plugin ($type, cached) ...'" >>$cache_file
198                                         "$cat_cmd" "$fname" >>"$cache_file"
199                         esac
200                         printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$fname" >>"$cache_file"
201                 fi
202         fi
203
204         # It is a success, even if only the plugin directory (and no script!)
205         # exists at all! Rationale: The script could be of an other type ...
206         return 0
207 }
208
209 # Make sure that "my" (=ZSH) directory is in the search path ...
210 if [[ -z "$AXZSH" ]]; then
211         _p="${0:h}"
212         [[ "$_p" != "." ]] && PATH="$PATH:${0:h}"
213         unset _p
214 fi
215
216 # Make sure that "SHELL" variable is set and exported
217 [[ -n "$SHELL" ]] || export SHELL=$(command -v zsh)
218
219 # Make sure that "AXZSH" variable is set and exported
220 [[ -n "$AXZSH" ]] || export AXZSH="$HOME/.axzsh"
221
222 # Check for "debug mode" ...
223 if [[ -f "$AXZSH/debug" || -f "$HOME/.axzsh.debug" ]]; then
224         export AXZSH_DEBUG=1
225         export POWERLEVEL9K_INSTANT_PROMPT=quiet
226         echo "AXZSH=$AXZSH"
227         echo "AXZSH_DEBUG=$AXZSH_DEBUG"
228         echo "AXZSH_PLUGIN_D=$AXZSH_PLUGIN_D"
229         echo "AXZSH_ZLOGIN_READ=$AXZSH_ZLOGIN_READ"
230         echo "AXZSH_ZPROFILE_READ=$AXZSH_ZPROFILE_READ"
231 fi
232
233 if [[ "$script_type" = "zprofile" ]]; then
234         # Load all "output" plugins first, that is, before the "zprofile stage":
235         axzsh_handle_stage "$script_name" "ax-io"
236 fi
237
238 axzsh_handle_stage "$script_name" "$script_type"
239
240 # Clean up ...
241 unfunction axzsh_handle_stage axzsh_load_plugin
242 unset script_name script_type
243
244 # Hints for external installers:
245 # - iTerm2: DON'T install "iterm2_shell_integration"!