Commit 882de69123d3ae253604b2ba8229881b0bf24e2c

Authored by Shrikant Sharat
1 parent 4b8d7db1f1

Fix current theme is listed in cleanup.

Current theme and the plugins installed on-spot in current session should not be
listed in the cleanup list. They were because of a problem with updating the
`$bundles` record. This is now fixed.

If the above `if` is directly piped to the below `while`, the contents inside
the `if` construct are run in a new subshell, so changes to the `$bundles`
variable are lost after the `if` construct finishes. So, we need the temporary
`$install_bundles` variable.

Showing 1 changed file with 9 additions and 4 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 # <bundle-name>, <repo-url>, <plugin-location>, <repo-local-clone-dir> 5 # <bundle-name>, <repo-url>, <plugin-location>, <repo-local-clone-dir>
6 # FIXME: Is not kept local by zsh! 6 # FIXME: Is not kept local by zsh!
7 local bundles="" 7 local bundles=""
8 8
9 # Syntaxes 9 # Syntaxes
10 # bundle <url> [<loc>=/] [<name>] 10 # bundle <url> [<loc>=/] [<name>]
11 bundle () { 11 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 name= 16 local name=
17 local load=true 17 local load=true
18 18
19 # Set spec values based on the positional arguments. 19 # Set spec values based on the positional arguments.
20 local position_args='url loc name' 20 local position_args='url loc name'
21 local i=1 21 local i=1
22 while ! [[ -z $1 || $1 == --*=* ]]; do 22 while ! [[ -z $1 || $1 == --*=* ]]; do
23 arg_name="$(echo "$position_args" | cut -d\ -f$i)" 23 arg_name="$(echo "$position_args" | cut -d\ -f$i)"
24 arg_value="$1" 24 arg_value="$1"
25 eval "local $arg_name='$arg_value'" 25 eval "local $arg_name='$arg_value'"
26 shift 26 shift
27 i=$(($i + 1)) 27 i=$(($i + 1))
28 done 28 done
29 29
30 # Check if url is just the plugin name. Super short syntax. 30 # Check if url is just the plugin name. Super short syntax.
31 if [[ "$url" != */* ]]; then 31 if [[ "$url" != */* ]]; then
32 loc="plugins/$url" 32 loc="plugins/$url"
33 url="$ANTIGEN_DEFAULT_REPO_URL" 33 url="$ANTIGEN_DEFAULT_REPO_URL"
34 fi 34 fi
35 35
36 # Set spec values from keyword arguments, if any. The remaining arguments 36 # Set spec values from keyword arguments, if any. The remaining arguments
37 # are all assumed to be keyword arguments. 37 # are all assumed to be keyword arguments.
38 while [[ $1 == --*=* ]]; do 38 while [[ $1 == --*=* ]]; do
39 arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 39 arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
40 arg_value="$(echo "$1" | cut -d= -f2)" 40 arg_value="$(echo "$1" | cut -d= -f2)"
41 eval "local $arg_name='$arg_value'" 41 eval "local $arg_name='$arg_value'"
42 shift 42 shift
43 done 43 done
44 44
45 # Resolve the url. 45 # Resolve the url.
46 if [[ $url != git://* && $url != https://* ]]; then 46 if [[ $url != git://* && $url != https://* ]]; then
47 url="${url%.git}" 47 url="${url%.git}"
48 name="$(basename "$url")" 48 name="$(basename "$url")"
49 url="https://github.com/$url.git" 49 url="https://github.com/$url.git"
50 fi 50 fi
51 51
52 # Plugin's repo will be cloned here. 52 # Plugin's repo will be cloned here.
53 local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \ 53 local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \
54 | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')" 54 | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')"
55 55
56 # Make an intelligent guess about the name of the plugin, if not already 56 # Make an intelligent guess about the name of the plugin, if not already
57 # done or is explicitly specified. 57 # done or is explicitly specified.
58 if [[ -z $name ]]; then 58 if [[ -z $name ]]; then
59 name="$(basename $url/$loc)" 59 name="$(basename $url/$loc)"
60 fi 60 fi
61 61
62 # Add it to the record. 62 # Add it to the record.
63 bundles="$bundles\n$name $url $loc $clone_dir" 63 bundles="$bundles\n$name $url $loc $clone_dir"
64 64
65 # Load it, unless specified otherwise. 65 # Load it, unless specified otherwise.
66 if $load; then 66 if $load; then
67 bundle-load "$name" 67 bundle-load "$name"
68 fi 68 fi
69 } 69 }
70 70
71 bundle-install () { 71 bundle-install () {
72 72
73 if [[ $1 == --update ]]; then 73 if [[ $1 == --update ]]; then
74 local update=true 74 local update=true
75 shift 75 shift
76 else 76 else
77 local update=false 77 local update=false
78 fi 78 fi
79 79
80 mkdir -p "$ANTIGEN_BUNDLE_DIR" 80 mkdir -p "$ANTIGEN_BUNDLE_DIR"
81 81
82 local handled_repos="" 82 local handled_repos=""
83 local install_bundles=""
83 84
84 if [[ $# != 0 ]]; then 85 if [[ $# != 0 ]]; then
85 # Record and install just the given plugin here and now. 86 # Record and install just the given plugin here and now.
86 bundle "$@" 87 bundle "$@"
87 echo "$bundles" | tail -1 88 install_bundles="$(echo "$bundles" | tail -1)"
88
89 else 89 else
90 # Install all the plugins, previously recorded. 90 # Install all the plugins, previously recorded.
91 echo-non-empty "$bundles" 91 install_bundles="$(echo-non-empty "$bundles")"
92 fi
92 93
93 fi | while read spec; do 94 # If the above `if` is directly piped to the below `while`, the contents
95 # inside the `if` construct are run in a new subshell, so changes to the
96 # `$bundles` variable are lost after the `if` construct finishes. So, we
97 # need the temporary `$install_bundles` variable.
98 echo "$install_bundles" | while read spec; do
94 99
95 local name="$(echo "$spec" | awk '{print $1}')" 100 local name="$(echo "$spec" | awk '{print $1}')"
96 local url="$(echo "$spec" | awk '{print $2}')" 101 local url="$(echo "$spec" | awk '{print $2}')"
97 local loc="$(echo "$spec" | awk '{print $3}')" 102 local loc="$(echo "$spec" | awk '{print $3}')"
98 local clone_dir="$(echo "$spec" | awk '{print $4}')" 103 local clone_dir="$(echo "$spec" | awk '{print $4}')"
99 104
100 if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then 105 if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then
101 if [[ ! -d $clone_dir ]]; then 106 if [[ ! -d $clone_dir ]]; then
102 git clone "$url" "$clone_dir" 107 git clone "$url" "$clone_dir"
103 elif $update; then 108 elif $update; then
104 git --git-dir "$clone_dir/.git" pull 109 git --git-dir "$clone_dir/.git" pull
105 fi 110 fi
106 111
107 handled_repos="$handled_repos\n$url" 112 handled_repos="$handled_repos\n$url"
108 fi 113 fi
109 114
110 if [[ $name != *.theme ]]; then 115 if [[ $name != *.theme ]]; then
111 echo Installing $name 116 echo Installing $name
112 local bundle_dest="$ANTIGEN_BUNDLE_DIR/$name" 117 local bundle_dest="$ANTIGEN_BUNDLE_DIR/$name"
113 test -e "$bundle_dest" && rm -rf "$bundle_dest" 118 test -e "$bundle_dest" && rm -rf "$bundle_dest"
114 ln -s "$clone_dir/$loc" "$bundle_dest" 119 ln -s "$clone_dir/$loc" "$bundle_dest"
115 else 120 else
116 mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" 121 mkdir -p "$ANTIGEN_BUNDLE_DIR/$name"
117 cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" 122 cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name"
118 fi 123 fi
119 124
120 bundle-load "$name" 125 bundle-load "$name"
121 126
122 done 127 done
123 128
124 # Initialize completions after installing 129 # Initialize completions after installing
125 bundle-apply 130 bundle-apply
126 131
127 } 132 }
128 133
129 bundle-install! () { 134 bundle-install! () {
130 bundle-install --update 135 bundle-install --update
131 } 136 }
132 137
133 bundle-cleanup () { 138 bundle-cleanup () {
134 139
135 # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record. 140 # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record.
136 local unidentified_bundles="$(comm -13 \ 141 local unidentified_bundles="$(comm -13 \
137 <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \ 142 <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \
138 <(ls -1 "$ANTIGEN_BUNDLE_DIR"))" 143 <(ls -1 "$ANTIGEN_BUNDLE_DIR"))"
139 144
140 if [[ -z $unidentified_bundles ]]; then 145 if [[ -z $unidentified_bundles ]]; then
141 echo "You don't have any unidentified bundles." 146 echo "You don't have any unidentified bundles."
142 return 0 147 return 0
143 fi 148 fi
144 149
145 echo The following bundles are not recorded: 150 echo The following bundles are not recorded:
146 echo "$unidentified_bundles" | sed 's/^/ /' 151 echo "$unidentified_bundles" | sed 's/^/ /'
147 152
148 echo -n '\nDelete them all? [y/N] ' 153 echo -n '\nDelete them all? [y/N] '
149 if read -q; then 154 if read -q; then
150 echo 155 echo
151 echo 156 echo
152 echo "$unidentified_bundles" | while read name; do 157 echo "$unidentified_bundles" | while read name; do
153 echo -n Deleting $name... 158 echo -n Deleting $name...
154 rm -rf "$ANTIGEN_BUNDLE_DIR/$name" 159 rm -rf "$ANTIGEN_BUNDLE_DIR/$name"
155 echo ' done.' 160 echo ' done.'
156 done 161 done
157 else 162 else
158 echo 163 echo
159 echo Nothing deleted. 164 echo Nothing deleted.
160 fi 165 fi
161 } 166 }
162 167
163 bundle-load () { 168 bundle-load () {
164 if [[ $1 == --init ]]; then 169 if [[ $1 == --init ]]; then
165 do_init=true 170 do_init=true
166 shift 171 shift
167 else 172 else
168 do_init=false 173 do_init=false
169 fi 174 fi
170 175
171 name="$1" 176 name="$1"
172 bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" 177 bundle_dir="$ANTIGEN_BUNDLE_DIR/$name"
173 178
174 # Source the plugin script 179 # Source the plugin script
175 script_loc="$bundle_dir/$name.plugin.zsh" 180 script_loc="$bundle_dir/$name.plugin.zsh"
176 if [[ -f $script_loc ]]; then 181 if [[ -f $script_loc ]]; then
177 source "$script_loc" 182 source "$script_loc"
178 fi 183 fi
179 184
180 # If the name of the plugin ends with `.lib`, all the *.zsh files in it are 185 # If the name of the plugin ends with `.lib`, all the *.zsh files in it are
181 # sourced. This is kind of a hack to source the libraries of oh-my-zsh. 186 # sourced. This is kind of a hack to source the libraries of oh-my-zsh.
182 if [[ $name == *.lib ]]; then 187 if [[ $name == *.lib ]]; then
183 # FIXME: This throws an error if no files match the given glob pattern. 188 # FIXME: This throws an error if no files match the given glob pattern.
184 for lib ($bundle_dir/*.zsh) source $lib 189 for lib ($bundle_dir/*.zsh) source $lib
185 fi 190 fi
186 191
187 # If the name ends with `.theme`, it is handled as if it were a zsh-theme 192 # If the name ends with `.theme`, it is handled as if it were a zsh-theme
188 # plugin. 193 # plugin.
189 if [[ $name == *.theme ]]; then 194 if [[ $name == *.theme ]]; then
190 local theme_file="$bundle_dir/${name%.theme}.zsh-theme" 195 local theme_file="$bundle_dir/${name%.theme}.zsh-theme"
191 test -f "$theme_file" && source "$theme_file" 196 test -f "$theme_file" && source "$theme_file"
192 fi 197 fi
193 198
194 # Add to $fpath, for completion(s) 199 # Add to $fpath, for completion(s)
195 fpath=($bundle_dir $fpath) 200 fpath=($bundle_dir $fpath)
196 201
197 if $do_init; then 202 if $do_init; then
198 bundle-init 203 bundle-init
199 fi 204 fi
200 } 205 }
201 206
202 bundle-lib () { 207 bundle-lib () {
203 bundle --name=oh-my-zsh.lib --loc=lib 208 bundle --name=oh-my-zsh.lib --loc=lib
204 } 209 }
205 210
206 bundle-theme () { 211 bundle-theme () {
207 local url="$ANTIGEN_DEFAULT_REPO_URL" 212 local url="$ANTIGEN_DEFAULT_REPO_URL"
208 local name="${1:-robbyrussell}" 213 local name="${1:-robbyrussell}"
209 bundle-install "$url" --name=$name.theme --loc=themes/$name.zsh-theme 214 bundle-install "$url" --name=$name.theme --loc=themes/$name.zsh-theme
210 } 215 }
211 216
212 bundle-apply () { 217 bundle-apply () {
213 # Initialize completion. 218 # Initialize completion.
214 compinit -i 219 compinit -i
215 } 220 }
216 221
217 # Does what it says. 222 # Does what it says.
218 echo-non-empty () { 223 echo-non-empty () {
219 echo "$@" | while read line; do 224 echo "$@" | while read line; do
220 [[ $line != "" ]] && echo $line 225 [[ $line != "" ]] && echo $line
221 done 226 done
222 } 227 }
223 228
224 -bundle-env-setup () { 229 -bundle-env-setup () {
225 # Pre-startup initializations 230 # Pre-startup initializations
226 -set-default ANTIGEN_DEFAULT_REPO_URL \ 231 -set-default ANTIGEN_DEFAULT_REPO_URL \
227 https://github.com/robbyrussell/oh-my-zsh.git 232 https://github.com/robbyrussell/oh-my-zsh.git
228 -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache 233 -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache
229 -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles 234 -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles
230 235
231 # Load the compinit module 236 # Load the compinit module
232 autoload -U compinit 237 autoload -U compinit
233 238
234 # Without the following, `compdef` function is not defined. 239 # Without the following, `compdef` function is not defined.
235 compinit -i 240 compinit -i
236 } 241 }
237 242
238 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 243 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
239 # not already set. 244 # not already set.
240 -set-default () { 245 -set-default () {
241 arg_name="$1" 246 arg_name="$1"
242 arg_value="$2" 247 arg_value="$2"
243 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 248 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
244 } 249 }
245 250
246 -bundle-env-setup 251 -bundle-env-setup