-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbootstrap.sh
executable file
·450 lines (381 loc) · 13 KB
/
bootstrap.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/bin/bash
os=$(uname -s | tr '[:upper:]' '[:lower:]' | sed 's,^darwin$,macos,') # macos or linux
bitness=$(getconf LONG_BIT) # 64 or 32
distro="$os-$(uname -m)"
short_arch=$(uname -m | sed 's,^x86_64$,x64,') # arm64 or x64
short_distro="$os-$short_arch"
install_from_package_manager() {
if declare -f "install_$1_package" >/dev/null; then
"install_$1_package" "$2"
elif command -v brew &>/dev/null; then
brew install "$1"
elif command -v pacman; then
sudo pacman -S --noconfirm "$1"
elif command -v dnf; then
sudo dnf install -y "$1"
elif command -v apt-get; then
sudo apt-get install -y "$1"
else
exit 1
fi
}
package_manager_semver() {
if command -v brew &>/dev/null; then
(brew info "$1" | head -n1 | parse_semver | head -n1) || (echo "package $1 not found in brew" && return 1)
elif command -v pacman &>/dev/null; then
pacman -Qi "$1" | parse_semver || (echo "package $1 not found in pacman" && return 1)
elif command -v dnf &>/dev/null; then
(dnf info "$1" 2>/dev/null | grep "^Version" | parse_semver | head -n1) || (echo "package $1 not found in dnf" && return 1)
elif command -v apt-get &>/dev/null; then
(apt-cache show "$1" 2>/dev/null | head -n1 | parse_semver) || (echo "package $1 not found in apt-get" && return 1)
else
exit 1
fi
}
package_semver() {
if declare -f "$1_package_semver" >/dev/null; then
"$1_package_semver" || (echo "Failed to get semver for based on custom script: $1" >&2 && return 1)
else
package_manager_semver "$1" || (echo "Failed to get semver for: $1" >&2 && return 1)
fi
}
install_fd_package() {
if command -v brew &>/dev/null; then
brew install fd
elif command -v pacman; then
sudo pacman -S --noconfirm fd
elif command -v dnf; then
sudo dnf install -y fd-find
elif command -v apt-get; then
sudo apt-get install -y fd-find
else
exit 1
fi
}
fd_package_semver() {
if command -v brew &>/dev/null; then
(brew info fd | head -n1 | parse_semver | head -n1) || (echo "package fd not found in brew" && return 1)
elif command -v pacman &>/dev/null; then
pacman -Qi fd | parse_semver || (echo "package fd not found in pacman" && return 1)
elif command -v dnf &>/dev/null; then
(dnf info fd-find 2>/dev/null | grep "^Version" | parse_semver | head -n1) || (echo "package fd not found in dnf" && return 1)
elif command -v apt-get &>/dev/null; then
(apt-cache show fd-find 2>/dev/null | head -n1 | parse_semver) || (echo "package fd not found in apt-get" && return 1)
else
exit 1
fi
}
install_explicitly() {
if declare -f "install_$1_from_release" >/dev/null; then
echo "Installing $1 $2 from release"
"install_$1_from_release" "$2"
elif declare -f "install_$1_from_source" >/dev/null; then
echo "Installing $1 $2 from source"
"install_$1_from_source" "$2"
else
echo "Unable to install: $1" >&2
exit 1
fi
}
install_package() {
local package=$1
local version=$2
get_package_semver=$(package_semver "$package")
local package_version="$get_package_semver"
if [ -z "$package_version" ]; then
echo "$package is not found in package manager"
elif semver_ge "$package_version" "$version"; then
echo "$package $package_version is available in package manager"
install_from_package_manager "$package" || (echo "Failed to install: $package" && exit 1)
else
echo "$package needs to be explicitly upgraded '$package_version' < '$version'"
fi
# Otherwise, install the package explicitly
install_explicitly "$package" "$version" || (echo "Failed to install: $package" && exit 1)
}
install_gh_package() {
sudo dnf install 'dnf-command(config-manager)'
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
sudo dnf install gh --repo gh-cli
}
semver_ge() {
local installed_version="$1"
local required_version="$2"
# Convert versions to arrays
IFS='.' read -r -a installed <<<"$installed_version"
IFS='.' read -r -a required <<<"$required_version"
# Ensure each version has three components
for i in {0..2}; do
installed[i]=$((10#${installed[i]:-0})) # Convert to decimal to handle leading zeros
required[i]=$((10#${required[i]:-0}))
done
# Compare versions
for i in {0..2}; do
if ((installed[i] > required[i])); then
return 0
elif ((installed[i] < required[i])); then
return 1
fi
done
return 0 # Versions are equal
}
install_glibc() {
local version=$1
# Download glibc 2.29
curl -fLO "https://ftp.gnu.org/gnu/glibc/glibc-$version.tar.gz"
# Download glibc 2.29 checksum
curl -fLO "https://ftp.gnu.org/gnu/glibc/glibc-$version.tar.gz.sig"
# Import the glibc PGP key
# gpg --keyserver keys.gnupg.net --recv-keys 16792B4EA25340F8
# PGP verify the checksum
gpg --verify "glibc-$version.tar.gz.sig"
tar -zxvf glibc-"$version".tar.gz
mkdir -p glibc-"$version"/build
cd glibc-"$version"/build || exit 1
../configure --prefix=/opt/glibc
make
sudo make install
}
install_neovim_from_source() {
if [ ! -d ~/lib/neovim ]; then
git clone https://github.com/neovim/neovim.git ~/lib/neovim --depth 1
fi
git -C ~/lib/neovim fetch --tags --force --prune
git -C ~/lib/neovim checkout "v$1"
cd ~/lib/neovim || exit 1
make install CMAKE_BUILD_TYPE=Release CMAKE_INSTALL_PREFIX="$HOME/.local"
}
# WARN: installing glibc makes this more like a dentist visit
# install_neovim_from_release() {
# # Pull latest neovim binary from the latest github release
# curl -fLO "https://github.com/neovim/neovim/releases/latest/download/nvim-$distro.tar.gz"
# # Also download the checksum file
# curl -fLO "https://github.com/neovim/neovim/releases/latest/download/nvim-$distro.tar.gz.sha256sum"
# # Verify the checksum
# sha256sum -c "nvim-$distro.tar.gz.sha256sum"
#
# # Extract the binary
# tar xzf "nvim-$distro.tar.gz"
#
# # Link the binary to ~/.local/bin
# ln -fs "$(pwd)/nvim-$distro/bin/nvim" "$HOME/.local/bin/nvim"
#
# if [ "$os" = "linux" ]; then
# # If bison isn't available, install it
# if ! command -v bison &>/dev/null; then
# install_package bison
# fi
#
# # install glibc if not installed or if version is not 2.29
# if getconf GNU_LIBC_VERSION | grep -q "glibc 2.29"; then
# echo "glibc is installed"
# else
# install_glibc "2.29"
# fi
# fi
# }
parse_semver() {
grep -Eo '[0-9]+\.[0-9]+(\.[0-9]+)?'
}
current_heuristic_semver() {
("$1" --version | parse_semver | head -n1) 2>/dev/null || return 1
}
neovim_current_semver() {
nvim --version 2>/dev/null | head -n1 | parse_semver | head -n1
}
ripgrep_current_semver() {
rg --version 2>/dev/null | head -n1 | parse_semver | head -n1
}
skim_current_semver() {
sk --version 2>/dev/null | parse_semver
}
git-delta_current_semver() {
delta --version 2>/dev/null | parse_semver
}
go_current_semver() {
go version | parse_semver | head -n1
}
install_glow_from_release() {
echo '[charm]
name=Charm
baseurl=https://repo.charm.sh/yum/
enabled=1
gpgcheck=1
gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/yum.repos.d/charm.repo
sudo yum install glow -y
}
installed_semver() {
explicit_current_semver "$1" || current_heuristic_semver "$1"
}
explicit_current_semver() {
if declare -f "$1_current_semver" >/dev/null; then
"$1_current_semver"
else
return 1
fi
}
install_package_version() {
local package=$1
local min_version=$2
local preferred_version=${3:-$min_version}
get_installed_semver=$(installed_semver "$package")
local current_version=$get_installed_semver
if [ -n "$current_version" ]; then
echo "Package $package is already installed at version $current_version <=> $min_version"
# check if current version is greater than or equal to min version
if semver_ge "$current_version" "$min_version"; then
# if current version is greater than or equal to min version, return
echo "$package $current_version >= $min_version"
return 0
fi
fi
# install package to preferred version
install_package "$package" "$preferred_version"
echo "Installed $package $(installed_semver "$package")"
}
install_fzf_from_source() {
if [ -d ~/lib/fzf ]; then
git -C ~/lib/fzf fetch --tags --force
git -C ~/lib/fzf checkout -f "v$1"
else
git clone --depth 1 https://github.com/junegunn/fzf.git ~/lib/fzf
fi
~/lib/fzf/install --all --no-fish --key-bindings --completion --no-update-rc --xdg
ln -fs ~/lib/fzf/bin/fzf "$HOME/.local/bin/fzf"
}
install_ripgrep_from_release() {
cargo install ripgrep -q --locked --version "$1"
}
install_starship_from_release() {
cargo install starship -q --locked --version "$1"
}
install_skim_from_release() {
cargo install skim -q --locked --version "$1"
}
install_git-delta_from_release() {
cargo install git-delta -q --locked --version "$1"
}
install_bat_from_release() {
cargo install bat -q --locked --version "$1"
}
install_fd_from_release() {
cargo install fd-find -q --locked --version "$1"
}
install_eza_from_release() {
cargo install eza -q --locked --version "$1"
}
install_shfmt_from_release() {
install_package_version go 1.22
go install mvdan.cc/sh/v3/cmd/[email protected]
}
install_node_from_release() {
mkdir -p ~/.nvm
# Download and install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# Download and install Node.js:
nvm install "$1" || (echo "Failed to install node@$1 via nvm" && exit 1)
nvm default use 20
# ~/.profile looks for ~/.nvm/alias/default and adds it to the PATH
source "$HOME"/.profile
}
install_stylua_from_release() {
npm install -g "@johnnymorganz/stylua-bin@$1"
}
install_bash-language-server_from_release() {
install_package_version node 20
npm install -g "bash-language-server@$1"
}
install_jq_from_release() {
curl -sfL "https://github.com/jqlang/jq/releases/download/jq-$1/jq-$os$bitness" -o /tmp/jq
curl -sfL "https://github.com/jqlang/jq/releases/download/jq-$1/sha256sum.txt" -o /tmp/jq-sha256sum.txt
sha256sum --quiet --strict --ignore-missing -c /tmp/jq-sha256sum.txt || (echo "Failed to verify jq checksum" && exit 1)
mv -f /tmp/jq "$HOME/.local/bin/jq"
chmod +x "$HOME/.local/bin/jq"
rm -rf /tmp/jq /tmp/jq-sha256sum /tmp/jq-sha256sum.txt
}
install_lua-language-server_from_release() {
curl -fLO "https://github.com/LuaLS/lua-language-server/releases/download/$1/lua-language-server-$1-$short_distro.tar.gz" --output-dir ~/lib
mkdir -p "$HOME/lib/lua-language-server-$1"
tar -zxf "$HOME/lib/lua-language-server-$1-$short_distro.tar.gz" -C "$HOME/lib/lua-language-server-$1"
ln -fs ~/lib/lua-language-server-"$1"/bin/lua-language-server "$HOME/.local/bin/lua-language-server"
}
install_typescript-language-server_from_release() {
install_package_version node 20
npm install -g typescript-language-server@"$1"
}
install_rust-analyzer_from_release() {
rustup component add rust-analyzer
}
zsh-autosuggestions_current_semver() {
if command -v brew &>/dev/null; then
ls "$(brew --prefix)/Cellar/zsh-autosuggestions" 3>/dev/null | parse_semver
else
head -n3 "$HOME/.zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh" 2>/dev/null | parse_semver
fi
}
install_gopls_from_release() {
install_package_version go 1.22
go install golang.org/x/tools/gopls@v"$1"
}
gopls_current_semver() {
gopls version 2>/dev/null | parse_semver
}
install_zsh-autosuggestions_from_source() {
if [ ! -d "$HOME/.zsh/plugins/zsh-autosuggestions" ]; then
git clone --depth 1 https://github.com/zsh-users/zsh-autosuggestions.git "$HOME/.zsh/plugins/zsh-autosuggestions"
fi
git -C "$HOME/.zsh/plugins/zsh-autosuggestions" fetch --tags --force
git -C "$HOME/.zsh/plugins/zsh-autosuggestions" checkout -f "v$1"
}
yaml-language-server_current_semver() {
npm info yaml-language-server version | parse_semver
}
install_yaml-language-server_from_release() {
install_package_version node 20
npm install -g yaml-language-server@"$1"
}
install_hexyl_from_release() {
cargo install hexyl -q --locked --version "$1"
}
install_dependencies() {
# install rust
install_package_version cargo 1.84.1
# terminal candy
install_package_version fzf 0.59.0
install_package_version starship 1.22.1
install_package_version atuin 18.4.0
install_package_version skim 0.16.0
install_package_version git-delta 0.18.2
install_package_version glow 2.0.0
install_package_version bat 0.25.0
install_package_version fd 10.2.0
install_package_version eza 0.20.19
install_package_version zsh-autosuggestions 0.7.1
# command candy
install_package_version gh 2.66.0
install_package_version jq 1.7.1
install_package_version ripgrep 14.1.0
install_package_version stylua 2.0.2
# editor
install_package_version neovim 0.10.4
install_package_version shfmt 3.10.0
install_package_version bash-language-server 5.4.3 # bash/sh
install_package_version lua-language-server 3.13.6 # lua
install_package_version rust-analyzer 1.84.1 # rust
install_package_version typescript-language-server 4.3.3 # typescript
install_package_version gopls 0.17.1 # go
install_package_version yaml-language-server 0.16.0 # yaml
# tools
install_package_version hexyl 0.16.0
}
# Detect if the user is running the script directly
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
set -e
if [ -d "$HOME"/.files ]; then
git -C "$HOME"/.files pull
else
git clone --depth 1 [email protected]:lanej/dotfiles.git "$HOME"/.files
fi
make -C "$HOME"/.files
install_dependencies
fi