Commit b84ae117e28c8464f652fcbd97432b587f4aca33

Authored by Gregory Hugaerts
1 parent 77c0b3a724

add check for pmodload, fallback to sourcing if it is not defined

Showing 1 changed file with 9 additions and 3 deletions Inline Diff

1 #!/bin/zsh 1 #!/bin/zsh
2 2
3 # Each line in this string has the following entries separated by a space 3 # Each line in this string has the following entries separated by a space
4 # character. 4 # character.
5 # <repo-url>, <plugin-location>, <bundle-type>, <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 --plugin-git pull 212 --plugin-git pull
213 # Update submodules. 213 # Update submodules.
214 pushd 214 pushd
215 cd "$clone_dir" 215 cd "$clone_dir"
216 git submodule update --recursive 216 git submodule update --recursive
217 popd 217 popd
218 # Get the new revision. 218 # Get the new revision.
219 local new_rev="$(--plugin-git rev-parse HEAD)" 219 local new_rev="$(--plugin-git rev-parse HEAD)"
220 fi 220 fi
221 221
222 # If its a specific branch that we want, checkout that branch. 222 # If its a specific branch that we want, checkout that branch.
223 if [[ $url == *\|* ]]; then 223 if [[ $url == *\|* ]]; then
224 local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/} 224 local current_branch=${$(--plugin-git symbolic-ref HEAD)##refs/heads/}
225 local requested_branch="${url#*|}" 225 local requested_branch="${url#*|}"
226 # Only do the checkout when we are not already on the branch. 226 # Only do the checkout when we are not already on the branch.
227 [[ $requested_branch != $current_branch ]] && 227 [[ $requested_branch != $current_branch ]] &&
228 --plugin-git checkout $requested_branch 228 --plugin-git checkout $requested_branch
229 fi 229 fi
230 230
231 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then 231 if ! [[ -z $old_rev || $old_rev == $new_rev ]]; then
232 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}. 232 echo Updated from ${old_rev:0:7} to ${new_rev:0:7}.
233 if $verbose; then 233 if $verbose; then
234 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..' 234 --plugin-git log --oneline --reverse --no-merges --stat '@{1}..'
235 fi 235 fi
236 fi 236 fi
237 237
238 # Remove the temporary git wrapper function. 238 # Remove the temporary git wrapper function.
239 unfunction -- --plugin-git 239 unfunction -- --plugin-git
240 240
241 } 241 }
242 242
243 -antigen-load () { 243 -antigen-load () {
244 244
245 local url="$1" 245 local url="$1"
246 local loc="$2" 246 local loc="$2"
247 local btype="$3" 247 local btype="$3"
248 local make_local_clone="$4" 248 local make_local_clone="$4"
249 249
250 # The full location where the plugin is located. 250 # The full location where the plugin is located.
251 local location 251 local location
252 if $make_local_clone; then 252 if $make_local_clone; then
253 location="$(-antigen-get-clone-dir "$url")/$loc" 253 location="$(-antigen-get-clone-dir "$url")/$loc"
254 else 254 else
255 location="$url" 255 location="$url"
256 fi 256 fi
257 257
258 if [[ $btype == theme ]]; then 258 if [[ $btype == theme ]]; then
259 259
260 # Of course, if its a theme, the location would point to the script 260 # Of course, if its a theme, the location would point to the script
261 # file. 261 # file.
262 source "$location" 262 source "$location"
263 263
264 else 264 else
265 265
266 # Source the plugin script. 266 # Source the plugin script.
267 # FIXME: I don't know. Looks very very ugly. Needs a better 267 # FIXME: I don't know. Looks very very ugly. Needs a better
268 # implementation once tests are ready. 268 # implementation once tests are ready.
269 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 269 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
270 270
271 if [[ -f $location/$script_loc ]]; then 271 if [[ -f $location/$script_loc ]]; then
272 # If we have a `*.plugin.zsh`, source it. 272 # If we have a `*.plugin.zsh`, source it.
273 source "$location/$script_loc" 273 source "$location/$script_loc"
274 274
275 elif [[ -f $location/init.zsh ]]; then 275 elif [[ -f $location/init.zsh ]]; then
276 # If we have a `init.zsh`, pmodload the module. 276 # If we have a `init.zsh`
277 local module="$(echo $loc|sed -e 's/^modules\///')" #remove modules/ from loc to find module name 277 if (( $+functions[pmodload] )); then
278 pmodload "$module" 278 # if pmodload is defined pmodload the module.
279 local module="$(echo $loc|sed -e 's/^modules\///')" #remove modules/ from loc to find module name
280 pmodload "$module"
281 else
282 # otherwise source it.
283 source "$location/init.zsh"
284 fi
279 285
280 elif ls "$location" | grep -qm1 '\.zsh$'; then 286 elif ls "$location" | grep -qm1 '\.zsh$'; then
281 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 287 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
282 # files. 288 # files.
283 for script ($location/*.zsh(N)) source "$script" 289 for script ($location/*.zsh(N)) source "$script"
284 290
285 elif ls "$location" | grep -qm1 '\.sh$'; then 291 elif ls "$location" | grep -qm1 '\.sh$'; then
286 # If there are no `*.zsh` files either, we look for and source any 292 # If there are no `*.zsh` files either, we look for and source any
287 # `*.sh` files instead. 293 # `*.sh` files instead.
288 for script ($location/*.sh(N)) source "$script" 294 for script ($location/*.sh(N)) source "$script"
289 295
290 fi 296 fi
291 297
292 # Add to $fpath, for completion(s). 298 # Add to $fpath, for completion(s).
293 fpath=($location $fpath) 299 fpath=($location $fpath)
294 300
295 fi 301 fi
296 302
297 } 303 }
298 304
299 antigen-cleanup () { 305 antigen-cleanup () {
300 306
301 # Cleanup unused repositories. 307 # Cleanup unused repositories.
302 308
303 local force=false 309 local force=false
304 if [[ $1 == --force ]]; then 310 if [[ $1 == --force ]]; then
305 force=true 311 force=true
306 fi 312 fi
307 313
308 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 314 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
309 echo "You don't have any bundles." 315 echo "You don't have any bundles."
310 return 0 316 return 0
311 fi 317 fi
312 318
313 # Find directores in ADOTDIR/repos, that are not in the bundles record. 319 # Find directores in ADOTDIR/repos, that are not in the bundles record.
314 local unused_clones="$(comm -13 \ 320 local unused_clones="$(comm -13 \
315 <(-antigen-echo-record | 321 <(-antigen-echo-record |
316 awk '$4 == "true" {print $1}' | 322 awk '$4 == "true" {print $1}' |
317 while read line; do 323 while read line; do
318 -antigen-get-clone-dir "$line" 324 -antigen-get-clone-dir "$line"
319 done | 325 done |
320 sort -u) \ 326 sort -u) \
321 <(ls -d "$ADOTDIR/repos/"* | sort -u))" 327 <(ls -d "$ADOTDIR/repos/"* | sort -u))"
322 328
323 if [[ -z $unused_clones ]]; then 329 if [[ -z $unused_clones ]]; then
324 echo "You don't have any unidentified bundles." 330 echo "You don't have any unidentified bundles."
325 return 0 331 return 0
326 fi 332 fi
327 333
328 echo 'You have clones for the following repos, but are not used.' 334 echo 'You have clones for the following repos, but are not used.'
329 echo "$unused_clones" | 335 echo "$unused_clones" |
330 while read line; do 336 while read line; do
331 -antigen-get-clone-url "$line" 337 -antigen-get-clone-url "$line"
332 done | 338 done |
333 sed -e 's/^/ /' -e 's/|/, branch /' 339 sed -e 's/^/ /' -e 's/|/, branch /'
334 340
335 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then 341 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
336 echo 342 echo
337 echo 343 echo
338 echo "$unused_clones" | while read line; do 344 echo "$unused_clones" | while read line; do
339 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..." 345 echo -n "Deleting clone for $(-antigen-get-clone-url "$line")..."
340 rm -rf "$line" 346 rm -rf "$line"
341 echo ' done.' 347 echo ' done.'
342 done 348 done
343 else 349 else
344 echo 350 echo
345 echo Nothing deleted. 351 echo Nothing deleted.
346 fi 352 fi
347 } 353 }
348 354
349 antigen-lib () { 355 antigen-lib () {
350 antigen-bundle --loc=lib 356 antigen-bundle --loc=lib
351 } 357 }
352 358
353 antigen-prezto-lib () { 359 antigen-prezto-lib () {
354 antigen-bundle sorin-ionescu/prezto 360 antigen-bundle sorin-ionescu/prezto
355 export ZDOTDIR=$ADOTDIR/repos/ 361 export ZDOTDIR=$ADOTDIR/repos/
356 } 362 }
357 363
358 antigen-theme () { 364 antigen-theme () {
359 365
360 if [[ "$1" != */* && "$1" != --* ]]; then 366 if [[ "$1" != */* && "$1" != --* ]]; then
361 # The first argument is just a name of the plugin, to be picked up from 367 # The first argument is just a name of the plugin, to be picked up from
362 # the default repo. 368 # the default repo.
363 local name="${1:-robbyrussell}" 369 local name="${1:-robbyrussell}"
364 antigen-bundle --loc=themes/$name --btype=theme 370 antigen-bundle --loc=themes/$name --btype=theme
365 371
366 else 372 else
367 antigen-bundle "$@" --btype=theme 373 antigen-bundle "$@" --btype=theme
368 374
369 fi 375 fi
370 376
371 } 377 }
372 378
373 antigen-apply () { 379 antigen-apply () {
374 # Initialize completion. 380 # Initialize completion.
375 # TODO: Only load completions if there are any changes to the bundle 381 # TODO: Only load completions if there are any changes to the bundle
376 # repositories. 382 # repositories.
377 compinit -i 383 compinit -i
378 } 384 }
379 385
380 antigen-list () { 386 antigen-list () {
381 # List all currently installed bundles. 387 # List all currently installed bundles.
382 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 388 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
383 echo "You don't have any bundles." >&2 389 echo "You don't have any bundles." >&2
384 return 1 390 return 1
385 else 391 else
386 -antigen-echo-record | sort -u 392 -antigen-echo-record | sort -u
387 fi 393 fi
388 } 394 }
389 395
390 antigen-snapshot () { 396 antigen-snapshot () {
391 397
392 local snapshot_file="${1:-antigen-shapshot}" 398 local snapshot_file="${1:-antigen-shapshot}"
393 399
394 # The snapshot content lines are pairs of repo-url and git version hash, in 400 # The snapshot content lines are pairs of repo-url and git version hash, in
395 # the form: 401 # the form:
396 # <version-hash> <repo-url> 402 # <version-hash> <repo-url>
397 local snapshot_content="$(-antigen-echo-record | 403 local snapshot_content="$(-antigen-echo-record |
398 grep 'true$' | 404 grep 'true$' |
399 sed 's/ .*$//' | 405 sed 's/ .*$//' |
400 sort -u | 406 sort -u |
401 while read url; do 407 while read url; do
402 local dir="$(-antigen-get-clone-dir "$url")" 408 local dir="$(-antigen-get-clone-dir "$url")"
403 local version_hash="$(cd "$dir" && git rev-parse HEAD)" 409 local version_hash="$(cd "$dir" && git rev-parse HEAD)"
404 echo "$version_hash $url" 410 echo "$version_hash $url"
405 done)" 411 done)"
406 412
407 { 413 {
408 # The first line in the snapshot file is for metadata, in the form: 414 # The first line in the snapshot file is for metadata, in the form:
409 # key='value'; key='value'; key='value'; 415 # key='value'; key='value'; key='value';
410 # Where `key`s are valid shell variable names. 416 # Where `key`s are valid shell variable names.
411 417
412 # Snapshot version. Has no relation to antigen version. If the snapshot 418 # Snapshot version. Has no relation to antigen version. If the snapshot
413 # file format changes, this number can be incremented. 419 # file format changes, this number can be incremented.
414 echo -n "version='1';" 420 echo -n "version='1';"
415 421
416 # Snapshot creation date+time. 422 # Snapshot creation date+time.
417 echo -n " created_on='$(date)';" 423 echo -n " created_on='$(date)';"
418 424
419 # Add a checksum with the md5 checksum of all the snapshot lines. 425 # Add a checksum with the md5 checksum of all the snapshot lines.
420 local checksum="$(echo "$snapshot_content" | md5sum)" 426 local checksum="$(echo "$snapshot_content" | md5sum)"
421 echo -n " checksum='${checksum%% *}';" 427 echo -n " checksum='${checksum%% *}';"
422 428
423 # A newline after the metadata and then the snapshot lines. 429 # A newline after the metadata and then the snapshot lines.
424 echo "\n$snapshot_content" 430 echo "\n$snapshot_content"
425 431
426 } > "$snapshot_file" 432 } > "$snapshot_file"
427 433
428 } 434 }
429 435
430 antigen-restore () { 436 antigen-restore () {
431 437
432 if [[ $# == 0 ]]; then 438 if [[ $# == 0 ]]; then
433 echo 'Please provide a snapshot file to restore from.' >&2 439 echo 'Please provide a snapshot file to restore from.' >&2
434 return 1 440 return 1
435 fi 441 fi
436 442
437 local snapshot_file="$1" 443 local snapshot_file="$1"
438 444
439 # TODO: Before doing anything with the snapshot file, verify its checksum. 445 # TODO: Before doing anything with the snapshot file, verify its checksum.
440 # If it fails, notify this to the user and confirm if restore should 446 # If it fails, notify this to the user and confirm if restore should
441 # proceed. 447 # proceed.
442 448
443 echo -n "Restoring from $snapshot_file..." 449 echo -n "Restoring from $snapshot_file..."
444 450
445 sed -n '1!p' "$snapshot_file" | 451 sed -n '1!p' "$snapshot_file" |
446 while read line; do 452 while read line; do
447 453
448 local version_hash="${line%% *}" 454 local version_hash="${line%% *}"
449 local url="${line##* }" 455 local url="${line##* }"
450 local clone_dir="$(-antigen-get-clone-dir "$url")" 456 local clone_dir="$(-antigen-get-clone-dir "$url")"
451 457
452 if [[ ! -d $clone_dir ]]; then 458 if [[ ! -d $clone_dir ]]; then
453 git clone "$url" "$clone_dir" > /dev/null 459 git clone "$url" "$clone_dir" > /dev/null
454 fi 460 fi
455 461
456 (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null 462 (cd "$clone_dir" && git checkout $version_hash) 2> /dev/null
457 463
458 done 464 done
459 465
460 echo ' done.' 466 echo ' done.'
461 echo 'Please open a new shell to get the restored changes.' 467 echo 'Please open a new shell to get the restored changes.'
462 } 468 }
463 469
464 antigen-help () { 470 antigen-help () {
465 cat <<EOF 471 cat <<EOF
466 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 472 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
467 shell scripts and utilities, put up on github. For further details and complete 473 shell scripts and utilities, put up on github. For further details and complete
468 documentation, visit the project's page at 'http://antigen.sharats.me'. 474 documentation, visit the project's page at 'http://antigen.sharats.me'.
469 EOF 475 EOF
470 } 476 }
471 477
472 # A syntax sugar to avoid the `-` when calling antigen commands. With this 478 # A syntax sugar to avoid the `-` when calling antigen commands. With this
473 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 479 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
474 antigen () { 480 antigen () {
475 local cmd="$1" 481 local cmd="$1"
476 shift 482 shift
477 "antigen-$cmd" "$@" 483 "antigen-$cmd" "$@"
478 } 484 }
479 485
480 -antigen-parse-args () { 486 -antigen-parse-args () {
481 # An argument parsing functionality to parse arguments the *antigen* way :). 487 # An argument parsing functionality to parse arguments the *antigen* way :).
482 # Takes one first argument (called spec), which dictates how to parse and 488 # Takes one first argument (called spec), which dictates how to parse and
483 # the rest of the arguments are parsed. Outputs a piece of valid shell code 489 # the rest of the arguments are parsed. Outputs a piece of valid shell code
484 # that can be passed to `eval` inside a function which creates the arguments 490 # that can be passed to `eval` inside a function which creates the arguments
485 # and their values as local variables. Suggested use is to set the defaults 491 # and their values as local variables. Suggested use is to set the defaults
486 # to all arguments first and then eval the output of this function. 492 # to all arguments first and then eval the output of this function.
487 493
488 # Spec: Only long argument supported. No support for parsing short options. 494 # Spec: Only long argument supported. No support for parsing short options.
489 # The spec must have two sections, separated by a `;`. 495 # The spec must have two sections, separated by a `;`.
490 # '<positional-arguments>;<keyword-only-arguments>' 496 # '<positional-arguments>;<keyword-only-arguments>'
491 # Positional arguments are passed as just values, like `command a b`. 497 # Positional arguments are passed as just values, like `command a b`.
492 # Keyword arguments are passed as a `--name=value` pair, like `command 498 # Keyword arguments are passed as a `--name=value` pair, like `command
493 # --arg1=a --arg2=b`. 499 # --arg1=a --arg2=b`.
494 500
495 # Each argument in the spec is separated by a `,`. Each keyword argument can 501 # Each argument in the spec is separated by a `,`. Each keyword argument can
496 # end in a `:` to specifiy that this argument wants a value, otherwise it 502 # end in a `:` to specifiy that this argument wants a value, otherwise it
497 # doesn't take a value. (The value in the output when the keyword argument 503 # doesn't take a value. (The value in the output when the keyword argument
498 # doesn't have a `:` is `true`). 504 # doesn't have a `:` is `true`).
499 505
500 # Arguments in either section can end with a `?` (should come after `:`, if 506 # Arguments in either section can end with a `?` (should come after `:`, if
501 # both are present), means optional. FIXME: Not yet implemented. 507 # both are present), means optional. FIXME: Not yet implemented.
502 508
503 # See the test file, tests/arg-parser.t for (working) examples. 509 # See the test file, tests/arg-parser.t for (working) examples.
504 510
505 local spec="$1" 511 local spec="$1"
506 shift 512 shift
507 513
508 # Sanitize the spec 514 # Sanitize the spec
509 spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')" 515 spec="$(echo "$spec" | tr '\n' ' ' | sed 's/[[:space:]]//g')"
510 516
511 local code='' 517 local code=''
512 518
513 --add-var () { 519 --add-var () {
514 test -z "$code" || code="$code\n" 520 test -z "$code" || code="$code\n"
515 code="${code}local $1='$2'" 521 code="${code}local $1='$2'"
516 } 522 }
517 523
518 local positional_args="$(echo "$spec" | cut -d\; -f1)" 524 local positional_args="$(echo "$spec" | cut -d\; -f1)"
519 local positional_args_count="$(echo $positional_args | 525 local positional_args_count="$(echo $positional_args |
520 awk -F, '{print NF}')" 526 awk -F, '{print NF}')"
521 527
522 # Set spec values based on the positional arguments. 528 # Set spec values based on the positional arguments.
523 local i=1 529 local i=1
524 while ! [[ -z $1 || $1 == --* ]]; do 530 while ! [[ -z $1 || $1 == --* ]]; do
525 531
526 if (( $i > $positional_args_count )); then 532 if (( $i > $positional_args_count )); then
527 echo "Only $positional_args_count positional arguments allowed." >&2 533 echo "Only $positional_args_count positional arguments allowed." >&2
528 echo "Found at least one more: '$1'" >&2 534 echo "Found at least one more: '$1'" >&2
529 return 535 return
530 fi 536 fi
531 537
532 local name_spec="$(echo "$positional_args" | cut -d, -f$i)" 538 local name_spec="$(echo "$positional_args" | cut -d, -f$i)"
533 local name="${${name_spec%\?}%:}" 539 local name="${${name_spec%\?}%:}"
534 local value="$1" 540 local value="$1"
535 541
536 if echo "$code" | grep -qm1 "^local $name="; then 542 if echo "$code" | grep -qm1 "^local $name="; then
537 echo "Argument '$name' repeated with the value '$value'". >&2 543 echo "Argument '$name' repeated with the value '$value'". >&2
538 return 544 return
539 fi 545 fi
540 546
541 --add-var $name "$value" 547 --add-var $name "$value"
542 548
543 shift 549 shift
544 i=$(($i + 1)) 550 i=$(($i + 1))
545 done 551 done
546 552
547 local keyword_args="$( 553 local keyword_args="$(
548 # Positional arguments can double up as keyword arguments too. 554 # Positional arguments can double up as keyword arguments too.
549 echo "$positional_args" | tr , '\n' | 555 echo "$positional_args" | tr , '\n' |
550 while read line; do 556 while read line; do
551 if [[ $line == *\? ]]; then 557 if [[ $line == *\? ]]; then
552 echo "${line%?}:?" 558 echo "${line%?}:?"
553 else 559 else
554 echo "$line:" 560 echo "$line:"
555 fi 561 fi
556 done 562 done
557 563
558 # Specified keyword arguments. 564 # Specified keyword arguments.
559 echo "$spec" | cut -d\; -f2 | tr , '\n' 565 echo "$spec" | cut -d\; -f2 | tr , '\n'
560 )" 566 )"
561 local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')" 567 local keyword_args_count="$(echo $keyword_args | awk -F, '{print NF}')"
562 568
563 # Set spec values from keyword arguments, if any. The remaining arguments 569 # Set spec values from keyword arguments, if any. The remaining arguments
564 # are all assumed to be keyword arguments. 570 # are all assumed to be keyword arguments.
565 while [[ $1 == --* ]]; do 571 while [[ $1 == --* ]]; do
566 # Remove the `--` at the start. 572 # Remove the `--` at the start.
567 local arg="${1#--}" 573 local arg="${1#--}"
568 574
569 # Get the argument name and value. 575 # Get the argument name and value.
570 if [[ $arg != *=* ]]; then 576 if [[ $arg != *=* ]]; then
571 local name="$arg" 577 local name="$arg"
572 local value='' 578 local value=''
573 else 579 else
574 local name="${arg%\=*}" 580 local name="${arg%\=*}"
575 local value="${arg#*=}" 581 local value="${arg#*=}"
576 fi 582 fi
577 583
578 if echo "$code" | grep -qm1 "^local $name="; then 584 if echo "$code" | grep -qm1 "^local $name="; then
579 echo "Argument '$name' repeated with the value '$value'". >&2 585 echo "Argument '$name' repeated with the value '$value'". >&2
580 return 586 return
581 fi 587 fi
582 588
583 # The specification for this argument, used for validations. 589 # The specification for this argument, used for validations.
584 local arg_line="$(echo "$keyword_args" | grep -m1 "^$name:\??\?")" 590 local arg_line="$(echo "$keyword_args" | grep -m1 "^$name:\??\?")"
585 591
586 # Validate argument and value. 592 # Validate argument and value.
587 if [[ -z $arg_line ]]; then 593 if [[ -z $arg_line ]]; then
588 # This argument is not known to us. 594 # This argument is not known to us.
589 echo "Unknown argument '$name'." >&2 595 echo "Unknown argument '$name'." >&2
590 return 596 return
591 597
592 elif (echo "$arg_line" | grep -qm1 ':') && [[ -z $value ]]; then 598 elif (echo "$arg_line" | grep -qm1 ':') && [[ -z $value ]]; then
593 # This argument needs a value, but is not provided. 599 # This argument needs a value, but is not provided.
594 echo "Required argument for '$name' not provided." >&2 600 echo "Required argument for '$name' not provided." >&2
595 return 601 return
596 602
597 elif (echo "$arg_line" | grep -vqm1 ':') && [[ ! -z $value ]]; then 603 elif (echo "$arg_line" | grep -vqm1 ':') && [[ ! -z $value ]]; then
598 # This argument doesn't need a value, but is provided. 604 # This argument doesn't need a value, but is provided.
599 echo "No argument required for '$name', but provided '$value'." >&2 605 echo "No argument required for '$name', but provided '$value'." >&2
600 return 606 return
601 607
602 fi 608 fi
603 609
604 if [[ -z $value ]]; then 610 if [[ -z $value ]]; then
605 value=true 611 value=true
606 fi 612 fi
607 613
608 --add-var "${name//-/_}" "$value" 614 --add-var "${name//-/_}" "$value"
609 shift 615 shift
610 done 616 done
611 617
612 echo "$code" 618 echo "$code"
613 619
614 unfunction -- --add-var 620 unfunction -- --add-var
615 621
616 } 622 }
617 623
618 # Echo the bundle specs as in the record. The first line is not echoed since it 624 # Echo the bundle specs as in the record. The first line is not echoed since it
619 # is a blank line. 625 # is a blank line.
620 -antigen-echo-record () { 626 -antigen-echo-record () {
621 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 627 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
622 } 628 }
623 629
624 -antigen-env-setup () { 630 -antigen-env-setup () {
625 # Pre-startup initializations. 631 # Pre-startup initializations.
626 -set-default ANTIGEN_DEFAULT_REPO_URL \ 632 -set-default ANTIGEN_DEFAULT_REPO_URL \
627 https://github.com/robbyrussell/oh-my-zsh.git 633 https://github.com/robbyrussell/oh-my-zsh.git
628 -set-default ADOTDIR $HOME/.antigen 634 -set-default ADOTDIR $HOME/.antigen
629 635
630 # Load the compinit module. Required for `compdef` to be defined, which is 636 # Load the compinit module. Required for `compdef` to be defined, which is
631 # used by many plugins to define completions. 637 # used by many plugins to define completions.
632 autoload -U compinit 638 autoload -U compinit
633 compinit -i 639 compinit -i
634 640
635 # Setup antigen's own completion. 641 # Setup antigen's own completion.
636 compdef _antigen antigen 642 compdef _antigen antigen
637 } 643 }
638 644
639 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 645 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
640 # not already set. 646 # not already set.
641 -set-default () { 647 -set-default () {
642 local arg_name="$1" 648 local arg_name="$1"
643 local arg_value="$2" 649 local arg_value="$2"
644 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 650 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
645 } 651 }
646 652
647 # Setup antigen's autocompletion 653 # Setup antigen's autocompletion
648 _antigen () { 654 _antigen () {
649 compadd \ 655 compadd \
650 bundle\ 656 bundle\
651 bundles\ 657 bundles\
652 update\ 658 update\
653 revert\ 659 revert\
654 list\ 660 list\
655 cleanup\ 661 cleanup\
656 lib\ 662 lib\
657 theme\ 663 theme\
658 apply\ 664 apply\
659 help 665 help
660 } 666 }
661 667
662 -antigen-env-setup 668 -antigen-env-setup
663 669