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