-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·150 lines (120 loc) · 3.33 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
#!/bin/sh
# Get the absolute path to the directory containing this script.
DOTFILES_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
echo "Running from $DOTFILES_DIR"
# shellcheck source=shell/.config/shell/lib.sh
if ! . "$DOTFILES_DIR/shell/.config/shell/lib.sh"; then
echo "Could not load shell/.config/shell/lib.sh" >&2
exit 1
fi
# Only run sudo if we're not root.
as_root() {
if [ "$(id -u)" -ne "0" ]; then
command sudo "$@"
else
"$@"
fi
}
ensure_file_does_not_exist() {
if [ ! -f "$1" ] || [ -L "$1" ]; then
return 0
fi
if mv "$1" "$1.bak"; then
echo "moved '$1' -> '$1.bak'"
return 0
fi
echo "'$1' could not be moved" >&2
return 1
}
install_packages() {
# Try to initialize Homebrew. This is useful if this script runs
# just after installing Homebrew, but before Homebrew's dotfiles
# are stow'd, so running `brew` will fail.
# shellcheck source=brew/.config/shell/profile.d/10-brew.sh
. "$DOTFILES_DIR/brew/.config/shell/profile.d/10-brew.sh" 2>/dev/null || true
if has winget.exe; then
echo "Installing packages using winget..."
if ! winget.exe import -i winget-packages.jsonc \
--accept-package-agreements --accept-source-agreements; then
echo "winget could not install all packages." >&2
exit 1
fi
echo "Successfully installed packages."
fi
if has brew; then
echo "Installing packages using Homebrew..."
if ! { brew update && brew bundle --file=Brewfile; } then
echo "Homebrew could not install all packages." >&2
exit 1
fi
elif has apt-get; then
echo "Installing packages using apt-get..."
if ! { as_root apt-get update \
&& as_root xargs -a apt-packages.txt apt-get install -y; } then
echo "apt could not install all packages." >&2
exit 1
fi
elif has apk; then
echo "Installing packages using apk..."
if ! { as_root apk update \
&& as_root xargs -a apk-packages.txt apk -v add; } then
echo "apk could not install all packages." >&2
exit 1
fi
else
echo "No package manager found" >&2
exit 1
fi
echo "Successfully installed packages."
return 0
}
stow_shell() {
ensure_file_does_not_exist "$HOME/.profile"
stow -R shell
}
maybe_stow_bash() {
if ! has bash; then
return 0
fi
ensure_file_does_not_exist "$HOME/.bash_profile"
ensure_file_does_not_exist "$HOME/.bashrc"
stow -R bash
}
maybe_stow_brew() {
if ! has brew; then
return 0
fi
stow -R brew
}
maybe_stow_starship() {
if ! has starship; then
return 0
fi
stow -R starship
}
maybe_stow_zsh() {
if ! has zsh; then
return 0
fi
ensure_file_does_not_exist "$HOME/.zshenv"
ensure_file_does_not_exist "$HOME/.zshrc"
stow -R zsh
}
maybe_stow_git() {
if ! has git; then
return 0
fi
ensure_file_does_not_exist "$HOME/.gitconfig"
stow -R git
git update-index --skip-worktree "git/.gitconfig"
git config --local core.hooksPath ".git-hooks/"
}
install_packages
stow_shell
maybe_stow_bash
maybe_stow_brew
maybe_stow_starship
maybe_stow_zsh
maybe_stow_git
echo "Bootstrapping complete"
return 0