-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
executable file
·293 lines (232 loc) · 7.87 KB
/
install.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
#!/bin/sh
# shellcheck disable=SC3043
set -eu
##################################
# install.sh is a GENERATED FILE #
##################################
# All changes should be made to installers/install.sh
# and included files therin, as the root one is compiled
# Tells the configuration to not worry
# that it might not find a dotfiles directory, and
# that it should make one instead.
export DOTFILES_INSTALLER=1
# BEGIN included from installers/prelude.sh
# Prelude which includes necessary scripts for the dotfiles installer to run
# BEGIN included from installers/configure.sh
# Initialize DOTFILES and related configuration variables
GITHUB_REPO="${GITHUB_REPO:-alexrudy/dotfiles}"
GIT_BRANCH="main"
export GITHUB_REPO GIT_BRANCH
DOTFILES_INSTALLER=${DOTFILES_INSTALLER:-}
DOTFILES="${DOTFILES:-${HOME}/.dotfiles/}"
if [ "$DOTFILES" = "/" ]; then
DOTFILES="${HOME}/.dotfiles/"
fi
if test -z "${DOTFILES_INSTALLER}"; then
if ! test -d "${DOTFILES}"; then
DOTFILES=$(readlink -f "$(dirname "$0")")
if test "${DOTFILES}" -eq "${HOME}"; then
echo "ERROR: DOTFILES cannot be found."
exit 1
fi
export DOTFILES
fi
fi
NONINTERACTIVE=1
export NONINTERACTIVE
DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND
TERM="${TERM:-dumb}"
export TERM
if test -z "${DOTFILES_INSTALLER}"; then
cd "${DOTFILES}"
fi
# END included from installers/configure.sh
# BEGIN included from installers/functions.sh
# Library of functions useful for installing.
# Everything here should be POSIX sh
# Allow shellcheck to ignore unused functions
LOGFILE="${LOGFILE:-${HOME}/.dotfiles-install.log}"
LEVEL=${LEVEL:-0}
DEBUG=${DEBUG:-0}
_spacer() {
spacer=""
j=0
while [ $j -lt "$LEVEL" ]; do
spacer="$spacer "
j=$(( j + 1 ))
done
echo "$spacer"
}
_log_timestamp() {
date +%H:%M:%S
}
_log_init() {
printf "$(_log_timestamp) [%-10.10s]: %s\n" "init" "$1" > "$LOGFILE"
printf "$(_log_timestamp) [%-10.10s]: %s\n" "init" "$(date)" >> "$LOGFILE"
printf "$(_log_timestamp) [%-10.10s]: %s\n" "init" "installing in ${DOTFILES}" >> "$LOGFILE"
}
_log() {
local message
message=$(echo "$2" | perl -pe's/[[:space:]]*[[:^ascii:]]+[[:space:]]*//' )
printf "$(_log_timestamp) [%-10.10s]: %s\n" "$1" "${message}" >> "$LOGFILE"
}
_print() {
local message color
message="$1"
color="$2"
if [ -t 1 ]; then
printf "$(_spacer)$(tput setaf "$color")%s$(tput sgr0)\n" "$message" 2> /dev/null
else
printf "$(_spacer)%s\n" "$message" 2> /dev/null
fi
}
_debug() {
local message color
message="$*"
_log "debug" "$message"
if [ "$DEBUG" -eq "1" ]; then
_print "$message" "4"
fi
}
_message() {
local message color
message="$*"
color=$(_color_code "$message")
_log "debug" "$message"
_print "$message" "7"
}
_process() {
message="$*"
_log "start(${LEVEL})" "$message"
_print "$message" "7"
LEVEL=$(( LEVEL + 1))
trap '_cleanup "$message"' EXIT
}
_finished() {
message="$*"
LEVEL=$(( LEVEL - 1))
color=$(_color_code "$message")
_log "finish(${LEVEL})" "$message"
_print "$message" "$color"
trap - EXIT
}
_error() {
message="$*"
LEVEL=$(( LEVEL - 1))
_log "error" "$message"
_print "$message" "1"
}
_cleanup() {
_error "⛔️ Install step $1 encountered an error"
}
_color_code() {
case "$*" in
✅*)
echo 2;;
⚠️*)
echo 3;;
⛔️*)
echo 1;;
❌*)
echo 1;;
*)
echo 7;;
esac
}
command_exists () {
type "$1" > /dev/null 2>&1
}
# END included from installers/functions.sh
# END included from installers/prelude.sh
install_dotfiles() {
_log_init "$0"
# BEGIN included from installers/download.sh
download_dotfiles() {
_process "📦 Acquiring Dotfiles"
if test -d "${DOTFILES}/.git" ; then
# BEGIN included from installers/downloaders/download-git-pull.sh
# Already included installers/prelude.sh
# shellcheck source=installers/prelude.sh
download_git_pull() {
if test -d "${DOTFILES}" ; then
if command_exists git; then
_message "🐙 Pull latest dotfiles from github"
if git -C "$DOTFILES" pull --quiet --recurse-submodules > /dev/null 2>&1 ; then
_message "🐙 Updated dotfiles git repo"
else
# Not a hard failure
_message "⚠️ Failed to update git repo"
fi
fi
else
exit 1
fi
}
download_git_pull
# END included from installers/downloaders/download-git-pull.sh
_finished "✅ ${DOTFILES} exists."
else
if command_exists git; then
# BEGIN included from installers/downloaders/download-git-clone.sh
# Already included installers/prelude.sh
# shellcheck source=installers/prelude.sh
download_git_clone() {
if command_exists git; then
_process "🐙 cloning ${GITHUB_REPO} from github"
git clone --recursive "https://github.com/${GITHUB_REPO}.git" "${DOTFILES}"
else
exit 1
fi
}
download_git_clone
# END included from installers/downloaders/download-git-clone.sh
_finished "✅ ${DOTFILES} cloned"
else
_message "⚠️ command git not found - falling back to tarball"
# BEGIN included from installers/downloaders/download-tarball.sh
# Already included installers/prelude.sh
# shellcheck source=installers/prelude.sh
download_tarball() {
if ! command_exists curl; then
if command_exists apt-get; then
apt-get update -y
apt-get install --no-install-recommends -y curl
else
_message "🛑 can't find git or curl, aborting!"
exit 1
fi
fi
_process "🌍 downloading archive of ${GITHUB_REPO} from github and extracting"
curl -fsLo /tmp/dotfiles.tar.gz "https://github.com/${GITHUB_REPO}/tarball/main"
mkdir -p "${DOTFILES}"
tar -zxf /tmp/dotfiles.tar.gz --strip-components 1 -C "${DOTFILES}"
rm -rf /tmp/dotfiles.tar.gz
_finished "✅ ${DOTFILES} created, repository downloaded and extracted"
}
download_tarball
# END included from installers/downloaders/download-tarball.sh
_finished "✅ ${DOTFILES} downloaded."
fi;
fi;
}
download() {
echo "$(date) [dotfiles] $0 $*" > "$LOGFILE"
echo "$(date) [dotfiles] installing in ${DOTFILES}" >> "$LOGFILE"
export DOWNLOAD=1
# Already included installers/configure.sh
# source=installers/configure.sh
# Already included installers/prelude.sh
# shellcheck source=installers/prelude.sh
_process "🌐 Downloading dotfiles to ${DOTFILES}'"
download_dotfiles
_finished "✅ ${DOTFILES} created, repository downloaded and extracted"
}
download "$@"
# END included from installers/download.sh
# This does not get literally included, so that running an old copy of `install.sh`
# will effectively self-update, grabbing the latest version from here.
# shellcheck source=installers/installer.sh # no-include
. "${DOTFILES}/installers/installer.sh"
}
install_dotfiles "$@"