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