]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
Clean up external repositories on "axzshctl reset-plugins"
[ax-zsh.git] / bin / axzshctl
1 #!/bin/zsh
2 #
3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015-2016 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                 case "$1" in
16                   "1"|"2") echo -n "! "; ;;
17                   *) echo -n "* "; ;;
18                 esac
19                 shift
20                 echo "$@"
21         }
22 fi
23 unset dir ax_common ax_common_sourced
24
25 function Usage {
26         echo "Usage: $NAME <command> [...]"
27         echo
28         echo "  enable-plugin <name|directory> [<name|directory> [...]]"
29         echo "    Enable plugin(s)."
30         echo
31         echo "  disable-plugin <name> [<name> [...]]"
32         echo "    Disable plugin(s)."
33         echo
34         echo "  reset-plugins"
35         echo "    Reset active plugins to the default set."
36         echo
37         echo "  enable-default-plugins"
38         echo "    Enable all default plugins."
39         echo
40         echo "  set-theme <name>|-"
41         echo "    Set active theme to <name>, or to the default."
42         echo
43         echo "  upgrade"
44         echo "    Upgrade AX-ZSH installation (requires Git)."
45         echo
46         exit 2
47 }
48
49 function NormalizedPluginName {
50         if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
51                 echo "$1" | sed -e 's|/|#|g'
52         elif [[ "$1" =~ "/" ]]; then
53                 basename "$1"
54         else
55                 echo "$1"
56         fi
57 }
58
59 function EnablePlugin {
60         local plugin=$(NormalizedPluginName "$1")
61         local dir="$AXZSH/active_plugins"
62
63         if [[ -h "$dir/$plugin" ]]; then
64                 ax_msg 1 "Plugin \"$1\" already active!"
65                 return 1
66         fi
67
68         if [[ "$1" =~ "^[[:alnum:]-]+/[[:alnum:]_-]+$" ]]; then
69                 # GitHub plugin
70                 mkdir -p "$AXZSH/repos"
71                 if [[ ! -e "$AXZSH/repos/$plugin" ]]; then
72                         ax_msg - "Cloning plugin from GitHub ..."
73                         git clone "https://github.com/$1.git" \
74                          "$AXZSH/repos/$plugin"
75                 fi
76         fi
77
78         for dname (
79                 "$plugin:A"
80                 "$AXZSH_PLUGIN_D/$plugin"
81                 "$ZSH_CUSTOM/$plugin"
82                 "$AXZSH/custom_plugins/$plugin"
83                 "$AXZSH/repos/$plugin"
84                 "$AXZSH/plugins/$plugin"
85                 "$AXZSH/default_plugins/$plugin"
86                 "$AXZSH/core/$plugin"
87         ); do
88                 [[ ! -d "$dname" ]] && continue
89                 mkdir -p "$dir"
90                 (
91                         cd "$dir" || exit 9
92                         ln -sv "$dname" "$PWD"
93                 )
94                 return $?
95         done
96
97         ax_msg 2 "Plugin \"$1\" not found!"
98         return 1
99 }
100
101 function DisablePlugin {
102         local plugin=$(NormalizedPluginName "$1")
103         local dir="$AXZSH/active_plugins"
104
105         if [[ ! -h "$dir/$plugin" ]]; then
106                 ax_msg 1 "Plugin \"$1\" not active?"
107                 return 1
108         fi
109
110         rm -v "$dir/$plugin"; r=$?
111         [ $r -eq 0 ] && rm -fr "$AXZSH/repos/$plugin"
112         return $r
113 }
114
115 function ResetPlugins {
116         local dir="$AXZSH/active_plugins"
117         local r1=0, r2=0
118
119         if [[ -e "$dir" ]]; then
120                 ax_msg - "Removing all symbolic links in $dir ..."
121                 find "$dir" -type l -print -delete; r1=$?
122         fi
123
124         ax_msg - "Removing all external repositories in \"$AXZSH/repos\" ..."
125         rm -fr "$AXZSH/repos"; r2=$?
126
127         [[ $r1 == 0 && $r2 == 0 ]] && return 0 || return 1
128 }
129
130 function EnableDefaultPlugins {
131         local dir="$AXZSH/active_plugins"
132
133         ax_msg - "Activating (linking) default plugins ..."
134         mkdir -p "$dir"
135         (
136                 cd "$dir" || exit 9
137                 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
138         )
139         return $?
140 }
141
142 function SetTheme {
143         local link_name="$AXZSH/active_theme"
144
145         if [ $# -ne 1 ]; then
146                 echo "Usage: axzsh_set_theme <name|->"
147                 return 1
148         fi
149
150         rm -f "$link_name" || return 1
151
152         if [ "$1" = "-" ]; then
153                 echo "Theme settings have been reset."
154                 return 0
155         fi
156
157         if [ -r "$1" ]; then
158                 theme="$1"
159         elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
160                 theme="$AXZSH/custom_themes/$1.axzshtheme"
161         elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
162                 theme="$AXZSH/themes/$1.axzshtheme"
163         else
164                 echo "Theme \"$1\" not found!"
165                 return 1
166         fi
167         ln -sv "$theme" "$link_name" || return 1
168         return $?
169 }
170
171 function UpgradeAXZSH {
172         if [[ $+commands[git] -eq 0 ]]; then
173                 ax_msg 2 "The git(1) command is not available!"
174                 return 1
175         fi
176         if [[ ! -d "$AXZSH/.git" ]]; then
177                 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
178                 return 1
179         fi
180
181         ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
182         ( cd "$AXZSH" && git pull --ff-only )
183 }
184
185 function UpgradeForeignPlugins {
186         if [[ $+commands[git] -eq 0 ]]; then
187                 ax_msg 2 "The git(1) command is not available!"
188                 return 1
189         fi
190
191         for dir ($AXZSH/repos/*(N)); do
192                 name=$(basename "$dir" | sed -e 's|#|/|g')
193                 if [ -d "$dir/.git" ]; then
194                         ax_msg - "Upgrading \"$name\" [git] ..."
195                         (
196                                 cd "$dir"
197                                 git pull --ff-only || ax_msg 2 "Pull failed!"
198                         )
199                 else
200                         ax_msg 2 "Unknown repository type!"
201                 fi
202         done
203 }
204
205 NAME="$0:t"
206
207 [[ $# -gt 0 ]] || Usage
208
209 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
210         ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
211         exit 3
212 fi
213
214 cmd="$1"
215 shift
216
217 case "$cmd" in
218         "enable-plugin")
219                 [[ $# -gt 0 ]] || Usage
220                 for plugin in "$@"; do
221                         EnablePlugin "$plugin"
222                 done
223                 ;;
224         "disable-plugin")
225                 [[ $# -gt 0 ]] || Usage
226                 for plugin in "$@"; do
227                         DisablePlugin "$plugin"
228                 done
229                 ;;
230         "reset-plugins")
231                 [[ $# -eq 0 ]] || Usage
232                 ResetPlugins
233                 EnableDefaultPlugins
234                 ;;
235         "enable-default-plugins")
236                 [[ $# -eq 0 ]] || Usage
237                 EnableDefaultPlugins
238                 ;;
239         "set-theme")
240                 [[ $# -eq 1 ]] || Usage
241                 SetTheme "$1"
242                 ;;
243         "upgrade")
244                 [[ $# -eq 0 ]] || Usage
245                 UpgradeAXZSH
246                 UpgradeForeignPlugins
247                 ;;
248         *)
249                 Usage
250 esac