Commit a91386310e3f1a82be137bb3d78a82800c5ec0d7
1 parent
452d4f52d3
Add a todo note on bundle-apply.
We can have a performance improvement at shell startup by loading completions in bundle-apply, only if any of the repositories changed.
Showing 1 changed file with 2 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 | # <repo-url>, <plugin-location>, <bundle-type> | 5 | # <repo-url>, <plugin-location>, <bundle-type> |
6 | # FIXME: Is not kept local by zsh! | 6 | # FIXME: Is not kept local by zsh! |
7 | local _ANTIGEN_BUNDLE_RECORD="" | 7 | local _ANTIGEN_BUNDLE_RECORD="" |
8 | 8 | ||
9 | # Syntaxes | 9 | # Syntaxes |
10 | # bundle <url> [<loc>=/] | 10 | # bundle <url> [<loc>=/] |
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 btype=plugin | 16 | local btype=plugin |
17 | 17 | ||
18 | # Set spec values based on the positional arguments. | 18 | # Set spec values based on the positional arguments. |
19 | local position_args='url loc' | 19 | local position_args='url loc' |
20 | local i=1 | 20 | local i=1 |
21 | while ! [[ -z $1 || $1 == --*=* ]]; do | 21 | while ! [[ -z $1 || $1 == --*=* ]]; do |
22 | local arg_name="$(echo "$position_args" | cut -d\ -f$i)" | 22 | local arg_name="$(echo "$position_args" | cut -d\ -f$i)" |
23 | local arg_value="$1" | 23 | local arg_value="$1" |
24 | eval "local $arg_name='$arg_value'" | 24 | eval "local $arg_name='$arg_value'" |
25 | shift | 25 | shift |
26 | i=$(($i + 1)) | 26 | i=$(($i + 1)) |
27 | done | 27 | done |
28 | 28 | ||
29 | # Check if url is just the plugin name. Super short syntax. | 29 | # Check if url is just the plugin name. Super short syntax. |
30 | if [[ "$url" != */* ]]; then | 30 | if [[ "$url" != */* ]]; then |
31 | loc="plugins/$url" | 31 | loc="plugins/$url" |
32 | url="$ANTIGEN_DEFAULT_REPO_URL" | 32 | url="$ANTIGEN_DEFAULT_REPO_URL" |
33 | fi | 33 | fi |
34 | 34 | ||
35 | # Set spec values from keyword arguments, if any. The remaining arguments | 35 | # Set spec values from keyword arguments, if any. The remaining arguments |
36 | # are all assumed to be keyword arguments. | 36 | # are all assumed to be keyword arguments. |
37 | while [[ $1 == --*=* ]]; do | 37 | while [[ $1 == --*=* ]]; do |
38 | local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" | 38 | local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" |
39 | local arg_value="$(echo "$1" | cut -d= -f2)" | 39 | local arg_value="$(echo "$1" | cut -d= -f2)" |
40 | eval "local $arg_name='$arg_value'" | 40 | eval "local $arg_name='$arg_value'" |
41 | shift | 41 | shift |
42 | done | 42 | done |
43 | 43 | ||
44 | # Resolve the url. | 44 | # Resolve the url. |
45 | if [[ $url != git://* && $url != https://* ]]; then | 45 | if [[ $url != git://* && $url != https://* ]]; then |
46 | url="${url%.git}" | 46 | url="${url%.git}" |
47 | url="https://github.com/$url.git" | 47 | url="https://github.com/$url.git" |
48 | fi | 48 | fi |
49 | 49 | ||
50 | # Add it to the record. | 50 | # Add it to the record. |
51 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" | 51 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" |
52 | 52 | ||
53 | -antigen-ensure-repo "$url" | 53 | -antigen-ensure-repo "$url" |
54 | 54 | ||
55 | bundle-load "$url" "$loc" "$btype" | 55 | bundle-load "$url" "$loc" "$btype" |
56 | 56 | ||
57 | } | 57 | } |
58 | 58 | ||
59 | -antigen-get-clone-dir () { | 59 | -antigen-get-clone-dir () { |
60 | # Takes a repo url and gives out the path that this url needs to be cloned | 60 | # Takes a repo url and gives out the path that this url needs to be cloned |
61 | # to. Doesn't actually clone anything. | 61 | # to. Doesn't actually clone anything. |
62 | # TODO: Memoize? | 62 | # TODO: Memoize? |
63 | echo -n $ADOTDIR/repos/ | 63 | echo -n $ADOTDIR/repos/ |
64 | echo "$1" | sed \ | 64 | echo "$1" | sed \ |
65 | -e 's/\.git$//' \ | 65 | -e 's/\.git$//' \ |
66 | -e 's./.-SLASH-.g' \ | 66 | -e 's./.-SLASH-.g' \ |
67 | -e 's.:.-COLON-.g' | 67 | -e 's.:.-COLON-.g' |
68 | } | 68 | } |
69 | 69 | ||
70 | -antigen-get-clone-url () { | 70 | -antigen-get-clone-url () { |
71 | # Takes a repo's clone dir and gives out the repo's original url that was | 71 | # Takes a repo's clone dir and gives out the repo's original url that was |
72 | # used to create the given directory path. | 72 | # used to create the given directory path. |
73 | # TODO: Memoize? | 73 | # TODO: Memoize? |
74 | echo "$1" | sed \ | 74 | echo "$1" | sed \ |
75 | -e "s:^$ADOTDIR/repos/::" \ | 75 | -e "s:^$ADOTDIR/repos/::" \ |
76 | -e 's/$/.git/' \ | 76 | -e 's/$/.git/' \ |
77 | -e 's.-SLASH-./.g' \ | 77 | -e 's.-SLASH-./.g' \ |
78 | -e 's.-COLON-.:.g' | 78 | -e 's.-COLON-.:.g' |
79 | } | 79 | } |
80 | 80 | ||
81 | -antigen-ensure-repo () { | 81 | -antigen-ensure-repo () { |
82 | 82 | ||
83 | local update=false | 83 | local update=false |
84 | if [[ $1 == --update ]]; then | 84 | if [[ $1 == --update ]]; then |
85 | update=true | 85 | update=true |
86 | shift | 86 | shift |
87 | fi | 87 | fi |
88 | 88 | ||
89 | local handled_repos="" | 89 | local handled_repos="" |
90 | local install_bundles="" | 90 | local install_bundles="" |
91 | 91 | ||
92 | local url="$1" | 92 | local url="$1" |
93 | local clone_dir="$(-antigen-get-clone-dir $url)" | 93 | local clone_dir="$(-antigen-get-clone-dir $url)" |
94 | 94 | ||
95 | if ! echo "$handled_repos" | grep -Fqm1 "$url"; then | 95 | if ! echo "$handled_repos" | grep -Fqm1 "$url"; then |
96 | if [[ ! -d $clone_dir ]]; then | 96 | if [[ ! -d $clone_dir ]]; then |
97 | git clone "$url" "$clone_dir" | 97 | git clone "$url" "$clone_dir" |
98 | elif $update; then | 98 | elif $update; then |
99 | git --git-dir "$clone_dir/.git" pull | 99 | git --git-dir "$clone_dir/.git" pull |
100 | fi | 100 | fi |
101 | 101 | ||
102 | handled_repos="$handled_repos\n$url" | 102 | handled_repos="$handled_repos\n$url" |
103 | fi | 103 | fi |
104 | 104 | ||
105 | } | 105 | } |
106 | 106 | ||
107 | bundle-update () { | 107 | bundle-update () { |
108 | # Update your bundles, i.e., `git pull` in all the plugin repos. | 108 | # Update your bundles, i.e., `git pull` in all the plugin repos. |
109 | -bundle-echo-record | awk '{print $1}' | sort -u | while read url; do | 109 | -bundle-echo-record | awk '{print $1}' | sort -u | while read url; do |
110 | -antigen-ensure-repo --update "$url" | 110 | -antigen-ensure-repo --update "$url" |
111 | done | 111 | done |
112 | } | 112 | } |
113 | 113 | ||
114 | bundle-load () { | 114 | bundle-load () { |
115 | 115 | ||
116 | local url="$1" | 116 | local url="$1" |
117 | local location="$(-antigen-get-clone-dir "$url")/$2" | 117 | local location="$(-antigen-get-clone-dir "$url")/$2" |
118 | local btype="$3" | 118 | local btype="$3" |
119 | 119 | ||
120 | if [[ $btype == theme ]]; then | 120 | if [[ $btype == theme ]]; then |
121 | 121 | ||
122 | # Of course, if its a theme, the location would point to the script | 122 | # Of course, if its a theme, the location would point to the script |
123 | # file. | 123 | # file. |
124 | source "$location" | 124 | source "$location" |
125 | 125 | ||
126 | else | 126 | else |
127 | 127 | ||
128 | # Source the plugin script | 128 | # Source the plugin script |
129 | # FIXME: I don't know. Looks very very ugly. Needs a better | 129 | # FIXME: I don't know. Looks very very ugly. Needs a better |
130 | # implementation once tests are ready. | 130 | # implementation once tests are ready. |
131 | local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" | 131 | local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" |
132 | if [[ -f $script_loc ]]; then | 132 | if [[ -f $script_loc ]]; then |
133 | # If we have a `*.plugin.zsh`, source it. | 133 | # If we have a `*.plugin.zsh`, source it. |
134 | source "$script_loc" | 134 | source "$script_loc" |
135 | elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then | 135 | elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then |
136 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` | 136 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` |
137 | # files. | 137 | # files. |
138 | for script ($location/*.zsh) source "$script" | 138 | for script ($location/*.zsh) source "$script" |
139 | fi | 139 | fi |
140 | 140 | ||
141 | # Add to $fpath, for completion(s) | 141 | # Add to $fpath, for completion(s) |
142 | fpath=($location $fpath) | 142 | fpath=($location $fpath) |
143 | 143 | ||
144 | fi | 144 | fi |
145 | 145 | ||
146 | } | 146 | } |
147 | 147 | ||
148 | bundle-cleanup () { | 148 | bundle-cleanup () { |
149 | 149 | ||
150 | if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then | 150 | if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then |
151 | echo "You don't have any bundles." | 151 | echo "You don't have any bundles." |
152 | return 0 | 152 | return 0 |
153 | fi | 153 | fi |
154 | 154 | ||
155 | # Find directores in ADOTDIR/repos, that are not in the bundles record. | 155 | # Find directores in ADOTDIR/repos, that are not in the bundles record. |
156 | local unused_clones="$(comm -13 \ | 156 | local unused_clones="$(comm -13 \ |
157 | <(-bundle-echo-record | awk '{print $1}' | sort -u) \ | 157 | <(-bundle-echo-record | awk '{print $1}' | sort -u) \ |
158 | <(ls "$ADOTDIR/repos" | while read line; do | 158 | <(ls "$ADOTDIR/repos" | while read line; do |
159 | -antigen-get-clone-url "$line" | 159 | -antigen-get-clone-url "$line" |
160 | done))" | 160 | done))" |
161 | 161 | ||
162 | if [[ -z $unused_clones ]]; then | 162 | if [[ -z $unused_clones ]]; then |
163 | echo "You don't have any unidentified bundles." | 163 | echo "You don't have any unidentified bundles." |
164 | return 0 | 164 | return 0 |
165 | fi | 165 | fi |
166 | 166 | ||
167 | echo 'You have clones for the following repos, but are not used.' | 167 | echo 'You have clones for the following repos, but are not used.' |
168 | echo "$unused_clones" | sed 's/^/ /' | 168 | echo "$unused_clones" | sed 's/^/ /' |
169 | 169 | ||
170 | echo -n '\nDelete them all? [y/N] ' | 170 | echo -n '\nDelete them all? [y/N] ' |
171 | if read -q; then | 171 | if read -q; then |
172 | echo | 172 | echo |
173 | echo | 173 | echo |
174 | echo "$unused_clones" | while read url; do | 174 | echo "$unused_clones" | while read url; do |
175 | echo -n "Deleting clone for $url..." | 175 | echo -n "Deleting clone for $url..." |
176 | rm -rf "$(-antigen-get-clone-dir $url)" | 176 | rm -rf "$(-antigen-get-clone-dir $url)" |
177 | echo ' done.' | 177 | echo ' done.' |
178 | done | 178 | done |
179 | else | 179 | else |
180 | echo | 180 | echo |
181 | echo Nothing deleted. | 181 | echo Nothing deleted. |
182 | fi | 182 | fi |
183 | } | 183 | } |
184 | 184 | ||
185 | bundle-lib () { | 185 | bundle-lib () { |
186 | bundle --loc=lib | 186 | bundle --loc=lib |
187 | } | 187 | } |
188 | 188 | ||
189 | bundle-theme () { | 189 | bundle-theme () { |
190 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 190 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
191 | local name="${1:-robbyrussell}" | 191 | local name="${1:-robbyrussell}" |
192 | bundle --loc=themes/$name.zsh-theme --btype=theme | 192 | bundle --loc=themes/$name.zsh-theme --btype=theme |
193 | } | 193 | } |
194 | 194 | ||
195 | bundle-apply () { | 195 | bundle-apply () { |
196 | # Initialize completion. | 196 | # Initialize completion. |
197 | # TODO: Only load completions if there are any changes to the bundle | ||
198 | # repositories. | ||
197 | compinit -i | 199 | compinit -i |
198 | } | 200 | } |
199 | 201 | ||
200 | bundle-list () { | 202 | bundle-list () { |
201 | # List all currently installed bundles | 203 | # List all currently installed bundles |
202 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then | 204 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then |
203 | echo "You don't have any bundles." >&2 | 205 | echo "You don't have any bundles." >&2 |
204 | return 1 | 206 | return 1 |
205 | else | 207 | else |
206 | -bundle-echo-record | awk '{print $1 " " $2 " " $3}' | 208 | -bundle-echo-record | awk '{print $1 " " $2 " " $3}' |
207 | fi | 209 | fi |
208 | } | 210 | } |
209 | 211 | ||
210 | # Echo the bundle specs as in the record. The first line is not echoed since it | 212 | # Echo the bundle specs as in the record. The first line is not echoed since it |
211 | # is a blank line. | 213 | # is a blank line. |
212 | -bundle-echo-record () { | 214 | -bundle-echo-record () { |
213 | echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' | 215 | echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' |
214 | } | 216 | } |
215 | 217 | ||
216 | -bundle-env-setup () { | 218 | -bundle-env-setup () { |
217 | # Pre-startup initializations | 219 | # Pre-startup initializations |
218 | -set-default ANTIGEN_DEFAULT_REPO_URL \ | 220 | -set-default ANTIGEN_DEFAULT_REPO_URL \ |
219 | https://github.com/robbyrussell/oh-my-zsh.git | 221 | https://github.com/robbyrussell/oh-my-zsh.git |
220 | -set-default ADOTDIR $HOME/.antigen | 222 | -set-default ADOTDIR $HOME/.antigen |
221 | 223 | ||
222 | # Load the compinit module | 224 | # Load the compinit module |
223 | autoload -U compinit | 225 | autoload -U compinit |
224 | 226 | ||
225 | # Without the following, `compdef` function is not defined. | 227 | # Without the following, `compdef` function is not defined. |
226 | compinit -i | 228 | compinit -i |
227 | } | 229 | } |
228 | 230 | ||
229 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is | 231 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is |
230 | # not already set. | 232 | # not already set. |
231 | -set-default () { | 233 | -set-default () { |
232 | local arg_name="$1" | 234 | local arg_name="$1" |
233 | local arg_value="$2" | 235 | local arg_value="$2" |
234 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" | 236 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" |
235 | } | 237 | } |
236 | 238 | ||
237 | -bundle-env-setup | 239 | -bundle-env-setup |
238 | 240 |