Commit 64df9931b5533c598f507bb01f6caef21fa4fb17
1 parent
3047bfeb73
Don't leave the --update argument after processing.
Showing 1 changed file with 1 additions and 0 deletions Inline Diff
antigen.zsh
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 | else | 76 | else |
76 | local update=false | 77 | local update=false |
77 | fi | 78 | fi |
78 | 79 | ||
79 | mkdir -p "$ANTIGEN_BUNDLE_DIR" | 80 | mkdir -p "$ANTIGEN_BUNDLE_DIR" |
80 | 81 | ||
81 | local handled_repos="" | 82 | local handled_repos="" |
82 | 83 | ||
83 | echo-non-empty "$bundles" | while read spec; do | 84 | echo-non-empty "$bundles" | while read spec; do |
84 | echo "-> $spec" | 85 | echo "-> $spec" |
85 | 86 | ||
86 | local name="$(echo "$spec" | awk '{print $1}')" | 87 | local name="$(echo "$spec" | awk '{print $1}')" |
87 | local url="$(echo "$spec" | awk '{print $2}')" | 88 | local url="$(echo "$spec" | awk '{print $2}')" |
88 | local loc="$(echo "$spec" | awk '{print $3}')" | 89 | local loc="$(echo "$spec" | awk '{print $3}')" |
89 | local clone_dir="$(echo "$spec" | awk '{print $4}')" | 90 | local clone_dir="$(echo "$spec" | awk '{print $4}')" |
90 | 91 | ||
91 | if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then | 92 | if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then |
92 | if [[ ! -d $clone_dir ]]; then | 93 | if [[ ! -d $clone_dir ]]; then |
93 | git clone "$url" "$clone_dir" | 94 | git clone "$url" "$clone_dir" |
94 | elif $update; then | 95 | elif $update; then |
95 | git --git-dir "$clone_dir/.git" pull | 96 | git --git-dir "$clone_dir/.git" pull |
96 | fi | 97 | fi |
97 | 98 | ||
98 | handled_repos="$handled_repos\n$url" | 99 | handled_repos="$handled_repos\n$url" |
99 | fi | 100 | fi |
100 | 101 | ||
101 | if [[ $name != *.theme ]]; then | 102 | if [[ $name != *.theme ]]; then |
102 | echo Installing $name | 103 | echo Installing $name |
103 | local bundle_dest="$ANTIGEN_BUNDLE_DIR/$name" | 104 | local bundle_dest="$ANTIGEN_BUNDLE_DIR/$name" |
104 | test -e "$bundle_dest" && rm -rf "$bundle_dest" | 105 | test -e "$bundle_dest" && rm -rf "$bundle_dest" |
105 | ln -s "$clone_dir/$loc" "$bundle_dest" | 106 | ln -s "$clone_dir/$loc" "$bundle_dest" |
106 | else | 107 | else |
107 | mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" | 108 | mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" |
108 | cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" | 109 | cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" |
109 | fi | 110 | fi |
110 | 111 | ||
111 | bundle-load "$name" | 112 | bundle-load "$name" |
112 | 113 | ||
113 | done | 114 | done |
114 | 115 | ||
115 | } | 116 | } |
116 | 117 | ||
117 | bundle-install! () { | 118 | bundle-install! () { |
118 | bundle-install --update | 119 | bundle-install --update |
119 | } | 120 | } |
120 | 121 | ||
121 | bundle-cleanup () { | 122 | bundle-cleanup () { |
122 | 123 | ||
123 | # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record. | 124 | # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record. |
124 | local unidentified_bundles="$(comm -13 \ | 125 | local unidentified_bundles="$(comm -13 \ |
125 | <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \ | 126 | <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \ |
126 | <(ls -1 "$ANTIGEN_BUNDLE_DIR"))" | 127 | <(ls -1 "$ANTIGEN_BUNDLE_DIR"))" |
127 | 128 | ||
128 | if [[ -z $unidentified_bundles ]]; then | 129 | if [[ -z $unidentified_bundles ]]; then |
129 | echo "You don't have any unidentified bundles." | 130 | echo "You don't have any unidentified bundles." |
130 | return 0 | 131 | return 0 |
131 | fi | 132 | fi |
132 | 133 | ||
133 | echo The following bundles are not recorded: | 134 | echo The following bundles are not recorded: |
134 | echo "$unidentified_bundles" | sed 's/^/ /' | 135 | echo "$unidentified_bundles" | sed 's/^/ /' |
135 | 136 | ||
136 | echo -n '\nDelete them all? [y/N] ' | 137 | echo -n '\nDelete them all? [y/N] ' |
137 | if read -q; then | 138 | if read -q; then |
138 | echo | 139 | echo |
139 | echo | 140 | echo |
140 | echo "$unidentified_bundles" | while read name; do | 141 | echo "$unidentified_bundles" | while read name; do |
141 | echo -n Deleting $name... | 142 | echo -n Deleting $name... |
142 | rm -rf "$ANTIGEN_BUNDLE_DIR/$name" | 143 | rm -rf "$ANTIGEN_BUNDLE_DIR/$name" |
143 | echo ' done.' | 144 | echo ' done.' |
144 | done | 145 | done |
145 | else | 146 | else |
146 | echo | 147 | echo |
147 | echo Nothing deleted. | 148 | echo Nothing deleted. |
148 | fi | 149 | fi |
149 | } | 150 | } |
150 | 151 | ||
151 | bundle-load () { | 152 | bundle-load () { |
152 | if [[ $1 == --init ]]; then | 153 | if [[ $1 == --init ]]; then |
153 | do_init=true | 154 | do_init=true |
154 | shift | 155 | shift |
155 | else | 156 | else |
156 | do_init=false | 157 | do_init=false |
157 | fi | 158 | fi |
158 | 159 | ||
159 | name="$1" | 160 | name="$1" |
160 | bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" | 161 | bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" |
161 | 162 | ||
162 | # Source the plugin script | 163 | # Source the plugin script |
163 | script_loc="$bundle_dir/$name.plugin.zsh" | 164 | script_loc="$bundle_dir/$name.plugin.zsh" |
164 | if [[ -f $script_loc ]]; then | 165 | if [[ -f $script_loc ]]; then |
165 | source "$script_loc" | 166 | source "$script_loc" |
166 | fi | 167 | fi |
167 | 168 | ||
168 | # If the name of the plugin ends with `.lib`, all the *.zsh files in it are | 169 | # If the name of the plugin ends with `.lib`, all the *.zsh files in it are |
169 | # sourced. This is kind of a hack to source the libraries of oh-my-zsh. | 170 | # sourced. This is kind of a hack to source the libraries of oh-my-zsh. |
170 | if [[ $name == *.lib ]]; then | 171 | if [[ $name == *.lib ]]; then |
171 | # FIXME: This throws an error if no files match the given glob pattern. | 172 | # FIXME: This throws an error if no files match the given glob pattern. |
172 | for lib ($bundle_dir/*.zsh) source $lib | 173 | for lib ($bundle_dir/*.zsh) source $lib |
173 | fi | 174 | fi |
174 | 175 | ||
175 | # If the name ends with `.theme`, it is handled as if it were a zsh-theme | 176 | # If the name ends with `.theme`, it is handled as if it were a zsh-theme |
176 | # plugin. | 177 | # plugin. |
177 | if [[ $name == *.theme ]]; then | 178 | if [[ $name == *.theme ]]; then |
178 | source "$bundle_dir/${name%.theme}.zsh-theme" | 179 | source "$bundle_dir/${name%.theme}.zsh-theme" |
179 | fi | 180 | fi |
180 | 181 | ||
181 | # Add to $fpath, for completion(s) | 182 | # Add to $fpath, for completion(s) |
182 | fpath=($bundle_dir $fpath) | 183 | fpath=($bundle_dir $fpath) |
183 | 184 | ||
184 | if $do_init; then | 185 | if $do_init; then |
185 | bundle-init | 186 | bundle-init |
186 | fi | 187 | fi |
187 | } | 188 | } |
188 | 189 | ||
189 | bundle-lib () { | 190 | bundle-lib () { |
190 | bundle --name=oh-my-zsh.lib --loc=lib | 191 | bundle --name=oh-my-zsh.lib --loc=lib |
191 | } | 192 | } |
192 | 193 | ||
193 | bundle-theme () { | 194 | bundle-theme () { |
194 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 195 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
195 | local name="${1:-robbyrussell}" | 196 | local name="${1:-robbyrussell}" |
196 | bundle "$url" --name=$name.theme --loc=themes/$name.zsh-theme | 197 | bundle "$url" --name=$name.theme --loc=themes/$name.zsh-theme |
197 | } | 198 | } |
198 | 199 | ||
199 | bundle-apply () { | 200 | bundle-apply () { |
200 | # Initialize completion. | 201 | # Initialize completion. |
201 | compinit -i | 202 | compinit -i |
202 | } | 203 | } |
203 | 204 | ||
204 | # Does what it says. | 205 | # Does what it says. |
205 | echo-non-empty () { | 206 | echo-non-empty () { |
206 | echo "$@" | while read line; do | 207 | echo "$@" | while read line; do |
207 | [[ $line != "" ]] && echo $line | 208 | [[ $line != "" ]] && echo $line |
208 | done | 209 | done |
209 | } | 210 | } |
210 | 211 | ||
211 | -bundle-env-setup () { | 212 | -bundle-env-setup () { |
212 | # Pre-startup initializations | 213 | # Pre-startup initializations |
213 | -set-default ANTIGEN_DEFAULT_REPO_URL \ | 214 | -set-default ANTIGEN_DEFAULT_REPO_URL \ |
214 | https://github.com/robbyrussell/oh-my-zsh.git | 215 | https://github.com/robbyrussell/oh-my-zsh.git |
215 | -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache | 216 | -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache |
216 | -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles | 217 | -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles |
217 | 218 | ||
218 | # Load the compinit module | 219 | # Load the compinit module |
219 | autoload -U compinit | 220 | autoload -U compinit |
220 | 221 | ||
221 | # Without the following, `compdef` function is not defined. | 222 | # Without the following, `compdef` function is not defined. |
222 | compinit -i | 223 | compinit -i |
223 | } | 224 | } |
224 | 225 | ||
225 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is | 226 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is |
226 | # not already set. | 227 | # not already set. |
227 | -set-default () { | 228 | -set-default () { |
228 | arg_name="$1" | 229 | arg_name="$1" |
229 | arg_value="$2" | 230 | arg_value="$2" |
230 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" | 231 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" |
231 | } | 232 | } |
232 | 233 | ||
233 | -bundle-env-setup | 234 | -bundle-env-setup |
234 | 235 |