]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
axzshctl: No need to regenerate caches on theme change
[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                         # Try to enable a theme in this "foreign" plugin, but
114                         # ignore errors: we don't know if this plugin provides
115                         # a theme at all ...
116                         SetTheme "${plugin#*#}" 2>/dev/null
117                 fi
118         fi
119
120         for dname (
121                 "$plugin:A"
122                 "$AXZSH_PLUGIN_D/$plugin"
123                 "$ZSH_CUSTOM/$plugin"
124                 "$AXZSH/custom_plugins/$plugin"
125                 "$AXZSH/repos/$plugin"
126                 "$AXZSH/plugins/$plugin"
127                 "$AXZSH/default_plugins/$plugin"
128                 "$AXZSH/core/$plugin"
129         ); do
130                 [[ ! -d "$dname" ]] && continue
131                 mkdir -p "$dir"
132                 (
133                         cd "$dir" || exit 9
134                         ln -sv "$dname" "$PWD"
135                 )
136                 return $?
137         done
138
139         ax_msg 2 "Plugin \"$1\" not found!"
140         return 1
141 }
142
143 function DisablePlugin {
144         local plugin=$(NormalizedPluginName "$1")
145         local dir="$AXZSH/active_plugins"
146
147         if [[ ! -h "$dir/$plugin" ]]; then
148                 ax_msg 1 "Plugin \"$1\" not active?"
149                 return 1
150         fi
151
152         rm -v "$dir/$plugin"; r=$?
153         [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
154         return $r
155 }
156
157 function ListEnabledPlugins {
158         for plugin ($AXZSH/active_plugins/*(N)); do
159                 print ${plugin:t:s/#/\//}
160         done
161         return 0
162 }
163
164 function ResetPlugins {
165         local dir="$AXZSH/active_plugins"
166         local r1=0, r2=0
167
168         if [[ -e "$dir" ]]; then
169                 ax_msg - "Removing all symbolic links in $dir ..."
170                 find "$dir" -type l -print -delete; r1=$?
171         fi
172
173         ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
174         rm -fr "$AXZSH/repos"; r2=$?
175
176         [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
177 }
178
179 function EnableDefaultPlugins {
180         local dir="$AXZSH/active_plugins"
181
182         ax_msg - "Activating (linking) default plugins ..."
183         mkdir -p "$dir"
184         (
185                 cd "$dir" || exit 9
186                 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
187         )
188         return $?
189 }
190
191 function SetTheme {
192         local link_name="$AXZSH/active_theme"
193
194         if [ "$1" = "-" ]; then
195                 rm -f "$link_name" || return 1
196                 echo "Theme settings have been reset."
197                 return 0
198         fi
199
200         if [ -r "$1" ]; then
201                 theme="$1"
202         elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
203                 theme="$AXZSH/custom_themes/$1.axzshtheme"
204         elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
205                 theme="$AXZSH/themes/$1.axzshtheme"
206         else
207                 # Look for theme inside of installed plugins:
208                 for dname (
209                         "$AXZSH/custom_themes"
210                         "$AXZSH/custom_plugins/"*(N)
211                         "$AXZSH/repos/"*(N)
212                 ); do
213                         if [[ -r "$dname/$1.axzshtheme" ]]; then
214                                 theme="$dname/$1.axzshtheme"
215                                 break
216                         elif [[ -r "$dname/$1.zsh-theme" ]]; then
217                                 theme="$dname/$1.zsh-theme"
218                                 break
219                         fi
220                 done
221                 if [[ -z "$theme" ]]; then
222                         echo "Theme \"$1\" not found!"
223                         return 1
224                 fi
225         fi
226         ln -fsv "$theme" "$link_name" || return 1
227         return $?
228 }
229
230 function UpgradeAXZSH {
231         if [[ $+commands[git] -eq 0 ]]; then
232                 ax_msg 2 "The git(1) command is not available!"
233                 return 1
234         fi
235         if [[ ! -d "$AXZSH/.git" ]]; then
236                 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
237                 return 1
238         fi
239
240         ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
241         ( cd "$AXZSH" && git pull --ff-only )
242 }
243
244 function UpgradeForeignPlugins {
245         if [[ $+commands[git] -eq 0 ]]; then
246                 ax_msg 2 "The git(1) command is not available!"
247                 return 1
248         fi
249
250         for dir ($AXZSH/repos/*(N)); do
251                 name=${dir:t:s/#/\//}
252                 if [ -d "$dir/.git" ]; then
253                         ax_msg - "Upgrading \"$name\" [git] ..."
254                         (
255                                 cd "$dir"
256                                 git pull --ff-only || ax_msg 2 "Pull failed!"
257                         )
258                 else
259                         ax_msg 2 "Unknown repository type!"
260                 fi
261         done
262 }
263
264 function CheckPlugins {
265         missing_plugins=()
266         invalid_plugins=()
267
268         ax_msg - "Checking plugins ..."
269         for dir ($AXZSH/plugins/*(N)); do
270                 plugin=${dir:t}
271
272                 # Test if plugin is already enabled
273                 [[ -e "$AXZSH/active_plugins/$plugin" ]] \
274                         && enabled=" (enabled)" \
275                         || unset enabled
276
277                 # Test plugin ...
278                 printf " - \"%s\"%s ... " "$plugin" "$enabled"
279                 new_plugin=""
280                 for script ($AXZSH/plugins/$plugin/$plugin.{zshrc,zprofile}); do
281                         [[ -r "$script" ]] || continue
282                         AXZSH_PLUGIN_CHECK=1 zsh -i -c "source $script"; r=$?
283                         if [[ $r -eq 0 ]]; then
284                                 new_plugin=$plugin
285                                 break
286                         fi
287                 done
288                 if [[ -n "$new_plugin" ]]; then
289                         detected_plugins+=($new_plugin)
290                         [[ -n "$enabled" ]] || missing_plugins+=($new_plugin)
291                         ax_msg 0 "OK."
292                 elif [[ $r -eq 91 ]]; then
293                         ax_msg 1 "ignored."
294                 elif [[ $r -eq 92 ]]; then
295                         ax_msg 1 "optional."
296                 else
297                         [[ -n "$enabled" ]] && invalid_plugins+=($plugin)
298                         ax_msg 2 "failed."
299                 fi
300         done
301         echo
302
303         result=0
304         if [[ -n "$missing_plugins" ]]; then
305                 ax_msg 1 "Run the following command to enable all missing plugins:"
306                 echo "$AXZSH/bin/axzshctl enable-plugin" $missing_plugins
307                 echo
308                 result=1
309         else
310                 ax_msg 0 "All detected plugins are already enabled."
311         fi
312
313         if [[ -n "$invalid_plugins" ]]; then
314                 ax_msg 1 "Run the following command to disable all failed plugins:"
315                 echo "$AXZSH/bin/axzshctl disable-plugin" $invalid_plugins
316                 result=1
317         else
318                 ax_msg 0 "No failed plugins are enabled."
319         fi
320
321         echo
322         return $result
323 }
324
325 NAME="$0:t"
326
327 [[ $# -gt 0 ]] || Usage
328
329 if [[ -z "$AXZSH" || ! -r "$AXZSH/ax.zsh" ]]; then
330         [[ -r "$HOME/.axzsh/ax.zsh" ]] && AXZSH="$HOME/.axzsh"
331         if [[ ! -r "$AXZSH/ax.zsh" ]]; then
332                 ax_msg 2 "Oops, \"AXZSH\" is not set or invalid and can't be autodetected!"
333                 exit 3
334         fi
335 fi
336
337 cmd="$1"
338 shift
339
340 case "$cmd" in
341         "enable")
342                 [[ $# -eq 0 ]] || Usage
343                 EnableAXZSH
344                 ;;
345         "disable")
346                 [[ $# -eq 0 ]] || Usage
347                 DisableAXZSH
348                 ;;
349         "enable-plugin")
350                 [[ $# -gt 0 ]] || Usage
351                 for plugin in "$@"; do
352                         EnablePlugin "$plugin"
353                 done
354                 UpdatePluginCache
355                 ;;
356         "disable-plugin")
357                 [[ $# -gt 0 ]] || Usage
358                 for plugin in "$@"; do
359                         DisablePlugin "$plugin"
360                 done
361                 UpdatePluginCache
362                 ;;
363         "list-enabled")
364                 [[ $# -eq 0 ]] || Usage
365                 ListEnabledPlugins
366                 ;;
367         "reset-plugins")
368                 [[ $# -eq 0 ]] || Usage
369                 ResetPlugins
370                 EnableDefaultPlugins
371                 UpdatePluginCache
372                 ;;
373         "enable-default-plugins")
374                 [[ $# -eq 0 ]] || Usage
375                 EnableDefaultPlugins && UpdatePluginCache
376                 ;;
377         "check-plugins")
378                 [[ $# -eq 0 ]] || Usage
379                 CheckPlugins
380                 ;;
381         "set-theme")
382                 [[ $# -eq 1 ]] || Usage
383                 SetTheme "$1"
384                 ;;
385         "upgrade")
386                 [[ $# -eq 0 ]] || Usage
387                 UpgradeAXZSH
388                 UpgradeForeignPlugins
389                 UpdatePluginCache
390                 ;;
391         "update-caches")
392                 [[ $# -eq 0 ]] || Usage
393                 UpdatePluginCache
394                 ;;
395         *)
396                 Usage
397 esac