]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
Fix detection of plugins stored on GitHub
[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
118         if [[ -e "$dir" ]]; then
119                 ax_msg - "Removing all symbolic links in $dir ..."
120                 find "$dir" -type l -print -delete
121         fi
122         return $?
123 }
124
125 function EnableDefaultPlugins {
126         local dir="$AXZSH/active_plugins"
127
128         ax_msg - "Activating (linking) default plugins ..."
129         mkdir -p "$dir"
130         (
131                 cd "$dir" || exit 9
132                 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
133         )
134         return $?
135 }
136
137 function SetTheme {
138         local link_name="$AXZSH/active_theme"
139
140         if [ $# -ne 1 ]; then
141                 echo "Usage: axzsh_set_theme <name|->"
142                 return 1
143         fi
144
145         rm -f "$link_name" || return 1
146
147         if [ "$1" = "-" ]; then
148                 echo "Theme settings have been reset."
149                 return 0
150         fi
151
152         if [ -r "$1" ]; then
153                 theme="$1"
154         elif [ -r "$AXZSH/custom_themes/$1.axzshtheme" ]; then
155                 theme="$AXZSH/custom_themes/$1.axzshtheme"
156         elif [ -r "$AXZSH/themes/$1.axzshtheme" ]; then
157                 theme="$AXZSH/themes/$1.axzshtheme"
158         else
159                 echo "Theme \"$1\" not found!"
160                 return 1
161         fi
162         ln -sv "$theme" "$link_name" || return 1
163         return $?
164 }
165
166 function UpgradeAXZSH {
167         if [[ $+commands[git] -eq 0 ]]; then
168                 ax_msg 2 "The git(1) command is not available!"
169                 return 1
170         fi
171         if [[ ! -d "$AXZSH/.git" ]]; then
172                 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
173                 return 1
174         fi
175
176         ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
177         ( cd "$AXZSH" && git pull --ff-only )
178 }
179
180 function UpgradeForeignPlugins {
181         if [[ $+commands[git] -eq 0 ]]; then
182                 ax_msg 2 "The git(1) command is not available!"
183                 return 1
184         fi
185
186         for dir ($AXZSH/repos/*(N)); do
187                 name=$(basename "$dir" | sed -e 's|#|/|g')
188                 if [ -d "$dir/.git" ]; then
189                         ax_msg - "Upgrading \"$name\" [git] ..."
190                         (
191                                 cd "$dir"
192                                 git pull --ff-only || ax_msg 2 "Pull failed!"
193                         )
194                 else
195                         ax_msg 2 "Unknown repository type!"
196                 fi
197         done
198 }
199
200 NAME="$0:t"
201
202 [[ $# -gt 0 ]] || Usage
203
204 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
205         ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
206         exit 3
207 fi
208
209 cmd="$1"
210 shift
211
212 case "$cmd" in
213         "enable-plugin")
214                 [[ $# -gt 0 ]] || Usage
215                 for plugin in "$@"; do
216                         EnablePlugin "$plugin"
217                 done
218                 ;;
219         "disable-plugin")
220                 [[ $# -gt 0 ]] || Usage
221                 for plugin in "$@"; do
222                         DisablePlugin "$plugin"
223                 done
224                 ;;
225         "reset-plugins")
226                 [[ $# -eq 0 ]] || Usage
227                 ResetPlugins
228                 EnableDefaultPlugins
229                 ;;
230         "enable-default-plugins")
231                 [[ $# -eq 0 ]] || Usage
232                 EnableDefaultPlugins
233                 ;;
234         "set-theme")
235                 [[ $# -eq 1 ]] || Usage
236                 SetTheme "$1"
237                 ;;
238         "upgrade")
239                 [[ $# -eq 0 ]] || Usage
240                 UpgradeAXZSH
241                 UpgradeForeignPlugins
242                 ;;
243         *)
244                 Usage
245 esac