Commit d535ba468a72b9ad19747d09e755fe857c1856bd

Authored by Shrikant Sharat
1 parent 24fbcce0a0

Introduced a new btype argument to bundle command.

The `name` argument will be phased out. Instead, the btype will indicate the
type of a plugin. Currently it can have valuse of plugin and theme.

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