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