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