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