Commit 4dceca4855362708a6199cf868463cdb66438e53

Authored by Shrikant Sharat
1 parent 0952e605da

Add support for boolean arguments to -bundle.

Currently, a keyword style argument given to -bundle command requires a value,
like `--name=val`. With this, just a `--name` would imply `--name=true`

Showing 1 changed file with 10 additions and 3 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>, <bundle-type> 5 # <repo-url>, <plugin-location>, <bundle-type>
6 # FIXME: Is not kept local by zsh! 6 # FIXME: Is not kept local by zsh!
7 local _ANTIGEN_BUNDLE_RECORD="" 7 local _ANTIGEN_BUNDLE_RECORD=""
8 8
9 # Syntaxes 9 # Syntaxes
10 # antigen-bundle <url> [<loc>=/] 10 # antigen-bundle <url> [<loc>=/]
11 # Keyword only arguments: 11 # Keyword only arguments:
12 # branch - The branch of the repo to use for this bundle. 12 # branch - The branch of the repo to use for this bundle.
13 antigen-bundle () { 13 antigen-bundle () {
14 14
15 # Bundle spec arguments' default values. 15 # Bundle spec arguments' default values.
16 local url="$ANTIGEN_DEFAULT_REPO_URL" 16 local url="$ANTIGEN_DEFAULT_REPO_URL"
17 local loc=/ 17 local loc=/
18 local branch= 18 local branch=
19 local btype=plugin 19 local btype=plugin
20 20
21 # Set spec values based on the positional arguments. 21 # Set spec values based on the positional arguments.
22 local position_args='url loc' 22 local position_args='url loc'
23 local i=1 23 local i=1
24 while ! [[ -z $1 || $1 == --*=* ]]; do 24 while ! [[ -z $1 || $1 == --*=* ]]; do
25 local arg_name="$(echo "$position_args" | cut -d\ -f$i)" 25 local arg_name="$(echo "$position_args" | cut -d\ -f$i)"
26 local arg_value="$1" 26 local arg_value="$1"
27 eval "local $arg_name='$arg_value'" 27 eval "local $arg_name='$arg_value'"
28 shift 28 shift
29 i=$(($i + 1)) 29 i=$(($i + 1))
30 done 30 done
31 31
32 # Check if url is just the plugin name. Super short syntax. 32 # Check if url is just the plugin name. Super short syntax.
33 if [[ "$url" != */* ]]; then 33 if [[ "$url" != */* ]]; then
34 loc="plugins/$url" 34 loc="plugins/$url"
35 url="$ANTIGEN_DEFAULT_REPO_URL" 35 url="$ANTIGEN_DEFAULT_REPO_URL"
36 fi 36 fi
37 37
38 # Set spec values from keyword arguments, if any. The remaining arguments 38 # Set spec values from keyword arguments, if any. The remaining arguments
39 # are all assumed to be keyword arguments. 39 # are all assumed to be keyword arguments.
40 while [[ $1 == --*=* ]]; do 40 while [[ $1 == --* ]]; do
41 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 41 local arg="${1#--}"
42 local arg_value="$(echo "$1" | cut -d= -f2)" 42
43 if [[ $arg != *=* ]]; then
44 arg="$arg=true"
45 fi
46
47 local arg_name="${arg%\=*}"
48 local arg_value="${arg#*\=}"
49
43 eval "local $arg_name='$arg_value'" 50 eval "local $arg_name='$arg_value'"
44 shift 51 shift
45 done 52 done
46 53
47 # Resolve the url. 54 # Resolve the url.
48 url="$(-antigen-resolve-bundle-url "$url")" 55 url="$(-antigen-resolve-bundle-url "$url")"
49 56
50 # Add the branch information to the url. 57 # Add the branch information to the url.
51 if [[ ! -z $branch ]]; then 58 if [[ ! -z $branch ]]; then
52 url="$url|$branch" 59 url="$url|$branch"
53 fi 60 fi
54 61
55 # Add it to the record. 62 # Add it to the record.
56 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" 63 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype"
57 64
58 # Ensure a clone exists for this repo. 65 # Ensure a clone exists for this repo.
59 -antigen-ensure-repo "$url" 66 -antigen-ensure-repo "$url"
60 67
61 # Load the plugin. 68 # Load the plugin.
62 -antigen-load "$url" "$loc" "$btype" 69 -antigen-load "$url" "$loc" "$btype"
63 70
64 } 71 }
65 72
66 -antigen-resolve-bundle-url () { 73 -antigen-resolve-bundle-url () {
67 # Given an acceptable short/full form of a bundle's repo url, this function 74 # Given an acceptable short/full form of a bundle's repo url, this function
68 # echoes the full form of the repo's clone url. 75 # echoes the full form of the repo's clone url.
69 76
70 local url="$1" 77 local url="$1"
71 78
72 # Expand short github url syntax: `username/reponame` 79 # Expand short github url syntax: `username/reponame`
73 if [[ $url != git://* && \ 80 if [[ $url != git://* && \
74 $url != https://* && \ 81 $url != https://* && \
75 $url != /* && \ 82 $url != /* && \
76 $url != git@github.com:*/* 83 $url != git@github.com:*/*
77 ]]; then 84 ]]; then
78 url="https://github.com/${url%.git}.git" 85 url="https://github.com/${url%.git}.git"
79 fi 86 fi
80 87
81 echo "$url" 88 echo "$url"
82 } 89 }
83 90
84 antigen-bundles () { 91 antigen-bundles () {
85 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 92 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
86 # are ignored. Everything else is given to `antigen-bundle` as is, no 93 # are ignored. Everything else is given to `antigen-bundle` as is, no
87 # quoting rules applied. 94 # quoting rules applied.
88 95
89 local line 96 local line
90 97
91 grep -v '^\s*$\|^#' | while read line; do 98 grep -v '^\s*$\|^#' | while read line; do
92 # Using `eval` so that we can use the shell-style quoting in each line 99 # Using `eval` so that we can use the shell-style quoting in each line
93 # piped to `antigen-bundles`. 100 # piped to `antigen-bundles`.
94 eval "antigen-bundle $line" 101 eval "antigen-bundle $line"
95 done 102 done
96 } 103 }
97 104
98 antigen-update () { 105 antigen-update () {
99 # Update your bundles, i.e., `git pull` in all the plugin repos. 106 # Update your bundles, i.e., `git pull` in all the plugin repos.
100 -antigen-echo-record \ 107 -antigen-echo-record \
101 | awk '{print $1}' \ 108 | awk '{print $1}' \
102 | sort -u \ 109 | sort -u \
103 | while read url; do 110 | while read url; do
104 echo "**** Pulling $url" 111 echo "**** Pulling $url"
105 -antigen-ensure-repo --update --verbose "$url" 112 -antigen-ensure-repo --update --verbose "$url"
106 echo 113 echo
107 done 114 done
108 } 115 }
109 116
110 -antigen-get-clone-dir () { 117 -antigen-get-clone-dir () {
111 # Takes a repo url and gives out the path that this url needs to be cloned 118 # Takes a repo url and gives out the path that this url needs to be cloned
112 # to. Doesn't actually clone anything. 119 # to. Doesn't actually clone anything.
113 # TODO: Memoize? 120 # TODO: Memoize?
114 121
115 # The url given. 122 # The url given.
116 local url="$1" 123 local url="$1"
117 124
118 # Echo the full path to the clone directory. 125 # Echo the full path to the clone directory.
119 echo -n $ADOTDIR/repos/ 126 echo -n $ADOTDIR/repos/
120 echo "$url" | sed \ 127 echo "$url" | sed \
121 -e 's./.-SLASH-.g' \ 128 -e 's./.-SLASH-.g' \
122 -e 's.:.-COLON-.g' \ 129 -e 's.:.-COLON-.g' \
123 -e 's.|.-PIPE-.g' 130 -e 's.|.-PIPE-.g'
124 } 131 }
125 132
126 -antigen-get-clone-url () { 133 -antigen-get-clone-url () {
127 # Takes a repo's clone dir and gives out the repo's original url that was 134 # Takes a repo's clone dir and gives out the repo's original url that was
128 # used to create the given directory path. 135 # used to create the given directory path.
129 # TODO: Memoize? 136 # TODO: Memoize?
130 echo "$1" | sed \ 137 echo "$1" | sed \
131 -e "s:^$ADOTDIR/repos/::" \ 138 -e "s:^$ADOTDIR/repos/::" \
132 -e 's.-SLASH-./.g' \ 139 -e 's.-SLASH-./.g' \
133 -e 's.-COLON-.:.g' \ 140 -e 's.-COLON-.:.g' \
134 -e 's.-PIPE-.|.g' 141 -e 's.-PIPE-.|.g'
135 } 142 }
136 143
137 -antigen-ensure-repo () { 144 -antigen-ensure-repo () {
138 145
139 # Ensure that a clone exists for the given repo url and branch. If the first 146 # Ensure that a clone exists for the given repo url and branch. If the first
140 # argument is `--update` and if a clone already exists for the given repo 147 # argument is `--update` and if a clone already exists for the given repo
141 # and branch, it is pull-ed, i.e., updated. 148 # and branch, it is pull-ed, i.e., updated.
142 149
143 # Argument defaults. 150 # Argument defaults.
144 # Check if we have to update. 151 # Check if we have to update.
145 local update=false 152 local update=false
146 # Verbose output. 153 # Verbose output.
147 local verbose=false 154 local verbose=false
148 155
149 # Load any boolean arguments specified. 156 # Load any boolean arguments specified.
150 while [[ $1 == --* ]]; do 157 while [[ $1 == --* ]]; do
151 eval "local '${1#--}=true'" 158 eval "local '${1#--}=true'"
152 shift 159 shift
153 done 160 done
154 161
155 # Get the clone's directory as per the given repo url and branch. 162 # Get the clone's directory as per the given repo url and branch.
156 local url="$1" 163 local url="$1"
157 local clone_dir="$(-antigen-get-clone-dir $url)" 164 local clone_dir="$(-antigen-get-clone-dir $url)"
158 165
159 # Clone if it doesn't already exist. 166 # Clone if it doesn't already exist.
160 if [[ ! -d $clone_dir ]]; then 167 if [[ ! -d $clone_dir ]]; then
161 git clone "${url%|*}" "$clone_dir" 168 git clone "${url%|*}" "$clone_dir"
162 elif $update; then 169 elif $update; then
163 # Save current revision. 170 # Save current revision.
164 old_rev="$(git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 171 old_rev="$(git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
165 rev-parse HEAD)" 172 rev-parse HEAD)"
166 # Pull changes if update requested. 173 # Pull changes if update requested.
167 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 174 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
168 pull 175 pull
169 # Get the new revision. 176 # Get the new revision.
170 new_rev="$(git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 177 new_rev="$(git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
171 rev-parse HEAD)" 178 rev-parse HEAD)"
172 fi 179 fi
173 180
174 # If its a specific branch that we want, checkout that branch. 181 # If its a specific branch that we want, checkout that branch.
175 if [[ $url == *\|* ]]; then 182 if [[ $url == *\|* ]]; then
176 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 183 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
177 checkout "${url#*|}" 184 checkout "${url#*|}"
178 fi 185 fi
179 186
180 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then 187 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then
181 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}. 188 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}.
182 if $verbose; then 189 if $verbose; then
183 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 190 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
184 log --oneline --reverse --no-merges --stat '@{1}..' 191 log --oneline --reverse --no-merges --stat '@{1}..'
185 fi 192 fi
186 fi 193 fi
187 194
188 } 195 }
189 196
190 -antigen-load () { 197 -antigen-load () {
191 198
192 local url="$1" 199 local url="$1"
193 local loc="$2" 200 local loc="$2"
194 local btype="$3" 201 local btype="$3"
195 202
196 # The full location where the plugin is located. 203 # The full location where the plugin is located.
197 local location="$(-antigen-get-clone-dir "$url")/$loc" 204 local location="$(-antigen-get-clone-dir "$url")/$loc"
198 205
199 if [[ $btype == theme ]]; then 206 if [[ $btype == theme ]]; then
200 207
201 # Of course, if its a theme, the location would point to the script 208 # Of course, if its a theme, the location would point to the script
202 # file. 209 # file.
203 source "$location" 210 source "$location"
204 211
205 else 212 else
206 213
207 # Source the plugin script 214 # Source the plugin script
208 # FIXME: I don't know. Looks very very ugly. Needs a better 215 # FIXME: I don't know. Looks very very ugly. Needs a better
209 # implementation once tests are ready. 216 # implementation once tests are ready.
210 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 217 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
211 218
212 if [[ -f $script_loc ]]; then 219 if [[ -f $script_loc ]]; then
213 # If we have a `*.plugin.zsh`, source it. 220 # If we have a `*.plugin.zsh`, source it.
214 source "$script_loc" 221 source "$script_loc"
215 222
216 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then 223 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then
217 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 224 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
218 # files. 225 # files.
219 for script ($location/*.zsh(N)) source "$script" 226 for script ($location/*.zsh(N)) source "$script"
220 227
221 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then 228 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then
222 # If there are no `*.zsh` files either, we look for and source any 229 # If there are no `*.zsh` files either, we look for and source any
223 # `*.sh` files instead. 230 # `*.sh` files instead.
224 for script ($location/*.sh(N)) source "$script" 231 for script ($location/*.sh(N)) source "$script"
225 232
226 fi 233 fi
227 234
228 # Add to $fpath, for completion(s). 235 # Add to $fpath, for completion(s).
229 fpath=($location $fpath) 236 fpath=($location $fpath)
230 237
231 fi 238 fi
232 239
233 } 240 }
234 241
235 antigen-cleanup () { 242 antigen-cleanup () {
236 243
237 # Cleanup unused repositories. 244 # Cleanup unused repositories.
238 245
239 local force=false 246 local force=false
240 if [[ $1 == --force ]]; then 247 if [[ $1 == --force ]]; then
241 force=true 248 force=true
242 fi 249 fi
243 250
244 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 251 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
245 echo "You don't have any bundles." 252 echo "You don't have any bundles."
246 return 0 253 return 0
247 fi 254 fi
248 255
249 # Find directores in ADOTDIR/repos, that are not in the bundles record. 256 # Find directores in ADOTDIR/repos, that are not in the bundles record.
250 local unused_clones="$(comm -13 \ 257 local unused_clones="$(comm -13 \
251 <(-antigen-echo-record \ 258 <(-antigen-echo-record \
252 | awk '{print $1}' \ 259 | awk '{print $1}' \
253 | while read line; do 260 | while read line; do
254 -antigen-get-clone-dir "$line" 261 -antigen-get-clone-dir "$line"
255 done \ 262 done \
256 | sort -u) \ 263 | sort -u) \
257 <(ls -d "$ADOTDIR/repos/"* | sort -u))" 264 <(ls -d "$ADOTDIR/repos/"* | sort -u))"
258 265
259 if [[ -z $unused_clones ]]; then 266 if [[ -z $unused_clones ]]; then
260 echo "You don't have any unidentified bundles." 267 echo "You don't have any unidentified bundles."
261 return 0 268 return 0
262 fi 269 fi
263 270
264 echo 'You have clones for the following repos, but are not used.' 271 echo 'You have clones for the following repos, but are not used.'
265 echo "$unused_clones" \ 272 echo "$unused_clones" \
266 | while read line; do 273 | while read line; do
267 -antigen-get-clone-url "$line" 274 -antigen-get-clone-url "$line"
268 done \ 275 done \
269 | sed -e 's/^/ /' -e 's/|/, branch /' 276 | sed -e 's/^/ /' -e 's/|/, branch /'
270 277
271 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then 278 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
272 echo 279 echo
273 echo 280 echo
274 echo "$unused_clones" | while read line; do 281 echo "$unused_clones" | while read line; do
275 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." 282 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..."
276 rm -rf "$line" 283 rm -rf "$line"
277 echo ' done.' 284 echo ' done.'
278 done 285 done
279 else 286 else
280 echo 287 echo
281 echo Nothing deleted. 288 echo Nothing deleted.
282 fi 289 fi
283 } 290 }
284 291
285 antigen-lib () { 292 antigen-lib () {
286 antigen-bundle --loc=lib 293 antigen-bundle --loc=lib
287 } 294 }
288 295
289 antigen-theme () { 296 antigen-theme () {
290 local name="${1:-robbyrussell}" 297 local name="${1:-robbyrussell}"
291 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 298 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
292 } 299 }
293 300
294 antigen-apply () { 301 antigen-apply () {
295 # Initialize completion. 302 # Initialize completion.
296 # TODO: Only load completions if there are any changes to the bundle 303 # TODO: Only load completions if there are any changes to the bundle
297 # repositories. 304 # repositories.
298 compinit -i 305 compinit -i
299 } 306 }
300 307
301 antigen-list () { 308 antigen-list () {
302 # List all currently installed bundles 309 # List all currently installed bundles
303 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 310 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
304 echo "You don't have any bundles." >&2 311 echo "You don't have any bundles." >&2
305 return 1 312 return 1
306 else 313 else
307 -antigen-echo-record | sort -u 314 -antigen-echo-record | sort -u
308 fi 315 fi
309 } 316 }
310 317
311 antigen-help () { 318 antigen-help () {
312 cat <<EOF 319 cat <<EOF
313 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 320 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
314 shell scripts and utilities, put up on github. For further details and complete 321 shell scripts and utilities, put up on github. For further details and complete
315 documentation, visit the project's page at 'http://antigen.sharats.me'. 322 documentation, visit the project's page at 'http://antigen.sharats.me'.
316 EOF 323 EOF
317 } 324 }
318 325
319 # A syntax sugar to avoid the `-` when calling antigen commands. With this 326 # A syntax sugar to avoid the `-` when calling antigen commands. With this
320 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 327 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
321 antigen () { 328 antigen () {
322 local cmd="$1" 329 local cmd="$1"
323 shift 330 shift
324 "antigen-$cmd" "$@" 331 "antigen-$cmd" "$@"
325 } 332 }
326 333
327 # Echo the bundle specs as in the record. The first line is not echoed since it 334 # Echo the bundle specs as in the record. The first line is not echoed since it
328 # is a blank line. 335 # is a blank line.
329 -antigen-echo-record () { 336 -antigen-echo-record () {
330 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 337 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
331 } 338 }
332 339
333 -antigen-env-setup () { 340 -antigen-env-setup () {
334 # Pre-startup initializations 341 # Pre-startup initializations
335 -set-default ANTIGEN_DEFAULT_REPO_URL \ 342 -set-default ANTIGEN_DEFAULT_REPO_URL \
336 https://github.com/robbyrussell/oh-my-zsh.git 343 https://github.com/robbyrussell/oh-my-zsh.git
337 -set-default ADOTDIR $HOME/.antigen 344 -set-default ADOTDIR $HOME/.antigen
338 345
339 # Load the compinit module 346 # Load the compinit module
340 autoload -U compinit 347 autoload -U compinit
341 348
342 # Without the following, `compdef` function is not defined. 349 # Without the following, `compdef` function is not defined.
343 compinit -i 350 compinit -i
344 } 351 }
345 352
346 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 353 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
347 # not already set. 354 # not already set.
348 -set-default () { 355 -set-default () {
349 local arg_name="$1" 356 local arg_name="$1"
350 local arg_value="$2" 357 local arg_value="$2"
351 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 358 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
352 } 359 }
353 360
354 -antigen-env-setup 361 -antigen-env-setup
355 362