Commit 499c0dd5dc947ec7cfe1e94e5096b6cd6951beb1

Authored by Shrikant Sharat
1 parent 3e16c47090

Added some comments.

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