-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudo.builder.sh
More file actions
executable file
·328 lines (284 loc) · 9.81 KB
/
Copy pathsudo.builder.sh
File metadata and controls
executable file
·328 lines (284 loc) · 9.81 KB
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
#!/bin/bash
set -e
## Ubuntu dependency installer. Use sudo.builder.macos.zsh on macOS.
## Available modes:
## - user (interactive installation)
## - docker (non-interactive, not all packages, implies root privileges)
## - auto (non-interactive, all packages)
_get_latest_tag() {
local repo=$1
curl --silent "https://api.github.com/repos/$repo/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"tag_name": *"([^"]+)".*/\1/'
}
_extract_exe_to_bin() {
local tar_archive=$1
local executable=$2
local tmp_dir=/tmp/$(uuidgen)
mkdir -p "$tmp_dir"
mv "$tar_archive" "$tmp_dir"
tar -xf "$tmp_dir/$tar_archive" -C "$tmp_dir" --strip-components=1 &&
cp "$tmp_dir/$executable" "$_HOME/.local/bin"
}
_install_with_apt() {
$_sudo apt-get -qq update
eval "$sudo_env $_sudo apt-get install -y $*"
}
ask_user() {
local yes_or_no_question=$1 default_answer=${2:-y} prompt
case $default_answer in
y | Y) prompt='[Yn]'; REPLY='y' ;;
n | N) prompt='[yN]'; REPLY='n' ;;
*) echo "Invalid default answer: $default_answer"; return 1 ;;
esac
if [[ $_MODE = user ]]; then
read -rn1 -p "$yes_or_no_question $prompt "
if [[ $REPLY =~ [yYnN] ]]; then
echo >&2
elif [[ -z $REPLY ]]; then
REPLY=$default_answer
echo >&2
else
>&2 echo -e "
Invalid input: $REPLY
Please, type 'y' or 'n'"
ask_user "$yes_or_no_question" "$default_answer"
fi
fi
}
select_packages() {
local package default_answer selected=()
for package in "$@"; do
[[ -n $package ]] || continue
default_answer=y
[[ $package = gitui ]] && default_answer=n
ask_user "Install $package?" "$default_answer"
[[ $REPLY =~ [yY] ]] && selected+=("$package")
done
printf '%s\n' "${selected[@]}"
}
select_default_packages() {
local package selected=()
for package in "$@"; do
[[ -n $package ]] || continue
[[ $package = gitui ]] && continue
selected+=("$package")
done
printf '%s\n' "${selected[@]}"
}
display_packages() {
local package
for package in "$@"; do
if [[ $package = gitui ]]; then
printf ' %s (default: no)\n' "$package"
else
printf ' %s\n' "$package"
fi
done
}
install() {
local sudo_env _sudo=sudo pip_opts=-U
local _MODE=${1:-user} _HOME=${2:-~}
## Check mode is valid
if [[ $_MODE != user && $_MODE != docker && $_MODE != auto ]]; then
echo -e "Invalid mode: $_MODE\nValid modes are 'user', 'docker', 'auto'"
return 1
fi
if [[ $_MODE != user ]]; then
pip_opts=--no-cache-dir
sudo_env=DEBIAN_FRONTEND=noninteractive
fi
if [[ $_MODE = docker ]]; then
_sudo=
fi
local apt_packages selected_packages
declare -a apt_package_array
mapfile -t apt_package_array < <(
sed -e 's;\(.*\)\(#.*\);\1;' apt.requirements.txt |
awk '{$1=$1}; NF {print}'
)
echo 'The root privileges are required to install system packages:'
display_packages "${apt_package_array[@]}"
if [[ $_MODE = user ]]; then
ask_user 'Install the default package set?'
if [[ $REPLY =~ [yY] ]]; then
mapfile -t selected_packages < <(select_default_packages "${apt_package_array[@]}")
else
ask_user 'Choose packages one by one?' n
if [[ $REPLY =~ [yY] ]]; then
mapfile -t selected_packages < <(select_packages "${apt_package_array[@]}")
else
selected_packages=()
fi
fi
else
echo 'Installing default package set.'
mapfile -t selected_packages < <(select_default_packages "${apt_package_array[@]}")
fi
apt_packages="${selected_packages[*]}"
if [[ -n $apt_packages ]]; then
_install_with_apt "$apt_packages"
else
echo 'No apt packages selected.'
fi
ask_user 'Install zsh (a more user friendly shell)?'
if [[ $REPLY =~ [yY] ]]; then
_install_with_apt zsh
fi
ask_user 'Install miniconda to create Python virtual envs?' n
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
curl -o miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash miniconda.sh -ubp "$HOME/miniconda"
## -b = batch mode (non-interactive),
## -u = update (ignore if installed already)
## -p <PREFIX> = installation prefix
"$HOME/miniconda/bin/conda" init ${SHELL##*/}
exec ${SHELL##*/}
fi
ask_user "Install Neovim's AppImage?"
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
local link=https://github.com/neovim/neovim/releases/latest/download/nvim-linux-x86_64.appimage
local dl_path="$_HOME/.local/bin"
mkdir -p "$dl_path"
curl -L $link -o "$dl_path/nvim"
chmod +x "$dl_path/nvim"
## No longer needed as we have ~/.local/bin on PATH
# $_sudo ln -sf "$dl_path/nvim" /usr/bin/nvim
fi
if [[ $_MODE = docker || $REPLY =~ [nN] ]]; then
ask_user 'Install Neovim from source?'
if [[ $REPLY =~ [yY] ]]; then
## NOTE: `pkg-config automake libtool-bin gettext`
## might be removed after the installation.
git clone https://github.com/neovim/neovim &&
cd neovim && git checkout stable &&
make CMAKE_BUILD_TYPE=Release &&
make install &&
cd .. && rm -rf neovim
fi
fi
ask_user 'Install Nerd fonts?'
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
local default=FiraCode
if [[ $_MODE != docker ]]; then
read -rp "Input fonts name [$default]:" FONTS
fi
local tag
tag=$(_get_latest_tag ryanoasis/nerd-fonts)
echo "Downloading ${FONTS:=$default} fonts.."
wget -nc "https://github.com/ryanoasis/nerd-fonts/releases/download/$tag/${FONTS:=$default}.zip"
unzip "$FONTS.zip" -d ~/.fonts
# fc-cache -fv # not sure it is helpful
fi
ask_user 'Install Node.js and npm?'
if [[ $REPLY =~ [yY] ]]; then
local tmp_dir
tmp_dir=/tmp/$(uuidgen)
mkdir -p "$tmp_dir" && cd "$tmp_dir" &&
wget -r -nd -A gz --accept-regex='node-.*-linux-x64\.tar\.gz' \
https://nodejs.org/download/release/latest/ &&
tar xf node* --strip-components=1 &&
mkdir -p "$_HOME/.local" &&
cp -r bin lib share "$_HOME/.local/" &&
# Configure npm to use local directories (no sudo needed)
mkdir -p "$_HOME/.local/share/npm-global" &&
"$_HOME/.local/bin/npm" config set prefix "$_HOME/.local/share/npm-global"
if [[ ":$PATH:" == *":$_HOME/.local/bin:"* ]] &&
[[ ":$PATH:" == *":$_HOME/.local/share/npm-global/bin:"* ]]; then
echo "Node.js and npm installed locally. PATH already configured."
fi
local shell_config=""
case "${SHELL##*/}" in
zsh)
shell_config="${ZDOTDIR:-$_HOME}/.zshrc"
;;
bash)
shell_config="$_HOME/.bashrc"
[[ ! -f "$shell_config" && -f "$_HOME/.bash_profile" ]] && {
shell_config="$_HOME/.bash_profile"
}
;;
*)
>&2 echo "Unsupported shell: $SHELL."
echo -n "Please, add \$HOME/.local/bin and"
echo " \$HOME/.local/share/npm-global/bin to your PATH manually."
return 1
;;
esac
local new_path_dirs="\$HOME/.local/bin:\$HOME/.local/share/npm-global/bin"
if grep -q "^\\(export \\)\\?PATH=" "$shell_config"; then
sed -i.bak "s|^\\(export \\)\\?PATH=|\\1PATH=\"$new_path_dirs:\"|" "$shell_config"
echo "Updated PATH in $shell_config"
else
{
echo ""
echo "# Added by evangelist installer"
echo "export PATH=\"$new_path_dirs:\$PATH\""
} >>"$shell_config"
echo "Added PATH export to $shell_config"
fi
echo "Node.js and npm installed locally. Restart your shell"
fi
ask_user "Install Neovim's extras?"
if [[ $_MODE = user && $REPLY =~ [yY] ]]; then
[[ -z $(command -v npm) ]] && {
echo 'npm is not installed! Skipping..'
} || {
npm install -g neovim
npm install -g tree-sitter-cli
pip3 install $pip_opts neovim
}
## Go
# local latest
# latest=$(
# curl -s "https://go.dev/dl/?mode=json" |
# grep -Po '"filename":\s*"\Kgo[0-9.]+linux-amd64.tar.gz' |
# head -1
# )
# wget -nc https://go.dev/dl/$latest &&
# rm -rf "$HOME/.local/bin/go" &&
# tar -C "$HOME/.local/bin" -xzf go1.23.2.linux-amd64.tar.gz
fi
ask_user "Download ru-dictionary for Neovim's spellchecker?"
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
local folder="${XDG_DATA_HOME:-~/.local/share}/nvim/site/spell"
curl -LO http://ftp.vim.org/vim/runtime/spell/ru.utf-8.spl &&
mkdir -p "$folder" && mv ru.utf-8.spl "$folder"
fi
ask_user 'Set up gitui (fancy git add-ons: UI for git in CLI)?' n
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
local tag
tag=$(_get_latest_tag extrawurst/gitui)
echo TAG $tag
curl -LO "https://github.com/extrawurst/gitui/releases/download/$tag/gitui-linux-x86_64.tar.gz" &&
_extract_exe_to_bin "gitui-linux-x86_64.tar.gz" gitui &&
mkdir -p "$_HOME/.config/gitui" &&
cp conf/git/key_bindings.ron "$_HOME/.config/gitui"
fi
ask_user 'Set up delta for prettier git diff? (works only after `evn install git`)'
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
local tag delta repo=dandavison/delta
tag=$(_get_latest_tag $repo)
delta=delta-$tag-x86_64-unknown-linux-musl
curl -LO "https://github.com/$repo/releases/download/$tag/$delta.tar.gz" &&
_extract_exe_to_bin "$delta.tar.gz" delta
fi
ask_user 'Install croc (p2p file transfer)?'
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
curl https://getcroc.schollz.com | bash
fi
ask_user 'Install Python dependencies?'
if [[ $REPLY =~ [yY] ]]; then
pip3 install $pip_opts -r pip.requirements.txt
fi
ask_user 'Install SafeEyes? To make some interval eye gymnastics'
if [[ $_MODE != docker && $REPLY =~ [yY] ]]; then
$_sudo add-apt-repository -y ppa:safeeyes-team/safeeyes
$_sudo apt-get update && eval $sudo_env $_sudo apt-get install -y safeeyes
fi
ask_user 'Install latexmk to enable VimTex plugin?'
if [[ $_MODE = user && $REPLY =~ [yY] ]]; then
eval $sudo_env $_sudo apt-get install -y latexmk
fi
}
install "$@"