]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
axzshctl: Add "update-caches" command
[ax-zsh.git] / bin / axzshctl
1 #!/usr/bin/env zsh
2 #
3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2017 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 fi
19 unset dir ax_common ax_common_sourced
20
21 function Usage {
22         echo "Usage: $NAME <command> [...]"
23         echo
24         echo "  enable"
25         echo "    Enable AX-ZSH altogether."
26         echo "  disable"
27         echo "    Disable AX-ZSH altogether."
28         echo
29         echo "  enable-plugin <name|directory> [<name|directory> [...]]"
30         echo "    Enable plugin(s)."
31         echo "  disable-plugin <name> [<name> [...]]"
32         echo "    Disable plugin(s)."
33         echo "  list-enabled"
34         echo "    List enabled plugins."
35         echo
36         echo "  reset-plugins"
37         echo "    Reset active plugins to the default set."
38         echo "  enable-default-plugins"
39         echo "    Enable all default plugins."
40         echo "  check-plugins"
41         echo "    Detect plugins which are \"useful\" on this system."
42         echo
43         echo "  set-theme <name>|-"
44         echo "    Set active theme to <name>, or to the default."
45         echo
46         echo "  upgrade"
47         echo "    Upgrade AX-ZSH installation (requires Git)."
48         echo "  update-caches"
49         echo "    Force rebuild of all cache files."
50         echo
51         exit 2
52 }
53
54 function UpdatePluginCache {
55         [[ -r "$AXZSH/cache" ]] || return 0
56
57         ax_msg - "Updating plugin cache ..."
58         rm -rf \
59                 $AXZSH/cache/zlogin.cache \
60                 $AXZSH/cache/zlogout.cache \
61                 $AXZSH/cache/zprofile.cache \
62                 $AXZSH/cache/zshrc.cache \
63                 || return 1
64         echo "Regenerating cache files ..."
65         zsh -ilc '' >/dev/null
66 }
67
68 function NormalizedPluginName {
69         if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
70                 echo "${1:gs/\//#}"
71         elif [[ "$1" =~ "/" ]]; then
72                 echo "${1:t}"
73         else
74                 echo "$1"
75         fi
76 }
77
78 function EnableAXZSH {
79         for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
80                 ln -sv "$AXZSH/ax.zsh" "$f" \
81                         || ax_msg 2 "Failed to create symbolic link for \"$f\"!"
82         done
83 }
84
85 function DisableAXZSH {
86         for f (~/.zlogin ~/.zlogout ~/.zprofile ~/.zshrc); do
87                 if [ -h "$f" ]; then
88                         rm -v "$f" || ax_msg 2 "Failed to remove \"$f\"!"
89                 elif [ -e "$f" ]; then
90                         ax_msg 2 "Error: Not removing \"$f\", it is not a symbolic link!"
91                 else
92                         ax_msg 1 "Warning: \"$f\" already does not exist. Ok."
93                 fi
94         done
95 }
96
97 function EnablePlugin {
98         local plugin=$(NormalizedPluginName "$1")
99         local dir="$AXZSH/active_plugins"
100
101         if [[ -h "$dir/$plugin" ]]; then
102                 ax_msg 1 "Plugin \"$1\" already active!"
103                 return 1
104         fi
105
106         if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
107                 # GitHub plugin
108                 mkdir -p "$AXZSH/repos"
109                 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
110                         ax_msg - "Cloning plugin from GitHub ..."
111                         git clone "https://github.com/$1.git" \
112                          "$AXZSH/repos/$plugin"
113                 fi
114         fi
115
116         for dname (
117                 "$plugin:A"
118                 "$AXZSH_PLUGIN_D/$plugin"
119                 "$ZSH_CUSTOM/$plugin"
120                 "$AXZSH/custom_plugins/$plugin"
121                 "$AXZSH/repos/$plugin"
122                 "$AXZSH/plugins/$plugin"
123                 "$AXZSH/default_plugins/$plugin"
124                 "$AXZSH/core/$plugin"
125         ); do
126                 [[ ! -d "$dname" ]] && continue
127                 mkdir -p "$dir"
128                 (
129                         cd "$dir" || exit 9
130                         ln -sv "$dname" "$PWD"
131                 )
132                 return $?
133         done
134
135         ax_msg 2 "Plugin \"$1\" not found!"
136         return 1
137 }
138
139 function DisablePlugin {
140         local plugin=$(NormalizedPluginName "$1")
141         local dir="$AXZSH/active_plugins"
142
143         if [[ ! -h "$dir/$plugin" ]]; then
144                 ax_msg 1 "Plugin \"$1\" not active?"
145                 return 1
146         fi
147
148         rm -v "$dir/$plugin"; r=$?
149         [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
150         return $r
151 }
152
153 function ListEnabledPlugins {
154         for plugin ($AXZSH/active_plugins/*(N)); do
155                 print ${plugin:t:s/#/\//}
156         done
157         return 0
158 }
159
160 function ResetPlugins {
161         local dir="$AXZSH/active_plugins"
162         local r1=0, r2=0
163
164         if [[ -e "$dir" ]]; then
165                 ax_msg - "Removing all symbolic links in $dir ..."
166                 find "$dir" -type l -print -delete; r1=$?
167         fi
168
169         ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
170         rm -fr "$AXZSH/repos"; r2=$?
171
172         [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
173 }
174
175 function EnableDefaultPlugins {
176         local dir="$AXZSH/active_plugins"
177
178         ax_msg - "Activating (linking) default plugins ..."
179         mkdir -p "$dir"
180         (
181                 cd "$dir" || exit 9
182                 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
183         )
184         return $?
185 }
186
187 function SetTheme {
188         local link_name="$AXZSH/active_theme"
189
190         if [ $# -ne 1 ]; then
191                 echo "Usage: axzsh_set_theme <name|->"
192                 return 1
193         fi
194
195         rm -f "$link_name" || return 1
196
197         if [ "$1" = "-" ]; then
198                 echo "Theme settings have been reset."
199                 return 0
200         fi
201
202         if [ -r "$1" ]; then
203                 theme="$1"
204         elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
205                 theme="$AXZSH/custom_themes/$1.axzshtheme"
206         elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
207                 theme="$AXZSH/themes/$1.axzshtheme"
208         else
209                 echo "Theme \"$1\" not found!"
210                 return 1
211         fi
212         ln -sv "$theme" "$link_name" || return 1
213         return $?
214 }
215
216 function UpgradeAXZSH {
217         if [[ $+commands[git] -eq 0 ]]; then
218                 ax_msg 2 "The git(1) command is not available!"
219                 return 1
220         fi
221         if [[ ! -d "$AXZSH/.git" ]]; then
222                 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
223                 return 1
224         fi
225
226         ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
227         ( cd "$AXZSH" && git pull --ff-only )
228 }
229
230 function UpgradeForeignPlugins {
231         if [[ $+commands[git] -eq 0 ]]; then
232                 ax_msg 2 "The git(1) command is not available!"
233                 return 1
234         fi
235
236         for dir ($AXZSH/repos/*(N)); do
237                 name=${dir:t:s/#/\//}
238                 if [ -d "$dir/.git" ]; then
239                         ax_msg - "Upgrading \"$name\" [git] ..."
240                         (
241                                 cd "$dir"
242                                 git pull --ff-only || ax_msg 2 "Pull failed!"
243                         )
244                 else
245                         ax_msg 2 "Unknown repository type!"
246                 fi
247         done
248 }
249
250 function CheckPlugins {
251         missing_plugins=()
252         invalid_plugins=()
253
254         ax_msg - "Checking plugins ..."
255         for dir ($AXZSH/plugins/*(N)); do
256                 plugin=${dir:t}
257
258                 # Test if plugin is already enabled
259                 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
260                         && enabled=" (enabled)" \
261                         || unset enabled
262
263                 # Test plugin ...
264                 printf " - \"%s\"%s ... " "$plugin" "$enabled"
265                 new_plugin=""
266                 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
267                         [[ -r "$script" ]] || continue
268                         AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
269                         if [[ $r -eq 0 ]]; then
270                                 new_plugin=$plugin
271                                 break
272                         fi
273                 done
274                 if [[ -n "$new_plugin" ]]; then
275                         detected_plugins+=($new_plugin)
276                         [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
277                         ax_msg 0 "OK."
278                 elif [[ $r -eq 91 ]]; then
279                         ax_msg 1 "ignored."
280                 elif [[ $r -eq 92 ]]; then
281                         ax_msg 1 "optional."
282                 else
283                         [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
284                         ax_msg 2 "failed."
285                 fi
286         done
287         echo
288
289         result=0
290         if [[ -n "$missing_plugins" ]]; then
291                 ax_msg 1 "Run the following command to enable all missing plugins:"
292                 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
293                 echo
294                 result=1
295         else
296                 ax_msg 0 "All detected plugins are already enabled."
297         fi
298
299         if [[ -n "$invalid_plugins" ]]; then
300                 ax_msg 1 "Run the following command to disable all failed plugins:"
301                 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
302                 result=1
303         else
304                 ax_msg 0 "No failed plugins are enabled."
305         fi
306
307         echo
308         return $result
309 }
310
311 NAME="$0:t"
312
313 [[ $# -gt 0 ]] || Usage
314
315 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
316         [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
317         if [[ ! -r "$AXZSH/ax.zsh" ]]; then
318                 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
319                 exit 3
320         fi
321 fi
322
323 cmd="$1"
324 shift
325
326 case "$cmd" in
327         "enable")
328                 [[ $# -eq 0 ]] || Usage
329                 EnableAXZSH
330                 ;;
331         "disable")
332                 [[ $# -eq 0 ]] || Usage
333                 DisableAXZSH
334                 ;;
335         "enable-plugin")
336                 [[ $# -gt 0 ]] || Usage
337                 for plugin in "$@"; do
338                         EnablePlugin "$plugin"
339                 done
340                 UpdatePluginCache
341                 ;;
342         "disable-plugin")
343                 [[ $# -gt 0 ]] || Usage
344                 for plugin in "$@"; do
345                         DisablePlugin "$plugin"
346                 done
347                 UpdatePluginCache
348                 ;;
349         "list-enabled")
350                 [[ $# -eq 0 ]] || Usage
351                 ListEnabledPlugins
352                 ;;
353         "reset-plugins")
354                 [[ $# -eq 0 ]] || Usage
355                 ResetPlugins
356                 EnableDefaultPlugins
357                 UpdatePluginCache
358                 ;;
359         "enable-default-plugins")
360                 [[ $# -eq 0 ]] || Usage
361                 EnableDefaultPlugins
362                 UpdatePluginCache
363                 ;;
364         "check-plugins")
365                 [[ $# -eq 0 ]] || Usage
366                 CheckPlugins
367                 ;;
368         "set-theme")
369                 [[ $# -eq 1 ]] || Usage
370                 SetTheme "$1"
371                 UpdatePluginCache
372                 ;;
373         "upgrade")
374                 [[ $# -eq 0 ]] || Usage
375                 UpgradeAXZSH
376                 UpgradeForeignPlugins
377                 UpdatePluginCache
378                 ;;
379         "update-caches")
380                 [[ $# -eq 0 ]] || Usage
381                 UpdatePluginCache
382                 ;;
383         *)
384                 Usage
385 esac