-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·627 lines (514 loc) · 15.9 KB
/
install.sh
File metadata and controls
executable file
·627 lines (514 loc) · 15.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#!/usr/bin/env bash
#
# install.sh
# -----------------------------------------------------------------------------
# User-level installer for "my-unicorn"
#
# This installer is responsible for:
# - Installing the CLI using `uv tool install`
# - Supporting version-specific installs using Git tags (example: v2.3.0)
# - Installing editable mode for local development contributors
# - Setting up shell autocomplete for bash/zsh
# - Copying the update helper script into ~/.local/bin
# - Checking PATH configuration for proper CLI access
#
# Supported modes:
#
# ./install.sh -i | --install [version]
# Standard installation using uv tool install
#
# ./install.sh -e | --editable
# Editable install using local source code for development
#
# ./install.sh -u | --uninstall
# Uninstall my-unicorn from user system
#
# ./install.sh -h | --help
# Show usage information
#
#
# Examples:
#
# ./install.sh -i
# Install latest version from GitHub
#
# ./install.sh -i 2.3.0
# Install tagged version v2.3.0
#
# ./install.sh -i v2.3.0-alpha
# Install tagged pre-release version directly
#
# ./install.sh -e
# Install editable mode for contributors
#
# Safety flags:
#
# -e Exit immediately if a command fails
# -u Exit if an unset variable is used
# -o pipefail
# Fail a pipeline if any command inside it fails
#
# NOTE:
# Using a single strict mode declaration to avoid redundancy.
set -Eeuo pipefail
# -----------------------------------------------------------------------------
# Global configuration
# -----------------------------------------------------------------------------
# Official GitHub repository used for installation
GITHUB_GIT_URL="git+https://github.com/Cyber-Syntax/my-unicorn"
readonly GITHUB_GIT_URL
# CLI executable name shown to users
CLI_NAME="my-unicorn"
readonly CLI_NAME
# Standard XDG user data location
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
readonly XDG_DATA_HOME
# Local install location for autocomplete assets and support files
INSTALL_DIR="$XDG_DATA_HOME/my-unicorn"
readonly INSTALL_DIR
# User-local executable directory
LOCAL_BIN_DIR="$HOME/.local/bin"
readonly LOCAL_BIN_DIR
# Update helper destination path
UPDATE_SCRIPT_PATH="$LOCAL_BIN_DIR/my-unicorn-update"
readonly UPDATE_SCRIPT_PATH
# -----------------------------------------------------------------------------
# Logging helpers
# -----------------------------------------------------------------------------
# Print normal informational messages to stdout
print_info() {
local message
message="$1"
printf '%s\n' "$message"
}
# Print errors to stderr so failures are visible and script-safe
print_error() {
local message
message="$1"
printf '❌ %s\n' "$message" >&2
}
# -----------------------------------------------------------------------------
# Utility helpers
# -----------------------------------------------------------------------------
# Return the absolute path of the current script directory.
#
# This helps us reliably locate project files such as:
# scripts/autocomplete.bash
# scripts/update.bash
#
# even when the installer is executed from another directory.
script_dir() {
local src_dir
if ! src_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; then
print_error "Unable to determine script directory."
return 1
fi
if [[ -z "${src_dir// /}" ]]; then
print_error "Resolved script directory is empty."
return 1
fi
printf '%s\n' "$src_dir"
}
# Ensure a required command exists before continuing.
#
# Example:
# check_dependency uv
#
# This prevents confusing failures later in the install flow.
check_dependency() {
local command_name
command_name="$1"
if ! command -v "$command_name" >/dev/null 2>&1; then
print_error "Required command not found: $command_name"
return 1
fi
}
# Normalize a version string into a Git tag.
#
# Examples:
#
# 2.3.0 -> v2.3.0
# v2.3.0 -> v2.3.0
# 2.3.0-alpha -> v2.3.0-alpha
#
# This matches the same logic used by the Python self-update module.
normalize_version_tag() {
local version
version="$1"
if [[ -z "${version// /}" ]]; then
print_error "Version value cannot be empty."
return 1
fi
version="${version//[$'\r\n\t']/}"
version="${version#"${version%%[![:space:]]*}"}"
version="${version%"${version##*[![:space:]]}"}"
if [[ -z "${version// /}" ]]; then
print_error "Version value is invalid after sanitization."
return 1
fi
if [[ "$version" =~ ^v ]]; then
printf '%s\n' "$version"
return
fi
printf 'v%s\n' "$version"
}
# -----------------------------------------------------------------------------
# PATH validation
# -----------------------------------------------------------------------------
# Check whether ~/.local/bin exists in PATH.
#
# uv installs CLI tools there for most user-level installs.
# If it is missing, users may install successfully but still be unable
# to run `my-unicorn`.
check_local_bin_in_path() {
if [[ ":$PATH:" == *":$LOCAL_BIN_DIR:"* ]]; then
print_info "✅ $LOCAL_BIN_DIR is already in your PATH"
return 0
fi
cat <<EOF
⚠️ IMPORTANT: $LOCAL_BIN_DIR is not in your PATH
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
To use '${CLI_NAME}' from anywhere, add this line to your shell config:
export PATH="\$HOME/.local/bin:\$PATH"
Example shell configs:
~/.bashrc
~/.zshrc
~/.zshenv
Then restart your terminal or run:
source ~/.bashrc
source ~/.zshrc
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EOF
return 1
}
# -----------------------------------------------------------------------------
# Autocomplete setup
# -----------------------------------------------------------------------------
# Execute autocomplete installer from the source repository.
#
# This delegates the shell-specific logic to:
# scripts/autocomplete.bash
setup_autocomplete_from_src() {
local src_dir
local autocomplete_helper
src_dir="$1"
autocomplete_helper="$src_dir/scripts/autocomplete.bash"
if [[ -x "$autocomplete_helper" ]]; then
print_info "🔁 Setting up autocomplete..."
if ! bash "$autocomplete_helper"; then
print_error "Autocomplete setup failed."
return 1
fi
return 0
fi
print_info "⚠️ Warning: Autocomplete helper not found at $autocomplete_helper"
}
# -----------------------------------------------------------------------------
# Update helper installation
# -----------------------------------------------------------------------------
# Copy update helper script into ~/.local/bin so users can run:
#
# my-unicorn-update
#
# This keeps updates easy and discoverable.
copy_update_script() {
local src_dir
local src_path
if ! src_dir="$(script_dir)"; then
return 1
fi
src_path="$src_dir/scripts/update.bash"
if [[ ! -f "$src_path" ]]; then
print_info "⚠️ Warning: Update script not found at $src_path, skipping copy."
return 0
fi
mkdir -p "$LOCAL_BIN_DIR"
if ! cp "$src_path" "$UPDATE_SCRIPT_PATH"; then
print_error "Failed to copy update script."
return 1
fi
if ! chmod +x "$UPDATE_SCRIPT_PATH"; then
print_error "Failed to make update script executable."
return 1
fi
print_info "✅ Update script copied to $UPDATE_SCRIPT_PATH"
}
# -----------------------------------------------------------------------------
# Installation methods
# -----------------------------------------------------------------------------
find_latest_prerelease_tag() {
local latest_tag
local repo_url
local tag_list
repo_url="https://github.com/Cyber-Syntax/my-unicorn.git"
if ! check_dependency "git"; then
return 1
fi
# Fetch remote tags without cloning the repository.
# We sort using version-aware sorting and keep the newest tag.
#
# Examples expected:
# v2.5.2-alpha
# v2.5.2-beta
# v2.5.2
#
# This ensures:
#
# ./install.sh -i
#
# installs the latest tagged release instead of the latest main branch commit.
if ! tag_list="$(
git ls-remote --tags --refs "$repo_url" 2>/dev/null |
awk '{print $2}' |
sed 's#refs/tags/##' |
grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9]+)*$' |
sort -V
)"; then
print_error "Failed to fetch remote tags from GitHub."
return 1
fi
if [[ -z "${tag_list// /}" ]]; then
print_error "No valid release tags were found."
return 1
fi
if ! latest_tag="$(printf '%s\n' "$tag_list" | tail -n 1)"; then
print_error "Failed to determine latest release tag."
return 1
fi
if [[ -z "${latest_tag// /}" ]]; then
print_error "Resolved latest tag is empty."
return 1
fi
printf '%s\n' "$latest_tag"
}
install_with_uv_tool() {
local requested_version
local version_tag
local install_target
local src_dir
requested_version="${1:-}"
if ! check_dependency "uv"; then
return 1
fi
if ! src_dir="$(script_dir)"; then
return 1
fi
# Behavior:
#
# ./install.sh -i
# -> installs latest prerelease/release tag from GitHub
#
# ./install.sh -i 2.5.2-alpha
# -> installs exact requested tag
#
# ./install.sh -i v2.5.2-alpha
# -> installs exact requested tag
#
# This prevents accidental installs from main branch HEAD.
if [[ -n "${requested_version// /}" ]]; then
if ! version_tag="$(normalize_version_tag "$requested_version")"; then
return 1
fi
print_info "🔖 Requested version detected: ${version_tag}"
else
print_info "🔍 No version provided. Resolving latest prerelease tag..."
if ! version_tag="$(find_latest_prerelease_tag)"; then
print_error "Unable to determine latest prerelease tag."
return 1
fi
print_info "🔖 Latest prerelease tag resolved: ${version_tag}"
fi
install_target="${GITHUB_GIT_URL}@${version_tag}"
print_info "🚀 Installing ${CLI_NAME} version ${version_tag} using 'uv tool install'..."
if ! cd "$src_dir"; then
print_error "Failed to change directory to: $src_dir"
return 1
fi
if ! uv tool install "$install_target" --force; then
print_error "uv tool install failed for target: $install_target"
return 1
fi
check_local_bin_in_path
setup_autocomplete_from_src "$src_dir"
copy_update_script
print_info "✅ Installation complete using uv tool."
print_info "Run '${CLI_NAME} --help' to get started."
}
# Editable installation for local development.
#
# This is intended for contributors working on the source code.
# Changes made locally are immediately reflected without reinstalling.
install_with_uv_editable() {
local src_dir
if ! check_dependency "uv"; then
return 1
fi
if ! src_dir="$(script_dir)"; then
return 1
fi
print_info "🔧 Installing ${CLI_NAME} in editable mode..."
if ! cd "$src_dir"; then
print_error "Failed to change directory to: $src_dir"
return 1
fi
if ! uv tool install --editable . --force; then
print_error "Editable installation failed."
return 1
fi
check_local_bin_in_path
setup_autocomplete_from_src "$src_dir"
copy_update_script
print_info "✅ Editable installation complete."
print_info "Changes to source code will be reflected immediately."
}
# -----------------------------------------------------------------------------
# Uninstall support
# -----------------------------------------------------------------------------
# Remove everything installed by my-unicorn at user level.
#
# This includes:
# - uv tool installation
# - local update script
# - autocomplete install directory (optional cleanup)
#
# Safe behavior:
# - Only affects user-local files
# - Requires explicit confirmation
# - Never modifies system files
uninstall_my_unicorn() {
local src_dir
local confirm
if ! src_dir="$(script_dir)"; then
return 1
fi
print_info "🧹 Uninstalling ${CLI_NAME}..."
printf "Are you sure you want to uninstall %s? (y/N): " "$CLI_NAME"
read -r confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
print_info "❎ Uninstall cancelled."
return 0
fi
# 1. Remove uv tool install
if check_dependency "uv"; then
if uv tool list 2>/dev/null | grep -q "$CLI_NAME"; then
if ! uv tool uninstall "$CLI_NAME"; then
print_error "Failed to uninstall via uv tool."
return 1
fi
print_info "🗑️ Removed uv tool installation"
else
print_info "ℹ️ uv tool installation not found"
fi
fi
# 2. Remove update script
if [[ -f "$UPDATE_SCRIPT_PATH" ]]; then
if ! rm -f "$UPDATE_SCRIPT_PATH"; then
print_error "Failed to remove update script."
return 1
fi
print_info "🗑️ Removed update script: $UPDATE_SCRIPT_PATH"
fi
# 3. Remove install directory (autocomplete + assets)
if [[ -d "$INSTALL_DIR" ]]; then
if ! rm -rf "$INSTALL_DIR"; then
print_error "Failed to remove install directory: $INSTALL_DIR"
return 1
fi
print_info "🗑️ Removed install directory: $INSTALL_DIR"
fi
cat <<EOF
⚠️ MANUAL CLEANUP (OPTIONAL)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
If you manually added completion config, remove from:
~/.bashrc
~/.zshrc
Directories (if still present):
~/.config/bash/completions/
~/.config/zsh/completions/
Then restart your shell.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EOF
print_info "✅ Uninstall complete."
}
# -----------------------------------------------------------------------------
# Usage
# -----------------------------------------------------------------------------
usage() {
local exit_code
# Default to exit code 0 for normal usage
# If an error code is provided, use 1 to indicate failure
exit_code="${1:-0}"
cat <<EOF
Usage: ./install.sh [OPTIONS]
Options:
-i, --install [version] Install using uv tool (default: latest prerelease tag)
-e, --editable Install in editable mode (development)
-u, --uninstall Remove my-unicorn from user system
-h, --help Show this help message
Examples:
./install.sh -i
./install.sh -i 2.5.2-alpha
./install.sh -e
./install.sh -u
EOF
exit "$exit_code"
}
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
main() {
local mode
local version
local arg_count
mode="${1:-}"
version="${2:-}"
arg_count="$#"
# Strict argument validation
#
# Allowed patterns:
# install: ./install.sh -i [version]
# editable: ./install.sh -e
# uninstall: ./install.sh -u
# help: ./install.sh -h
#
# Reject any malformed invocation such as:
# ./install.sh -e extra
# ./install.sh -u something wrong
# ./install.sh -i v1 v2 v3
#
if [[ "$arg_count" -gt 2 ]]; then
print_error "Too many arguments provided."
usage 1
fi
case "$mode" in
-i | --install)
install_with_uv_tool "$version"
;;
-e | --editable)
if [[ "$arg_count" -gt 1 ]]; then
print_error "Editable mode does not accept additional arguments."
usage 1
fi
install_with_uv_editable
;;
-u | --uninstall)
if [[ "$arg_count" -gt 1 ]]; then
print_error "Uninstall mode does not accept additional arguments."
usage 1
fi
uninstall_my_unicorn
;;
-h | --help | "")
if [[ "$arg_count" -gt 1 ]]; then
print_error "Help does not accept additional arguments."
usage 1
fi
usage 0
;;
*)
print_error "Unknown option: $mode"
usage 1
;;
esac
}
main "$@"