Commit 280ec448116d001c3f5bd1ee18e286759393437c

Authored by Shrikant Sharat
1 parent d535ba468a

name argument for bundle command is gone.

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