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