Commit 72fae27c8c3cbd791dca1fd50261fb367e9a28dd

Authored by Shrikant Sharat
1 parent 47c6bacd22

Inputs to `comm` should be sorted.

Showing 1 changed file with 2 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.-SLASH-./.g' \ 135 -e 's.-SLASH-./.g' \
136 -e 's.-COLON-.:.g' \ 136 -e 's.-COLON-.:.g' \
137 -e 's.-PIPE-.|.g' \ 137 -e 's.-PIPE-.|.g' \
138 | awk '/^\// {print; next} {print $0 ".git"}' 138 | awk '/^\// {print; next} {print $0 ".git"}'
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(N)) source "$script" 205 for script ($location/*.zsh(N)) 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(N)) source "$script" 210 for script ($location/*.sh(N)) 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 \ 232 <(-antigen-echo-record \
233 | awk '{print ($4 == "-" ? $1 : $1 "|" $4)}' \ 233 | awk '{print ($4 == "-" ? $1 : $1 "|" $4)}' \
234 | sort -u) \ 234 | sort -u) \
235 <(ls "$ADOTDIR/repos" \ 235 <(ls "$ADOTDIR/repos" \
236 | while read line; do 236 | while read line; do
237 -antigen-get-clone-url "$line" 237 -antigen-get-clone-url "$line"
238 done))" 238 done \
239 | sort -u))"
239 240
240 if [[ -z $unused_clones ]]; then 241 if [[ -z $unused_clones ]]; then
241 echo "You don't have any unidentified bundles." 242 echo "You don't have any unidentified bundles."
242 return 0 243 return 0
243 fi 244 fi
244 245
245 echo 'You have clones for the following repos, but are not used.' 246 echo 'You have clones for the following repos, but are not used.'
246 echo "$unused_clones" \ 247 echo "$unused_clones" \
247 | sed -e 's/^/ /' -e 's/|/, branch /' 248 | sed -e 's/^/ /' -e 's/|/, branch /'
248 249
249 echo -n '\nDelete them all? [y/N] ' 250 echo -n '\nDelete them all? [y/N] '
250 if read -q; then 251 if read -q; then
251 echo 252 echo
252 echo 253 echo
253 echo "$unused_clones" | while read url; do 254 echo "$unused_clones" | while read url; do
254 echo -n "Deleting clone for $url..." 255 echo -n "Deleting clone for $url..."
255 rm -rf "$(-antigen-get-clone-dir $url)" 256 rm -rf "$(-antigen-get-clone-dir $url)"
256 echo ' done.' 257 echo ' done.'
257 done 258 done
258 else 259 else
259 echo 260 echo
260 echo Nothing deleted. 261 echo Nothing deleted.
261 fi 262 fi
262 } 263 }
263 264
264 antigen-lib () { 265 antigen-lib () {
265 antigen-bundle --loc=lib 266 antigen-bundle --loc=lib
266 } 267 }
267 268
268 antigen-theme () { 269 antigen-theme () {
269 local name="${1:-robbyrussell}" 270 local name="${1:-robbyrussell}"
270 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 271 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
271 } 272 }
272 273
273 antigen-apply () { 274 antigen-apply () {
274 # Initialize completion. 275 # Initialize completion.
275 # TODO: Only load completions if there are any changes to the bundle 276 # TODO: Only load completions if there are any changes to the bundle
276 # repositories. 277 # repositories.
277 compinit -i 278 compinit -i
278 } 279 }
279 280
280 antigen-list () { 281 antigen-list () {
281 # List all currently installed bundles 282 # List all currently installed bundles
282 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 283 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
283 echo "You don't have any bundles." >&2 284 echo "You don't have any bundles." >&2
284 return 1 285 return 1
285 else 286 else
286 -antigen-echo-record | sort -u 287 -antigen-echo-record | sort -u
287 fi 288 fi
288 } 289 }
289 290
290 antigen-help () { 291 antigen-help () {
291 cat <<EOF 292 cat <<EOF
292 Antigen is a plugin management system for zsh. It makes it easy to grab awesome 293 Antigen is a plugin management system for zsh. It makes it easy to grab awesome
293 shell scripts and utilities, put up on github. For further details and complete 294 shell scripts and utilities, put up on github. For further details and complete
294 documentation, visit the project's page at 'http://antigen.sharats.me'. 295 documentation, visit the project's page at 'http://antigen.sharats.me'.
295 EOF 296 EOF
296 } 297 }
297 298
298 # A syntax sugar to avoid the `-` when calling antigen commands. With this 299 # A syntax sugar to avoid the `-` when calling antigen commands. With this
299 # function, you can write `antigen-bundle` as `antigen bundle` and so on. 300 # function, you can write `antigen-bundle` as `antigen bundle` and so on.
300 antigen () { 301 antigen () {
301 local cmd="$1" 302 local cmd="$1"
302 shift 303 shift
303 "antigen-$cmd" "$@" 304 "antigen-$cmd" "$@"
304 } 305 }
305 306
306 # Echo the bundle specs as in the record. The first line is not echoed since it 307 # Echo the bundle specs as in the record. The first line is not echoed since it
307 # is a blank line. 308 # is a blank line.
308 -antigen-echo-record () { 309 -antigen-echo-record () {
309 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 310 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
310 } 311 }
311 312
312 -antigen-env-setup () { 313 -antigen-env-setup () {
313 # Pre-startup initializations 314 # Pre-startup initializations
314 -set-default ANTIGEN_DEFAULT_REPO_URL \ 315 -set-default ANTIGEN_DEFAULT_REPO_URL \
315 https://github.com/robbyrussell/oh-my-zsh.git 316 https://github.com/robbyrussell/oh-my-zsh.git
316 -set-default ADOTDIR $HOME/.antigen 317 -set-default ADOTDIR $HOME/.antigen
317 318
318 # Load the compinit module 319 # Load the compinit module
319 autoload -U compinit 320 autoload -U compinit
320 321
321 # Without the following, `compdef` function is not defined. 322 # Without the following, `compdef` function is not defined.
322 compinit -i 323 compinit -i
323 } 324 }
324 325
325 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 326 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
326 # not already set. 327 # not already set.
327 -set-default () { 328 -set-default () {
328 local arg_name="$1" 329 local arg_name="$1"
329 local arg_value="$2" 330 local arg_value="$2"
330 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 331 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
331 } 332 }
332 333
333 -antigen-env-setup 334 -antigen-env-setup
334 335