-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.sh
executable file
·284 lines (243 loc) · 7.31 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
#!/bin/sh
# TODO: make everything support $DOTFILES_VERBOSE
DOTFILES_VERBOSE="${DOTFILES_VERBOSE:-true}"
DOTFILES_REPO="${DOTFILES_REPO:-mwilliammyers/dotfiles}"
DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.config/dotfiles}"
debug() {
if [ "x$DOTFILES_DEBUG" = 'xtrue' ]; then
printf "$(tput bold)${@}$(tput sgr0)\n"
fi
}
info() {
if [ "x$DOTFILES_VERBOSE" = 'xtrue' ]; then
printf "$(tput bold)${@}$(tput sgr0)\n"
fi
}
warn() {
printf "$(tput setaf 3)${@}$(tput sgr0)\n"
}
error() {
>&2 printf "$(tput bold)$(tput setaf 1)${@}$(tput sgr0)\n"
}
die() {
rc="${?}"
error "${@}"
exit "${rc}"
}
is_truthy() {
# TODO: better way to do this in POSIX shell?
case "x$1" in
xTRUE) return 0;;
xtrue) return 0;;
xTrue) return 0;;
xT) return 0;;
xt) return 0;;
xYES) return 0;;
xyes) return 0;;
xYes) return 0;;
xY) return 0;;
xy) return 0;;
x1) return 0;;
*) return 1;;
esac
}
update_package_index() {
if [ -x "$(command -v apt-get)" ]; then
sudo apt-get update
elif [ -x "$(command -v brew)" ]; then
# homebrew doesn't print anything at the beginning
info "Updating Homebrew; this could take a while..."
brew update
elif [ -x "$(command -v dnf)" ]; then
dnf check-update
elif [ -x "$(command -v yum)" ]; then
yum check-update
elif [ -x "$(command -v zypper)" ]; then
sudo zypper refresh
elif [ -x "$(command -v port)" ]; then
sudo port selfupdate
fi
}
# TODO: update first, only if we haven't already
_install_packages() {
if [ -x "$(command -v apt-get)" ]; then
sudo apt-get install -y "${@}"
elif [ -x "$(command -v brew)" ]; then
# TODO: DOTFILES_HOMEBREW_OPTS
# TODO: better way to pass options?
if is_truthy "${DOTFILES_HOMEBREW_CASK}"; then
env HOMEBREW_NO_ANALYTICS=1 HOMEBREW_NO_GITHUB_API=1 \
brew cask install "${@}"
else
env HOMEBREW_NO_ANALYTICS=1 HOMEBREW_NO_GITHUB_API=1 \
brew install "${@}"
fi
# unset DOTFILES_HOMEBREW_OPTS
unset DOTFILES_HOMEBREW_CASK
elif [ -x "$(command -v pacman)" ]; then
sudo pacman -Syu "${@}"
elif [ -x "$(command -v dnf)" ]; then
sudo dnf -y "${@}"
elif [ -x "$(command -v yum)" ]; then
sudo yum -y "${@}"
elif [ -x "$(command -v zypper)" ]; then
sudo zypper install "${@}"
elif [ -x "$(command -v pkg)" ]; then
sudo pkg install "${@}"
elif [ -x "$(command -v pkg_add)" ]; then
pkg_add "${@}"
elif [ -x "$(command -v port)" ]; then
sudo port install "${@}"
elif [ -x "$(command -v emerge)" ]; then
emerge "${@}"
elif [ -x "$(command -v pkgin)" ]; then
pkgin -y install "${@}"
elif [ -x "$(command -v nix-env)" ]; then
nix-env -i "${@}"
else
return 1
fi
}
install_packages() {
if [ $# -ne 0 ] && [ "x$@" != 'x' ]; then
info "Installing ${@}..."
if _install_packages $@; then
info "Installed ${@}"
else
warn "Could not install ${@}"
fi
fi
}
command_is_executable() {
if [ -x "$(command -v ${1})" ]; then
debug "${1} is already installed; skipping..."
return 0
else
return 1
fi
}
install_packages_if_necessary() {
packages=""
# echo "$@" | tr ' ' '\n' | while read package; do
for package in $@; do
command_is_executable "$package" || packages="$package $packages"
done
install_packages "$packages"
}
# TODO: make this smarter
package_exists() {
if [ -x "$(command -v apt-cache)" ]; then
sudo apt-cache show "${@}" 2>/dev/null >/dev/null
elif [ -x "$(command -v brew)" ]; then
# we don't need this info and it is faster without it
env HOMEBREW_NO_ANALYTICS=1 HOMEBREW_NO_GITHUB_API=1 \
brew info "${@}" 2>/dev/null >/dev/null
else
return 0
fi
}
# TODO: add suport with other OSes
try_add_apt_repository() {
if grep -q "ID=ubuntu" /etc/os-release 2>/dev/null; then
if ! [ -x "$(command -v add-apt-repository)" ]; then
sudo apt-get install -y software-properties-common
fi
sudo add-apt-repository -y "${@}"
sudo apt-get update
return 0
fi
return 1
}
# TODO: test for sudo without trying it first without sudo (pip will install to ~/.local)?
safe_pip3() {
sudo -H pip3 install -U "${@}"
}
# TODO: is this the best way to detect if we need sudo?
safe_npm_global() {
npm install -g "${@}" || sudo -H npm install -g "${@}"
}
configure_single_package() {
source_config_dir="$1"
dest_config_dir="$2"
package=$(basename $1)
info "Configuring $package"
mkdir -p "$dest_config_dir" 2> /dev/null
for source_file in "$source_config_dir"/*; do
dest_file="$dest_config_dir"/$(basename "$source_file")
if [ -e "$dest_file" ]; then
warn "Overwriting existing configuration file: $dest_file"
rm -ri "$dest_file"
fi
ln -sv "$source_file" "$dest_file"
done
}
os_copy_to_clipboard() {
info "Attempting to copy to system clipboard..."
if [ -x "$(command -v pbcopy)" ]; then
printf "%s" "$1" | pbcopy
elif [ -x "$(command -v xsel)" ]; then
printf "%s" "$1" | xsel -ib
else
warn "Could not copy to system clipboard"
fi
}
os_open() {
info "Attempting to open: $1"
if [ -x "$(command -v xdg-open)" ]; then
xdg-open "$1"
elif [ -x "$(command -v open)" ]; then
open "$1"
else
warn "Could not find suitable program to open: $1"
fi
}
git_pull_or_clone() {
git -C "${2}" config --get remote.origin.url 2>/dev/null | grep -q "${DOTFILES_REPO}"
if [ "${?}" -eq 0 ]; then
git -C "${2}" pull --ff-only --depth=1
else
# Do not use recursive to avoid:
# `Fetched in submodule path <path> but it did not contain <hash>. Direct fetching of that commit failed.`
git clone "${1}" "${2}" --depth=1
fi
}
# install prerequisites
if [ "$(uname -s)" = "Darwin" ]; then
xcode-select --install 2> /dev/null
if ! [ -x "$(command -v brew)" ]; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
fi
install_packages_if_necessary "git" "curl" >> /dev/null
# bootstrap!
if is_truthy "${DOTFILES_BOOTSTRAP:-1}"; then
git_pull_or_clone "https://github.com/${DOTFILES_REPO}.git" "${DOTFILES_DIR}" \
|| die "dotfiles must be up to date"
cd "${DOTFILES_DIR}" || die "Could not find dotfiles directory"
chmod u+x ./*.sh
./google-cloud-sdk.sh
./ssh.sh
./fish.sh
./git.sh
./fd.sh
./ripgrep.sh
install_packages_if_necessary "rsync" "python3" "fzf" "bat" "exa"
install_packages "git-delta"
./nodejs.sh
./neovim.sh
# ./sublime-text.sh
./docker.sh
if [ "$(uname -s)" = "Darwin" ]; then
# TODO: add support for other platforms for docker
DOTFILES_HOMEBREW_CASK=true install_packages_if_necessary \
google-chrome \
slack \
appcleaner
install_packages_if_necessary "trash"
./iterm2.sh
fi
# TODO: these take forever...
./rust.sh
# ./musl-cross.sh
./system.sh
fi