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