]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
axzshctl: Be verbose for "axzshctl update-caches" in debug mode
[ax-zsh.git] / bin / axzshctl
1 #!/usr/bin/env zsh
2 #
3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2021 Alexander Barton <alex@barton.de>
5 #
6
7 # Embedded ax-common compatibility functions ...
8 function ax_msg {
9         case "$1" in
10                 "0")    c="32"; ;;
11                 "1")    c="33"; ;;
12                 "2")    c="31"; ;;
13                 "-")    c="1";  ;;
14                 *)      c="0";  ;;
15         esac
16         shift
17         printf "\e[${c}m%s\e[0m\n" "$@"
18 }
19 function ax_error {
20         ax_msg 2 "$@" >&2
21 }
22
23 function Version {
24         echo "ax-zsh -- Modular configuration system for the Z shell (ZSH)"
25         echo "Copyright (c) 2015-2019 Alexander Barton <alex@barton.de>."
26         echo "Licensed under the terms of the MIT license, see LICENSE.md for details."
27         echo "Homepage: <https://github.com/alexbarton/ax-zsh>"
28         if [[ -d "$AXZSH/.git" && -n "$commands[git]" ]]; then
29                 echo -n "Version: Git ID "
30                 ( cd "$AXZSH" && git describe --always )
31         fi
32         echo
33         exit 0
34 }
35
36 function Usage {
37         echo "Usage: $NAME <command> [...]"
38         echo
39         echo "  enable"
40         echo "    Enable AX-ZSH altogether."
41         echo "  disable"
42         echo "    Disable AX-ZSH altogether."
43         echo
44         echo "  enable-plugin <name|directory> [<name|directory> [...]]"
45         echo "    Enable plugin(s)."
46         echo "  disable-plugin <name> [<name> [...]]"
47         echo "    Disable plugin(s)."
48         echo "  list-enabled"
49         echo "    List enabled plugins."
50         echo
51         echo "  reset-plugins"
52         echo "    Reset active plugins to the default set."
53         echo "  enable-default-plugins"
54         echo "    Enable all default plugins."
55         echo "  check-plugins"
56         echo "    Detect plugins which are \"useful\" on this system."
57         echo
58         echo "  set-theme {<name>|-}"
59         echo "    Set active theme to <name>, or to the default."
60         echo
61         echo "  upgrade"
62         echo "    Upgrade AX-ZSH installation (requires Git)."
63         echo "  update-caches"
64         echo "    Force rebuild of all cache files."
65         echo
66         exit 0
67 }
68
69 function UpdatePluginCache {
70         [[ -r "$AXZSH/cache" ]] || return 0
71
72         [[ "$1" = "-v" ]] && ax_msg - "Invalidating & updating caches ..."
73
74         if [[ -d "$ZSH_CACHE_DIR" ]]; then
75                 [[ "$1" = "-v" ]] && echo "Removing ZSH cache folder ..."
76                 rm -fr "$ZSH_CACHE_DIR"
77         fi
78
79         [[ "$1" = "-v" ]] && echo "Removing AX-ZSH cache files ..."
80         rm -rf \
81                 $AXZSH/cache/ax-io.cache \
82                 $AXZSH/cache/zlogin.cache \
83                 $AXZSH/cache/zlogout.cache \
84                 $AXZSH/cache/zprofile.cache \
85                 $AXZSH/cache/zshrc.cache \
86                 || return 1
87
88         echo "Regenerating AX-ZSH cache ..."
89         [[ -z "$AXZSH_DEBUG" ]] \
90                 && AXZSH_PLUGIN_CHECK=1 zsh -ilc '' >/dev/null \
91                 || AXZSH_PLUGIN_CHECK=1 zsh -ilc ''
92 }
93
94 function NormalizedPluginName {
95         if [[ "$1" =~ "^@?[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
96                 echo "${1:gs/\//#}"
97         elif [[ "$1" =~ "/" ]]; then
98                 echo "${1:t}"
99         else
100                 echo "$1"
101         fi
102 }
103
104 function EnableAXZSH {
105         for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
106                 ln -s "$AXZSH/ax.zsh" "$f" \
107                         || ax_error "Failed to create symbolic link for \"$f\"!"
108         done
109         if [[ -z "$AXZSH_FPATH" ]]; then
110                 echo "AX-ZSH was enabled. Now you should restart your shell, for example like this:"
111                 echo "$ exec -l \"\$SHELL\""
112         fi
113 }
114
115 function DisableAXZSH {
116         for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
117                 if [[ -h "$f" ]]; then
118                         rm "$f" || ax_msg 2 "Failed to remove \"$f\"!"
119                 elif [[ -e "$f" ]]; then
120                         ax_error "Error: Not removing \"$f\", it is not a symbolic link!"
121                 else
122                         ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
123                 fi
124         done
125 }
126
127 function EnablePlugin {
128         local plugin=$(NormalizedPluginName "$1")
129         local dir="$AXZSH/active_plugins"
130         local name="$plugin"
131
132         if [[ -h "$dir/$plugin" ]]; then
133                 ax_msg 1 "Plugin \"$1\" already active!"
134                 return 1
135         fi
136
137         if [[ "$1" =~ "^@[[:alnum:]-]+/[[:alnum:]_.+-]+$" ]]; then
138                 # GitHub plugin repository (like OhMyZsh)
139                 local repo="${1##@}"
140                 repo="${repo%/*}"
141                 mkdir -p "$AXZSH/repos/@$repo"
142                 if [[ ! -d "$AXZSH/repos/@$repo/plugins" ]]; then
143                         ax_msg - "Cloning \"$repo\" from GitHub ..."
144                         git clone --depth=1 "https://github.com/$repo/$repo.git" \
145                          "$AXZSH/repos/@$repo" \
146                                 || ax_error "Failed to clone \"$repo\" repository!"
147                 fi
148                 plugin="@$repo/plugins/${1#*/}"
149                 echo "Trying to enable \"$1\" ..."
150         elif [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_.-]+$" ]]; then
151                 # GitHub plugin
152                 mkdir -p "$AXZSH/repos"
153                 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
154                         ax_msg - "Cloning module from GitHub ..."
155                         git clone --depth=1 "https://github.com/$1.git" \
156                          "$AXZSH/repos/$plugin" \
157                                 || ax_error "Failed to clone repository!"
158                 fi
159                 # Try to enable a theme in this "foreign module", but ignore
160                 # errors: we don't know if this module provides a theme or is
161                 # a "regular" plugin ...
162                 if SetTheme "$plugin" 2>/dev/null; then
163                         ax_msg 0 "Module \"$1\" was enabled as theme \"${plugin#*#}\"."
164                         # A theme was enabled: So assume that this is a theme
165                         # and don't enable it as plugin.
166                         return 0
167                 fi
168                 echo "Trying to enable \"$1\" as plugin ..."
169         elif ! [[ "$1" =~ "^[[:alnum:]_.-]+$" ]]; then
170                 ax_error "Invalid plugin name!"
171                 return 1
172         fi
173
174         for dname (
175                 "$plugin:A"
176                 "$AXZSH_PLUGIN_D/$plugin"
177                 "$ZSH_CUSTOM/$plugin"
178                 "$AXZSH/custom_plugins/$plugin"
179                 "$AXZSH/repos/$plugin"
180                 "$AXZSH/plugins/$plugin"
181                 "$AXZSH/default_plugins/$plugin"
182                 "$AXZSH/core/$plugin"
183         ); do
184                 [[ ! -d "$dname" ]] && continue
185                 mkdir -p "$dir"
186                 if ! (
187                         cd "$dir" || exit 9
188                         ln -s "$dname" "$PWD/$name"
189                 ); then
190                         ax_error "Failed to create link!"
191                         return 1
192                 fi
193                 ax_msg 0 "Plugin \"$1\" enabled."
194                 return 0
195         done
196
197         ax_error "Plugin \"$1\" not found!"
198         return 1
199 }
200
201 function DisablePlugin {
202         local plugin=$(NormalizedPluginName "$1")
203         local dir="$AXZSH/active_plugins"
204         local r=-1
205
206         # Active theme?
207         if [[ $(readlink "$AXZSH/active_theme") = "$AXZSH/repos/$plugin/"* ]]; then
208                 rm "$AXZSH/active_theme"; r=$?
209         fi
210
211         # Active plugin?
212         if [[ -h "$dir/$plugin" ]]; then
213                 rm "$dir/$plugin"; r=$?
214         fi
215
216         if [[ $r -eq -1 ]]; then
217                 ax_msg 1 "Plugin \"$1\" not active, nothing to do?"
218                 r=1
219         fi
220
221         if [[ "$plugin" = *"#"* ]]; then
222                 # Name matches a cloned repository, try to clean up!
223                 echo "Cleaning up cloned repository ..."
224                 rm -fr "$AXZSH/repos/$plugin"
225         fi
226
227         return $r
228 }
229
230 function ListEnabledPlugins {
231         for plugin ($AXZSH/active_plugins/*(N)); do
232                 print ${plugin:t:s/#/\//}
233         done
234         return 0
235 }
236
237 function ResetPlugins {
238         local dir="$AXZSH/active_plugins"
239         local r1=0, r2=0
240
241         if [[ -e "$dir" ]]; then
242                 ax_msg - "Removing all symbolic links in $dir ..."
243                 find "$dir" -type l -print -delete; r1=$?
244         fi
245
246         ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
247         rm -fr "$AXZSH/repos"; r2=$?
248
249         [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
250 }
251
252 function EnableDefaultPlugins {
253         local dir="$AXZSH/active_plugins"
254
255         ax_msg - "Activating default plugins ..."
256         mkdir -p "$dir"
257         (
258                 cd "$dir" || exit 9
259                 ln -sf "$AXZSH/default_plugins/"* "$PWD"
260         )
261         return $?
262 }
263
264 function SetTheme {
265         local link_name="$AXZSH/active_theme"
266
267         # --- Powerlevel10k ---
268         # Remove "instant prompt" configuration, if any ...
269         rm -f "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
270
271         if [[ "$1" = "-" ]]; then
272                 rm -f "$link_name" || return 1
273                 ax_msg 0 "Theme settings have been reset."
274                 return 0
275         fi
276
277         if [[ -r "$1" ]]; then
278                 theme="$1"
279         elif [[ -r "$AXZSH/custom_themes/$1.axzshtheme" ]]; then
280                 theme="$AXZSH/custom_themes/$1.axzshtheme"
281         elif [[ -r "$AXZSH/themes/$1.axzshtheme" ]]; then
282                 theme="$AXZSH/themes/$1.axzshtheme"
283         else
284                 # Look for theme in specific remote module:
285                 for f (
286                         "$AXZSH/repos/$1/"*.axzshtheme(N[1])
287                         "$AXZSH/repos/$1/"*.zsh-theme(N[1])
288                 ); do
289                         if [[ -r "$f" ]]; then
290                                 theme="$f"
291                                 break
292                         fi
293                 done
294
295                 # Look for theme inside of installed plugins:
296                 for dname (
297                         "$AXZSH/custom_themes"
298                         "$AXZSH/custom_plugins/"*(N)
299                         "$AXZSH/repos/"*(N)
300                 ); do
301                         if [[ -r "$dname/$1.axzshtheme" ]]; then
302                                 theme="$dname/$1.axzshtheme"
303                                 break
304                         elif [[ -r "$dname/$1.zsh-theme" ]]; then
305                                 theme="$dname/$1.zsh-theme"
306                                 break
307                         fi
308                 done
309
310                 if [[ -z "$theme" ]]; then
311                         ax_error "Theme \"$1\" not found!"
312                         return 1
313                 fi
314         fi
315         ln -fs "$theme" "$link_name" || return 1
316         return $?
317 }
318
319 function UpgradeAXZSH {
320         if [[ $+commands[git] -eq 0 ]]; then
321                 ax_error "The git(1) command is not available!"
322                 return 1
323         fi
324         if [[ ! -d "$AXZSH/.git" ]]; then
325                 ax_error "AX-ZSH seems not to be installed using Git. Can't upgrade!"
326                 return 1
327         fi
328
329         ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
330         (
331                 set -e
332                 cd "$AXZSH"
333                 git pull --ff-only || ax_error "Git pull failed!"
334                 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
335         )
336 }
337
338 function UpgradeForeignPlugins {
339         if [[ $+commands[git] -eq 0 ]]; then
340                 ax_error "The git(1) command is not available!"
341                 return 1
342         fi
343
344         for dir ($AXZSH/repos/*(N)); do
345                 name=${dir:t:s/#/\//}
346                 if [[ -d "$dir/.git" ]]; then
347                         ax_msg - "Upgrading \"$name\" [git] ..."
348                         (
349                                 set -e
350                                 cd "$dir"
351                                 git pull --ff-only || ax_error "Git pull failed!"
352                                 git log --pretty=format:"%C(yellow)%h %C(blue)%ar %C(green)%an %Creset%s" ORIG_HEAD..
353                         )
354                 else
355                         ax_error "Unknown repository type!"
356                 fi
357         done
358 }
359
360 function CheckPlugins {
361         missing_plugins=()
362         invalid_plugins=()
363
364         # Building cache file for all zshrc core files:
365         if ! T=$(mktemp); then
366                 ax_error "Failed to create temporary file!"
367                 return 1
368         fi
369         for p in $AXZSH/core/*/*.zshrc; do
370                 [[ "$(basename "$p")" == "01_zprofile.zshrc" ]] && continue
371                 printf "# BEGIN: %s\naxzsh_plugin_init()\n{\n" "$p" >>"$T"
372                 cat "$p" >>"$T"
373                 printf "}\naxzsh_plugin_init\n# END: %s\n\n" "$p" >>"$T"
374         done
375
376         ax_msg - "Checking plugins ..."
377         for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
378                 plugin=${dir:t}
379
380                 # Test if plugin is already enabled
381                 if [[ -e "$AXZSH/active_plugins/$plugin" ]]; then
382                         printf ' \e[1;32m+\e[m "\e[1m%s\e[m" ... ' "${plugin}"
383                         enabled=1
384                 else
385                         printf ' \e[1;31m-\e[m "%s" ... ' "${plugin}"
386                         unset enabled
387                 fi
388
389                 # Test plugin ...
390                 new_plugin=""
391                 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
392                         [[ -r "$script" ]] || continue
393                         (
394                                 AXZSH_PLUGIN_CHECK=1
395                                 source "$T"
396                                 axzsh_plugin_fnc() { source "$script" }
397                                 axzsh_plugin_fnc
398                         ); r=$?
399                         [[ $r -eq 0 ]] && new_plugin=$plugin
400                         break
401                 done
402                 if [[ -n "$new_plugin" ]]; then
403                         detected_plugins+=($new_plugin)
404                         [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
405                         ax_msg 0 "OK."
406                 elif [[ $r -eq 91 ]]; then
407                         ax_msg 1 "ignored."
408                 elif [[ $r -eq 92 ]]; then
409                         ax_msg 1 "optional."
410                 else
411                         [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
412                         ax_msg 2 "failed ($r)."
413                 fi
414         done
415         rm -f "$T"
416         echo
417
418         result=0
419         if [[ -n "$missing_plugins" ]]; then
420                 ax_msg 1 "Run the following command to enable all missing plugins:"
421                 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
422                 echo
423                 result=1
424         else
425                 ax_msg 0 "All detected plugins are already enabled."
426         fi
427
428         if [[ -n "$invalid_plugins" ]]; then
429                 ax_msg 1 "Run the following command to disable all failed plugins:"
430                 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
431                 result=1
432         else
433                 ax_msg 0 "No failed plugins are enabled."
434         fi
435
436         echo
437         return $result
438 }
439
440 NAME="$0:t"
441
442 [[ $# -gt 0 ]] || Usage
443
444 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
445         [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
446         if [[ ! -r "$AXZSH/ax.zsh" ]]; then
447                 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
448                 exit 3
449         fi
450 fi
451
452 cmd="$1"
453 shift
454
455 case "$cmd" in
456         "enable")
457                 [[ $# -eq 0 ]] || Usage
458                 EnableAXZSH
459                 ;;
460         "disable")
461                 [[ $# -eq 0 ]] || Usage
462                 DisableAXZSH
463                 ;;
464         "enable-plugin")
465                 [[ $# -gt 0 ]] || Usage
466                 for plugin in "$@"; do
467                         EnablePlugin "$plugin"
468                 done
469                 UpdatePluginCache
470                 ;;
471         "disable-plugin")
472                 [[ $# -gt 0 ]] || Usage
473                 for plugin in "$@"; do
474                         DisablePlugin "$plugin"
475                 done
476                 UpdatePluginCache
477                 ;;
478         "list-enabled")
479                 [[ $# -eq 0 ]] || Usage
480                 ListEnabledPlugins
481                 ;;
482         "reset-plugins")
483                 [[ $# -eq 0 ]] || Usage
484                 ResetPlugins
485                 EnableDefaultPlugins
486                 UpdatePluginCache
487                 ;;
488         "enable-default-plugins")
489                 [[ $# -eq 0 ]] || Usage
490                 EnableDefaultPlugins && UpdatePluginCache
491                 ;;
492         "check-plugins")
493                 [[ $# -eq 0 ]] || Usage
494                 CheckPlugins
495                 ;;
496         "set-theme")
497                 [[ $# -eq 1 ]] || Usage
498                 SetTheme "$1"
499                 ;;
500         "upgrade")
501                 [[ $# -eq 0 ]] || Usage
502                 UpgradeAXZSH
503                 UpgradeForeignPlugins
504                 UpdatePluginCache
505                 ;;
506         "update-caches")
507                 [[ $# -eq 0 ]] || Usage
508                 UpdatePluginCache -v
509                 ;;
510         "--version"|"version")
511                 Version >&2
512                 ;;
513         "--help"|"help")
514                 Usage >&2
515                 ;;
516         *)
517                 ax_error "Invalid command \"$cmd\"!"
518                 ax_error "Try \"$0 --help\" for more information."
519                 exit 2
520 esac