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