]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
d0eca718904f16b6da8d7d92ad4b577987972462
[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 # Include "ax-common.sh", if available:
8 for dir ("$HOME/lib" "$HOME/.ax" /usr/local /opt/ax /usr); do
9         [[ -z "$ax_common_sourced" ]] || break
10         ax_common="${dir}/lib/ax/ax-common.sh"
11         [[ -r "$ax_common" ]] && source "$ax_common"
12 done
13 if [[ -z "$ax_common_sourced" ]]; then
14         function ax_msg {
15                 shift
16                 echo "$@"
17         }
18         function ax_error {
19                 ax_msg 2 "$@" >&2
20         }
21 fi
22 unset dir ax_common ax_common_sourced
23
24 function Version {
25         echo "ax-zsh -- Modular configuration system for the Z shell (ZSH)"
26         echo "Copyright (c) 2015-2019 Alexander Barton <alex@barton.de>."
27         echo "Licensed under the terms of the MIT license, see LICENSE.md for details."
28         echo "Homepage: <https://github.com/alexbarton/ax-zsh>"
29         if [[ -d "$AXZSH/.git" && -n "$commands[git]" ]]; then
30                 echo -n "Version: Git ID "
31                 ( cd "$AXZSH" && git describe --always )
32         fi
33         echo
34         exit 0
35 }
36
37 function Usage {
38         echo "Usage: $NAME <command> [...]"
39         echo
40         echo "  enable"
41         echo "    Enable AX-ZSH altogether."
42         echo "  disable"
43         echo "    Disable AX-ZSH altogether."
44         echo
45         echo "  enable-plugin <name|directory> [<name|directory> [...]]"
46         echo "    Enable plugin(s)."
47         echo "  disable-plugin <name> [<name> [...]]"
48         echo "    Disable plugin(s)."
49         echo "  list-enabled"
50         echo "    List enabled plugins."
51         echo
52         echo "  reset-plugins"
53         echo "    Reset active plugins to the default set."
54         echo "  enable-default-plugins"
55         echo "    Enable all default plugins."
56         echo "  check-plugins"
57         echo "    Detect plugins which are \"useful\" on this system."
58         echo
59         echo "  set-theme <name>|-"
60         echo "    Set active theme to <name>, or to the default."
61         echo
62         echo "  upgrade"
63         echo "    Upgrade AX-ZSH installation (requires Git)."
64         echo "  update-caches"
65         echo "    Force rebuild of all cache files."
66         echo
67         exit 0
68 }
69
70 function UpdatePluginCache {
71         [[ -r "$AXZSH/cache" ]] || return 0
72
73         [[ "$1" = "-v" ]] && ax_msg - "Updating plugin cache ..."
74         rm -rf \
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         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/*(N)); do
328                 plugin=${dir:t}
329
330                 # Test if plugin is already enabled
331                 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
332                         && enabled=" (enabled)" \
333                         || unset enabled
334
335                 # Test plugin ...
336                 printf " - \"%s\"%s ... " "$plugin" "$enabled"
337                 new_plugin=""
338                 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
339                         [[ -r "$script" ]] || continue
340                         AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
341                         if [[ $r -eq 0 ]]; then
342                                 new_plugin=$plugin
343                                 break
344                         fi
345                 done
346                 if [[ -n "$new_plugin" ]]; then
347                         detected_plugins+=($new_plugin)
348                         [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
349                         ax_msg 0 "OK."
350                 elif [[ $r -eq 91 ]]; then
351                         ax_msg 1 "ignored."
352                 elif [[ $r -eq 92 ]]; then
353                         ax_msg 1 "optional."
354                 else
355                         [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
356                         ax_msg 2 "failed."
357                 fi
358         done
359         echo
360
361         result=0
362         if [[ -n "$missing_plugins" ]]; then
363                 ax_msg 1 "Run the following command to enable all missing plugins:"
364                 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
365                 echo
366                 result=1
367         else
368                 ax_msg 0 "All detected plugins are already enabled."
369         fi
370
371         if [[ -n "$invalid_plugins" ]]; then
372                 ax_msg 1 "Run the following command to disable all failed plugins:"
373                 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
374                 result=1
375         else
376                 ax_msg 0 "No failed plugins are enabled."
377         fi
378
379         echo
380         return $result
381 }
382
383 NAME="$0:t"
384
385 [[ $# -gt 0 ]] || Usage
386
387 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
388         [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
389         if [[ ! -r "$AXZSH/ax.zsh" ]]; then
390                 ax_error "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
391                 exit 3
392         fi
393 fi
394
395 cmd="$1"
396 shift
397
398 case "$cmd" in
399         "enable")
400                 [[ $# -eq 0 ]] || Usage
401                 EnableAXZSH
402                 ;;
403         "disable")
404                 [[ $# -eq 0 ]] || Usage
405                 DisableAXZSH
406                 ;;
407         "enable-plugin")
408                 [[ $# -gt 0 ]] || Usage
409                 for plugin in "$@"; do
410                         EnablePlugin "$plugin"
411                 done
412                 UpdatePluginCache
413                 ;;
414         "disable-plugin")
415                 [[ $# -gt 0 ]] || Usage
416                 for plugin in "$@"; do
417                         DisablePlugin "$plugin"
418                 done
419                 UpdatePluginCache
420                 ;;
421         "list-enabled")
422                 [[ $# -eq 0 ]] || Usage
423                 ListEnabledPlugins
424                 ;;
425         "reset-plugins")
426                 [[ $# -eq 0 ]] || Usage
427                 ResetPlugins
428                 EnableDefaultPlugins
429                 UpdatePluginCache
430                 ;;
431         "enable-default-plugins")
432                 [[ $# -eq 0 ]] || Usage
433                 EnableDefaultPlugins && UpdatePluginCache
434                 ;;
435         "check-plugins")
436                 [[ $# -eq 0 ]] || Usage
437                 CheckPlugins
438                 ;;
439         "set-theme")
440                 [[ $# -eq 1 ]] || Usage
441                 SetTheme "$1"
442                 ;;
443         "upgrade")
444                 [[ $# -eq 0 ]] || Usage
445                 UpgradeAXZSH
446                 UpgradeForeignPlugins
447                 UpdatePluginCache
448                 ;;
449         "update-caches")
450                 [[ $# -eq 0 ]] || Usage
451                 UpdatePluginCache -v
452                 ;;
453         "--version"|"version")
454                 Version >&2
455                 ;;
456         "--help"|"help")
457                 Usage >&2
458                 ;;
459         *)
460                 ax_error "Invalid command \"$cmd\"!"
461                 ax_error "Try \"$0 --help\" for more information."
462                 exit 2
463 esac