Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·209 lines (169 loc) · 4.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·209 lines (169 loc) · 4.2 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
#!/usr/bin/env bash
set -euo pipefail
APP='gitd'
PACKAGE='@enbox/gitd'
INSTALL_DIR="${HOME}/.${APP}/bin"
REQUESTED_VERSION="${VERSION:-}"
NO_MODIFY_PATH=false
usage() {
cat <<'EOF'
gitd installer
Usage: install.sh [options]
Options:
-h, --help Show this help message
-v, --version <version> Install a specific version (example: 0.9.6)
--no-modify-path Do not modify shell profile files
Examples:
curl -fsSL https://gitd.sh/install | bash
curl -fsSL https://gitd.sh/install | bash -s -- --version 0.9.6
EOF
}
fail() {
printf 'error: %s\n' "$*" >&2
exit 1
}
has_command() {
command -v "$1" >/dev/null 2>&1
}
http_get() {
if has_command curl; then
curl -fsSL "$1"
return
fi
if has_command wget; then
wget -qO- "$1"
return
fi
fail 'curl or wget is required'
}
detect_os() {
case "$(uname -s)" in
Linux) printf 'linux' ;;
Darwin) printf 'darwin' ;;
*) fail 'unsupported operating system' ;;
esac
}
resolve_package_version() {
if [ -z "$REQUESTED_VERSION" ]; then
printf 'latest'
return
fi
case "$REQUESTED_VERSION" in
v*) printf '%s' "${REQUESTED_VERSION#v}" ;;
*) printf '%s' "$REQUESTED_VERSION" ;;
esac
}
ensure_bun() {
if has_command bun; then
return
fi
export BUN_INSTALL="${BUN_INSTALL:-$HOME/.bun}"
printf '==> Bun is required; installing Bun to %s\n' "$BUN_INSTALL"
http_get 'https://bun.sh/install' | bash
export PATH="${BUN_INSTALL}/bin:$PATH"
if ! has_command bun; then
fail "Bun installed, but ${BUN_INSTALL}/bin/bun is not available"
fi
}
shell_quote() {
printf '%q' "$1"
}
write_wrapper() {
local name="$1"
local source="$2"
local bun_path="$3"
local out="${INSTALL_DIR}/${name}"
if [ ! -e "$source" ]; then
fail "bun did not install ${name}"
fi
local quoted_bun
quoted_bun="$(shell_quote "$bun_path")"
local quoted_source
quoted_source="$(shell_quote "$source")"
{
printf '#!/usr/bin/env bash\n'
printf 'exec %s %s "$@"\n' "$quoted_bun" "$quoted_source"
} > "$out"
chmod +x "$out"
}
add_to_path() {
local shell_name
shell_name="$(basename "${SHELL:-bash}")"
local line
line="export PATH=${INSTALL_DIR}:\$PATH"
local files=''
case "$shell_name" in
zsh) files="${ZDOTDIR:-$HOME}/.zshrc ${ZDOTDIR:-$HOME}/.zshenv" ;;
bash) files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile" ;;
*) files="$HOME/.profile" ;;
esac
local config=''
for file in $files; do
if [ -f "$file" ]; then
config="$file"
break
fi
done
if [ -z "$config" ]; then
printf 'Add this to your shell profile:\n'
printf ' %s\n' "$line"
return
fi
if grep -Fxq "$line" "$config"; then
return
fi
if [ -w "$config" ]; then
printf '\n# gitd\n%s\n' "$line" >> "$config"
printf 'Updated PATH in %s\n' "$config"
return
fi
printf 'Add this to your shell profile:\n'
printf ' %s\n' "$line"
}
main() {
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
if [[ -z "${2:-}" ]]; then
fail '--version requires an argument'
fi
REQUESTED_VERSION="$2"
shift 2
;;
--no-modify-path)
NO_MODIFY_PATH=true
shift
;;
*)
fail "unknown option: $1"
;;
esac
done
detect_os >/dev/null
ensure_bun
local package_version
package_version="$(resolve_package_version)"
[ -n "$package_version" ] || fail 'unable to determine package version'
local package_spec="${PACKAGE}@${package_version}"
printf '==> Installing %s\n' "$package_spec"
bun add -g "$package_spec"
mkdir -p "$INSTALL_DIR"
local bun_path
bun_path="$(command -v bun)"
local bun_global_bin
bun_global_bin="$(bun pm bin -g)"
write_wrapper 'gitd' "${bun_global_bin}/gitd" "$bun_path"
write_wrapper 'git-remote-did' "${bun_global_bin}/git-remote-did" "$bun_path"
write_wrapper 'git-remote-did-credential' "${bun_global_bin}/git-remote-did-credential" "$bun_path"
if [ "$NO_MODIFY_PATH" = false ] && [[ ":$PATH:" != *":${INSTALL_DIR}:"* ]]; then
add_to_path
fi
printf '==> Installed to %s\n' "$INSTALL_DIR"
"${INSTALL_DIR}/gitd" --version
printf 'Run: gitd setup\n'
}
main "$@"