-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathinstall.sh
More file actions
436 lines (360 loc) · 12.9 KB
/
install.sh
File metadata and controls
436 lines (360 loc) · 12.9 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
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
#!/bin/sh
# install.sh — Download and install the latest devo binary for Linux / macOS.
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/7df-lab/devo/main/install.sh | sh
# curl -fsSL https://raw.githubusercontent.com/7df-lab/devo/main/install.sh | sh -s -- --version v0.1.2
set -eu
APP="devo"
REPO="7df-lab/devo"
INSTALL_DIR_DEFAULT="${HOME}/.devo/bin"
MUTED="$(printf '\033[0;2m')"
RED="$(printf '\033[0;31m')"
ORANGE="$(printf '\033[38;5;214m')"
NC="$(printf '\033[0m')"
requested_version="${VERSION:-}"
binary_path=""
no_modify_path="false"
install_dir="${DEVO_INSTALL_DIR:-$INSTALL_DIR_DEFAULT}"
usage() {
cat <<EOF
devo Installer
Usage: install.sh [options]
Options:
-h, --help Display this help message
-v, --version <version> Install a specific version (for example: v0.1.2)
-b, --binary <path> Install from a local binary instead of downloading
--install-dir <dir> Install into a custom directory
--no-modify-path Don't modify shell config files
Environment:
VERSION Same as --version
DEVO_INSTALL_DIR Same as --install-dir
Examples:
curl -fsSL https://raw.githubusercontent.com/7df-lab/devo/main/install.sh | sh
curl -fsSL https://raw.githubusercontent.com/7df-lab/devo/main/install.sh | sh -s -- --version v0.1.2
./install.sh --binary ./target/release/devo
EOF
}
print_message() {
level="$1"
message="$2"
case "$level" in
info) color="$NC" ;;
warning) color="$ORANGE" ;;
error) color="$RED" ;;
*) color="$NC" ;;
esac
printf '%b%s%b\n' "$color" "$message" "$NC"
}
die() {
print_message error "$1" >&2
exit 1
}
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
if [ -n "${2:-}" ]; then
requested_version="$2"
shift 2
else
die "Error: --version requires a version argument"
fi
;;
-b|--binary)
if [ -n "${2:-}" ]; then
binary_path="$2"
shift 2
else
die "Error: --binary requires a path argument"
fi
;;
--install-dir)
if [ -n "${2:-}" ]; then
install_dir="$2"
shift 2
else
die "Error: --install-dir requires a directory argument"
fi
;;
--no-modify-path)
no_modify_path="true"
shift
;;
*)
print_message warning "Warning: Unknown option '$1'" >&2
shift
;;
esac
done
require_command() {
command_name="$1"
hint="$2"
if ! command -v "$command_name" >/dev/null 2>&1; then
die "$hint"
fi
}
normalize_version() {
version="$1"
version="${version#v}"
printf 'v%s\n' "$version"
}
detect_target() {
raw_os="$(uname -s)"
raw_arch="$(uname -m)"
case "$raw_os" in
Linux) os="unknown-linux-gnu" ;;
Darwin) os="apple-darwin" ;;
*)
die "Unsupported OS: $raw_os. This installer supports Linux and macOS. For Windows, use install.ps1."
;;
esac
case "$raw_arch" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*)
die "Unsupported architecture: $raw_arch"
;;
esac
printf '%s-%s\n' "$arch" "$os"
}
resolve_latest_version() {
require_command curl "Error: 'curl' is required but not installed."
require_command sed "Error: 'sed' is required but not installed."
latest="$(
curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' \
| sed -n '1p'
)"
if [ -z "$latest" ]; then
die "Failed to resolve the latest release version"
fi
printf '%s\n' "$latest"
}
release_exists() {
version_tag="$1"
http_status="$(curl -sSL -o /dev/null -w '%{http_code}' "https://github.com/${REPO}/releases/tag/${version_tag}" || true)"
[ "$http_status" = "200" ]
}
path_contains() {
case ":${PATH:-}:" in
*:"$1":*) return 0 ;;
*) return 1 ;;
esac
}
can_install_to_dir() {
dir="$1"
if [ -d "$dir" ]; then
[ -w "$dir" ]
return
fi
parent="$(dirname "$dir")"
while [ ! -d "$parent" ]; do
next_parent="$(dirname "$parent")"
if [ "$next_parent" = "$parent" ]; then
return 1
fi
parent="$next_parent"
done
[ -w "$parent" ]
}
choose_shell_profile() {
shell_name="${SHELL##*/}"
xdg_config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
case "$shell_name" in
zsh)
for candidate in "${ZDOTDIR:-$HOME}/.zshrc" "${ZDOTDIR:-$HOME}/.zshenv" "$xdg_config_home/zsh/.zshrc" "$xdg_config_home/zsh/.zshenv"; do
if [ -f "$candidate" ]; then
printf '%s\n' "$candidate"
return
fi
done
printf '%s\n' "${ZDOTDIR:-$HOME}/.zshrc"
;;
bash)
for candidate in "$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.profile" "$xdg_config_home/bash/.bashrc" "$xdg_config_home/bash/.bash_profile"; do
if [ -f "$candidate" ]; then
printf '%s\n' "$candidate"
return
fi
done
printf '%s\n' "$HOME/.bashrc"
;;
sh|dash|ksh|ash)
if [ -f "$HOME/.profile" ]; then
printf '%s\n' "$HOME/.profile"
else
printf '%s\n' "$HOME/.profile"
fi
;;
fish)
if [ -f "$HOME/.config/fish/config.fish" ]; then
printf '%s\n' "$HOME/.config/fish/config.fish"
else
printf '%s\n' "$HOME/.config/fish/config.fish"
fi
;;
*)
printf '\n'
;;
esac
}
ensure_path_in_profile() {
target_install_dir="$1"
profile="$2"
shell_name="${SHELL##*/}"
if [ -z "$profile" ]; then
return 1
fi
if [ -e "$profile" ] && [ ! -w "$profile" ]; then
return 1
fi
mkdir -p "$(dirname "$profile")"
case "$shell_name" in
fish)
path_line="fish_add_path $target_install_dir"
;;
*)
path_line="export PATH=\"$target_install_dir:\$PATH\""
;;
esac
if [ -f "$profile" ] && grep -F "$path_line" "$profile" >/dev/null 2>&1; then
return 0
fi
{
printf '\n'
printf '# added by devo installer\n'
printf '%s\n' "$path_line"
} >> "$profile"
}
print_path_hint() {
target_install_dir="$1"
if path_contains "$target_install_dir"; then
return
fi
if [ "$no_modify_path" = "true" ]; then
print_message warning "Add ${target_install_dir} to your PATH to run devo from any terminal:"
if [ "${SHELL##*/}" = "fish" ]; then
print_message info " fish_add_path ${target_install_dir}"
else
print_message info " export PATH=\"${target_install_dir}:\$PATH\""
fi
return
fi
profile="$(choose_shell_profile)"
if ensure_path_in_profile "$target_install_dir" "$profile"; then
print_message info "${MUTED}Updated PATH in ${NC}${profile}"
print_message info "${MUTED}Open a new terminal or run:${NC}"
print_message info " . \"$profile\""
return
fi
print_message warning "Couldn't update your shell profile automatically."
if [ "${SHELL##*/}" = "fish" ]; then
print_message info "Add this line to your shell config:"
print_message info " fish_add_path ${target_install_dir}"
else
print_message info "Add this line to your shell config:"
print_message info " export PATH=\"${target_install_dir}:\$PATH\""
fi
}
check_version() {
expected_version="$1"
if ! command -v "$APP" >/dev/null 2>&1; then
return
fi
installed_path="$(command -v "$APP")"
installed_version="$("$APP" --version 2>/dev/null || printf '')"
normalized_expected="${expected_version#v}"
if printf '%s' "$installed_version" | grep -F "$normalized_expected" >/dev/null 2>&1; then
print_message info "${MUTED}${APP} ${NC}${expected_version}${MUTED} is already installed at ${NC}${installed_path}"
exit 0
fi
if [ -n "$installed_version" ]; then
print_message info "${MUTED}Found existing ${APP} at ${NC}${installed_path}${MUTED} (${NC}${installed_version}${MUTED})${NC}"
fi
}
find_extracted_binary() {
search_dir="$1"
found_binary="$(find "$search_dir" -name "$APP" -type f | sed -n '1p')"
if [ -z "$found_binary" ]; then
die "Failed to locate the ${APP} binary inside the downloaded archive"
fi
printf '%s\n' "$found_binary"
}
install_from_binary() {
source_binary="$1"
[ -f "$source_binary" ] || die "Binary not found at ${source_binary}"
mkdir -p "$install_dir"
cp "$source_binary" "${install_dir}/${APP}"
chmod 755 "${install_dir}/${APP}"
}
download_and_install() {
target="$1"
version_tag="$2"
require_command curl "Error: 'curl' is required but not installed."
require_command tar "Error: 'tar' is required but not installed."
require_command find "Error: 'find' is required but not installed."
archive_name="${APP}-${version_tag}-${target}.tar.gz"
archive_url="https://github.com/${REPO}/releases/download/${version_tag}/${archive_name}"
print_message info ""
print_message info "${MUTED}Installing ${NC}${APP} ${MUTED}version: ${NC}${version_tag}"
print_message info "${MUTED}Target: ${NC}${target}"
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/${APP}-install.XXXXXX")"
trap 'rm -rf "$tmp_dir"' EXIT INT TERM
curl -fL --progress-bar "$archive_url" -o "$tmp_dir/$archive_name"
tar -xzf "$tmp_dir/$archive_name" -C "$tmp_dir"
extracted_binary="$(find_extracted_binary "$tmp_dir")"
mkdir -p "$install_dir"
install -m 755 "$extracted_binary" "${install_dir}/${APP}"
rm -rf "$tmp_dir"
trap - EXIT INT TERM
}
print_banner() {
printf '\n'
printf '%b%s%b\n' "$MUTED" "░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░" "$NC"
printf '%b%s%b\n' "$MUTED" "░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░" "$NC"
printf '%b%s%b\n' "$MUTED" "░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░" "$NC"
printf '%b%s%b\n' "$MUTED" "░▒▓█▓▒░░▒▓█▓▒░▒▓██████▓▒░ ░▒▓█▓▒▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░" "$NC"
printf '%b%s%b\n' "$MUTED" "░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░" "$NC"
printf '%b%s%b\n' "$MUTED" "░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░" "$NC"
printf '%b%s%b\n' "$MUTED" "░▒▓███████▓▒░░▒▓████████▓▒░ ░▒▓██▓▒░ ░▒▓██████▓▒░" "$NC"
printf '\n'
}
main() {
print_banner
if [ -n "$binary_path" ]; then
print_message info ""
print_message info "${MUTED}Installing ${NC}${APP} ${MUTED}from local binary: ${NC}${binary_path}"
install_from_binary "$binary_path"
else
target="$(detect_target)"
if [ -z "$requested_version" ]; then
version_tag="$(resolve_latest_version)"
else
version_tag="$(normalize_version "$requested_version")"
if ! release_exists "$version_tag"; then
die "Release ${version_tag} not found. See https://github.com/${REPO}/releases"
fi
fi
check_version "$version_tag"
download_and_install "$target" "$version_tag"
fi
print_path_hint "$install_dir"
if [ -n "${GITHUB_ACTIONS:-}" ] && [ "$GITHUB_ACTIONS" = "true" ] && [ -n "${GITHUB_PATH:-}" ]; then
printf '%s\n' "$install_dir" >> "$GITHUB_PATH"
print_message info "${MUTED}Added ${NC}${install_dir}${MUTED} to \$GITHUB_PATH${NC}"
fi
print_message info "${MUTED}${APP} is ready.${NC}"
print_message info ""
print_message info " cd <project> ${MUTED}# open your workspace${NC}"
print_message info " devo onboard ${MUTED}# first-run setup${NC}"
print_message info ""
print_message info "${MUTED}Docs: ${NC}https://github.com/${REPO}#readme"
}
if ! can_install_to_dir "$install_dir"; then
die "Install directory is not writable or cannot be created: ${install_dir}"
fi
main