Commit 2ac7c0cb0ac5795033c20544a9d4117f76ce9597

Authored by Shrikant Sharat
1 parent c17d243a69

Bundles are installed without a bundle-install command.

When a plugin is specified with the `bundle` command, the plugin is immediately
installed and loaded. No need of running a bundle-install and restarting your
shell.

Showing 1 changed file with 29 additions and 0 deletions Inline Diff

1 #!/bin/zsh 1 #!/bin/zsh
2 2
3 # Each line in this string has the following entries separated by a space 3 # Each line in this string has the following entries separated by a space
4 # character. 4 # character.
5 # <repo-url>, <plugin-location>, <repo-local-clone-dir>, 5 # <repo-url>, <plugin-location>, <repo-local-clone-dir>,
6 # <bundle-type> 6 # <bundle-type>
7 # FIXME: Is not kept local by zsh! 7 # FIXME: Is not kept local by zsh!
8 local _ANTIGEN_BUNDLE_RECORD="" 8 local _ANTIGEN_BUNDLE_RECORD=""
9 9
10 # Syntaxes 10 # Syntaxes
11 # bundle <url> [<loc>=/] 11 # bundle <url> [<loc>=/]
12 bundle () { 12 bundle () {
13 13
14 # Bundle spec arguments' default values. 14 # Bundle spec arguments' default values.
15 local url="$ANTIGEN_DEFAULT_REPO_URL" 15 local url="$ANTIGEN_DEFAULT_REPO_URL"
16 local loc=/ 16 local loc=/
17 local btype=plugin 17 local btype=plugin
18 18
19 # Set spec values based on the positional arguments. 19 # Set spec values based on the positional arguments.
20 local position_args='url loc' 20 local position_args='url loc'
21 local i=1 21 local i=1
22 while ! [[ -z $1 || $1 == --*=* ]]; do 22 while ! [[ -z $1 || $1 == --*=* ]]; do
23 local arg_name="$(echo "$position_args" | cut -d\ -f$i)" 23 local arg_name="$(echo "$position_args" | cut -d\ -f$i)"
24 local arg_value="$1" 24 local arg_value="$1"
25 eval "local $arg_name='$arg_value'" 25 eval "local $arg_name='$arg_value'"
26 shift 26 shift
27 i=$(($i + 1)) 27 i=$(($i + 1))
28 done 28 done
29 29
30 # Check if url is just the plugin name. Super short syntax. 30 # Check if url is just the plugin name. Super short syntax.
31 if [[ "$url" != */* ]]; then 31 if [[ "$url" != */* ]]; then
32 loc="plugins/$url" 32 loc="plugins/$url"
33 url="$ANTIGEN_DEFAULT_REPO_URL" 33 url="$ANTIGEN_DEFAULT_REPO_URL"
34 fi 34 fi
35 35
36 # Set spec values from keyword arguments, if any. The remaining arguments 36 # Set spec values from keyword arguments, if any. The remaining arguments
37 # are all assumed to be keyword arguments. 37 # are all assumed to be keyword arguments.
38 while [[ $1 == --*=* ]]; do 38 while [[ $1 == --*=* ]]; do
39 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 39 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
40 local arg_value="$(echo "$1" | cut -d= -f2)" 40 local arg_value="$(echo "$1" | cut -d= -f2)"
41 eval "local $arg_name='$arg_value'" 41 eval "local $arg_name='$arg_value'"
42 shift 42 shift
43 done 43 done
44 44
45 # Resolve the url. 45 # Resolve the url.
46 if [[ $url != git://* && $url != https://* ]]; then 46 if [[ $url != git://* && $url != https://* ]]; then
47 url="${url%.git}" 47 url="${url%.git}"
48 url="https://github.com/$url.git" 48 url="https://github.com/$url.git"
49 fi 49 fi
50 50
51 # Plugin's repo will be cloned here. 51 # Plugin's repo will be cloned here.
52 local clone_dir="$ADOTDIR/repos/$(echo "$url" \ 52 local clone_dir="$ADOTDIR/repos/$(echo "$url" \
53 | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')" 53 | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')"
54 54
55 # Add it to the record. 55 # Add it to the record.
56 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $clone_dir $btype" 56 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $clone_dir $btype"
57 57
58 -antigen-ensure-repo "$url" "$clone_dir"
59
58 bundle-load "$clone_dir/$loc" "$btype" 60 bundle-load "$clone_dir/$loc" "$btype"
61
62 }
63
64 -antigen-ensure-repo () {
65
66 local update=false
67 if [[ $1 == --update ]]; then
68 update=true
69 shift
70 fi
71
72 local handled_repos=""
73 local install_bundles=""
74
75 local url="$1"
76 local clone_dir="$2"
77
78 if ! echo "$handled_repos" | grep -Fqm1 "$url"; then
79 if [[ ! -d $clone_dir ]]; then
80 git clone "$url" "$clone_dir"
81 elif $update; then
82 git --git-dir "$clone_dir/.git" pull
83 fi
84
85 handled_repos="$handled_repos\n$url"
86 fi
87
59 } 88 }
60 89
61 bundle-install () { 90 bundle-install () {
62 91
63 local update=false 92 local update=false
64 if [[ $1 == --update ]]; then 93 if [[ $1 == --update ]]; then
65 update=true 94 update=true
66 shift 95 shift
67 fi 96 fi
68 97
69 mkdir -p "$ADOTDIR/bundles" 98 mkdir -p "$ADOTDIR/bundles"
70 99
71 local handled_repos="" 100 local handled_repos=""
72 local install_bundles="" 101 local install_bundles=""
73 102
74 if [[ $# != 0 ]]; then 103 if [[ $# != 0 ]]; then
75 # Record and install just the given plugin here and now. 104 # Record and install just the given plugin here and now.
76 bundle "$@" 105 bundle "$@"
77 install_bundles="$(-bundle-echo-record | tail -1)" 106 install_bundles="$(-bundle-echo-record | tail -1)"
78 else 107 else
79 # Install all the plugins, previously recorded. 108 # Install all the plugins, previously recorded.
80 install_bundles="$(-bundle-echo-record)" 109 install_bundles="$(-bundle-echo-record)"
81 fi 110 fi
82 111
83 # If the above `if` is directly piped to the below `while`, the contents 112 # If the above `if` is directly piped to the below `while`, the contents
84 # inside the `if` construct are run in a new subshell, so changes to the 113 # inside the `if` construct are run in a new subshell, so changes to the
85 # `$_ANTIGEN_BUNDLE_RECORD` variable are lost after the `if` construct 114 # `$_ANTIGEN_BUNDLE_RECORD` variable are lost after the `if` construct
86 # finishes. So, we need the temporary `$install_bundles` variable. 115 # finishes. So, we need the temporary `$install_bundles` variable.
87 echo "$install_bundles" | while read spec; do 116 echo "$install_bundles" | while read spec; do
88 117
89 local name="$(echo "$spec" | awk '{print $1}')" 118 local name="$(echo "$spec" | awk '{print $1}')"
90 local url="$(echo "$spec" | awk '{print $2}')" 119 local url="$(echo "$spec" | awk '{print $2}')"
91 local loc="$(echo "$spec" | awk '{print $3}')" 120 local loc="$(echo "$spec" | awk '{print $3}')"
92 local clone_dir="$(echo "$spec" | awk '{print $4}')" 121 local clone_dir="$(echo "$spec" | awk '{print $4}')"
93 local btype="$(echo "$spec" | awk '{print $5}')" 122 local btype="$(echo "$spec" | awk '{print $5}')"
94 123
95 if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then 124 if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then
96 if [[ ! -d $clone_dir ]]; then 125 if [[ ! -d $clone_dir ]]; then
97 git clone "$url" "$clone_dir" 126 git clone "$url" "$clone_dir"
98 elif $update; then 127 elif $update; then
99 git --git-dir "$clone_dir/.git" pull 128 git --git-dir "$clone_dir/.git" pull
100 fi 129 fi
101 130
102 handled_repos="$handled_repos\n$url" 131 handled_repos="$handled_repos\n$url"
103 fi 132 fi
104 133
105 bundle-load "$clone_dir/$loc" "$btype" 134 bundle-load "$clone_dir/$loc" "$btype"
106 135
107 done 136 done
108 137
109 # Initialize completions after installing 138 # Initialize completions after installing
110 bundle-apply 139 bundle-apply
111 140
112 } 141 }
113 142
114 bundle-install! () { 143 bundle-install! () {
115 bundle-install --update 144 bundle-install --update
116 } 145 }
117 146
118 bundle-cleanup () { 147 bundle-cleanup () {
119 148
120 if [[ ! -d "$ADOTDIR/bundles" || \ 149 if [[ ! -d "$ADOTDIR/bundles" || \
121 "$(ls "$ADOTDIR/bundles/" | wc -l)" == 0 ]]; then 150 "$(ls "$ADOTDIR/bundles/" | wc -l)" == 0 ]]; then
122 echo "You don't have any bundles." 151 echo "You don't have any bundles."
123 return 0 152 return 0
124 fi 153 fi
125 154
126 # Find directores in ADOTDIR/bundles, that are not in the bundles record. 155 # Find directores in ADOTDIR/bundles, that are not in the bundles record.
127 local unidentified_bundles="$(comm -13 \ 156 local unidentified_bundles="$(comm -13 \
128 <(-bundle-echo-record | awk '{print $1}' | sort) \ 157 <(-bundle-echo-record | awk '{print $1}' | sort) \
129 <(ls -1 "$ADOTDIR/bundles"))" 158 <(ls -1 "$ADOTDIR/bundles"))"
130 159
131 if [[ -z $unidentified_bundles ]]; then 160 if [[ -z $unidentified_bundles ]]; then
132 echo "You don't have any unidentified bundles." 161 echo "You don't have any unidentified bundles."
133 return 0 162 return 0
134 fi 163 fi
135 164
136 echo The following bundles are not recorded: 165 echo The following bundles are not recorded:
137 echo "$unidentified_bundles" | sed 's/^/ /' 166 echo "$unidentified_bundles" | sed 's/^/ /'
138 167
139 echo -n '\nDelete them all? [y/N] ' 168 echo -n '\nDelete them all? [y/N] '
140 if read -q; then 169 if read -q; then
141 echo 170 echo
142 echo 171 echo
143 echo "$unidentified_bundles" | while read name; do 172 echo "$unidentified_bundles" | while read name; do
144 echo -n Deleting $name... 173 echo -n Deleting $name...
145 rm -rf "$ADOTDIR/bundles/$name" 174 rm -rf "$ADOTDIR/bundles/$name"
146 echo ' done.' 175 echo ' done.'
147 done 176 done
148 else 177 else
149 echo 178 echo
150 echo Nothing deleted. 179 echo Nothing deleted.
151 fi 180 fi
152 } 181 }
153 182
154 bundle-load () { 183 bundle-load () {
155 184
156 local location="$1" 185 local location="$1"
157 local btype="$2" 186 local btype="$2"
158 187
159 if [[ $btype == theme ]]; then 188 if [[ $btype == theme ]]; then
160 189
161 # Of course, if its a theme, the location would point to the script 190 # Of course, if its a theme, the location would point to the script
162 # file. 191 # file.
163 source "$location" 192 source "$location"
164 193
165 else 194 else
166 195
167 # Source the plugin script 196 # Source the plugin script
168 # FIXME: I don't know. Looks very very ugly. Needs a better 197 # FIXME: I don't know. Looks very very ugly. Needs a better
169 # implementation once tests are ready. 198 # implementation once tests are ready.
170 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" 199 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')"
171 if [[ -f $script_loc ]]; then 200 if [[ -f $script_loc ]]; then
172 # If we have a `*.plugin.zsh`, source it. 201 # If we have a `*.plugin.zsh`, source it.
173 source "$script_loc" 202 source "$script_loc"
174 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then 203 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then
175 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 204 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
176 # files. 205 # files.
177 for script ($location/*.zsh) source "$script" 206 for script ($location/*.zsh) source "$script"
178 fi 207 fi
179 208
180 # Add to $fpath, for completion(s) 209 # Add to $fpath, for completion(s)
181 fpath=($location $fpath) 210 fpath=($location $fpath)
182 211
183 fi 212 fi
184 213
185 } 214 }
186 215
187 bundle-lib () { 216 bundle-lib () {
188 bundle --loc=lib 217 bundle --loc=lib
189 } 218 }
190 219
191 bundle-theme () { 220 bundle-theme () {
192 local url="$ANTIGEN_DEFAULT_REPO_URL" 221 local url="$ANTIGEN_DEFAULT_REPO_URL"
193 local name="${1:-robbyrussell}" 222 local name="${1:-robbyrussell}"
194 bundle --loc=themes/$name.zsh-theme --btype=theme 223 bundle --loc=themes/$name.zsh-theme --btype=theme
195 } 224 }
196 225
197 bundle-apply () { 226 bundle-apply () {
198 # Initialize completion. 227 # Initialize completion.
199 # TODO: Doesn't look like this is really necessary. Need to investigate. 228 # TODO: Doesn't look like this is really necessary. Need to investigate.
200 compinit -i 229 compinit -i
201 } 230 }
202 231
203 bundle-list () { 232 bundle-list () {
204 # List all currently installed bundles 233 # List all currently installed bundles
205 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 234 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
206 echo "You don't have any bundles." >&2 235 echo "You don't have any bundles." >&2
207 return 1 236 return 1
208 else 237 else
209 -bundle-echo-record | awk '{print $1 " " $2 " " $3}' 238 -bundle-echo-record | awk '{print $1 " " $2 " " $3}'
210 fi 239 fi
211 } 240 }
212 241
213 # Echo the bundle specs as in the record. The first line is not echoed since it 242 # Echo the bundle specs as in the record. The first line is not echoed since it
214 # is a blank line. 243 # is a blank line.
215 -bundle-echo-record () { 244 -bundle-echo-record () {
216 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 245 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
217 } 246 }
218 247
219 -bundle-env-setup () { 248 -bundle-env-setup () {
220 # Pre-startup initializations 249 # Pre-startup initializations
221 -set-default ANTIGEN_DEFAULT_REPO_URL \ 250 -set-default ANTIGEN_DEFAULT_REPO_URL \
222 https://github.com/robbyrussell/oh-my-zsh.git 251 https://github.com/robbyrussell/oh-my-zsh.git
223 -set-default ADOTDIR $HOME/.antigen 252 -set-default ADOTDIR $HOME/.antigen
224 253
225 # Load the compinit module 254 # Load the compinit module
226 autoload -U compinit 255 autoload -U compinit
227 256
228 # Without the following, `compdef` function is not defined. 257 # Without the following, `compdef` function is not defined.
229 compinit -i 258 compinit -i
230 } 259 }
231 260
232 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 261 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
233 # not already set. 262 # not already set.
234 -set-default () { 263 -set-default () {
235 local arg_name="$1" 264 local arg_name="$1"
236 local arg_value="$2" 265 local arg_value="$2"
237 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 266 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
238 } 267 }
239 268
240 -bundle-env-setup 269 -bundle-env-setup
241 270