-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall
More file actions
267 lines (238 loc) · 7.22 KB
/
Copy pathinstall
File metadata and controls
267 lines (238 loc) · 7.22 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
#!/usr/bin/env bash
# Install arx from GitHub releases (GoReleaser archives).
#
# Linux / macOS:
# curl -fsSL https://get.arx.ber.run/install | bash
#
# Environment (optional):
# ARX_REPO GitHub repo as owner/name (default: berbyte/arx-community)
# ARX_VERSION Release tag or bare semver (e.g. v1.2.3 or 1.2.3). Default: latest release.
# ARX_BIN_DIR Install directory. If unset, tries $HOME/.local/bin then $HOME/bin, then the
# first writable absolute directory already on your PATH.
# ARX_POST_INSTALL Optional args after onboarding: e.g. "cursor" -> arx install cursor.
# If the first word is "install", the value is used as the full argv (legacy).
# ARX_GITHUB_API Override GitHub API base (default: https://api.github.com)
#
# Requires: curl, tar, mkdir, chmod, jq
set -euo pipefail
ARX_REPO="${ARX_REPO:-berbyte/arx-community}"
# Set if user exported ARX_BIN_DIR (even empty); used to avoid overriding an explicit choice.
if [ -n "${ARX_BIN_DIR+x}" ]; then
ARX_BIN_DIR_SET_BY_USER=1
else
ARX_BIN_DIR_SET_BY_USER=0
fi
ARX_BIN_DIR="${ARX_BIN_DIR:-$HOME/.local/bin}"
ARX_GITHUB_API="${ARX_GITHUB_API:-https://api.github.com}"
need_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "arx install: required command not found: $1" >&2
exit 1
}
}
# First writable absolute directory on PATH (left to right). Skips empty, relative, and common system roots.
first_writable_path_dir() {
local IFS=':' p
for p in $PATH; do
[[ -z "$p" || "$p" == "." ]] && continue
p="${p%/}"
[[ "$p" != /* ]] && continue
case "$p" in
/bin | /sbin | /usr/bin | /usr/sbin | /usr/ucb | /) continue ;;
esac
if [[ "$p" == '~'* ]]; then
p="${HOME}${p:1}"
fi
if [[ -d "$p" && -w "$p" ]]; then
printf '%s\n' "$p"
return 0
fi
done
return 1
}
# If ARX_BIN_DIR was not set by the user: prefer home bin dirs, else any writable dir on PATH.
resolve_install_dir() {
if [[ "$ARX_BIN_DIR_SET_BY_USER" -eq 1 ]]; then
return 0
fi
local d picked
for d in "${HOME}/.local/bin" "${HOME}/bin"; do
if mkdir -p "$d" 2>/dev/null && [[ -d "$d" && -w "$d" ]]; then
ARX_BIN_DIR="$d"
return 0
fi
done
if picked="$(first_writable_path_dir)"; then
ARX_BIN_DIR="$picked"
echo "arx install: using ${ARX_BIN_DIR} (writable directory on your PATH)." >&2
return 0
fi
echo "arx install: no writable install directory found; set ARX_BIN_DIR." >&2
exit 1
}
semver_file() {
local v="$1"
echo "${v#[vV]}"
}
ensure_tag() {
local v="$1"
if [[ "$v" == [vV]* ]]; then
echo "$v"
else
echo "v${v}"
fi
}
detect_os() {
case "$(uname -s)" in
Linux*) echo linux ;;
Darwin*) echo darwin ;;
*)
echo "arx install: unsupported OS (need Linux or macOS): $(uname -s)" >&2
exit 1
;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64 | amd64) echo amd64 ;;
aarch64 | arm64) echo arm64 ;;
*)
echo "arx install: unsupported CPU: $(uname -m)" >&2
exit 1
;;
esac
}
# Bundle name: arx_<semver>_<os>_<arch>.tar.gz (GoReleaser name_template, project_name arx)
bundle_url_from_release() {
local json="$1" os="$2" arch="$3" exact="${4:-}"
need_cmd jq
if [[ -n "$exact" ]]; then
jq -r --arg n "$exact" '.assets[] | select(.name == $n) | .browser_download_url' <<<"$json" | head -n1
else
jq -r --arg sfx "_${os}_${arch}.tar.gz" \
'.assets[] | select(.name | startswith("arx_") and endswith($sfx)) | .browser_download_url' \
<<<"$json" | head -n1
fi
}
bundle_url_from_releases_page() {
local json="$1" asset="$2"
need_cmd jq
jq -r --arg n "$asset" '.[] | .assets[] | select(.name == $n) | .browser_download_url' <<<"$json" | head -n1
}
github_get() {
local path="$1"
curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${ARX_GITHUB_API}${path}"
}
resolve_latest_bundle_url() {
local os="$1" arch="$2"
need_cmd curl
local json url
json="$(github_get "/repos/${ARX_REPO}/releases/latest")"
url="$(bundle_url_from_release "$json" "$os" "$arch")"
if [[ -z "$url" ]]; then
echo "arx install: no arx_*_${os}_${arch}.tar.gz in latest release for ${ARX_REPO}" >&2
exit 1
fi
printf '%s\n' "$url"
}
resolve_pinned_bundle_url() {
local ver="$1" os="$2" arch="$3"
local tag asset json url=""
tag="$(ensure_tag "$ver")"
asset="arx_${ver}_${os}_${arch}.tar.gz"
need_cmd curl
if json="$(github_get "/repos/${ARX_REPO}/releases/tags/${tag}" 2>/dev/null)"; then
url="$(bundle_url_from_release "$json" "$os" "$arch" "$asset")"
fi
if [[ -z "$url" ]]; then
local page=1
while true; do
json="$(github_get "/repos/${ARX_REPO}/releases?per_page=100&page=${page}")"
url="$(bundle_url_from_releases_page "$json" "$asset")"
if [[ -n "$url" ]]; then
break
fi
local count
count="$(jq 'length' <<<"$json")"
if [[ "$count" -lt 100 ]]; then
break
fi
page=$((page + 1))
done
fi
if [[ -z "$url" ]]; then
echo "arx install: could not find asset ${asset} in releases for ${ARX_REPO}" >&2
exit 1
fi
printf '%s\n' "$url"
}
find_in_tree() {
local root="$1" base="$2"
if [[ -f "${root}/${base}" ]]; then
printf '%s\n' "${root}/${base}"
return 0
fi
find "$root" -type f -name "$base" | head -n1
}
main() {
need_cmd mkdir
need_cmd chmod
need_cmd jq
need_cmd tar
resolve_install_dir
local os arch
os="$(detect_os)"
arch="$(detect_arch)"
local ver url
if [[ -n "${ARX_VERSION:-}" ]]; then
ver="$(semver_file "$ARX_VERSION")"
url="$(resolve_pinned_bundle_url "$ver" "$os" "$arch")"
else
url="$(resolve_latest_bundle_url "$os" "$arch")"
fi
# Global (not local): EXIT trap runs after main returns; with set -u, a local tmpdir
# would be out of scope and trigger "tmpdir: unbound variable" in the trap.
_arx_install_tmpdir="$(mktemp -d)"
trap 'rm -rf "${_arx_install_tmpdir:-}"' EXIT
echo "Downloading bundle ..."
curl -fsL --retry 3 --retry-delay 1 -o "${_arx_install_tmpdir}/bundle.tar.gz" "$url"
mkdir -p "$ARX_BIN_DIR"
tar -xzf "${_arx_install_tmpdir}/bundle.tar.gz" -C "$_arx_install_tmpdir"
local arx_src
arx_src="$(find_in_tree "$_arx_install_tmpdir" arx)"
if [[ -z "$arx_src" || ! -f "$arx_src" ]]; then
echo "arx install: expected arx not found in archive" >&2
exit 1
fi
local dest="${ARX_BIN_DIR}/arx"
mv "$arx_src" "$dest"
chmod +x "$dest"
echo "Installed: $dest"
# curl … | bash leaves stdin connected to the pipe, not the keyboard. Interactive
# prompts in arx install must read from the controlling terminal instead.
run_arx_install() {
if [[ -r /dev/tty ]]; then
"$@" < /dev/tty
else
"$@"
fi
}
# Onboard: configure editors / hooks via the installed binary.
if [[ -n "${ARX_POST_INSTALL:-}" ]]; then
read -r -a post_args <<<"$ARX_POST_INSTALL"
if [[ "${post_args[0]:-}" == "install" ]]; then
echo "Running: $dest ${post_args[*]}"
run_arx_install "$dest" "${post_args[@]}"
else
echo "Running: $dest install ${post_args[*]}"
run_arx_install "$dest" install "${post_args[@]}"
fi
else
echo "Running: $dest install"
run_arx_install "$dest" install
fi
}
main "$@"