Repository navigation
Expand file tree
/
Copy pathinstaller
More file actions
executable file
·252 lines (217 loc) · 5.54 KB
/
Copy pathinstaller
File metadata and controls
executable file
·252 lines (217 loc) · 5.54 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
#!/usr/bin/env sh
set -u
DOTFILES_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
TMP_BASE=${TMPDIR:-/tmp}
TMP_BASE=${TMP_BASE%/}
LOG_DIR="$TMP_BASE/dotfiles-installer-$(date +%Y%m%d-%H%M%S)"
INSTALLERS_FILE="$LOG_DIR/installers.txt"
SUMMARY_FILE="$LOG_DIR/summary.txt"
DRY_RUN=0
if [ -t 1 ]; then
IS_TTY=1
else
IS_TTY=0
fi
if [ "$IS_TTY" -eq 1 ] && [ "${NO_COLOR:-}" = "" ]; then
BOLD=$(printf '\033[1m')
DIM=$(printf '\033[2m')
RED=$(printf '\033[31m')
GREEN=$(printf '\033[32m')
YELLOW=$(printf '\033[33m')
BLUE=$(printf '\033[34m')
CYAN=$(printf '\033[36m')
RESET=$(printf '\033[0m')
else
BOLD=""
DIM=""
RED=""
GREEN=""
YELLOW=""
BLUE=""
CYAN=""
RESET=""
fi
mkdir -p "$LOG_DIR"
touch "$SUMMARY_FILE"
cleanup() {
[ -f "$INSTALLERS_FILE" ] && rm -f "$INSTALLERS_FILE"
}
trap cleanup EXIT INT TERM
usage() {
printf '%s\n' "Usage: ./installer [--dry-run]"
printf '%s\n' ""
printf '%s\n' "Options:"
printf '%s\n' " -n, --dry-run Show the installers that would run without executing them."
printf '%s\n' " -h, --help Show this help."
}
print_banner() {
if [ "$IS_TTY" -eq 1 ]; then
clear 2>/dev/null || true
fi
printf '%s\n' "${CYAN}${BOLD}"
printf '%s\n' ' ____ _ __ _ _ '
printf '%s\n' ' | _ \ ___ | |_ / _(_) | ___ ___ '
printf '%s\n' ' | | | |/ _ \| __| |_| | |/ _ \/ __|'
printf '%s\n' ' | |_| | (_) | |_| _| | | __/\__ \'
printf '%s\n' ' |____/ \___/ \__|_| |_|_|\___||___/'
printf '%s\n' "${RESET}"
printf '%s\n' "${DIM}A nicer setup flow for this machine.${RESET}"
printf '%s\n\n' "${DIM}Logs: $LOG_DIR${RESET}"
}
fail() {
printf '%s\n' "${RED}${BOLD}Error:${RESET} $1" >&2
exit 1
}
human_name() {
script=$1
dir=$(dirname "$script")
if [ "$dir" = "." ]; then
printf '%s' "Root"
else
basename "$dir"
fi
}
duration() {
seconds=$1
minutes=$((seconds / 60))
remainder=$((seconds % 60))
if [ "$minutes" -gt 0 ]; then
printf '%sm %ss' "$minutes" "$remainder"
else
printf '%ss' "$remainder"
fi
}
spinner_frame() {
case $1 in
0) printf '|' ;;
1) printf '/' ;;
2) printf '-' ;;
*) printf '\' ;;
esac
}
line_clear() {
if [ "$IS_TTY" -eq 1 ]; then
printf '\r\033[2K'
fi
}
run_installer() {
script=$1
index=$2
total=$3
name=$(human_name "$script")
log_file="$LOG_DIR/${name}.log"
start=$(date +%s)
printf '%s' "${BLUE}[$index/$total]${RESET} ${BOLD}$name${RESET} ${DIM}$script${RESET} "
(
cd "$DOTFILES_DIR" || exit 1
sh "$script"
) >"$log_file" 2>&1 &
pid=$!
frame=0
if [ "$IS_TTY" -eq 1 ]; then
while kill -0 "$pid" 2>/dev/null; do
frame_char=$(spinner_frame "$frame")
line_clear
printf '%s %s' "${BLUE}[$index/$total]${RESET} ${BOLD}$name${RESET}" "${YELLOW}$frame_char${RESET}"
frame=$(((frame + 1) % 4))
sleep 0.12
done
fi
wait "$pid"
status=$?
end=$(date +%s)
elapsed=$(duration "$((end - start))")
if [ "$IS_TTY" -eq 1 ]; then
line_start=$(line_clear)
else
line_start=""
fi
if [ "$status" -eq 0 ]; then
printf '%s%s %s %s\n' "$line_start" "${GREEN}[OK]${RESET}" "${BOLD}$name${RESET}" "${DIM}$elapsed${RESET}"
printf 'OK\t%s\t%s\t%s\n' "$name" "$elapsed" "$log_file" >>"$SUMMARY_FILE"
else
printf '%s%s %s %s\n' "$line_start" "${RED}[FAIL]${RESET}" "${BOLD}$name${RESET}" "${DIM}$elapsed${RESET}"
printf 'FAIL\t%s\t%s\t%s\n' "$name" "$elapsed" "$log_file" >>"$SUMMARY_FILE"
printf '%s\n' "${RED}Installer failed:${RESET} $script"
printf '%s\n\n' "${DIM}Last log lines:${RESET}"
tail -n 24 "$log_file" 2>/dev/null || true
printf '\n%s\n' "${DIM}Full log: $log_file${RESET}"
exit "$status"
fi
}
run_dry_run() {
printf '%s\n\n' "${YELLOW}${BOLD}Dry run:${RESET} no installers will be executed."
index=1
while IFS= read -r installer; do
name=$(human_name "$installer")
printf ' %s %s %s\n' "${BLUE}[$index/$total]${RESET}" "${BOLD}$name${RESET}" "${DIM}$installer${RESET}"
index=$((index + 1))
done <"$INSTALLERS_FILE"
}
print_summary() {
printf '\n%s\n' "${BOLD}Summary${RESET}"
while IFS=' ' read -r status name elapsed log_file; do
case "$status" in
OK) label="${GREEN}[OK]${RESET}" ;;
FAIL) label="${RED}[FAIL]${RESET}" ;;
*) label="[$status]" ;;
esac
printf ' %s %-18s %s %s\n' "$label" "$name" "$elapsed" "${DIM}$log_file${RESET}"
done <"$SUMMARY_FILE"
printf '\n%s\n' "${GREEN}${BOLD}Done.${RESET} ${DIM}Installer logs stay available at $LOG_DIR${RESET}"
}
while [ "$#" -gt 0 ]; do
case "$1" in
-n|--dry-run)
DRY_RUN=1
;;
-h|--help)
usage
exit 0
;;
*)
usage >&2
exit 2
;;
esac
shift
done
if [ "$DRY_RUN" -eq 0 ]; then
if [ "$(id -u)" -eq 0 ]; then
fail "Don't run this as root."
fi
if ! command -v brew >/dev/null 2>&1; then
fail "Homebrew required. Install it from https://brew.sh/"
fi
fi
cd "$DOTFILES_DIR" || fail "Could not enter dotfiles directory: $DOTFILES_DIR"
print_banner
find . -name install.sh -type f | sort | awk '
$0 == "./homebrew/install.sh" {
homebrew = $0
next
}
{
print
}
END {
if (homebrew != "") {
print homebrew
}
}
' >"$INSTALLERS_FILE"
total=$(wc -l <"$INSTALLERS_FILE" | tr -d ' ')
if [ "$total" -eq 0 ]; then
fail "No install.sh files found."
fi
printf '%s\n\n' "${BOLD}Found $total installers.${RESET}"
if [ "$DRY_RUN" -eq 1 ]; then
run_dry_run
exit 0
fi
index=1
while IFS= read -r installer; do
run_installer "$installer" "$index" "$total"
index=$((index + 1))
done <"$INSTALLERS_FILE"
print_summary