-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·152 lines (136 loc) · 4.5 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·152 lines (136 loc) · 4.5 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
#!/usr/bin/env bash
set -u
script_dir_from_argv() {
case "${0##*/}" in
bash|sh|zsh|-) return 1 ;;
esac
[ -f "$0" ] || return 1
cd "$(dirname "$0")" && pwd -P
}
SCRIPT_DIR=$(script_dir_from_argv || printf '')
SRC=""
if [ -n "$SCRIPT_DIR" ]; then
if [ -f "$SCRIPT_DIR/apps/cli/bin/workbranch" ]; then
SRC="$SCRIPT_DIR/apps/cli/bin/workbranch"
else
SRC="$SCRIPT_DIR/bin/workbranch"
fi
fi
WORKBRANCH_DEFAULT_RAW_BASE_URL="https://raw.githubusercontent.com/tkhwang/workbranch/main"
WORKBRANCH_RAW_BASE_URL="${WORKBRANCH_RAW_BASE_URL:-$WORKBRANCH_DEFAULT_RAW_BASE_URL}"
DEFAULT_DEST_DIR="${HOME}/.local/bin"
expand_target_dir() {
value=$1
case "$value" in
\~) printf '%s' "$HOME" ;;
\~/*) printf '%s/%s' "$HOME" "${value#~/}" ;;
*) printf '%s' "$value" ;;
esac
}
prompt_read() {
prompt=$1
value=""
if [ -t 0 ]; then
IFS= read -r -e -p "$prompt" value || :
elif [ -z "$SCRIPT_DIR" ] && [ -r /dev/tty ] && { : </dev/tty; } 2>/dev/null; then
# When installed via `curl ... | bash`, stdin is the script body.
# Read interactive answers from the terminal instead of consuming script lines.
printf '%s' "$prompt" >/dev/tty
IFS= read -r value </dev/tty || :
elif [ -n "$SCRIPT_DIR" ]; then
printf '%s' "$prompt" >&2
IFS= read -r value || :
else
# Non-interactive stdin-script execution has no safe prompt stream.
# Return empty so callers use their defaults.
printf '%s' "$prompt" >&2
fi
printf '%s' "$value"
}
profile_for_shell() {
shell_name=${SHELL##*/}
case "$shell_name" in
zsh) printf '%s' "$HOME/.zshrc" ;;
bash) printf '%s' "$HOME/.bash_profile" ;;
*) return 1 ;;
esac
}
append_path_entry() {
profile=$1
dest_dir=$2
line="export PATH=\"$dest_dir:\$PATH\""
mkdir -p "$(dirname "$profile")" || { printf '[-] Error: failed to create profile directory for %s\n' "$profile" >&2; exit 1; }
touch "$profile" || { printf '[-] Error: failed to write %s\n' "$profile" >&2; exit 1; }
if grep -Fqx "$line" "$profile" 2>/dev/null; then
printf '[*] PATH entry already exists in %s\n' "$profile"
else
{
printf '\n# Added by workbranch installer\n'
printf '%s\n' "$line"
} >> "$profile" || { printf '[-] Error: failed to update %s\n' "$profile" >&2; exit 1; }
printf '[+] Added PATH entry to %s\n' "$profile"
fi
printf '[*] Restart your shell or run: source %s\n' "$profile"
}
print_direct_usage() {
printf '[*] Run directly:\n\n'
printf ' %s help\n\n' "$DEST"
printf '[*] Or add this manually to your shell config:\n\n'
printf ' export PATH="%s:$%s"\n' "$DEST_DIR" PATH
}
download_file() {
url=$1
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url"
elif command -v wget >/dev/null 2>&1; then
wget -qO- "$url"
else
printf '[-] Error: curl or wget is required for standalone install\n' >&2
exit 1
fi
}
is_checkout_install() {
[ -n "$SCRIPT_DIR" ] || return 1
[ -f "$SRC" ] || return 1
[ -d "$SCRIPT_DIR/.git" ] || [ -f "$SCRIPT_DIR/.git" ]
}
printf '[*] Install workbranch\n'
input_dir=$(prompt_read "[*] Target directory [$DEFAULT_DEST_DIR]: ")
if [ -z "$input_dir" ]; then
DEST_DIR="$DEFAULT_DEST_DIR"
else
DEST_DIR=$(expand_target_dir "$input_dir")
fi
DEST="$DEST_DIR/workbranch"
mkdir -p "$DEST_DIR" || { printf '[-] Error: failed to create %s\n' "$DEST_DIR" >&2; exit 1; }
if is_checkout_install; then
cp "$SRC" "$DEST" || { printf '[-] Error: failed to install workbranch\n' >&2; exit 1; }
else
download_file "$WORKBRANCH_RAW_BASE_URL/apps/cli/bin/workbranch" > "$DEST" \
|| download_file "$WORKBRANCH_RAW_BASE_URL/bin/workbranch" > "$DEST" \
|| { rm -f "$DEST"; printf '[-] Error: failed to download workbranch\n' >&2; exit 1; }
printf '[+] Downloaded workbranch from %s\n' "$WORKBRANCH_RAW_BASE_URL"
fi
chmod +x "$DEST" || { printf '[-] Error: failed to mark executable: %s\n' "$DEST" >&2; exit 1; }
printf '[+] Installed workbranch to %s\n' "$DEST"
cat <<USAGE
[*] Try it now:
workbranch help show all commands
workbranch init create your first workbranch project
USAGE
case ":${PATH}:" in
*":${DEST_DIR}:"*) ;;
*)
printf '[-] Warning: %s is not on your PATH.\n' "$DEST_DIR"
if profile=$(profile_for_shell); then
answer=$(prompt_read "[*] Add it to your PATH now? [y/N]: ")
case "$answer" in
y|Y|yes|YES) append_path_entry "$profile" "$DEST_DIR" ;;
*) print_direct_usage ;;
esac
else
printf '[*] Automatic shell profile update is only supported for zsh and bash.\n'
print_direct_usage
fi
;;
esac