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