Commit 39379a87845eae73deb76c187732563a03c00140

Authored by Shrikant Sharat
1 parent 8a241a1678

Move url resolving to a separate function.

This is done so that url resolving logic can be tested and to make it easier to
extend it to more complex resolving rules in the future.

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