Commit 1cae4fc91f098d37437a3e1dd6384df5b83911fd

Authored by Shrikant Sharat
1 parent a5e21a6a10

Some refactoring and more tests to url resolver.

Showing 2 changed files with 11 additions and 2 deletions Inline Diff

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