Commit 49dc1fb41307102d38fd59f1176ce29f105ed3a0

Authored by Xavier Garrido
1 parent 3ddbb44cc1

fix argument order

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>, <has-local-clone> 5 # <repo-url>, <plugin-location>, <bundle-type>, <has-local-clone>
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 # Parse the given arguments. (Will overwrite the above values). 22 # Parse the given arguments. (Will overwrite the above values).
23 eval "$(-antigen-parse-args \ 23 eval "$(-antigen-parse-args \
24 'url?, loc? ; branch:?, no-local-clone?, btype:?' \ 24 'url?, loc? ; branch:?, no-local-clone?, btype:?' \
25 "$@")" 25 "$@")"
26 26
27 # Check if url is just the plugin name. Super short syntax. 27 # Check if url is just the plugin name. Super short syntax.
28 if [[ "$url" != */* ]]; then 28 if [[ "$url" != */* ]]; then
29 loc="plugins/$url" 29 loc="plugins/$url"
30 url="$ANTIGEN_DEFAULT_REPO_URL" 30 url="$ANTIGEN_DEFAULT_REPO_URL"
31 fi 31 fi
32 32
33 # Resolve the url. 33 # Resolve the url.
34 url="$(-antigen-resolve-bundle-url "$url")" 34 url="$(-antigen-resolve-bundle-url "$url")"
35 35
36 # Add the branch information to the url. 36 # Add the branch information to the url.
37 if [[ ! -z $branch ]]; then 37 if [[ ! -z $branch ]]; then
38 url="$url|$branch" 38 url="$url|$branch"
39 fi 39 fi
40 40
41 # The `make_local_clone` variable better represents whether there should be 41 # The `make_local_clone` variable better represents whether there should be
42 # a local clone made. For cloning to be avoided, firstly, the `$url` should 42 # a local clone made. For cloning to be avoided, firstly, the `$url` should
43 # be an absolute local path and `$branch` should be empty. In addition to 43 # be an absolute local path and `$branch` should be empty. In addition to
44 # these two conditions, either the `--no-local-clone` option should be 44 # these two conditions, either the `--no-local-clone` option should be
45 # given, or `$url` should not a git repo. 45 # given, or `$url` should not a git repo.
46 local make_local_clone=true 46 local make_local_clone=true
47 if [[ $url == /* && -z $branch && 47 if [[ $url == /* && -z $branch &&
48 ( $no_local_clone == true || ! -d $url/.git ) ]]; then 48 ( $no_local_clone == true || ! -d $url/.git ) ]]; then
49 make_local_clone=false 49 make_local_clone=false
50 fi 50 fi
51 51
52 # Add the theme extension to `loc`, if this is a theme. 52 # Add the theme extension to `loc`, if this is a theme.
53 if [[ $btype == theme && $loc != *.zsh-theme ]]; then 53 if [[ $btype == theme && $loc != *.zsh-theme ]]; then
54 loc="$loc.zsh-theme" 54 loc="$loc.zsh-theme"
55 fi 55 fi
56 56
57 # Add it to the record. 57 # Add it to the record.
58 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" 58 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype"
59 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD $make_local_clone" 59 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD $make_local_clone"
60 60
61 # Ensure a clone exists for this repo, if needed. 61 # Ensure a clone exists for this repo, if needed.
62 if $make_local_clone; then 62 if $make_local_clone; then
63 -antigen-ensure-repo "$url" 63 -antigen-ensure-repo "$url"
64 fi 64 fi
65 65
66 # Load the plugin. 66 # Load the plugin.
67 -antigen-load "$url" "$loc" "$btype" "$make_local_clone" 67 -antigen-load "$url" "$loc" "$btype" "$make_local_clone"
68 68
69 } 69 }
70 70
71 -antigen-resolve-bundle-url () { 71 -antigen-resolve-bundle-url () {
72 # Given an acceptable short/full form of a bundle's repo url, this function 72 # Given an acceptable short/full form of a bundle's repo url, this function
73 # echoes the full form of the repo's clone url. 73 # echoes the full form of the repo's clone url.
74 74
75 local url="$1" 75 local url="$1"
76 76
77 # Expand short github url syntax: `username/reponame`. 77 # Expand short github url syntax: `username/reponame`.
78 if [[ $url != git://* && 78 if [[ $url != git://* &&
79 $url != https://* && 79 $url != https://* &&
80 $url != /* && 80 $url != /* &&
81 $url != git@github.com:*/* 81 $url != git@github.com:*/*
82 ]]; then 82 ]]; then
83 url="https://github.com/${url%.git}.git" 83 url="https://github.com/${url%.git}.git"
84 fi 84 fi
85 85
86 echo "$url" 86 echo "$url"
87 } 87 }
88 88
89 antigen-bundles () { 89 antigen-bundles () {
90 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 90 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
91 # are ignored. Everything else is given to `antigen-bundle` as is, no 91 # are ignored. Everything else is given to `antigen-bundle` as is, no
92 # quoting rules applied. 92 # quoting rules applied.
93 93
94 local line 94 local line
95 95
96 grep -v '^\s*$\|^#' | while read line; do 96 grep -v '^\s*$\|^#' | while read line; do
97 # Using `eval` so that we can use the shell-style quoting in each line 97 # Using `eval` so that we can use the shell-style quoting in each line
98 # piped to `antigen-bundles`. 98 # piped to `antigen-bundles`.
99 eval "antigen-bundle $line" 99 eval "antigen-bundle $line"
100 done 100 done
101 } 101 }
102 102
103 antigen-update () { 103 antigen-update () {
104 # Update your bundles, i.e., `git pull` in all the plugin repos. 104 # Update your bundles, i.e., `git pull` in all the plugin repos.
105 105
106 date > $ADOTDIR/revert-info 106 date > $ADOTDIR/revert-info
107 107
108 -antigen-echo-record | 108 -antigen-echo-record |
109 awk '$4 == "true" {print $1}' | 109 awk '$4 == "true" {print $1}' |
110 sort -u | 110 sort -u |
111 while read url; do 111 while read url; do
112 echo "**** Pulling $url" 112 echo "**** Pulling $url"
113 113
114 local clone_dir="$(-antigen-get-clone-dir "$url")" 114 local clone_dir="$(-antigen-get-clone-dir "$url")"
115 if [[ -d "$clone_dir" ]]; then 115 if [[ -d "$clone_dir" ]]; then
116 (echo -n "$clone_dir:" 116 (echo -n "$clone_dir:"
117 cd "$clone_dir" 117 cd "$clone_dir"
118 git rev-parse HEAD) >> $ADOTDIR/revert-info 118 git rev-parse HEAD) >> $ADOTDIR/revert-info
119 fi 119 fi
120 120
121 -antigen-ensure-repo "$url" --update --verbose 121 -antigen-ensure-repo "$url" --update --verbose
122 122
123 echo 123 echo
124 done 124 done
125 } 125 }
126 126
127 antigen-revert () { 127 antigen-revert () {
128 if ! [[ -f $ADOTDIR/revert-info ]]; then 128 if ! [[ -f $ADOTDIR/revert-info ]]; then
129 echo 'No revert information available. Cannot revert.' >&2 129 echo 'No revert information available. Cannot revert.' >&2
130 fi 130 fi
131 131
132 cat $ADOTDIR/revert-info | sed '1!p' | while read line; do 132 cat $ADOTDIR/revert-info | sed '1!p' | while read line; do
133 dir="$(echo "$line" | cut -d: -f1)" 133 dir="$(echo "$line" | cut -d: -f1)"
134 git --git-dir="$dir/.git" --work-tree="$dir" \ 134 git --git-dir="$dir/.git" --work-tree="$dir" \
135 checkout "$(echo "$line" | cut -d: -f2)" 2> /dev/null 135 checkout "$(echo "$line" | cut -d: -f2)" 2> /dev/null
136 done 136 done
137 137
138 echo "Reverted to state before running -update on $( 138 echo "Reverted to state before running -update on $(
139 cat $ADOTDIR/revert-info | sed -n 1p)." 139 cat $ADOTDIR/revert-info | sed -n 1p)."
140 } 140 }
141 141
142 -antigen-get-clone-dir () { 142 -antigen-get-clone-dir () {
143 # Takes a repo url and gives out the path that this url needs to be cloned 143 # Takes a repo url and gives out the path that this url needs to be cloned
144 # to. Doesn't actually clone anything. 144 # to. Doesn't actually clone anything.
145 echo -n $ADOTDIR/repos/ 145 echo -n $ADOTDIR/repos/
146 146
147 if [[ "$1" == "https://github.com/sorin-ionescu/prezto.git" ]]; then 147 if [[ "$1" == "https://github.com/sorin-ionescu/prezto.git" ]]; then
148 # Prezto's directory *has* to be `.zprezto`. 148 # Prezto's directory *has* to be `.zprezto`.
149 echo .zprezto 149 echo .zprezto
150 150
151 else 151 else
152 echo "$1" | sed \ 152 echo "$1" | sed \
153 -e 's./.-SLASH-.g' \ 153 -e 's./.-SLASH-.g' \
154 -e 's.:.-COLON-.g' \ 154 -e 's.:.-COLON-.g' \
155 -e 's.|.-PIPE-.g' 155 -e 's.|.-PIPE-.g'
156 156
157 fi 157 fi
158 } 158 }
159 159
160 -antigen-get-clone-url () { 160 -antigen-get-clone-url () {
161 # Takes a repo's clone dir and gives out the repo's original url that was 161 # Takes a repo's clone dir and gives out the repo's original url that was
162 # used to create the given directory path. 162 # used to create the given directory path.
163 163
164 if [[ "$1" == ".zprezto" ]]; then 164 if [[ "$1" == ".zprezto" ]]; then
165 # Prezto's (in `.zprezto`), is assumed to be from `sorin-ionescu`'s 165 # Prezto's (in `.zprezto`), is assumed to be from `sorin-ionescu`'s
166 # remote. 166 # remote.
167 echo https://github.com/sorin-ionescu/prezto.git 167 echo https://github.com/sorin-ionescu/prezto.git
168 168
169 else 169 else
170 echo "$1" | sed \ 170 echo "$1" | sed \
171 -e "s:^$ADOTDIR/repos/::" \ 171 -e "s:^$ADOTDIR/repos/::" \
172 -e 's.-SLASH-./.g' \ 172 -e 's.-SLASH-./.g' \
173 -e 's.-COLON-.:.g' \ 173 -e 's.-COLON-.:.g' \
174 -e 's.-PIPE-.|.g' 174 -e 's.-PIPE-.|.g'
175 175
176 fi 176 fi
177 } 177 }
178 178
179 -antigen-ensure-repo () { 179 -antigen-ensure-repo () {
180 180
181 # Ensure that a clone exists for the given repo url and branch. If the first 181 # Ensure that a clone exists for the given repo url and branch. If the first
182 # argument is `--update` and if a clone already exists for the given repo 182 # argument is `--update` and if a clone already exists for the given repo
183 # and branch, it is pull-ed, i.e., updated. 183 # and branch, it is pull-ed, i.e., updated.
184 184
185 # Argument defaults. 185 # Argument defaults.
186 # The url. No sane default for this, so just empty. 186 # The url. No sane default for this, so just empty.
187 local url= 187 local url=
188 # Check if we have to update. 188 # Check if we have to update.
189 local update=false 189 local update=false
190 # Verbose output. 190 # Verbose output.
191 local verbose=false 191 local verbose=false
192 192
193 eval "$(-antigen-parse-args 'url ; update?, verbose?' "$@")" 193 eval "$(-antigen-parse-args 'url ; update?, verbose?' "$@")"
194 shift $# 194 shift $#
195 195
196 # Get the clone's directory as per the given repo url and branch. 196 # Get the clone's directory as per the given repo url and branch.
197 local clone_dir="$(-antigen-get-clone-dir $url)" 197 local clone_dir="$(-antigen-get-clone-dir $url)"
198 198
199 # A temporary function wrapping the `git` command with repeated arguments. 199 # A temporary function wrapping the `git` command with repeated arguments.
200 --plugin-git () { 200 --plugin-git () {
201 eval git --no-pager \ 201 eval git --no-pager \
202 --git-dir=$clone_dir/.git --work-tree=$clone_dir "$@" 202 --git-dir=$clone_dir/.git --work-tree=$clone_dir "$@"
203 } 203 }
204 204
205 # Clone if it doesn't already exist. 205 # Clone if it doesn't already exist.
206 if [[ ! -d $clone_dir ]]; then 206 if [[ ! -d $clone_dir ]]; then
207 git clone --recursive "${url%|*}" "$clone_dir" 207 git clone --recursive "${url%|*}" "$clone_dir"
208 elif $update; then 208 elif $update; then
209 # Save current revision. 209 # Save current revision.
210 local old_rev="$(--plugin-git rev-parse HEAD)" 210 local old_rev="$(--plugin-git rev-parse HEAD)"
211 # Pull changes if update requested. 211 # Pull changes if update requested.
212 (cd "$clone_dir" && git pull --no-pager) 212 (cd "$clone_dir" && git --no-pager pull)
213 #--plugin-git pull 213 #--plugin-git pull
214 # Update submodules. 214 # Update submodules.
215 (cd "$clone_dir" && git submodule update --recursive) 215 (cd "$clone_dir" && git submodule update --recursive)
216 # Get the new revision. 216 # Get the new revision.
217 local new_rev="$(--plugin-git rev-parse HEAD)" 217 local new_rev="$(--plugin-git rev-parse HEAD)"
218 fi 218 fi
219 219
220 # If its a specific branch that we want, checkout that branch. 220 # If its a specific branch that we want, checkout that branch.
221 if [[ $url == *\|* ]]; then 221 if [[ $url == *\|* ]]; then
222 local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/} 222 local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/}
223 local requested_branch="${url#*|}" 223 local requested_branch="${url#*|}"
224 # Only do the checkout when we are not already on the branch. 224 # Only do the checkout when we are not already on the branch.
225 [[ $requested_branch != $current_branch ]] && 225 [[ $requested_branch != $current_branch ]] &&
226 --plugin-git checkout $requested_branch 226 --plugin-git checkout $requested_branch
227 fi 227 fi
228 228
229 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then 229 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then
230 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}. 230 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}.
231 if $verbose; then 231 if $verbose; then
232 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..' 232 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..'
233 fi 233 fi
234 fi 234 fi
235 235
236 # Remove the temporary git wrapper function. 236 # Remove the temporary git wrapper function.
237 unfunction -- --plugin-git 237 unfunction -- --plugin-git
238 238
239 } 239 }
240 240
241 -antigen-load () { 241 -antigen-load () {
242 242
243 local url="$1" 243 local url="$1"
244 local loc="$2" 244 local loc="$2"
245 local btype="$3" 245 local btype="$3"
246 local make_local_clone="$4" 246 local make_local_clone="$4"
247 247
248 # The full location where the plugin is located. 248 # The full location where the plugin is located.
249 local location 249 local location
250 if $make_local_clone; then 250 if $make_local_clone; then
251 location="$(-antigen-get-clone-dir "$url")/$loc" 251 location="$(-antigen-get-clone-dir "$url")/$loc"
252 else 252 else
253 location="$url" 253 location="$url"
254 fi 254 fi
255 255
256 if [[ $btype == theme ]]; then 256 if [[ $btype == theme ]]; then
257 257
258 # Of course, if its a theme, the location would point to the script 258 # Of course, if its a theme, the location would point to the script
259 # file. 259 # file.
260 source "$location" 260 source "$location"
261 261
262 else 262 else
263 263
264 # Source the plugin script. 264 # Source the plugin script.
265 # FIXME: I don't know. Looks very very ugly. Needs a better 265 # FIXME: I don't know. Looks very very ugly. Needs a better
266 # implementation once tests are ready. 266 # implementation once tests are ready.
267 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 267 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
268 268
269 if [[ -f $location/$script_loc ]]; then 269 if [[ -f $location/$script_loc ]]; then
270 # If we have a `*.plugin.zsh`, source it. 270 # If we have a `*.plugin.zsh`, source it.
271 source "$location/$script_loc" 271 source "$location/$script_loc"
272 272
273 elif [[ -f $location/init.zsh ]]; then 273 elif [[ -f $location/init.zsh ]]; then
274 # If we have a `init.zsh`, source it. 274 # If we have a `init.zsh`, source it.
275 source "$location/init.zsh" 275 source "$location/init.zsh"
276 276
277 elif ls "$location" | grep -qm1 '\.zsh$'; then 277 elif ls "$location" | grep -qm1 '\.zsh$'; then
278 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 278 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
279 # files. 279 # files.
280 for script ($location/*.zsh(N)) source "$script" 280 for script ($location/*.zsh(N)) source "$script"
281 281
282 elif ls "$location" | grep -qm1 '\.sh$'; then 282 elif ls "$location" | grep -qm1 '\.sh$'; then
283 # If there are no `*.zsh` files either, we look for and source any 283 # If there are no `*.zsh` files either, we look for and source any
284 # `*.sh` files instead. 284 # `*.sh` files instead.
285 for script ($location/*.sh(N)) source "$script" 285 for script ($location/*.sh(N)) source "$script"
286 286
287 fi 287 fi
288 288
289 # Add to $fpath, for completion(s). 289 # Add to $fpath, for completion(s).
290 fpath=($location $fpath) 290 fpath=($location $fpath)
291 291
292 fi 292 fi
293 293
294 } 294 }
295 295
296 antigen-cleanup () { 296 antigen-cleanup () {
297 297
298 # Cleanup unused repositories. 298 # Cleanup unused repositories.
299 299
300 local force=false 300 local force=false
301 if [[ $1 == --force ]]; then 301 if [[ $1 == --force ]]; then
302 force=true 302 force=true
303 fi 303 fi
304 304
305 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 305 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
306 echo "You don't have any bundles." 306 echo "You don't have any bundles."
307 return 0 307 return 0
308 fi 308 fi
309 309
310 # Find directores in ADOTDIR/repos, that are not in the bundles record. 310 # Find directores in ADOTDIR/repos, that are not in the bundles record.
311 local unused_clones="$(comm -13 \ 311 local unused_clones="$(comm -13 \
312 <(-antigen-echo-record | 312 <(-antigen-echo-record |
313 awk '$4 == "true" {print $1}' | 313 awk '$4 == "true" {print $1}' |
314 while read line; do 314 while read line; do
315 -antigen-get-clone-dir "$line" 315 -antigen-get-clone-dir "$line"
316 done | 316 done |
317 sort -u) \ 317 sort -u) \
318 <(ls -d "$ADOTDIR/repos/"* | sort -u))" 318 <(ls -d "$ADOTDIR/repos/"* | sort -u))"
319 319
320 if [[ -z $unused_clones ]]; then 320 if [[ -z $unused_clones ]]; then
321 echo "You don't have any unidentified bundles." 321 echo "You don't have any unidentified bundles."
322 return 0 322 return 0
323 fi 323 fi
324 324
325 echo 'You have clones for the following repos, but are not used.' 325 echo 'You have clones for the following repos, but are not used.'
326 echo "$unused_clones" | 326 echo "$unused_clones" |
327 while read line; do 327 while read line; do
328 -antigen-get-clone-url "$line" 328 -antigen-get-clone-url "$line"
329 done | 329 done |
330 sed -e 's/^/ /' -e 's/|/, branch /' 330 sed -e 's/^/ /' -e 's/|/, branch /'
331 331
332 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then 332 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
333 echo 333 echo
334 echo 334 echo
335 echo "$unused_clones" | while read line; do 335 echo "$unused_clones" | while read line; do
336 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." 336 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..."
337 rm -rf "$line" 337 rm -rf "$line"
338 echo ' done.' 338 echo ' done.'
339 done 339 done
340 else 340 else
341 echo 341 echo
342 echo Nothing deleted. 342 echo Nothing deleted.
343 fi 343 fi
344 } 344 }
345 345
346 antigen-lib () { 346 antigen-lib () {
347 antigen-bundle --loc=lib 347 antigen-bundle --loc=lib
348 } 348 }
349 349
350 antigen-prezto-lib () { 350 antigen-prezto-lib () {
351 antigen-bundle sorin-ionescu/prezto 351 antigen-bundle sorin-ionescu/prezto
352 export ZDOTDIR=$ADOTDIR/repos/ 352 export ZDOTDIR=$ADOTDIR/repos/
353 } 353 }
354 354
355 antigen-theme () { 355 antigen-theme () {
356 356
357 if [[ "$1" != */* && "$1" != --* ]]; then 357 if [[ "$1" != */* && "$1" != --* ]]; then
358 # The first argument is just a name of the plugin, to be picked up from 358 # The first argument is just a name of the plugin, to be picked up from
359 # the default repo. 359 # the default repo.
360 local name="${1:-robbyrussell}" 360 local name="${1:-robbyrussell}"
361 antigen-bundle --loc=themes/$name --btype=theme 361 antigen-bundle --loc=themes/$name --btype=theme
362 362
363 else 363 else
364 antigen-bundle "$@" --btype=theme 364 antigen-bundle "$@" --btype=theme
365 365
366 fi 366 fi
367 367
368 } 368 }
369 369
370 antigen-apply () { 370 antigen-apply () {
371 # Initialize completion. 371 # Initialize completion.
372 # TODO: Only load completions if there are any changes to the bundle 372 # TODO: Only load completions if there are any changes to the bundle
373 # repositories. 373 # repositories.
374 compinit -i 374 compinit -i
375 } 375 }
376 376
377 antigen-list () { 377 antigen-list () {
378 # List all currently installed bundles. 378 # List all currently installed bundles.
379 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 379 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
380 echo "You don't have any bundles." >&2 380 echo "You don't have any bundles." >&2
381 return 1 381 return 1
382 else 382 else
383 -antigen-echo-record | sort -u 383 -antigen-echo-record | sort -u
384 fi 384 fi
385 } 385 }
386 386
387 antigen-snapshot () { 387 antigen-snapshot () {
388 388
389 local snapshot_file="${1:-antigen-shapshot}" 389 local snapshot_file="${1:-antigen-shapshot}"
390 390
391 # The snapshot content lines are pairs of repo-url and git version hash, in 391 # The snapshot content lines are pairs of repo-url and git version hash, in
392 # the form: 392 # the form:
393 # <version-hash> <repo-url> 393 # <version-hash> <repo-url>
394 local snapshot_content="$(-antigen-echo-record | 394 local snapshot_content="$(-antigen-echo-record |
395 grep 'true$' | 395 grep 'true$' |
396 sed 's/ .*$//' | 396 sed 's/ .*$//' |
397 sort -u | 397 sort -u |
398 while read url; do 398 while read url; do
399 local dir="$(-antigen-get-clone-dir "$url")" 399 local dir="$(-antigen-get-clone-dir "$url")"
400 local version_hash="$(cd "$dir" && git rev-parse HEAD)" 400 local version_hash="$(cd "$dir" && git rev-parse HEAD)"
401 echo "$version_hash $url" 401 echo "$version_hash $url"
402 done)" 402 done)"
403 403
404 { 404 {
405 # The first line in the snapshot file is for metadata, in the form: 405 # The first line in the snapshot file is for metadata, in the form:
406 # key='value'; key='value'; key='value'; 406 # key='value'; key='value'; key='value';
407 # Where `key`s are valid shell variable names. 407 # Where `key`s are valid shell variable names.
408 408
409 # Snapshot version. Has no relation to antigen version. If the snapshot 409 # Snapshot version. Has no relation to antigen version. If the snapshot
410 # file format changes, this number can be incremented. 410 # file format changes, this number can be incremented.
411 echo -n "version='1';" 411 echo -n "version='1';"
412 412
413 # Snapshot creation date+time. 413 # Snapshot creation date+time.
414 echo -n " created_on='$(date)';" 414 echo -n " created_on='$(date)';"
415 415
416 # Add a checksum with the md5 checksum of all the snapshot lines. 416 # Add a checksum with the md5 checksum of all the snapshot lines.
417 local checksum="$(echo "$snapshot_content" | md5sum)" 417 local checksum="$(echo "$snapshot_content" | md5sum)"
418 echo -n " checksum='${checksum%% *}';" 418 echo -n " checksum='${checksum%% *}';"
419 419
420 # A newline after the metadata and then the snapshot lines. 420 # A newline after the metadata and then the snapshot lines.
421 echo "\n$snapshot_content" 421 echo "\n$snapshot_content"
422 422
423 } > "$snapshot_file" 423 } > "$snapshot_file"
424 424
425 } 425 }
426 426
427 antigen-restore () { 427 antigen-restore () {
428 428
429 if [[ $# == 0 ]]; then 429 if [[ $# == 0 ]]; then
430 echo 'Please provide a snapshot file to restore from.' >&2 430 echo 'Please provide a snapshot file to restore from.' >&2
431 return 1 431 return 1
432 fi 432 fi
433 433
434 local snapshot_file="$1" 434 local snapshot_file="$1"
435 435
436 # TODO: Before doing anything with the snapshot file, verify its checksum. 436 # TODO: Before doing anything with the snapshot file, verify its checksum.
437 # If it fails, notify this to the user and confirm if restore should 437 # If it fails, notify this to the user and confirm if restore should
438 # proceed. 438 # proceed.
439 439
440 echo -n "Restoring from $snapshot_file..." 440 echo -n "Restoring from $snapshot_file..."
441 441
442 sed -n '1!p' "$snapshot_file" | 442 sed -n '1!p' "$snapshot_file" |
443 while read line; do 443 while read line; do
444 444
445 local version_hash="${line%% *}" 445 local version_hash="${line%% *}"
446 local url="${line##* }" 446 local url="${line##* }"
447 local clone_dir="$(-antigen-get-clone-dir "$url")" 447 local clone_dir="$(-antigen-get-clone-dir "$url")"
448 448
449 if [[ ! -d $clone_dir ]]; then 449 if [[ ! -d $clone_dir ]]; then
450 git clone "$url" "$clone_dir" > /dev/null 450 git clone "$url" "$clone_dir" > /dev/null
451 fi 451 fi
452 452
453 (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null 453 (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null
454 454
455 done 455 done
456 456
457 echo ' done.' 457 echo ' done.'
458 echo 'Please open a new shell to get the restored changes.' 458 echo 'Please open a new shell to get the restored changes.'
459 } 459 }
460 460
461 antigen-help () { 461 antigen-help () {
462 cat <<EOF 462 cat <<EOF
463 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 463 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
464 shell scripts and utilities, put up on github. For further details and complete 464 shell scripts and utilities, put up on github. For further details and complete
465 documentation, visit the project's page at 'http://antigen.sharats.me'. 465 documentation, visit the project's page at 'http://antigen.sharats.me'.
466 EOF 466 EOF
467 } 467 }
468 468
469 # A syntax sugar to avoid the `-` when calling antigen commands. With this 469 # A syntax sugar to avoid the `-` when calling antigen commands. With this
470 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 470 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
471 antigen () { 471 antigen () {
472 local cmd="$1" 472 local cmd="$1"
473 shift 473 shift
474 "antigen-$cmd" "$@" 474 "antigen-$cmd" "$@"
475 } 475 }
476 476
477 -antigen-parse-args () { 477 -antigen-parse-args () {
478 # An argument parsing functionality to parse arguments the *antigen* way :). 478 # An argument parsing functionality to parse arguments the *antigen* way :).
479 # Takes one first argument (called spec), which dictates how to parse and 479 # Takes one first argument (called spec), which dictates how to parse and
480 # the rest of the arguments are parsed. Outputs a piece of valid shell code 480 # the rest of the arguments are parsed. Outputs a piece of valid shell code
481 # that can be passed to `eval` inside a function which creates the arguments 481 # that can be passed to `eval` inside a function which creates the arguments
482 # and their values as local variables. Suggested use is to set the defaults 482 # and their values as local variables. Suggested use is to set the defaults
483 # to all arguments first and then eval the output of this function. 483 # to all arguments first and then eval the output of this function.
484 484
485 # Spec: Only long argument supported. No support for parsing short options. 485 # Spec: Only long argument supported. No support for parsing short options.
486 # The spec must have two sections, separated by a `;`. 486 # The spec must have two sections, separated by a `;`.
487 # '<positional-arguments>;<keyword-only-arguments>' 487 # '<positional-arguments>;<keyword-only-arguments>'
488 # Positional arguments are passed as just values, like `command a b`. 488 # Positional arguments are passed as just values, like `command a b`.
489 # Keyword arguments are passed as a `--name=value` pair, like `command 489 # Keyword arguments are passed as a `--name=value` pair, like `command
490 # --arg1=a --arg2=b`. 490 # --arg1=a --arg2=b`.
491 491
492 # Each argument in the spec is separated by a `,`. Each keyword argument can 492 # Each argument in the spec is separated by a `,`. Each keyword argument can
493 # end in a `:` to specifiy that this argument wants a value, otherwise it 493 # end in a `:` to specifiy that this argument wants a value, otherwise it
494 # doesn't take a value. (The value in the output when the keyword argument 494 # doesn't take a value. (The value in the output when the keyword argument
495 # doesn't have a `:` is `true`). 495 # doesn't have a `:` is `true`).
496 496
497 # Arguments in either section can end with a `?` (should come after `:`, if 497 # Arguments in either section can end with a `?` (should come after `:`, if
498 # both are present), means optional. FIXME: Not yet implemented. 498 # both are present), means optional. FIXME: Not yet implemented.
499 499
500 # See the test file, tests/arg-parser.t for (working) examples. 500 # See the test file, tests/arg-parser.t for (working) examples.
501 501
502 local spec="$1" 502 local spec="$1"
503 shift 503 shift
504 504
505 # Sanitize the spec 505 # Sanitize the spec
506 spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')" 506 spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')"
507 507
508 local code='' 508 local code=''
509 509
510 --add-var () { 510 --add-var () {
511 test -z "$code" || code="$code\n" 511 test -z "$code" || code="$code\n"
512 code="${code}local $1='$2'" 512 code="${code}local $1='$2'"
513 } 513 }
514 514
515 local positional_args="$(echo "$spec" | cut -d\; -f1)" 515 local positional_args="$(echo "$spec" | cut -d\; -f1)"
516 local positional_args_count="$(echo $positional_args | 516 local positional_args_count="$(echo $positional_args |
517 awk -F, '{print NF}')" 517 awk -F, '{print NF}')"
518 518
519 # Set spec values based on the positional arguments. 519 # Set spec values based on the positional arguments.
520 local i=1 520 local i=1
521 while ! [[ -z $1 || $1 == --* ]]; do 521 while ! [[ -z $1 || $1 == --* ]]; do
522 522
523 if (( $i > $positional_args_count )); then 523 if (( $i > $positional_args_count )); then
524 echo "Only $positional_args_count positional arguments allowed." >&2 524 echo "Only $positional_args_count positional arguments allowed." >&2
525 echo "Found at least one more: '$1'" >&2 525 echo "Found at least one more: '$1'" >&2
526 return 526 return
527 fi 527 fi
528 528
529 local name_spec="$(echo "$positional_args" | cut -d, -f$i)" 529 local name_spec="$(echo "$positional_args" | cut -d, -f$i)"
530 local name="${${name_spec%\?}%:}" 530 local name="${${name_spec%\?}%:}"
531 local value="$1" 531 local value="$1"
532 532
533 if echo "$code" | grep -qm1 "^local $name="; then 533 if echo "$code" | grep -qm1 "^local $name="; then
534 echo "Argument '$name' repeated with the value '$value'". >&2 534 echo "Argument '$name' repeated with the value '$value'". >&2
535 return 535 return
536 fi 536 fi
537 537
538 --add-var $name "$value" 538 --add-var $name "$value"
539 539
540 shift 540 shift
541 i=$(($i + 1)) 541 i=$(($i + 1))
542 done 542 done
543 543
544 local keyword_args="$( 544 local keyword_args="$(
545 # Positional arguments can double up as keyword arguments too. 545 # Positional arguments can double up as keyword arguments too.
546 echo "$positional_args" | tr , '\n' | 546 echo "$positional_args" | tr , '\n' |
547 while read line; do 547 while read line; do
548 if [[ $line == *\? ]]; then 548 if [[ $line == *\? ]]; then
549 echo "${line%?}:?" 549 echo "${line%?}:?"
550 else 550 else
551 echo "$line:" 551 echo "$line:"
552 fi 552 fi
553 done 553 done
554 554
555 # Specified keyword arguments. 555 # Specified keyword arguments.
556 echo "$spec" | cut -d\; -f2 | tr , '\n' 556 echo "$spec" | cut -d\; -f2 | tr , '\n'
557 )" 557 )"
558 local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')" 558 local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')"
559 559
560 # Set spec values from keyword arguments, if any. The remaining arguments 560 # Set spec values from keyword arguments, if any. The remaining arguments
561 # are all assumed to be keyword arguments. 561 # are all assumed to be keyword arguments.
562 while [[ $1 == --* ]]; do 562 while [[ $1 == --* ]]; do
563 # Remove the `--` at the start. 563 # Remove the `--` at the start.
564 local arg="${1#--}" 564 local arg="${1#--}"
565 565
566 # Get the argument name and value. 566 # Get the argument name and value.
567 if [[ $arg != *=* ]]; then 567 if [[ $arg != *=* ]]; then
568 local name="$arg" 568 local name="$arg"
569 local value='' 569 local value=''
570 else 570 else
571 local name="${arg%\=*}" 571 local name="${arg%\=*}"
572 local value="${arg#*=}" 572 local value="${arg#*=}"
573 fi 573 fi
574 574
575 if echo "$code" | grep -qm1 "^local $name="; then 575 if echo "$code" | grep -qm1 "^local $name="; then
576 echo "Argument '$name' repeated with the value '$value'". >&2 576 echo "Argument '$name' repeated with the value '$value'". >&2
577 return 577 return
578 fi 578 fi
579 579
580 # The specification for this argument, used for validations. 580 # The specification for this argument, used for validations.
581 local arg_line="$(echo "$keyword_args" | grep -m1 "^$name:\??\?")" 581 local arg_line="$(echo "$keyword_args" | grep -m1 "^$name:\??\?")"
582 582
583 # Validate argument and value. 583 # Validate argument and value.
584 if [[ -z $arg_line ]]; then 584 if [[ -z $arg_line ]]; then
585 # This argument is not known to us. 585 # This argument is not known to us.
586 echo "Unknown argument '$name'." >&2 586 echo "Unknown argument '$name'." >&2
587 return 587 return
588 588
589 elif (echo "$arg_line" | grep -qm1 ':') && [[ -z $value ]]; then 589 elif (echo "$arg_line" | grep -qm1 ':') && [[ -z $value ]]; then
590 # This argument needs a value, but is not provided. 590 # This argument needs a value, but is not provided.
591 echo "Required argument for '$name' not provided." >&2 591 echo "Required argument for '$name' not provided." >&2
592 return 592 return
593 593
594 elif (echo "$arg_line" | grep -vqm1 ':') && [[ ! -z $value ]]; then 594 elif (echo "$arg_line" | grep -vqm1 ':') && [[ ! -z $value ]]; then
595 # This argument doesn't need a value, but is provided. 595 # This argument doesn't need a value, but is provided.
596 echo "No argument required for '$name', but provided '$value'." >&2 596 echo "No argument required for '$name', but provided '$value'." >&2
597 return 597 return
598 598
599 fi 599 fi
600 600
601 if [[ -z $value ]]; then 601 if [[ -z $value ]]; then
602 value=true 602 value=true
603 fi 603 fi
604 604
605 --add-var "${name//-/_}" "$value" 605 --add-var "${name//-/_}" "$value"
606 shift 606 shift
607 done 607 done
608 608
609 echo "$code" 609 echo "$code"
610 610
611 unfunction -- --add-var 611 unfunction -- --add-var
612 612
613 } 613 }
614 614
615 # Echo the bundle specs as in the record. The first line is not echoed since it 615 # Echo the bundle specs as in the record. The first line is not echoed since it
616 # is a blank line. 616 # is a blank line.
617 -antigen-echo-record () { 617 -antigen-echo-record () {
618 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 618 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
619 } 619 }
620 620
621 -antigen-env-setup () { 621 -antigen-env-setup () {
622 # Pre-startup initializations. 622 # Pre-startup initializations.
623 -set-default ANTIGEN_DEFAULT_REPO_URL \ 623 -set-default ANTIGEN_DEFAULT_REPO_URL \
624 https://github.com/robbyrussell/oh-my-zsh.git 624 https://github.com/robbyrussell/oh-my-zsh.git
625 -set-default ADOTDIR $HOME/.antigen 625 -set-default ADOTDIR $HOME/.antigen
626 626
627 # Load the compinit module. Required for `compdef` to be defined, which is 627 # Load the compinit module. Required for `compdef` to be defined, which is
628 # used by many plugins to define completions. 628 # used by many plugins to define completions.
629 autoload -U compinit 629 autoload -U compinit
630 compinit -i 630 compinit -i
631 631
632 # Setup antigen's own completion. 632 # Setup antigen's own completion.
633 compdef _antigen antigen 633 compdef _antigen antigen
634 } 634 }
635 635
636 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 636 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
637 # not already set. 637 # not already set.
638 -set-default () { 638 -set-default () {
639 local arg_name="$1" 639 local arg_name="$1"
640 local arg_value="$2" 640 local arg_value="$2"
641 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 641 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
642 } 642 }
643 643
644 # Setup antigen's autocompletion 644 # Setup antigen's autocompletion
645 _antigen () { 645 _antigen () {
646 compadd \ 646 compadd \
647 bundle\ 647 bundle\
648 bundles\ 648 bundles\
649 update\ 649 update\
650 revert\ 650 revert\
651 list\ 651 list\
652 cleanup\ 652 cleanup\
653 lib\ 653 lib\
654 theme\ 654 theme\
655 apply\ 655 apply\
656 help 656 help
657 } 657 }
658 658
659 -antigen-env-setup 659 -antigen-env-setup
660 660