Commit fee42de18a1e28df805abbd0cf10b8b5dc26f458

Authored by Shrikant Sharat
1 parent fd547d9586

Add a small antigen-help command.

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