Commit d1b387004743274060271a9461cebb86b1cf12d5
1 parent
b05176f256
Fix bundle name having `.git` in the end.
Showing 1 changed file with 1 additions and 1 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 | # 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="https://github.com/$url.git" | ||
42 | name="$(basename "$url")" | 41 | name="$(basename "$url")" |
42 | url="https://github.com/$url.git" | ||
43 | fi | 43 | fi |
44 | 44 | ||
45 | # Plugin's repo will be cloned here. | 45 | # Plugin's repo will be cloned here. |
46 | local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \ | 46 | local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \ |
47 | | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')" | 47 | | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')" |
48 | 48 | ||
49 | # Make an intelligent guess about the name of the plugin, if not already | 49 | # Make an intelligent guess about the name of the plugin, if not already |
50 | # done or is explicitly specified. | 50 | # done or is explicitly specified. |
51 | if [[ -z $name ]]; then | 51 | if [[ -z $name ]]; then |
52 | name="$(basename $url/$loc)" | 52 | name="$(basename $url/$loc)" |
53 | fi | 53 | fi |
54 | 54 | ||
55 | # Add it to the record. | 55 | # Add it to the record. |
56 | bundles="$bundles\n$name $url $loc $clone_dir" | 56 | bundles="$bundles\n$name $url $loc $clone_dir" |
57 | 57 | ||
58 | # Load it, unless specified otherwise. | 58 | # Load it, unless specified otherwise. |
59 | if $load; then | 59 | if $load; then |
60 | bundle-load "$name" | 60 | bundle-load "$name" |
61 | fi | 61 | fi |
62 | } | 62 | } |
63 | 63 | ||
64 | bundle-install () { | 64 | bundle-install () { |
65 | 65 | ||
66 | if [[ $1 == --update ]]; then | 66 | if [[ $1 == --update ]]; then |
67 | local update=true | 67 | local update=true |
68 | else | 68 | else |
69 | local update=false | 69 | local update=false |
70 | fi | 70 | fi |
71 | 71 | ||
72 | mkdir -p "$ANTIGEN_BUNDLE_DIR" | 72 | mkdir -p "$ANTIGEN_BUNDLE_DIR" |
73 | 73 | ||
74 | local handled_repos="" | 74 | local handled_repos="" |
75 | 75 | ||
76 | echo-non-empty "$bundles" | while read spec; do | 76 | echo-non-empty "$bundles" | while read spec; do |
77 | 77 | ||
78 | local name="$(echo "$spec" | awk '{print $1}')" | 78 | local name="$(echo "$spec" | awk '{print $1}')" |
79 | local url="$(echo "$spec" | awk '{print $2}')" | 79 | local url="$(echo "$spec" | awk '{print $2}')" |
80 | local loc="$(echo "$spec" | awk '{print $3}')" | 80 | local loc="$(echo "$spec" | awk '{print $3}')" |
81 | local clone_dir="$(echo "$spec" | awk '{print $4}')" | 81 | local clone_dir="$(echo "$spec" | awk '{print $4}')" |
82 | 82 | ||
83 | if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then | 83 | if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then |
84 | if [[ ! -d $clone_dir ]]; then | 84 | if [[ ! -d $clone_dir ]]; then |
85 | git clone "$url" "$clone_dir" | 85 | git clone "$url" "$clone_dir" |
86 | elif $update; then | 86 | elif $update; then |
87 | git --git-dir "$clone_dir/.git" pull | 87 | git --git-dir "$clone_dir/.git" pull |
88 | fi | 88 | fi |
89 | 89 | ||
90 | handled_repos="$handled_repos\n$url" | 90 | handled_repos="$handled_repos\n$url" |
91 | fi | 91 | fi |
92 | 92 | ||
93 | if [[ $name != *.theme ]]; then | 93 | if [[ $name != *.theme ]]; then |
94 | cp -r "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" | 94 | cp -r "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" |
95 | else | 95 | else |
96 | mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" | 96 | mkdir -p "$ANTIGEN_BUNDLE_DIR/$name" |
97 | cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" | 97 | cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name" |
98 | fi | 98 | fi |
99 | 99 | ||
100 | bundle-load "$name" | 100 | bundle-load "$name" |
101 | 101 | ||
102 | done | 102 | done |
103 | 103 | ||
104 | } | 104 | } |
105 | 105 | ||
106 | bundle-install! () { | 106 | bundle-install! () { |
107 | bundle-install --update | 107 | bundle-install --update |
108 | } | 108 | } |
109 | 109 | ||
110 | bundle-load () { | 110 | bundle-load () { |
111 | if [[ $1 == --init ]]; then | 111 | if [[ $1 == --init ]]; then |
112 | do_init=true | 112 | do_init=true |
113 | shift | 113 | shift |
114 | else | 114 | else |
115 | do_init=false | 115 | do_init=false |
116 | fi | 116 | fi |
117 | 117 | ||
118 | name="$1" | 118 | name="$1" |
119 | bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" | 119 | bundle_dir="$ANTIGEN_BUNDLE_DIR/$name" |
120 | 120 | ||
121 | # Source the plugin script | 121 | # Source the plugin script |
122 | script_loc="$bundle_dir/$name.plugin.zsh" | 122 | script_loc="$bundle_dir/$name.plugin.zsh" |
123 | if [[ -f $script_loc ]]; then | 123 | if [[ -f $script_loc ]]; then |
124 | source "$script_loc" | 124 | source "$script_loc" |
125 | fi | 125 | fi |
126 | 126 | ||
127 | # If the name of the plugin ends with `.lib`, all the *.zsh files in it are | 127 | # If the name of the plugin ends with `.lib`, all the *.zsh files in it are |
128 | # sourced. This is kind of a hack to source the libraries of oh-my-zsh. | 128 | # sourced. This is kind of a hack to source the libraries of oh-my-zsh. |
129 | if [[ $name == *.lib ]]; then | 129 | if [[ $name == *.lib ]]; then |
130 | # FIXME: This throws an error if no files match the given glob pattern. | 130 | # FIXME: This throws an error if no files match the given glob pattern. |
131 | for lib ($bundle_dir/*.zsh) source $lib | 131 | for lib ($bundle_dir/*.zsh) source $lib |
132 | fi | 132 | fi |
133 | 133 | ||
134 | # If the name ends with `.theme`, it is handled as if it were a zsh-theme | 134 | # If the name ends with `.theme`, it is handled as if it were a zsh-theme |
135 | # plugin. | 135 | # plugin. |
136 | if [[ $name == *.theme ]]; then | 136 | if [[ $name == *.theme ]]; then |
137 | source "$bundle_dir/${name%.theme}.zsh-theme" | 137 | source "$bundle_dir/${name%.theme}.zsh-theme" |
138 | fi | 138 | fi |
139 | 139 | ||
140 | # Add to $fpath, if it provides completion | 140 | # Add to $fpath, if it provides completion |
141 | if [[ -f "$bundle_dir/_$name" ]]; then | 141 | if [[ -f "$bundle_dir/_$name" ]]; then |
142 | fpath=($bundle_dir $fpath) | 142 | fpath=($bundle_dir $fpath) |
143 | fi | 143 | fi |
144 | 144 | ||
145 | if $do_init; then | 145 | if $do_init; then |
146 | bundle-init | 146 | bundle-init |
147 | fi | 147 | fi |
148 | } | 148 | } |
149 | 149 | ||
150 | bundle-lib () { | 150 | bundle-lib () { |
151 | bundle --name=oh-my-zsh.lib --loc=lib | 151 | bundle --name=oh-my-zsh.lib --loc=lib |
152 | } | 152 | } |
153 | 153 | ||
154 | bundle-theme () { | 154 | bundle-theme () { |
155 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 155 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
156 | local name="${1:-robbyrussell}" | 156 | local name="${1:-robbyrussell}" |
157 | bundle "$url" --name=$name.theme --loc=themes/$name.zsh-theme | 157 | bundle "$url" --name=$name.theme --loc=themes/$name.zsh-theme |
158 | } | 158 | } |
159 | 159 | ||
160 | bundle-init () { | 160 | bundle-init () { |
161 | # Initialize completion. | 161 | # Initialize completion. |
162 | # FIXME: Ensure this runs only once. | 162 | # FIXME: Ensure this runs only once. |
163 | autoload -U compinit | 163 | autoload -U compinit |
164 | compinit -i | 164 | compinit -i |
165 | } | 165 | } |
166 | 166 | ||
167 | # A python command wrapper. Almost the same as `python -c`, but dedents the | 167 | # A python command wrapper. Almost the same as `python -c`, but dedents the |
168 | # source string. | 168 | # source string. |
169 | py () { | 169 | py () { |
170 | code="$1" | 170 | code="$1" |
171 | 171 | ||
172 | # Find indentation from first line. | 172 | # Find indentation from first line. |
173 | indent="$(echo "$code" | grep -m1 -v '^$' | grep -o '^\s*' | wc -c)" | 173 | indent="$(echo "$code" | grep -m1 -v '^$' | grep -o '^\s*' | wc -c)" |
174 | 174 | ||
175 | # Strip that many spaces in the start from each line. | 175 | # Strip that many spaces in the start from each line. |
176 | if [[ $indent != 0 ]]; then | 176 | if [[ $indent != 0 ]]; then |
177 | indent=$(($indent - 1)) | 177 | indent=$(($indent - 1)) |
178 | code="$(echo "$code" | sed "s/^\s\{$indent\}//")" | 178 | code="$(echo "$code" | sed "s/^\s\{$indent\}//")" |
179 | fi | 179 | fi |
180 | 180 | ||
181 | # Run the piece of code. | 181 | # Run the piece of code. |
182 | python -c "$code" | 182 | python -c "$code" |
183 | } | 183 | } |
184 | 184 | ||
185 | # Does what it says. | 185 | # Does what it says. |
186 | echo-non-empty () { | 186 | echo-non-empty () { |
187 | echo "$@" | while read line; do | 187 | echo "$@" | while read line; do |
188 | [[ $line != "" ]] && echo $line | 188 | [[ $line != "" ]] && echo $line |
189 | done | 189 | done |
190 | } | 190 | } |
191 | 191 | ||
192 | -bundle-env-setup () { | 192 | -bundle-env-setup () { |
193 | -set-default ANTIGEN_DEFAULT_REPO_URL \ | 193 | -set-default ANTIGEN_DEFAULT_REPO_URL \ |
194 | https://github.com/robbyrussell/oh-my-zsh.git | 194 | https://github.com/robbyrussell/oh-my-zsh.git |
195 | -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache | 195 | -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache |
196 | -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles | 196 | -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles |
197 | } | 197 | } |
198 | 198 | ||
199 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is | 199 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is |
200 | # not already set. | 200 | # not already set. |
201 | -set-default () { | 201 | -set-default () { |
202 | arg_name="$1" | 202 | arg_name="$1" |
203 | arg_value="$2" | 203 | arg_value="$2" |
204 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" | 204 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" |
205 | } | 205 | } |
206 | 206 | ||
207 | -bundle-env-setup | 207 | -bundle-env-setup |
208 | bundle-init | 208 | bundle-init |