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