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