Commit 3c5ff1c1e2d09d35963ca574b4dd57e7e6a2b7b2

Authored by Shrikant Sharat
1 parent a4f6e2fc87

Add antigen-bundles command.

This command lets you bulk-add bundles with one command, and piping bundle
specs, one on each line into this command. Best used with heredoc syntax.

Showing 1 changed file with 14 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 # <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 # antigen-bundle <url> [<loc>=/] 10 # antigen-bundle <url> [<loc>=/]
11 antigen-bundle () { 11 antigen-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://* && $url != /* ]]; then 45 if [[ $url != git://* && $url != https://* && $url != /* ]]; 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 -antigen-load "$url" "$loc" "$btype" 55 -antigen-load "$url" "$loc" "$btype"
56 56
57 } 57 }
58 58
59 antigen-bundles () {
60 # Bulk add many bundles at one go. Empty lines and lines starting with a `#`
61 # are ignored. Everything else is given to `antigen-bundle` as is, no
62 # quoting rules applied.
63
64 local line
65
66 grep -v '^$\|^#' | while read line; do
67 # Using `eval` so that we can use the shell-style quoting in each line
68 # piped to `antigen-bundles`.
69 eval "antigen-bundle $line"
70 done
71 }
72
59 antigen-bundle-update () { 73 antigen-bundle-update () {
60 # Update your bundles, i.e., `git pull` in all the plugin repos. 74 # Update your bundles, i.e., `git pull` in all the plugin repos.
61 -antigen-echo-record | awk '{print $1}' | sort -u | while read url; do 75 -antigen-echo-record | awk '{print $1}' | sort -u | while read url; do
62 -antigen-ensure-repo --update "$url" 76 -antigen-ensure-repo --update "$url"
63 done 77 done
64 } 78 }
65 79
66 -antigen-get-clone-dir () { 80 -antigen-get-clone-dir () {
67 # Takes a repo url and gives out the path that this url needs to be cloned 81 # Takes a repo url and gives out the path that this url needs to be cloned
68 # to. Doesn't actually clone anything. 82 # to. Doesn't actually clone anything.
69 # TODO: Memoize? 83 # TODO: Memoize?
70 echo -n $ADOTDIR/repos/ 84 echo -n $ADOTDIR/repos/
71 echo "$1" | sed \ 85 echo "$1" | sed \
72 -e 's/\.git$//' \ 86 -e 's/\.git$//' \
73 -e 's./.-SLASH-.g' \ 87 -e 's./.-SLASH-.g' \
74 -e 's.:.-COLON-.g' 88 -e 's.:.-COLON-.g'
75 } 89 }
76 90
77 -antigen-get-clone-url () { 91 -antigen-get-clone-url () {
78 # Takes a repo's clone dir and gives out the repo's original url that was 92 # Takes a repo's clone dir and gives out the repo's original url that was
79 # used to create the given directory path. 93 # used to create the given directory path.
80 # TODO: Memoize? 94 # TODO: Memoize?
81 echo "$1" | sed \ 95 echo "$1" | sed \
82 -e "s:^$ADOTDIR/repos/::" \ 96 -e "s:^$ADOTDIR/repos/::" \
83 -e 's/$/.git/' \ 97 -e 's/$/.git/' \
84 -e 's.-SLASH-./.g' \ 98 -e 's.-SLASH-./.g' \
85 -e 's.-COLON-.:.g' 99 -e 's.-COLON-.:.g'
86 } 100 }
87 101
88 -antigen-ensure-repo () { 102 -antigen-ensure-repo () {
89 103
90 local update=false 104 local update=false
91 if [[ $1 == --update ]]; then 105 if [[ $1 == --update ]]; then
92 update=true 106 update=true
93 shift 107 shift
94 fi 108 fi
95 109
96 local url="$1" 110 local url="$1"
97 local clone_dir="$(-antigen-get-clone-dir $url)" 111 local clone_dir="$(-antigen-get-clone-dir $url)"
98 112
99 if [[ ! -d $clone_dir ]]; then 113 if [[ ! -d $clone_dir ]]; then
100 git clone "$url" "$clone_dir" 114 git clone "$url" "$clone_dir"
101 elif $update; then 115 elif $update; then
102 git --git-dir "$clone_dir/.git" pull 116 git --git-dir "$clone_dir/.git" pull
103 fi 117 fi
104 118
105 } 119 }
106 120
107 -antigen-load () { 121 -antigen-load () {
108 122
109 local url="$1" 123 local url="$1"
110 local location="$(-antigen-get-clone-dir "$url")/$2" 124 local location="$(-antigen-get-clone-dir "$url")/$2"
111 local btype="$3" 125 local btype="$3"
112 126
113 if [[ $btype == theme ]]; then 127 if [[ $btype == theme ]]; then
114 128
115 # Of course, if its a theme, the location would point to the script 129 # Of course, if its a theme, the location would point to the script
116 # file. 130 # file.
117 source "$location" 131 source "$location"
118 132
119 else 133 else
120 134
121 # Source the plugin script 135 # Source the plugin script
122 # FIXME: I don't know. Looks very very ugly. Needs a better 136 # FIXME: I don't know. Looks very very ugly. Needs a better
123 # implementation once tests are ready. 137 # implementation once tests are ready.
124 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" 138 local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')"
125 if [[ -f $script_loc ]]; then 139 if [[ -f $script_loc ]]; then
126 # If we have a `*.plugin.zsh`, source it. 140 # If we have a `*.plugin.zsh`, source it.
127 source "$script_loc" 141 source "$script_loc"
128 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then 142 elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then
129 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` 143 # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
130 # files. 144 # files.
131 for script ($location/*.zsh) source "$script" 145 for script ($location/*.zsh) source "$script"
132 fi 146 fi
133 147
134 # Add to $fpath, for completion(s) 148 # Add to $fpath, for completion(s)
135 fpath=($location $fpath) 149 fpath=($location $fpath)
136 150
137 fi 151 fi
138 152
139 } 153 }
140 154
141 antigen-cleanup () { 155 antigen-cleanup () {
142 156
143 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then 157 if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then
144 echo "You don't have any bundles." 158 echo "You don't have any bundles."
145 return 0 159 return 0
146 fi 160 fi
147 161
148 # Find directores in ADOTDIR/repos, that are not in the bundles record. 162 # Find directores in ADOTDIR/repos, that are not in the bundles record.
149 local unused_clones="$(comm -13 \ 163 local unused_clones="$(comm -13 \
150 <(-antigen-echo-record | awk '{print $1}' | sort -u) \ 164 <(-antigen-echo-record | awk '{print $1}' | sort -u) \
151 <(ls "$ADOTDIR/repos" | while read line; do 165 <(ls "$ADOTDIR/repos" | while read line; do
152 -antigen-get-clone-url "$line" 166 -antigen-get-clone-url "$line"
153 done))" 167 done))"
154 168
155 if [[ -z $unused_clones ]]; then 169 if [[ -z $unused_clones ]]; then
156 echo "You don't have any unidentified bundles." 170 echo "You don't have any unidentified bundles."
157 return 0 171 return 0
158 fi 172 fi
159 173
160 echo 'You have clones for the following repos, but are not used.' 174 echo 'You have clones for the following repos, but are not used.'
161 echo "$unused_clones" | sed 's/^/ /' 175 echo "$unused_clones" | sed 's/^/ /'
162 176
163 echo -n '\nDelete them all? [y/N] ' 177 echo -n '\nDelete them all? [y/N] '
164 if read -q; then 178 if read -q; then
165 echo 179 echo
166 echo 180 echo
167 echo "$unused_clones" | while read url; do 181 echo "$unused_clones" | while read url; do
168 echo -n "Deleting clone for $url..." 182 echo -n "Deleting clone for $url..."
169 rm -rf "$(-antigen-get-clone-dir $url)" 183 rm -rf "$(-antigen-get-clone-dir $url)"
170 echo ' done.' 184 echo ' done.'
171 done 185 done
172 else 186 else
173 echo 187 echo
174 echo Nothing deleted. 188 echo Nothing deleted.
175 fi 189 fi
176 } 190 }
177 191
178 antigen-lib () { 192 antigen-lib () {
179 antigen-bundle --loc=lib 193 antigen-bundle --loc=lib
180 } 194 }
181 195
182 antigen-theme () { 196 antigen-theme () {
183 local name="${1:-robbyrussell}" 197 local name="${1:-robbyrussell}"
184 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme 198 antigen-bundle --loc=themes/$name.zsh-theme --btype=theme
185 } 199 }
186 200
187 antigen-apply () { 201 antigen-apply () {
188 # Initialize completion. 202 # Initialize completion.
189 # TODO: Only load completions if there are any changes to the bundle 203 # TODO: Only load completions if there are any changes to the bundle
190 # repositories. 204 # repositories.
191 compinit -i 205 compinit -i
192 } 206 }
193 207
194 antigen-list () { 208 antigen-list () {
195 # List all currently installed bundles 209 # List all currently installed bundles
196 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then 210 if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
197 echo "You don't have any bundles." >&2 211 echo "You don't have any bundles." >&2
198 return 1 212 return 1
199 else 213 else
200 -antigen-echo-record 214 -antigen-echo-record
201 fi 215 fi
202 } 216 }
203 217
204 # Echo the bundle specs as in the record. The first line is not echoed since it 218 # Echo the bundle specs as in the record. The first line is not echoed since it
205 # is a blank line. 219 # is a blank line.
206 -antigen-echo-record () { 220 -antigen-echo-record () {
207 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' 221 echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
208 } 222 }
209 223
210 -antigen-env-setup () { 224 -antigen-env-setup () {
211 # Pre-startup initializations 225 # Pre-startup initializations
212 -set-default ANTIGEN_DEFAULT_REPO_URL \ 226 -set-default ANTIGEN_DEFAULT_REPO_URL \
213 https://github.com/robbyrussell/oh-my-zsh.git 227 https://github.com/robbyrussell/oh-my-zsh.git
214 -set-default ADOTDIR $HOME/.antigen 228 -set-default ADOTDIR $HOME/.antigen
215 229
216 # Load the compinit module 230 # Load the compinit module
217 autoload -U compinit 231 autoload -U compinit
218 232
219 # Without the following, `compdef` function is not defined. 233 # Without the following, `compdef` function is not defined.
220 compinit -i 234 compinit -i
221 } 235 }
222 236
223 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is 237 # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
224 # not already set. 238 # not already set.
225 -set-default () { 239 -set-default () {
226 local arg_name="$1" 240 local arg_name="$1"
227 local arg_value="$2" 241 local arg_value="$2"
228 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" 242 eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
229 } 243 }
230 244
231 -antigen-env-setup 245 -antigen-env-setup
232 246