Commit 2e6d0c4163963af391b33946aa967b6e08c47fa4

Authored by Shrikant Sharat
1 parent 015b9e5e9c

Fix #5. Ignore spaces in lines given to `-bundles`.

When the input to `antigen-bundles` contains lines that are completely made up
of white space, the command fails to ignore that line. This is now fixed.

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 antigen-bundle () { 11 antigen-bundle () {
12 12
13 # Bundle spec arguments' default values. 13 # Bundle spec arguments' default values.
14 local url="$ANTIGEN_DEFAULT_REPO_URL" 14 local url="$ANTIGEN_DEFAULT_REPO_URL"
15 local loc=/ 15 local loc=/
16 local btype=plugin 16 local btype=plugin
17 17
18 # Set spec values based on the positional arguments. 18 # Set spec values based on the positional arguments.
19 local position_args='url loc' 19 local position_args='url loc'
20 local i=1 20 local i=1
21 while ! [[ -z $1 || $1 == --*=* ]]; do 21 while ! [[ -z $1 || $1 == --*=* ]]; do
22 local arg_name="$(echo "$position_args" | cut -d\ -f$i)" 22 local arg_name="$(echo "$position_args" | cut -d\ -f$i)"
23 local arg_value="$1" 23 local arg_value="$1"
24 eval "local $arg_name='$arg_value'" 24 eval "local $arg_name='$arg_value'"
25 shift 25 shift
26 i=$(($i + 1)) 26 i=$(($i + 1))
27 done 27 done
28 28
29 # Check if url is just the plugin name. Super short syntax. 29 # Check if url is just the plugin name. Super short syntax.
30 if [[ "$url" != */* ]]; then 30 if [[ "$url" != */* ]]; then
31 loc="plugins/$url" 31 loc="plugins/$url"
32 url="$ANTIGEN_DEFAULT_REPO_URL" 32 url="$ANTIGEN_DEFAULT_REPO_URL"
33 fi 33 fi
34 34
35 # Set spec values from keyword arguments, if any. The remaining arguments 35 # Set spec values from keyword arguments, if any. The remaining arguments
36 # are all assumed to be keyword arguments. 36 # are all assumed to be keyword arguments.
37 while [[ $1 == --*=* ]]; do 37 while [[ $1 == --*=* ]]; do
38 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 38 local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
39 local arg_value="$(echo "$1" | cut -d= -f2)" 39 local arg_value="$(echo "$1" | cut -d= -f2)"
40 eval "local $arg_name='$arg_value'" 40 eval "local $arg_name='$arg_value'"
41 shift 41 shift
42 done 42 done
43 43
44 # Resolve the url. 44 # Resolve the url.
45 if [[ $url != git://* && $url != https://* && $url != /* ]]; then 45 if [[ $url != git://* && $url != https://* && $url != /* ]]; then
46 url="${url%.git}" 46 url="${url%.git}"
47 url="https://github.com/$url.git" 47 url="https://github.com/$url.git"
48 fi 48 fi
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" 51 _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype"
52 52
53 -antigen-ensure-repo "$url" 53 -antigen-ensure-repo "$url"
54 54
55 -antigen-load "$url" "$loc" "$btype" 55 -antigen-load "$url" "$loc" "$btype"
56 56
57 } 57 }
58 58
59 antigen-bundles () { 59 antigen-bundles () {
60 # Bulk add many bundles at one go. Empty lines and lines starting with a `#` 60 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
61 # are ignored. Everything else is given to `antigen-bundle` as is, no 61 # are ignored. Everything else is given to `antigen-bundle` as is, no
62 # quoting rules applied. 62 # quoting rules applied.
63 63
64 local line 64 local line
65 65
66 grep -v '^$\|^#' | while read line; do 66 grep -v '^\s*$\|^#' | while read line; do
67 # Using `eval` so that we can use the shell-style quoting in each line 67 # Using `eval` so that we can use the shell-style quoting in each line
68 # piped to `antigen-bundles`. 68 # piped to `antigen-bundles`.
69 eval "antigen-bundle $line" 69 eval "antigen-bundle $line"
70 done 70 done
71 } 71 }
72 72
73 antigen-update () { 73 antigen-update () {
74 # Update your bundles, i.e., `git pull` in all the plugin repos. 74 # Update your bundles, i.e., `git pull` in all the plugin repos.
75 -antigen-echo-record | awk '{print $1}' | sort -u | while read url; do 75 -antigen-echo-record | awk '{print $1}' | sort -u | while read url; do
76 -antigen-ensure-repo --update "$url" 76 -antigen-ensure-repo --update "$url"
77 done 77 done
78 } 78 }
79 79
80 -antigen-get-clone-dir () { 80 -antigen-get-clone-dir () {
81 # Takes a repo url and gives out the path that this url needs to be cloned 81 # Takes a repo url and gives out the path that this url needs to be cloned
82 # to. Doesn't actually clone anything. 82 # to. Doesn't actually clone anything.
83 # TODO: Memoize? 83 # TODO: Memoize?
84 echo -n $ADOTDIR/repos/ 84 echo -n $ADOTDIR/repos/
85 echo "$1" | sed \ 85 echo "$1" | sed \
86 -e 's/\.git$//' \ 86 -e 's/\.git$//' \
87 -e 's./.-SLASH-.g' \ 87 -e 's./.-SLASH-.g' \
88 -e 's.:.-COLON-.g' 88 -e 's.:.-COLON-.g'
89 } 89 }
90 90
91 -antigen-get-clone-url () { 91 -antigen-get-clone-url () {
92 # Takes a repo's clone dir and gives out the repo's original url that was 92 # Takes a repo's clone dir and gives out the repo's original url that was
93 # used to create the given directory path. 93 # used to create the given directory path.
94 # TODO: Memoize? 94 # TODO: Memoize?
95 echo "$1" | sed \ 95 echo "$1" | sed \
96 -e "s:^$ADOTDIR/repos/::" \ 96 -e "s:^$ADOTDIR/repos/::" \
97 -e 's/$/.git/' \ 97 -e 's/$/.git/' \
98 -e 's.-SLASH-./.g' \ 98 -e 's.-SLASH-./.g' \
99 -e 's.-COLON-.:.g' 99 -e 's.-COLON-.:.g'
100 } 100 }
101 101
102 -antigen-ensure-repo () { 102 -antigen-ensure-repo () {
103 103
104 local update=false 104 local update=false
105 if [[ $1 == --update ]]; then 105 if [[ $1 == --update ]]; then
106 update=true 106 update=true
107 shift 107 shift
108 fi 108 fi
109 109
110 local url="$1" 110 local url="$1"
111 local clone_dir="$(-antigen-get-clone-dir $url)" 111 local clone_dir="$(-antigen-get-clone-dir $url)"
112 112
113 if [[ ! -d $clone_dir ]]; then 113 if [[ ! -d $clone_dir ]]; then
114 git clone "$url" "$clone_dir" 114 git clone "$url" "$clone_dir"
115 elif $update; then 115 elif $update; then
116 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull 116 git --git-dir "$clone_dir/.git" --work-tree "$clone_dir" pull
117 fi 117 fi
118 118
119 } 119 }
120 120
121 -antigen-load () { 121 -antigen-load () {
122 122
123 local url="$1" 123 local url="$1"
124 local location="$(-antigen-get-clone-dir "$url")/$2" 124 local location="$(-antigen-get-clone-dir "$url")/$2"
125 local btype="$3" 125 local btype="$3"
126 126
127 if [[ $btype == theme ]]; then 127 if [[ $btype == theme ]]; then
128 128
129 # Of course, if its a theme, the location would point to the script 129 # Of course, if its a theme, the location would point to the script
130 # file. 130 # file.
131 source "$location" 131 source "$location"
132 132
133 else 133 else
134 134
135 # Source the plugin script 135 # Source the plugin script
136 # FIXME: I don't know. Looks very very ugly. Needs a better 136 # FIXME: I don't know. Looks very very ugly. Needs a better
137 # implementation once tests are ready. 137 # implementation once tests are ready.
138 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" 138 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')"
139 if [[ -f $script_loc ]]; then 139 if [[ -f $script_loc ]]; then
140 # If we have a `*.plugin.zsh`, source it. 140 # If we have a `*.plugin.zsh`, source it.
141 source "$script_loc" 141 source "$script_loc"
142 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then 142 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then
143 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 143 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
144 # files. 144 # files.
145 for script ($location/*.zsh) source "$script" 145 for script ($location/*.zsh) source "$script"
146 elif [[ ! -z "$(ls "$location" | grep -m1 '.sh$')" ]]; then 146 elif [[ ! -z "$(ls "$location" | grep -m1 '.sh$')" ]]; then
147 # If there are no `*.zsh` files either, we look for and source any 147 # If there are no `*.zsh` files either, we look for and source any
148 # `*.sh` files instead. 148 # `*.sh` files instead.
149 for script ($location/*.sh) source "$script" 149 for script ($location/*.sh) source "$script"
150 fi 150 fi
151 151
152 # Add to $fpath, for completion(s) 152 # Add to $fpath, for completion(s)
153 fpath=($location $fpath) 153 fpath=($location $fpath)
154 154
155 fi 155 fi
156 156
157 } 157 }
158 158
159 antigen-cleanup () { 159 antigen-cleanup () {
160 160
161 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 161 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
162 echo "You don't have any bundles." 162 echo "You don't have any bundles."
163 return 0 163 return 0
164 fi 164 fi
165 165
166 # Find directores in ADOTDIR/repos, that are not in the bundles record. 166 # Find directores in ADOTDIR/repos, that are not in the bundles record.
167 local unused_clones="$(comm -13 \ 167 local unused_clones="$(comm -13 \
168 <(-antigen-echo-record | awk '{print $1}' | sort -u) \ 168 <(-antigen-echo-record | awk '{print $1}' | sort -u) \
169 <(ls "$ADOTDIR/repos" | while read line; do 169 <(ls "$ADOTDIR/repos" | while read line; do
170 -antigen-get-clone-url "$line" 170 -antigen-get-clone-url "$line"
171 done))" 171 done))"
172 172
173 if [[ -z $unused_clones ]]; then 173 if [[ -z $unused_clones ]]; then
174 echo "You don't have any unidentified bundles." 174 echo "You don't have any unidentified bundles."
175 return 0 175 return 0
176 fi 176 fi
177 177
178 echo 'You have clones for the following repos, but are not used.' 178 echo 'You have clones for the following repos, but are not used.'
179 echo "$unused_clones" | sed 's/^/ /' 179 echo "$unused_clones" | sed 's/^/ /'
180 180
181 echo -n '\nDelete them all? [y/N] ' 181 echo -n '\nDelete them all? [y/N] '
182 if read -q; then 182 if read -q; then
183 echo 183 echo
184 echo 184 echo
185 echo "$unused_clones" | while read url; do 185 echo "$unused_clones" | while read url; do
186 echo -n "Deleting clone for $url..." 186 echo -n "Deleting clone for $url..."
187 rm -rf "$(-antigen-get-clone-dir $url)" 187 rm -rf "$(-antigen-get-clone-dir $url)"
188 echo ' done.' 188 echo ' done.'
189 done 189 done
190 else 190 else
191 echo 191 echo
192 echo Nothing deleted. 192 echo Nothing deleted.
193 fi 193 fi
194 } 194 }
195 195
196 antigen-lib () { 196 antigen-lib () {
197 antigen-bundle --loc=lib 197 antigen-bundle --loc=lib
198 } 198 }
199 199
200 antigen-theme () { 200 antigen-theme () {
201 local name="${1:-robbyrussell}" 201 local name="${1:-robbyrussell}"
202 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 202 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
203 } 203 }
204 204
205 antigen-apply () { 205 antigen-apply () {
206 # Initialize completion. 206 # Initialize completion.
207 # TODO: Only load completions if there are any changes to the bundle 207 # TODO: Only load completions if there are any changes to the bundle
208 # repositories. 208 # repositories.
209 compinit -i 209 compinit -i
210 } 210 }
211 211
212 antigen-list () { 212 antigen-list () {
213 # List all currently installed bundles 213 # List all currently installed bundles
214 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 214 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
215 echo "You don't have any bundles." >&2 215 echo "You don't have any bundles." >&2
216 return 1 216 return 1
217 else 217 else
218 -antigen-echo-record 218 -antigen-echo-record
219 fi 219 fi
220 } 220 }
221 221
222 # Echo the bundle specs as in the record. The first line is not echoed since it 222 # Echo the bundle specs as in the record. The first line is not echoed since it
223 # is a blank line. 223 # is a blank line.
224 -antigen-echo-record () { 224 -antigen-echo-record () {
225 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 225 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
226 } 226 }
227 227
228 -antigen-env-setup () { 228 -antigen-env-setup () {
229 # Pre-startup initializations 229 # Pre-startup initializations
230 -set-default ANTIGEN_DEFAULT_REPO_URL \ 230 -set-default ANTIGEN_DEFAULT_REPO_URL \
231 https://github.com/robbyrussell/oh-my-zsh.git 231 https://github.com/robbyrussell/oh-my-zsh.git
232 -set-default ADOTDIR $HOME/.antigen 232 -set-default ADOTDIR $HOME/.antigen
233 233
234 # Load the compinit module 234 # Load the compinit module
235 autoload -U compinit 235 autoload -U compinit
236 236
237 # Without the following, `compdef` function is not defined. 237 # Without the following, `compdef` function is not defined.
238 compinit -i 238 compinit -i
239 } 239 }
240 240
241 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 241 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
242 # not already set. 242 # not already set.
243 -set-default () { 243 -set-default () {
244 local arg_name="$1" 244 local arg_name="$1"
245 local arg_value="$2" 245 local arg_value="$2"
246 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 246 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
247 } 247 }
248 248
249 -antigen-env-setup 249 -antigen-env-setup
250 250