Commit b43a3cf1f11c5b87a1bb3a36fc1381d8cd095adf

Authored by Shrikant Sharat
1 parent ec9febf8cf

Keyword arguments no longer *need* to have an `=`.

Keyword arguments of the syntax `--arg` are supported, not just `--arg=val`.

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