Commit 3aa0d2d8a35e2bb32082d638dd48a052d93e03da

Authored by Shrikant Sharat
1 parent 9666bd499d

Fix duplicate entries in antigen-list's output.

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

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