]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
axzshctl: Implement "upgrade" command
[ax-zsh.git] / bin / axzshctl
1 #!/bin/zsh
2 #
3 # AX-ZSH: Alex' Modular ZSH Configuration
4 # Copyright (c) 2015 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 "$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 "  upgrade"
41         echo "    Upgrade AX-ZSH installation (requires Git)."
42         echo
43         exit 2
44 }
45
46 function EnablePlugin {
47         local dir="$AXZSH/active_plugins"
48
49         if [[ -h "$dir/$1" ]]; then
50                 ax_msg 1 "Plugin \"$1\" already active!"
51                 return 1
52         fi
53
54         for dname (
55                 "$plugin:A"
56                 "$AXZSH_PLUGIN_D/$plugin"
57                 "$ZSH_CUSTOM/$plugin"
58                 "$AXZSH/custom_plugins/$plugin"
59                 "$AXZSH/plugins/$plugin"
60                 "$AXZSH/default_plugins/$plugin"
61                 "$AXZSH/core/$plugin"
62         ); do
63                 [[ ! -d "$dname" ]] && continue
64                 mkdir -p "$dir"
65                 (
66                         cd "$dir" || exit 9
67                         ln -sv "$dname" "$PWD"
68                 )
69                 return $?
70         done
71
72         ax_msg 2 "Plugin \"$1\" not found!"
73         return 1
74 }
75
76 function DisablePlugin {
77         local dir="$AXZSH/active_plugins"
78
79         if [[ ! -h "$dir/$1" ]]; then
80                 ax_msg 1 "Plugin \"$1\" not active?"
81                 return 1
82         fi
83
84         rm -v "$dir/$1"
85         return $?
86 }
87
88 function ResetPlugins {
89         local dir="$AXZSH/active_plugins"
90
91         if [[ -e "$dir" ]]; then
92                 ax_msg - "Removing all symbolic links in $dir ..."
93                 find "$dir" -type l -print -delete
94         fi
95         return $?
96 }
97
98 function EnableDefaultPlugins {
99         local dir="$AXZSH/active_plugins"
100
101         ax_msg - "Activating (linking) default plugins ..."
102         mkdir -p "$dir"
103         (
104                 cd "$dir" || exit 9
105                 ln -sfv "$AXZSH/default_plugins/"* "$PWD"
106         )
107         return $?
108 }
109
110 function UpgradeAXZSH {
111         if [[ $+commands[git] -eq 0 ]]; then
112                 ax_msg 2 "The git(1) command is not available!"
113                 return 1
114         fi
115         if [[ ! -d "$AXZSH/.git" ]]; then
116                 ax_msg 2 "AX-ZSH seems not to be installed using Git. Can't upgrade!"
117                 return 1
118         fi
119
120         ax_msg - "Upgrading AX-ZSH in \"$AXZSH\" using git(1) ..."
121         ( cd "$AXZSH" && git pull --ff-only )
122 }
123
124 NAME="$(basename "$0")"
125
126 [[ $# -gt 0 ]] || Usage
127
128 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
129         ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
130         exit 3
131 fi
132
133 cmd="$1"
134 shift
135
136 case "$cmd" in
137         "enable-plugin")
138                 [[ $# -gt 0 ]] || Usage
139                 for plugin in "$@"; do
140                         EnablePlugin "$plugin"
141                 done
142                 ;;
143         "disable-plugin")
144                 [[ $# -gt 0 ]] || Usage
145                 for plugin in "$@"; do
146                         DisablePlugin "$plugin"
147                 done
148                 ;;
149         "reset-plugins")
150                 [[ $# -eq 0 ]] || Usage
151                 ResetPlugins
152                 EnableDefaultPlugins
153                 ;;
154         "enable-default-plugins")
155                 [[ $# -eq 0 ]] || Usage
156                 EnableDefaultPlugins
157                 ;;
158         "upgrade")
159                 [[ $# -eq 0 ]] || Usage
160                 UpgradeAXZSH
161                 ;;
162         *)
163                 Usage
164 esac