-
Notifications
You must be signed in to change notification settings - Fork 1
/
install
executable file
·419 lines (376 loc) · 12.9 KB
/
install
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env bash
PS4='+${BASH_SOURCE}: ${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): } '
set -x
scriptdir=$(realpath $(dirname "$0"))
get_ubuntu_version() {
# Assume "jammy" if unknown due to missing lsb_release package
# (common in Docker containers and debootstrap chroot's)
# All we really care about is "xenial" vs "not xenial".
lsb_release -cs 2>/dev/null || echo 'jammy'
}
mklink() {
local real_src="$(realpath $1)"
local real_dest="$2"
mkdir_verbose $(dirname $real_dest)
if [[ -h "$real_dest" ]]; then
local tmp
if ! tmp="$(readlink -f ${real_dest})"; then
echo Removing broken symlink at ${real_dest}
\rm -f "${real_dest}"
else
:
# real_dest="${tmp}"
fi
else
:
# real_dest="$(realpath --canonicalize-missing $2)"
fi
local relative_src=$(python3 -c "import os.path; print(os.path.relpath('${real_src}', '$(realpath $(dirname ${real_dest}))'))")
if [[ -h "${real_dest}" ]]; then
local existing_src="$(readlink ${real_dest})"
if [[ "${existing_src}" == "${relative_src}" ]]; then
return 0
elif [[ "${opt_force}" == '0' ]]; then
echo "Not overwriting existing symlink: ${real_dest} -> ${existing_src}"
echo " (Would link this to ${relative_src})"
return 1
else
[[ $opt_verbose == 1 ]] && echo rm -f "${real_dest}"
rm -f "${real_dest}"
fi
elif [[ -e "${real_dest}" ]]; then
if [[ "${opt_force}" == '0' ]]; then
echo "Not overwriting existing real file: ${real_dest}"
echo " (Would link this to ${relative_src})"
return 1
else
[[ $opt_verbose == 1 ]] && echo rm -f "${real_dest}"
rm -f "${real_dest}"
fi
fi
[[ $opt_verbose == 1 ]] && echo ln -s "${relative_src}" "${real_dest}"
ln -s "${relative_src}" "${real_dest}"
}
mkdir_verbose() {
dir="$1"
if [[ ! -d "${dir}" ]]; then
[[ $opt_verbose == 1 ]] && echo mkdir -p "${dir}"
mkdir -p "${dir}"
fi
}
lntree() {
src_tree="$1"
dest_tree="$2"
mkdir_verbose "${dest_tree}"
pushd "$src_tree" >/dev/null || return 1
for file in $(find .); do
file="${file#.}"
file="${file#/}"
if [[ -d "${file}" ]]; then
mkdir_verbose "${dest_tree}/${file}"
elif [[ -f "${file}" ]]; then
mklink "${file}" "${dest_tree}/${file}"
fi
done
popd >/dev/null
}
promptyn() {
local prompt="$1"
local default="Y"
local prompt_yn="[Yn]"
if [[ "${2^^}" == N ]]; then
default='N'
prompt_yn="[yN]"
fi
local REPLY=unset
while [[ -n "${REPLY}" && "${REPLY^^}" != [YN] ]]; do
echo
read -p "$prompt $prompt_yn "
done
[[ -z "$REPLY" ]] && REPLY="$default"
[[ "${REPLY^^}" == Y ]] # This expression is the return value
}
# We used to make a symlink pointing from ~/.config/htop/htoprc to our
# revision-controlled version, but because htop gratuitously rewrites this
# file, this caused the hcc-dotfiles workspace to nearly always be dirty. So,
# instead, we just one-time copy our revision-controlled one into place. After
# that, any updates will have to be made manually or by re-running this install
# script.
fixup_htoprc() {
if [[ -r config/.config/htop/htoprc-default ]]; then
# Git rid of symlink left over from the "old way"
if [[ -L ~/.config/htop/htoprc ]]; then
rm ~/.config/htop/htoprc
fi
# Make symlink to "-default" one for convenience of of manual updating:
mklink config/.config/htop/htoprc-default ~/.config/htop/htoprc-default
# But make the active one that htop will keep modifying be a real file:
cp config/.config/htop/htoprc-default ~/.config/htop/htoprc
fi
}
opt_verbose=1
opt_force=0
opt_yes=0
opt_debug=0
opt_eatmydata=''
opt_apt=1
opt_locale=1
opt_nodejs=1
opt_rust=1
opt_fzf=1
opt_nvim=1
opt_nvim_plugins=1
# Transform long options to short ones because getopts builtin is dumb and we don't
# want to rely on external getopt.
for arg in "$@"; do
shift
case "$arg" in
"--force") set -- "$@" "-f" ;;
"--yes") set -- "$@" "-y" ;;
"--eatmydata") set -- "$@" "-e" ;;
"--debug") set -- "$@" "-d" ;;
"--no-apt") set -- "$@" "-a" ;;
"--no-locale") set -- "$@" "-l" ;;
"--no-nodejs") set -- "$@" "-n" ;;
"--no-rust") set -- "$@" "-r" ;;
"--no-fzf") set -- "$@" "-z" ;;
"--no-nvim") set -- "$@" "-m" ;;
"--no-nvim-plugins") set -- "$@" "-M" ;;
"--no-tmux") set -- "$@" "-t" ;; # Note this option is ignored.
"--minimal") set -- "$@" "-a" "-l" "-n" "-r" "-z" "-m" "-M";;
"--help") set -- "$@" "-h" ;;
*) set -- "$@" $arg ;;
esac
done
while getopts "fyedalnrzmMth" opt; do
case ${opt} in
f)
opt_force=1
;;
y)
opt_yes=1
;;
e)
eatmydata true || sudo apt install -y eatmydata
eatmydata true && opt_eatmydata=eatmydata
;;
d)
opt_debug=1
PS4='+${BASH_SOURCE}: ${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): } '
set -x
;;
a)
opt_apt=0
;;
l)
opt_locale=0
;;
n)
opt_nodejs=0
;;
r)
opt_rust=0
;;
z)
opt_fzf=0
;;
m)
opt_nvim=0
;;
M)
opt_nvim_plugins=0
;;
t)
echo "WARNING: The --no-tmux (or -t) option has been removed"
;;
h|\?)
set +x
echo "Usage: $0 options"
echo " -f, --force Force creation of symlinks, overwriting existing"
echo " -y, --yes Answer Yes to all questions"
echo " -e, --eatmydata Run apt installs under eatmydata for speed"
echo " -d, --debug Give debug output (shell set -x)"
echo " --no-apt Do not run any APT commands"
echo " --no-locale Do not set up locales"
echo " --no-nodejs Do not run install Node.js (for this user)"
echo " --no-rust Do not run install rust & cargo (for this user)"
echo " --no-fzf Do not install or upgrade fzf"
echo " --no-nvim Do not run install Nvim"
echo " --no-nvim-plugins Do not run install Nvim plugins"
echo " --minimal Equiv. to --no-apt --no-locale --no-nodejs --no-rust --no-fzf --no-nvim --no-nvim-plugins"
echo " -h, --help Print this message"
exit 1
esac
done
is_yes() {
[[ "$opt_yes" == "1" ]]
}
required_packages_common=(
apt-utils
locales
git
curl
fuse # Needed by nvim, since it is an AppImage
golang # Needed by jqls nvim language server
python3-dev
python3-pip
python3-virtualenv
python3-venv # Needed by various nvim language servers
python3-setuptools
ripgrep # Needed by nvim Telescope grep
tmux
zsh
)
# NOTE: These packages do not exist on focal & later
required_packages_xenial=(
python-dev
python-pip
python-virtualenv
python-setuptools
)
do_install_packages() {
apt list -qq "$@"
echo
if is_yes || promptyn 'Install any packages in the above list that are missing?' Y; then
sudo ${opt_eatmydata} apt install -y "$@"
fi
}
install_packages() {
sudo ${opt_eatmydata} apt update
[[ -n ${opt_eatmydata} ]] && sync
do_install_packages "${required_packages_common[@]}"
ubuntu_ver=$(get_ubuntu_version)
[[ $ubuntu_ver == "xenial" ]] && install_packages "${required_packages_xenial[@]}"
[[ -n ${opt_eatmydata} ]] && sync
}
setup_tmux() {
# Upgrade path from old versions of hcc-dotfiles, which made ~/.tmux be a symlink into
# this repo. Now we make it be a real dir, and symlink the individual files therein.
# This is done so that the stuff that tpm plugin manager downloads won't be inside
# this repo's workspace.
if [[ -L ~/.tmux ]]; then
rm ~/.tmux
fi
mkdir -p ~/.tmux
local f
for f in config/.tmux/* ; do
mklink $f ~/.tmux/${f#config/.tmux/}
done
mklink config/.tmux.conf ~/.tmux.conf
# Install or upgrade TPM (tmux plugin manager) and install configured plugins headlessly
local tpm_revision=v3.1.0
mkdir -p ~/.tmux/plugins
if [[ -e ~/tmux/plugins/tpm/.git ]]; then
( cd ~/.tmux/plugins/tpm; git fetch; git checkout ${tpm_revision} )
else
git clone -b ${tpm_revision} https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
fi
TMUX= ~/.tmux/plugins/tpm/bin/install_plugins
}
cd "${scriptdir}"
[[ $opt_apt -eq 1 ]] && install_packages
setup_locale() {
sudo locale-gen en_US.UTF-8
sudo update-locale LANG=en_US.UTF-8
}
[[ $opt_locale -eq 1 ]] && setup_locale
git submodule update --init --recursive
mklink config/.zsh ~/.zsh
mklink config/.zshrc ~/.zshrc
mklink bin ~/bin
mklink config/.gvimrc ~/.gvimrc
mklink config/.vimrc ~/.vimrc
mklink config/.vim ~/.vim
mklink config/.vim ~/.config/nvim # Neovim will share .vim config
mklink config/.tmux.conf ~/.tmux.conf
mklink .fonts ~/.fonts
mklink config/.terminfo ~/.terminfo
mklink config/.gitconfig ~/.gitconfig
echo
setup_tmux
fixup_htoprc
# Only needed for xenial
upgrade_pip() {
# Last version of pip that runs in Python 2.7 and 3.5
pip install --user --upgrade pip==20.3.4
pip3 install --user --upgrade pip==20.3.4
hash -r # Teach this shell about the new pip
}
inst_nodejs() {
mkdir -p ~/.local
local script_name=$(mktemp /tmp/nodejs-install-script.XXXXXX)
curl -Ls install-node.now.sh/lts >> ${script_name}
chmod +x ${script_name}
local prefix=~/.local # Put in temp variable to cause tilde expansion
${script_name} --prefix=${prefix} --yes
}
inst_rust() {
local script_name=$(mktemp /tmp/rust-install-script.XXXXXX)
curl -Ls https://sh.rustup.rs >> ${script_name}
chmod +x ${script_name}
${script_name} -y
# Make sure that THIS shell has cargo in $PATH so that nvim setup can work below.
source ~/.cargo/env
}
inst_or_upgrade_fzf() {
if [[ -d ~/.fzf ]]; then
echo "FZF is already installed. Upgrading it."
(cd ~/.fzf && git pull)
else
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
fi
~/.fzf/install --all
}
inst_nvim() {
local neovim_version=stable
echo Installing Neovim ${neovim_version} AppImage in ~/.local/bin
mkdir -p ~/.local/bin/
curl -L -o ~/.local/bin/nvim-${neovim_version}.appimage \
"https://github.com/neovim/neovim/releases/download/${neovim_version}/nvim.appimage"
chmod +x ~/.local/bin/nvim-${neovim_version}.appimage
ln -s nvim-${neovim_version}.appimage ~/.local/bin/nvim
}
inst_neovim_remote() {
# Since this not used by nvim itself but by the user in a shell, put this
# in ~/.local rather than a Python virtenv
pip3 install --user wheel
pip3 install --user neovim-remote
}
zsh_first_run() {
echo "Running zsh to complete one-time setup. May produce messages that"
echo "Look like errors, but you can ignore these."
echo
touch ~/.z # Keep .fzf from complaining
NO_SSH_AGENT=1 zsh -i -c exit
}
# After upgrading pip in ~/.local/bin, we want to use it, so make sure the
# .local dirs are in the path for this script
export PATH=~/bin:~/.local/usr/local/bin:~/.local/usr/bin:~/.local/bin:$PATH
zsh_first_run
if [[ $ubuntu_ver == "xenial" ]]; then
if is_yes || promptyn "Do you want to upgrade pip (recommended for Ubuntu 16.04 xenial)?" Y; then
upgrade_pip
fi
fi
if [[ $opt_nodejs == 1 ]] && (is_yes || promptyn 'Do you want to install node.js in ~/.local (Needed by various Neovim plugins)' Y); then
inst_nodejs
fi
if [[ $opt_rust == 1 ]] && (is_yes || promptyn 'Do you want to install rust for this user (Needed by various Neovim plugins)' Y); then
inst_rust
fi
if [[ $opt_fzf == 1 ]] && (is_yes || promptyn "Do you want to install (or upgrade) fzf?" Y); then
inst_or_upgrade_fzf
fi
if [[ $opt_nvim == 1 ]] && (is_yes || promptyn 'Do you want to install Neovim AppImage?' Y); then
inst_nvim
inst_neovim_remote
fi
if [[ $opt_nvim_plugins == 1 ]] && (is_yes || promptyn 'Will now launch Neovim in headless mode to install plugins. Proceed?' Y); then
if ! which nvr; then # Might have just done this in the $opt_nvim case
inst_neovim_remote
fi
nvim --headless -c InstallAllPluginsSync -c qa
# nvim typically exits with status 1 even though it succeeded.
# Prevent whole script from exiting with failure status:
true
fi