]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
axzshctl: Embed "ax-common" compatibility functions
[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         ( cd "$AXZSH" && git pull --ff-only )
300 }
301
302 function UpgradeForeignPlugins {
303         if [[ $+commands[git] -eq 0 ]]; then
304                 ax_error "The git(1) command is not available!"
305                 return 1
306         fi
307
308         for dir ($AXZSH/repos/*(N)); do
309                 name=${dir:t:s/#/\//}
310                 if [[ -d "$dir/.git" ]]; then
311                         ax_msg - "Upgrading \"$name\" [git] ..."
312                         (
313                                 cd "$dir"
314                                 git pull --ff-only || ax_error "Pull failed!"
315                         )
316                 else
317                         ax_error "Unknown repository type!"
318                 fi
319         done
320 }
321
322 function CheckPlugins {
323         missing_plugins=()
324         invalid_plugins=()
325
326         ax_msg - "Checking plugins ..."
327         for dir ($AXZSH/plugins/[a-z0-9]*(N)); do
328                 plugin=${dir:t}
329
330                 # Test if plugin is already enabled
331                 if [[ -e "$AXZSH/active_plugins/$plugin" ]]; then
332                         printf ' \e[1;32m+\e[m "\e[1m%s\e[m" ... ' "${plugin}"
333                         enabled=1
334                 else
335                         printf ' \e[1;31m-\e[m "%s" ... ' "${plugin}"
336                         unset enabled
337                 fi
338
339                 # Test plugin ...
340                 new_plugin=""
341                 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile,ax-io}); do
342                         [[ -r "$script" ]] || continue
343                         AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
344                         if [[ $r -eq 0 ]]; then
345                                 new_plugin=$plugin
346                         fi
347                         break
348                 done
349                 if [[ -n "$new_plugin" ]]; then
350                         detected_plugins+=($new_plugin)
351                         [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
352                         ax_msg 0 "OK."
353                 elif [[ $r -eq 91 ]]; then
354                         ax_msg 1 "ignored."
355                 elif [[ $r -eq 92 ]]; then
356                         ax_msg 1 "optional."
357                 else
358                         [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
359                         ax_msg 2 "failed ($r)."
360                 fi
361         done
362         echo
363
364         result=0
365         if [[ -n "$missing_plugins" ]]; then
366                 ax_msg 1 "Run the following command to enable all missing plugins:"
367                 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
368                 echo
369                 result=1
370         else
371                 ax_msg 0 "All detected plugins are already enabled."
372         fi
373
374         if [[ -n "$invalid_plugins" ]]; then
375                 ax_msg 1 "Run the following command to disable all failed plugins:"
376                 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
377                 result=1
378         else
379                 ax_msg 0 "No failed plugins are enabled."
380         fi
381
382         echo
383         return $result
384 }
385
386 NAME="$0:t"
387
388 [[ $# -gt 0 ]] || Usage
389
390 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
391         [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
392         if [[ ! -r "$AXZSH/ax.zsh" ]]; then
393                 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
394                 exit 3
395         fi
396 fi
397
398 cmd="$1"
399 shift
400
401 case "$cmd" in
402         "enable")
403                 [[ $# -eq 0 ]] || Usage
404                 EnableAXZSH
405                 ;;
406         "disable")
407                 [[ $# -eq 0 ]] || Usage
408                 DisableAXZSH
409                 ;;
410         "enable-plugin")
411                 [[ $# -gt 0 ]] || Usage
412                 for plugin in "$@"; do
413                         EnablePlugin "$plugin"
414                 done
415                 UpdatePluginCache
416                 ;;
417         "disable-plugin")
418                 [[ $# -gt 0 ]] || Usage
419                 for plugin in "$@"; do
420                         DisablePlugin "$plugin"
421                 done
422                 UpdatePluginCache
423                 ;;
424         "list-enabled")
425                 [[ $# -eq 0 ]] || Usage
426                 ListEnabledPlugins
427                 ;;
428         "reset-plugins")
429                 [[ $# -eq 0 ]] || Usage
430                 ResetPlugins
431                 EnableDefaultPlugins
432                 UpdatePluginCache
433                 ;;
434         "enable-default-plugins")
435                 [[ $# -eq 0 ]] || Usage
436                 EnableDefaultPlugins && UpdatePluginCache
437                 ;;
438         "check-plugins")
439                 [[ $# -eq 0 ]] || Usage
440                 CheckPlugins
441                 ;;
442         "set-theme")
443                 [[ $# -eq 1 ]] || Usage
444                 SetTheme "$1"
445                 ;;
446         "upgrade")
447                 [[ $# -eq 0 ]] || Usage
448                 UpgradeAXZSH
449                 UpgradeForeignPlugins
450                 UpdatePluginCache
451                 ;;
452         "update-caches")
453                 [[ $# -eq 0 ]] || Usage
454                 UpdatePluginCache -v
455                 ;;
456         "--version"|"version")
457                 Version >&2
458                 ;;
459         "--help"|"help")
460                 Usage >&2
461                 ;;
462         *)
463                 ax_error "Invalid command \"$cmd\"!"
464                 ax_error "Try \"$0 --help\" for more information."
465                 exit 2
466 esac