Commit bb140864c4b09aca310d1227dd94dc365c934f8e

Authored by Shrikant Sharat
1 parent 1ba08957c4

The plugin name should not have `.git`.

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