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