]> arthur.barton.de Git - ax-zsh.git/blob - bin/axzshctl
Fix axzshctl to handle plugin names as well as path names
[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         exit 2
38 }
39
40 function EnablePlugin {
41         local dir="$AXZSH/active_plugins"
42
43         if [[ -h "$dir/$1" ]]; then
44                 ax_msg 1 "Plugin \"$1\" already active!"
45                 return 1
46         fi
47
48         for dname (
49                 "$plugin:A"
50                 "$AXZSH_PLUGIN_D/$plugin"
51                 "$ZSH_CUSTOM/$plugin"
52                 "$AXZSH/plugins/$plugin"
53                 "$AXZSH/default_plugins/$plugin"
54                 "$AXZSH/core/$plugin"
55         ); do
56                 [[ ! -d "$dname" ]] && continue
57                 mkdir -p "$dir"
58                 (
59                         cd "$dir" || exit 9
60                         ln -sv "$dname" "$PWD"
61                 )
62                 return $?
63         done
64
65         ax_msg 2 "Plugin \"$1\" not found!"
66         return 1
67 }
68
69 function DisablePlugin {
70         local dir="$AXZSH/active_plugins"
71
72         if [[ ! -h "$dir/$1" ]]; then
73                 ax_msg 1 "Plugin \"$1\" not active?"
74                 return 1
75         fi
76
77         rm -v "$dir/$1"
78         return $?
79 }
80
81 function ResetPlugins {
82         local dir="$AXZSH/active_plugins"
83
84         if [[ -e "$dir" ]]; then
85                 ax_msg - "Removing all symbolic links in $dir ..."
86                 find "$dir" -type l -print -delete
87         fi
88
89         ax_msg - "Activating (linking) default plugins ..."
90         mkdir -p "$dir"
91         (
92                 cd "$dir" || exit 9
93                 ln -sv "$AXZSH/default_plugins/"* "$PWD"
94         )
95         return $?
96 }
97
98 NAME="$(basename "$0")"
99
100 [[ $# -gt 0 ]] || Usage
101
102 if [[ -z "$AXZSH" || ! -d "$AXZSH" ]]; then
103         ax_msg 2 "Oops, \"AXZSH\" is not set or invalid!"
104         exit 3
105 fi
106
107 cmd="$1"
108 shift
109
110 case "$cmd" in
111         "enable-plugin")
112                 [[ $# -gt 0 ]] || Usage
113                 for plugin in "$@"; do
114                         EnablePlugin "$plugin"
115                 done
116                 ;;
117         "disable-plugin")
118                 [[ $# -gt 0 ]] || Usage
119                 for plugin in "$@"; do
120                         DisablePlugin "$plugin"
121                 done
122                 ;;
123         "reset-plugins")
124                 [[ $# -eq 0 ]] || Usage
125                 ResetPlugins
126                 ;;
127         *)
128                 Usage
129 esac