Commit 1abf1faf6793322438faa46239e17d66cff26d3b

Authored by Shrikant Sharat
1 parent bb140864c4

Plugins can be specified by just the name.

Super short syntax for specifying the plugins. Just give the name to `bundle`
command it will be downloaded from the default antigen repo.

Showing 1 changed file with 6 additions and 0 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.
31 if [[ "$url" != */* ]]; then
32 loc="plugins/$url"
33 url="$ANTIGEN_DEFAULT_REPO_URL"
34 fi
35
30 # Set spec values from keyword arguments, if any. The remaining arguments 36 # Set spec values from keyword arguments, if any. The remaining arguments
31 # are all assumed to be keyword arguments. 37 # are all assumed to be keyword arguments.
32 while [[ $1 == --*=* ]]; do 38 while [[ $1 == --*=* ]]; do
33 arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" 39 arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
34 arg_value="$(echo "$1" | cut -d= -f2)" 40 arg_value="$(echo "$1" | cut -d= -f2)"
35 eval "local $arg_name='$arg_value'" 41 eval "local $arg_name='$arg_value'"
36 shift 42 shift
37 done 43 done
38 44
39 # Resolve the url. 45 # Resolve the url.
40 if [[ $url != git://* && $url != https://* ]]; then 46 if [[ $url != git://* && $url != https://* ]]; then
41 url="${url%.git}" 47 url="${url%.git}"
42 name="$(basename "$url")" 48 name="$(basename "$url")"
43 url="https://github.com/$url.git" 49 url="https://github.com/$url.git"
44 fi 50 fi
45 51
46 # Plugin's repo will be cloned here. 52 # Plugin's repo will be cloned here.
47 local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \ 53 local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \
48 | 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')"
49 55
50 # 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
51 # done or is explicitly specified. 57 # done or is explicitly specified.
52 if [[ -z $name ]]; then 58 if [[ -z $name ]]; then
53 name="$(basename $url/$loc)" 59 name="$(basename $url/$loc)"
54 fi 60 fi
55 61
56 # Add it to the record. 62 # Add it to the record.
57 bundles="$bundles\n$name $url $loc $clone_dir" 63 bundles="$bundles\n$name $url $loc $clone_dir"
58 64
59 # Load it, unless specified otherwise. 65 # Load it, unless specified otherwise.
60 if $load; then 66 if $load; then
61 bundle-load "$name" 67 bundle-load "$name"
62 fi 68 fi
63 } 69 }
64 70
65 bundle-install () { 71 bundle-install () {
66 72
67 if [[ $1 == --update ]]; then 73 if [[ $1 == --update ]]; then
68 local update=true 74 local update=true
69 else 75 else
70 local update=false 76 local update=false
71 fi 77 fi
72 78
73 mkdir -p "$ANTIGEN_BUNDLE_DIR" 79 mkdir -p "$ANTIGEN_BUNDLE_DIR"
74 80
75 local handled_repos="" 81 local handled_repos=""
76 82
77 echo-non-empty "$bundles" | while read spec; do 83 echo-non-empty "$bundles" | while read spec; do
78 echo "-> $spec" 84 echo "-> $spec"
79 85
80 local name="$(echo "$spec" | awk '{print $1}')" 86 local name="$(echo "$spec" | awk '{print $1}')"
81 local url="$(echo "$spec" | awk '{print $2}')" 87 local url="$(echo "$spec" | awk '{print $2}')"
82 local loc="$(echo "$spec" | awk '{print $3}')" 88 local loc="$(echo "$spec" | awk '{print $3}')"
83 local clone_dir="$(echo "$spec" | awk '{print $4}')" 89 local clone_dir="$(echo "$spec" | awk '{print $4}')"
84 90
85 if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then 91 if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then
86 if [[ ! -d $clone_dir ]]; then 92 if [[ ! -d $clone_dir ]]; then
87 git clone "$url" "$clone_dir" 93 git clone "$url" "$clone_dir"
88 elif $update; then 94 elif $update; then
89 git --git-dir "$clone_dir/.git" pull 95 git --git-dir "$clone_dir/.git" pull
90 fi 96 fi
91 97
92 handled_repos="$handled_repos\n$url" 98 handled_repos="$handled_repos\n$url"
93 fi 99 fi
94 100
95 if [[ $name != *.theme ]]; then 101 if [[ $name != *.theme ]]; then
96 echo Installing $name 102 echo Installing $name
97 mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" 103 mkdir -p "$ANTIGEN_BUNDLE_DIR/$name"
98 pushd "$clone_dir/$loc" > /dev/null 104 pushd "$clone_dir/$loc" > /dev/null
99 ls | grep -Fv '.git' \ 105 ls | grep -Fv '.git' \
100 | xargs cp -rt "$ANTIGEN_BUNDLE_DIR/$name" 106 | xargs cp -rt "$ANTIGEN_BUNDLE_DIR/$name"
101 popd > /dev/null 107 popd > /dev/null
102 else 108 else
103 mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" 109 mkdir -p "$ANTIGEN_BUNDLE_DIR/$name"
104 cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" 110 cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name"
105 fi 111 fi
106 112
107 bundle-load "$name" 113 bundle-load "$name"
108 114
109 done 115 done
110 116
111 } 117 }
112 118
113 bundle-install! () { 119 bundle-install! () {
114 bundle-install --update 120 bundle-install --update
115 } 121 }
116 122
117 bundle-cleanup () { 123 bundle-cleanup () {
118 124
119 # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record. 125 # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record.
120 local unidentified_bundles="$(comm -13 \ 126 local unidentified_bundles="$(comm -13 \
121 <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \ 127 <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \
122 <(ls -1 "$ANTIGEN_BUNDLE_DIR"))" 128 <(ls -1 "$ANTIGEN_BUNDLE_DIR"))"
123 129
124 if [[ -z $unidentified_bundles ]]; then 130 if [[ -z $unidentified_bundles ]]; then
125 echo "You don't have any unidentified bundles." 131 echo "You don't have any unidentified bundles."
126 return 0 132 return 0
127 fi 133 fi
128 134
129 echo The following bundles are not recorded: 135 echo The following bundles are not recorded:
130 echo "$unidentified_bundles" | sed 's/^/ /' 136 echo "$unidentified_bundles" | sed 's/^/ /'
131 137
132 echo -n '\nDelete them all? [y/N] ' 138 echo -n '\nDelete them all? [y/N] '
133 if read -q; then 139 if read -q; then
134 echo 140 echo
135 echo 141 echo
136 echo "$unidentified_bundles" | while read name; do 142 echo "$unidentified_bundles" | while read name; do
137 echo -n Deleting $name... 143 echo -n Deleting $name...
138 rm -rf "$ANTIGEN_BUNDLE_DIR/$name" 144 rm -rf "$ANTIGEN_BUNDLE_DIR/$name"
139 echo ' done.' 145 echo ' done.'
140 done 146 done
141 else 147 else
142 echo 148 echo
143 echo Nothing deleted. 149 echo Nothing deleted.
144 fi 150 fi
145 } 151 }
146 152
147 bundle-load () { 153 bundle-load () {
148 if [[ $1 == --init ]]; then 154 if [[ $1 == --init ]]; then
149 do_init=true 155 do_init=true
150 shift 156 shift
151 else 157 else
152 do_init=false 158 do_init=false
153 fi 159 fi
154 160
155 name="$1" 161 name="$1"
156 bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" 162 bundle_dir="$ANTIGEN_BUNDLE_DIR/$name"
157 163
158 # Source the plugin script 164 # Source the plugin script
159 script_loc="$bundle_dir/$name.plugin.zsh" 165 script_loc="$bundle_dir/$name.plugin.zsh"
160 if [[ -f $script_loc ]]; then 166 if [[ -f $script_loc ]]; then
161 source "$script_loc" 167 source "$script_loc"
162 fi 168 fi
163 169
164 # If the name of the plugin ends with `.lib`, all the *.zsh files in it are 170 # If the name of the plugin ends with `.lib`, all the *.zsh files in it are
165 # sourced. This is kind of a hack to source the libraries of oh-my-zsh. 171 # sourced. This is kind of a hack to source the libraries of oh-my-zsh.
166 if [[ $name == *.lib ]]; then 172 if [[ $name == *.lib ]]; then
167 # FIXME: This throws an error if no files match the given glob pattern. 173 # FIXME: This throws an error if no files match the given glob pattern.
168 for lib ($bundle_dir/*.zsh) source $lib 174 for lib ($bundle_dir/*.zsh) source $lib
169 fi 175 fi
170 176
171 # If the name ends with `.theme`, it is handled as if it were a zsh-theme 177 # If the name ends with `.theme`, it is handled as if it were a zsh-theme
172 # plugin. 178 # plugin.
173 if [[ $name == *.theme ]]; then 179 if [[ $name == *.theme ]]; then
174 source "$bundle_dir/${name%.theme}.zsh-theme" 180 source "$bundle_dir/${name%.theme}.zsh-theme"
175 fi 181 fi
176 182
177 # Add to $fpath, if it provides completion 183 # Add to $fpath, if it provides completion
178 if [[ -f "$bundle_dir/_$name" ]]; then 184 if [[ -f "$bundle_dir/_$name" ]]; then
179 fpath=($bundle_dir $fpath) 185 fpath=($bundle_dir $fpath)
180 fi 186 fi
181 187
182 if $do_init; then 188 if $do_init; then
183 bundle-init 189 bundle-init
184 fi 190 fi
185 } 191 }
186 192
187 bundle-lib () { 193 bundle-lib () {
188 bundle --name=oh-my-zsh.lib --loc=lib 194 bundle --name=oh-my-zsh.lib --loc=lib
189 } 195 }
190 196
191 bundle-theme () { 197 bundle-theme () {
192 local url="$ANTIGEN_DEFAULT_REPO_URL" 198 local url="$ANTIGEN_DEFAULT_REPO_URL"
193 local name="${1:-robbyrussell}" 199 local name="${1:-robbyrussell}"
194 bundle "$url" --name=$name.theme --loc=themes/$name.zsh-theme 200 bundle "$url" --name=$name.theme --loc=themes/$name.zsh-theme
195 } 201 }
196 202
197 bundle-init () { 203 bundle-init () {
198 # Initialize completion. 204 # Initialize completion.
199 # FIXME: Ensure this runs only once. 205 # FIXME: Ensure this runs only once.
200 autoload -U compinit 206 autoload -U compinit
201 compinit -i 207 compinit -i
202 } 208 }
203 209
204 # Does what it says. 210 # Does what it says.
205 echo-non-empty () { 211 echo-non-empty () {
206 echo "$@" | while read line; do 212 echo "$@" | while read line; do
207 [[ $line != "" ]] && echo $line 213 [[ $line != "" ]] && echo $line
208 done 214 done
209 } 215 }
210 216
211 -bundle-env-setup () { 217 -bundle-env-setup () {
212 -set-default ANTIGEN_DEFAULT_REPO_URL \ 218 -set-default ANTIGEN_DEFAULT_REPO_URL \
213 https://github.com/robbyrussell/oh-my-zsh.git 219 https://github.com/robbyrussell/oh-my-zsh.git
214 -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache 220 -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache
215 -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles 221 -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles
216 } 222 }
217 223
218 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 224 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
219 # not already set. 225 # not already set.
220 -set-default () { 226 -set-default () {
221 arg_name="$1" 227 arg_name="$1"
222 arg_value="$2" 228 arg_value="$2"
223 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 229 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
224 } 230 }
225 231
226 -bundle-env-setup 232 -bundle-env-setup
227 bundle-init 233 bundle-init
228 234