forked from zdharma-continuum/zinit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
Β·327 lines (271 loc) Β· 8.83 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env bash
{ # Colors
COLOR_RESET='[0m'
COLOR_BOLD_RED='[1;31m'
COLOR_BOLD_GREEN='[1;32m'
COLOR_BOLD_YELLOW='[1;33m'
COLOR_BOLD_BLUE='[1;34m'
COLOR_BOLD_MAGENTA='[1;35m'
COLOR_BOLD_CYAN='[1;36m'
# The over-the-top fancy ones
COLOR_PALE_MAGENTA='[38;5;177m'
COLOR_BOLD_WHITE_ON_BLACK='[1;37;40m'
}
echo_fancy() {
emoji="$1"
color="$2"
shift 2
msg=""
# prepend emoji, unless NO_EMOJI is set
if [ -z "$NO_EMOJI" ]; then
msg="$emoji"
fi
# wrap every word in color (needed in case there are custom colors in
# the message itself), unless NO_COLOR is set
for str in "$@"; do
# FIXME: NO_COLOR only applies if there are no colors in the msg
if [ -z "$NO_COLOR" ]; then
msg="${msg}${color}"
fi
msg="${msg}${str}"
done
# Actual output
echo "${msg}${COLOR_RESET}" >&2
# fake "local" vars
unset emoji color str msg
}
echo_info() {
echo_fancy "π΅" "${COLOR_BOLD_BLUE}" "INFO: ${*}"
}
echo_success() {
echo_fancy "β
" "${COLOR_BOLD_GREEN}" "SUCCESS: ${*}"
}
echo_warning() {
echo_fancy "π§" "${COLOR_BOLD_YELLOW}" "WARNING: ${*}"
}
echo_error() {
echo_fancy "β" "${COLOR_BOLD_RED}" "ERROR: ${*}"
}
check_dependencies() {
zsh_min_version=5.5
if ! zsh -sfc \
'autoload is-at-least;
is-at-least $1 $ZSH_VERSION' "$zsh_min_version"; then
echo_warning "ZSH version 5.5+ is recommended for zinit." \
"It'll still work, but be warned."
fi
if ! command -v git > /dev/null 2>&1; then
echo_error "${COLOR_BOLD_GREEN}git${COLOR_RESET} is not installed"
exit 1
fi
unset zsh_min_version
}
show_environment() {
echo_info "About to setup zinit from $ZINIT_REPO" \
"(branch: $ZINIT_BRANCH - commit: ${ZINIT_COMMIT:-N/A})" \
"to ${ZINIT_INSTALL_DIR}"
}
create_zinit_home() {
if ! test -d "${ZINIT_HOME}"; then
mkdir -p "${ZINIT_HOME}"
chmod g-w "${ZINIT_HOME}"
chmod o-w "${ZINIT_HOME}"
fi
}
create_zinit_tmpdir() {
# use -d instead of --directory for macos (BSD) compatibility
ZINIT_TMPDIR="$(mktemp -d)"
if [ ! -d "$ZINIT_TMPDIR" ]; then
echo_error "Tempdir creation failed. This ain't good"
exit 1
fi
trap 'rm -rvf "$ZINIT_TMPDIR"' EXIT INT
}
# Get the download-progress bar tool
download_git_output_processor() {
url="https://raw.githubusercontent.com/${ZINIT_REPO}/${ZINIT_COMMIT:-${ZINIT_BRANCH}}/share/git-process-output.zsh"
script_path="${ZINIT_TMPDIR}/git-process-output.zsh"
echo_info "Fetching git-process-output.zsh from $url"
if command -v curl > /dev/null 2>&1; then
curl -fsSL -o "$script_path" "$url"
elif command -v wget > /dev/null 2>&1; then
wget -q -O "$script_path" "$url"
fi
# shellcheck disable=2181
if [ "$?" -eq 0 ]; then
chmod a+x "$script_path" 2> /dev/null
echo_success 'Download finished!'
else
echo_warning "Download failed."
fi
unset url script_path
}
zinit_git_exec() {
command git -C "${ZINIT_INSTALL_DIR}" "$@"
}
zinit_checkout_ref() {
ref="${ZINIT_BRANCH}"
git_obj_type="branch"
if [ -n "$ZINIT_COMMIT" ]; then
ref="$ZINIT_COMMIT"
git_obj_type="commit"
fi
if zinit_git_exec checkout "$ref" > /dev/null 2>&1; then
echo_success "Checked out $git_obj_type $ref"
else
echo_error "Failed to check out $git_obj_type $ref"
fi
unset ref git_obj_type
}
zinit_current_version() {
zinit_git_exec describe --tags 2> /dev/null
}
zinit_update() {
cd "${ZINIT_INSTALL_DIR}" || {
echo_error "Failed to cd to ${ZINIT_INSTALL_DIR}"
exit 1
}
echo_info "Updating ${COLOR_BOLD_CYAN}zinit${COLOR_RESET} in" \
"in ${COLOR_BOLD_MAGENTA}${ZINIT_INSTALL_DIR}"
{ # Clean up repo
zinit_git_exec clean -d -f -f
zinit_git_exec reset --hard HEAD
} > /dev/null 2>&1
# fetch our branch (to ensure the target commit exists locally)
zinit_git_exec fetch origin "$ZINIT_BRANCH"
zinit_checkout_ref
if zinit_git_exec pull origin "$ZINIT_BRANCH"; then
echo_success "Updated zinit to $(zinit_current_version)"
fi
}
zinit_install() {
cd "${ZINIT_HOME}" || {
echo_error "Failed to cd to ${ZINIT_HOME}"
exit 1
}
echo_info "Installing ${COLOR_BOLD_CYAN}zinit${COLOR_RESET} to " \
"${COLOR_BOLD_MAGENTA}${ZINIT_INSTALL_DIR}"
{
command git clone --progress --branch "$ZINIT_BRANCH" \
"https://github.com/${ZINIT_REPO}" \
"${ZINIT_REPO_DIR_NAME}" 2>&1 | {
"${ZINIT_TMPDIR}/git-process-output.zsh" || cat
}
} 2> /dev/null
zinit_checkout_ref
if [ -d "${ZINIT_REPO_DIR_NAME}" ]; then
echo_success "Zinit succesfully installed to " \
"${COLOR_BOLD_GREEN}${ZINIT_INSTALL_DIR}"
echo_info "Zinit Version: ${COLOR_BOLD_GREEN}$(zinit_current_version)"
else
echo_error "Failed to install Zinit to ${COLOR_BOLD_YELLOW}${ZINIT_INSTALL_DIR}"
fi
}
# Modify .zshrc
edit_zshrc() {
rc_update=1
if grep -E '(zinit|zplugin)\.zsh' "${ZSHRC}" > /dev/null 2>&1; then
echo_warning "${ZSHRC} already contains zinit commands. Not making any changes."
rc_update=0
fi
if [ $rc_update -eq 1 ]; then
echo_info "Updating ${ZSHRC} (10 lines of code, at the bottom)"
zinit_home_escaped=${ZINIT_HOME//$HOME/\$HOME}
command cat <<- EOF >> "$ZSHRC"
### Added by Zinit's installer
if [[ ! -f ${zinit_home_escaped}/${ZINIT_REPO_DIR_NAME}/zinit.zsh ]]; then
print -P "%F{33} %F{220}Installing %F{33}ZDHARMA-CONTINUUM%F{220} Initiative Plugin Manager (%F{33}${ZINIT_REPO}%F{220})β¦%f"
command mkdir -p "${zinit_home_escaped}" && command chmod g-rwX "${zinit_home_escaped}"
command git clone https://github.com/${ZINIT_REPO} "${zinit_home_escaped}/${ZINIT_REPO_DIR_NAME}" && \\
print -P "%F{33} %F{34}Installation successful.%f%b" || \\
print -P "%F{160} The clone has failed.%f%b"
fi
source "${zinit_home_escaped}/${ZINIT_REPO_DIR_NAME}/zinit.zsh"
autoload -Uz _zinit
(( \${+_comps} )) && _comps[zinit]=_zinit
EOF
fi
unset rc_update zinit_home_escaped
}
query_for_annexes() {
zshrc_annex_file="$(mktemp)"
command cat <<- EOF >> "$zshrc_annex_file"
# Load a few important annexes, without Turbo
# (this is currently required for annexes)
zinit light-mode for \\
zdharma-continuum/zinit-annex-as-monitor \\
zdharma-continuum/zinit-annex-bin-gem-node \\
zdharma-continuum/zinit-annex-patch-dl \\
zdharma-continuum/zinit-annex-rust
EOF
# Ask user if we should add the annexes to their zshrc
# If NO_INPUT is set, but NO_ANNEXES is the annexes bit gets appended to the
# config (ie. default to yes if NO_INPUT, unless NO_ANNEXES)
reply=n
if [ -n "$NO_INPUT" ]; then
[ -z "$NO_ANNEXES" ] && reply=y
else
echo "${COLOR_PALE_MAGENTA}${COLOR_RESET} Would you like to add 4 useful plugins" \
"- the most useful annexes (Zinit extensions that add new" \
"functions-features to the plugin manager) to the zshrc as well?" \
"It will be the following snippet:"
command cat "$zshrc_annex_file"
# shellcheck disable=2059
printf "${COLOR_PALE_MAGENTA}${COLOR_RESET} Enter y/n and press Return: "
read -r reply
fi
if [ "$reply" = y ] || [ "$reply" = Y ]; then
command cat "$zshrc_annex_file" >> "$ZSHRC"
echo_info "Installing annexes"
zsh -ic "@zinit-scheduler burst"
echo_success 'Done!'
else
echo_warning "Skipped the annexes."
fi
command cat <<- EOF >> "$ZSHRC"
### End of Zinit's installer chunk
EOF
unset reply zshrc_annex_file
}
display_tutorial() {
command cat <<- EOF
π» ${COLOR_BOLD_WHITE_ON_BLACK}Welcome!${COLOR_RESET}
Now to get started you can check out the following:
- The ${COLOR_BOLD_WHITE_ON_BLACK}README${COLOR_RESET} section on the ice-modifiers:
π§ https://github.com/${ZINIT_REPO}#ice-modifiers
- There's also an ${COLOR_BOLD_WHITE_ON_BLACK}introduction${COLOR_RESET} to Zinit on the wiki:
π https://zdharma-continuum.github.io/zinit/wiki/INTRODUCTION/
- The ${COLOR_BOLD_WHITE_ON_BLACK}For-Syntax${COLOR_RESET} article on the wiki, which hilights some best practises:
π https://zdharma-continuum.github.io/zinit/wiki/For-Syntax/
π Need help?
- π¬ Get in touch with us on Gitter: https://gitter.im/zdharma-continuum
- π Or on GitHub: https://github.com/zdharma-continuum
EOF
}
# Globals. Can be overridden.
ZINIT_REPO="${ZINIT_REPO:-zdharma-continuum/zinit}"
ZINIT_BRANCH="${ZINIT_BRANCH:-main}"
ZINIT_COMMIT="${ZINIT_COMMIT:-}" # no default value
ZINIT_HOME="${ZINIT_HOME:-${XDG_DATA_HOME:-${HOME}/.local/share}/zinit}"
ZINIT_REPO_DIR_NAME="${ZINIT_REPO_DIR_NAME:-zinit.git}"
ZINIT_INSTALL_DIR=${ZINIT_INSTALL_DIR:-${ZINIT_HOME}/${ZINIT_REPO_DIR_NAME}}
ZSHRC="${ZSHRC:-${ZDOTDIR:-${HOME}}/.zshrc}"
show_environment
check_dependencies
create_zinit_home
create_zinit_tmpdir
download_git_output_processor
if [ -d "${ZINIT_INSTALL_DIR}/.git" ]; then
zinit_update
ZINIT_UPDATE=1
else
zinit_install
fi
if [ -z "$NO_EDIT" ]; then
edit_zshrc
[ -z "$ZINIT_UPDATE" ] && query_for_annexes
fi
if [ -z "$NO_TUTORIAL" ]; then
display_tutorial
fi
# vim: set ft=sh et ts=2 sw=2 :