Commit 85236bb7621b301b244f8e9a2d243095e04cdab9

Authored by Shrikant Sharat
1 parent ff5f83bb8f

Removed shebang line and added meta comments. Fix #56.

Showing 1 changed file with 5 additions and 1 deletions Inline Diff

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