Commit 1f541f429a2f836b2376e20326c6497bf8d151ae

Authored by Shrikant Sharat
1 parent 4098b408dc

Add --force argument to -cleanup.

The force argument makes the `-cleanup` command behave in a non-interactive
fashion, by deleting the unused clones without asking for a confirmation.

Showing 1 changed file with 6 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 if [[ $url != git://* && \ 72 if [[ $url != git://* && \
73 $url != https://* && \ 73 $url != https://* && \
74 $url != /* && \ 74 $url != /* && \
75 $url != git@github.com:*/* 75 $url != git@github.com:*/*
76 ]]; then 76 ]]; then
77 url="${url%.git}" 77 url="${url%.git}"
78 url="https://github.com/$url.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/\.git$//' \ 119 -e 's/\.git$//' \
120 -e 's./.-SLASH-.g' \ 120 -e 's./.-SLASH-.g' \
121 -e 's.:.-COLON-.g' \ 121 -e 's.:.-COLON-.g' \
122 -e 's.|.-PIPE-.g' 122 -e 's.|.-PIPE-.g'
123 } 123 }
124 124
125 -antigen-get-clone-url () { 125 -antigen-get-clone-url () {
126 # 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
127 # used to create the given directory path. 127 # used to create the given directory path.
128 # TODO: Memoize? 128 # TODO: Memoize?
129 echo "$1" | sed \ 129 echo "$1" | sed \
130 -e "s:^$ADOTDIR/repos/::" \ 130 -e "s:^$ADOTDIR/repos/::" \
131 -e 's.-SLASH-./.g' \ 131 -e 's.-SLASH-./.g' \
132 -e 's.-COLON-.:.g' \ 132 -e 's.-COLON-.:.g' \
133 -e 's.-PIPE-.|.g' \ 133 -e 's.-PIPE-.|.g' \
134 | awk '/^\// {print; next} {print $0 ".git"}' 134 | awk '/^\// {print; next} {print $0 ".git"}'
135 } 135 }
136 136
137 -antigen-ensure-repo () { 137 -antigen-ensure-repo () {
138 138
139 # 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
140 # 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
141 # and branch, it is pull-ed, i.e., updated. 141 # and branch, it is pull-ed, i.e., updated.
142 142
143 # Check if we have to update. 143 # Check if we have to update.
144 local update=false 144 local update=false
145 if [[ $1 == --update ]]; then 145 if [[ $1 == --update ]]; then
146 update=true 146 update=true
147 shift 147 shift
148 fi 148 fi
149 149
150 # 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.
151 local url="$1" 151 local url="$1"
152 local clone_dir="$(-antigen-get-clone-dir $url)" 152 local clone_dir="$(-antigen-get-clone-dir $url)"
153 153
154 # Clone if it doesn't already exist. 154 # Clone if it doesn't already exist.
155 if [[ ! -d $clone_dir ]]; then 155 if [[ ! -d $clone_dir ]]; then
156 git clone "${url%|*}" "$clone_dir" 156 git clone "${url%|*}" "$clone_dir"
157 elif $update; then 157 elif $update; then
158 # Pull changes if update requested. 158 # Pull changes if update requested.
159 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull 159 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull
160 fi 160 fi
161 161
162 # If its a specific branch that we want, checkout that branch. 162 # If its a specific branch that we want, checkout that branch.
163 if [[ $url == *\|* ]]; then 163 if [[ $url == *\|* ]]; then
164 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \ 164 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" \
165 checkout "${url#*|}" 165 checkout "${url#*|}"
166 fi 166 fi
167 167
168 } 168 }
169 169
170 -antigen-load () { 170 -antigen-load () {
171 171
172 local url="$1" 172 local url="$1"
173 local loc="$2" 173 local loc="$2"
174 local btype="$3" 174 local btype="$3"
175 175
176 # The full location where the plugin is located. 176 # The full location where the plugin is located.
177 local location="$(-antigen-get-clone-dir "$url")/$loc" 177 local location="$(-antigen-get-clone-dir "$url")/$loc"
178 178
179 if [[ $btype == theme ]]; then 179 if [[ $btype == theme ]]; then
180 180
181 # Of course, if its a theme, the location would point to the script 181 # Of course, if its a theme, the location would point to the script
182 # file. 182 # file.
183 source "$location" 183 source "$location"
184 184
185 else 185 else
186 186
187 # Source the plugin script 187 # Source the plugin script
188 # FIXME: I don't know. Looks very very ugly. Needs a better 188 # FIXME: I don't know. Looks very very ugly. Needs a better
189 # implementation once tests are ready. 189 # implementation once tests are ready.
190 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')" 190 local script_loc="$(ls "$location" | grep -m1 '\.plugin\.zsh$')"
191 191
192 if [[ -f $script_loc ]]; then 192 if [[ -f $script_loc ]]; then
193 # If we have a `*.plugin.zsh`, source it. 193 # If we have a `*.plugin.zsh`, source it.
194 source "$script_loc" 194 source "$script_loc"
195 195
196 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then 196 elif [[ ! -z "$(ls "$location" | grep -m1 '\.zsh$')" ]]; then
197 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 197 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
198 # files. 198 # files.
199 for script ($location/*.zsh(N)) source "$script" 199 for script ($location/*.zsh(N)) source "$script"
200 200
201 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then 201 elif [[ ! -z "$(ls "$location" | grep -m1 '\.sh$')" ]]; then
202 # If there are no `*.zsh` files either, we look for and source any 202 # If there are no `*.zsh` files either, we look for and source any
203 # `*.sh` files instead. 203 # `*.sh` files instead.
204 for script ($location/*.sh(N)) source "$script" 204 for script ($location/*.sh(N)) source "$script"
205 205
206 fi 206 fi
207 207
208 # Add to $fpath, for completion(s). 208 # Add to $fpath, for completion(s).
209 fpath=($location $fpath) 209 fpath=($location $fpath)
210 210
211 fi 211 fi
212 212
213 } 213 }
214 214
215 antigen-cleanup () { 215 antigen-cleanup () {
216 216
217 # Cleanup unused repositories. 217 # Cleanup unused repositories.
218 218
219 local force=false
220 if [[ $1 == --force ]]; then
221 force=true
222 fi
223
219 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 224 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
220 echo "You don't have any bundles." 225 echo "You don't have any bundles."
221 return 0 226 return 0
222 fi 227 fi
223 228
224 # Find directores in ADOTDIR/repos, that are not in the bundles record. 229 # Find directores in ADOTDIR/repos, that are not in the bundles record.
225 local unused_clones="$(comm -13 \ 230 local unused_clones="$(comm -13 \
226 <(-antigen-echo-record | awk '{print $1}' | sort -u) \ 231 <(-antigen-echo-record | awk '{print $1}' | sort -u) \
227 <(ls "$ADOTDIR/repos" \ 232 <(ls "$ADOTDIR/repos" \
228 | while read line; do 233 | while read line; do
229 -antigen-get-clone-url "$line" 234 -antigen-get-clone-url "$line"
230 done \ 235 done \
231 | sort -u))" 236 | sort -u))"
232 237
233 if [[ -z $unused_clones ]]; then 238 if [[ -z $unused_clones ]]; then
234 echo "You don't have any unidentified bundles." 239 echo "You don't have any unidentified bundles."
235 return 0 240 return 0
236 fi 241 fi
237 242
238 echo 'You have clones for the following repos, but are not used.' 243 echo 'You have clones for the following repos, but are not used.'
239 echo "$unused_clones" \ 244 echo "$unused_clones" \
240 | sed -e 's/^/ /' -e 's/|/, branch /' 245 | sed -e 's/^/ /' -e 's/|/, branch /'
241 246
242 echo -n '\nDelete them all? [y/N] ' 247 if $force || (echo -n '\nDelete them all? [y/N] '; read -q); then
243 if read -q; then
244 echo 248 echo
245 echo 249 echo
246 echo "$unused_clones" | while read line; do 250 echo "$unused_clones" | while read line; do
247 echo -n "Deleting clone for $line..." 251 echo -n "Deleting clone for $line..."
248 rm -rf "$(-antigen-get-clone-dir $line)" 252 rm -rf "$(-antigen-get-clone-dir $line)"
249 echo ' done.' 253 echo ' done.'
250 done 254 done
251 else 255 else
252 echo 256 echo
253 echo Nothing deleted. 257 echo Nothing deleted.
254 fi 258 fi
255 } 259 }
256 260
257 antigen-lib () { 261 antigen-lib () {
258 antigen-bundle --loc=lib 262 antigen-bundle --loc=lib
259 } 263 }
260 264
261 antigen-theme () { 265 antigen-theme () {
262 local name="${1:-robbyrussell}" 266 local name="${1:-robbyrussell}"
263 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 267 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
264 } 268 }
265 269
266 antigen-apply () { 270 antigen-apply () {
267 # Initialize completion. 271 # Initialize completion.
268 # 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
269 # repositories. 273 # repositories.
270 compinit -i 274 compinit -i
271 } 275 }
272 276
273 antigen-list () { 277 antigen-list () {
274 # List all currently installed bundles 278 # List all currently installed bundles
275 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 279 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
276 echo "You don't have any bundles." >&2 280 echo "You don't have any bundles." >&2
277 return 1 281 return 1
278 else 282 else
279 -antigen-echo-record | sort -u 283 -antigen-echo-record | sort -u
280 fi 284 fi
281 } 285 }
282 286
283 antigen-help () { 287 antigen-help () {
284 cat <<EOF 288 cat <<EOF
285 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
286 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
287 documentation, visit the project's page at 'http://antigen.sharats.me'. 291 documentation, visit the project's page at 'http://antigen.sharats.me'.
288 EOF 292 EOF
289 } 293 }
290 294
291 # 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
292 # 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.
293 antigen () { 297 antigen () {
294 local cmd="$1" 298 local cmd="$1"
295 shift 299 shift
296 "antigen-$cmd" "$@" 300 "antigen-$cmd" "$@"
297 } 301 }
298 302
299 # 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
300 # is a blank line. 304 # is a blank line.
301 -antigen-echo-record () { 305 -antigen-echo-record () {
302 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 306 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
303 } 307 }
304 308
305 -antigen-env-setup () { 309 -antigen-env-setup () {
306 # Pre-startup initializations 310 # Pre-startup initializations
307 -set-default ANTIGEN_DEFAULT_REPO_URL \ 311 -set-default ANTIGEN_DEFAULT_REPO_URL \
308 https://github.com/robbyrussell/oh-my-zsh.git 312 https://github.com/robbyrussell/oh-my-zsh.git
309 -set-default ADOTDIR $HOME/.antigen 313 -set-default ADOTDIR $HOME/.antigen
310 314
311 # Load the compinit module 315 # Load the compinit module
312 autoload -U compinit 316 autoload -U compinit
313 317
314 # Without the following, `compdef` function is not defined. 318 # Without the following, `compdef` function is not defined.
315 compinit -i 319 compinit -i
316 } 320 }
317 321
318 # 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
319 # not already set. 323 # not already set.
320 -set-default () { 324 -set-default () {
321 local arg_name="$1" 325 local arg_name="$1"
322 local arg_value="$2" 326 local arg_value="$2"
323 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 327 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
324 } 328 }
325 329
326 -antigen-env-setup 330 -antigen-env-setup