-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstandalone-installer-unix
executable file
·266 lines (219 loc) · 8.47 KB
/
standalone-installer-unix
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
#!/bin/bash
#
# Bash program to download the latest Nextstrain CLI standalone installation
# archive for Linux or macOS, extract it into the current user's app data
# directory, and check if PATH includes the installation destination.
#
# It maintains rough parity with the PowerShell program for Windows,
# standalone-installer-windows.
#
# Set DESTINATION to change the installation location.
#
# Set VERSION to change the version downloaded and installed, or pass the
# desired version as the first argument to this program.
#
set -euo pipefail
shopt -s failglob
: "${NEXTSTRAIN_DOT_ORG:=https://nextstrain.org}"
# Globals declared here to make them more obvious, but set by main() to avoid
# source ordering requirements and delay execution until the full file is
# parsed.
declare VERSION KERNEL TARGET DESTINATION
# Wrap everything in a function which we call at the end to avoid execution of
# a partially-downloaded program.
main() {
VERSION="${1:-${VERSION:-latest}}"
KERNEL="${KERNEL:-$(uname -s)}"
TARGET="${TARGET:-$(target-triple)}"
DESTINATION="${DESTINATION:-${NEXTSTRAIN_HOME:-$HOME/.nextstrain}/cli-standalone}"
local archive archive_url tmp
archive="standalone-${TARGET}.tar.gz"
archive_url="${NEXTSTRAIN_DOT_ORG}/cli/download/${VERSION}/${archive}"
# Move into a temporary working dir
tmp="$(mktemp -d)"
if ! debug; then
# shellcheck disable=SC2064
trap "rm -rf $tmp" EXIT
fi
pushd "$tmp" >/dev/null
log "Temporary working directory: $tmp"
log "Downloading $archive_url"
curl "$archive_url" \
--fail --show-error --location --proto "=https" \
> "$archive"
log "Extracting $archive"
mkdir standalone
tar xz"$(if-debug v)"f "$archive" -C standalone
if [[ -d "$DESTINATION" ]]; then
log "Removing existing $DESTINATION"
rm -rf "$DESTINATION"
fi
log "Installing to $DESTINATION"
mkdir -p "$(dirname "$DESTINATION")"
mv standalone "$DESTINATION"
popd >/dev/null
if ! debug; then
log "Cleaning up"
rm -rf "$tmp"
fi
log "Checking installation"
local installed_version
if ! installed_version="$("$DESTINATION"/nextstrain --version)"; then
die "installation check failed: unable to run \`\"$DESTINATION\"/nextstrain --version\`; look for error message above."
fi
cat <<~~
______________________________________________________________________________
Nextstrain CLI ($installed_version) installed to $DESTINATION.
~~
if [[ "$(type -p nextstrain 2>/dev/null)" != "$DESTINATION/nextstrain" ]]; then
local shell rc
shell="$(default-shell)"
rc="$(shell-rc "$shell")"
cat <<~~
To make the "nextstrain" command available in your default shell ($shell)
without using the full path, please run these two commands now:
printf '\n%s\n' 'eval "\$("$DESTINATION/nextstrain" init-shell $shell)"' >> $rc
eval "\$("$DESTINATION/nextstrain" init-shell $shell)"
The first adds a line to your shell initialization file ($rc) for future
sessions. The second sets up your current shell session. You only need to run
these once.
~~
fi
}
target-triple() {
local machine vendor os
machine="$(uname -m)"
case "$KERNEL" in
Linux)
[[ "$machine" == x86_64 ]] || die "unsupported architecture: $machine"
vendor=unknown
os=linux-gnu
;;
Darwin)
# Rosetta 2 will not be needed for the standalone version of
# Nextstrain CLI itself as of 8.2.0. The Conda runtime still
# requires it and the Docker runtime can benefit from it, but we
# now check for it in their setup instead.
# -trs, 1 Feb 2024
version-gte 8.2.0 \
|| [[ "$machine" != arm64 ]] \
|| pgrep -qU _oahd 2>/dev/null \
|| die "Rosetta 2 not enabled. Please run:"$'\n\n'" softwareupdate --install-rosetta"$'\n\n'"and then retry this installation of Nextstrain CLI."
# aarch64 builds will only be available starting with 8.2.0. Older
# versions only provide x86_64, which will run under Rosetta.
case "$machine" in
x86_64) ;;
arm64) version-gte 8.2.0 && machine=aarch64 || machine=x86_64;;
*) die "unsupported architecture: $machine";;
esac
vendor=apple
os=darwin
;;
*)
die "unknown kernel: $KERNEL"
esac
echo "$machine-$vendor-$os"
}
version-gte() {
local minimum="$1"
# If $minimum sorts before $VERSION, then $VERSION is at least ≥$minimum.
# Note that non-numeric values sort by byte order, so "latest" and
# "pr-build/123" and so on end up after (greater than) a numeric value like
# 8.1.0. Both macOS/BSD sort and GNU sort support -V.
[[ $(printf '%s\n%s\n' "$minimum" "$VERSION" | sort -V | head -n1) == "$minimum" ]]
}
default-shell() {
local shell
case "$KERNEL" in
Linux)
shell=$(getent passwd "$(id -u)" | awk -F: '{print $NF}');;
Darwin)
shell=$(dscl . -read ~/ UserShell | sed 's/UserShell: //');;
*)
die "unknown kernel: $KERNEL"
esac
shell="$(basename "$shell")"
# Remove any -x.y version from the name
echo "${shell%%-*}"
}
shell-rc() {
local shell="$1"
# Paths below are presumed to be meta-char safe and returned with
# unexpanded ~/… prefixes for a nicer appearance in suggested commands,
# i.e. expansion of ~ is expected to happen after the user pastes and runs
# the commands, rather than here and now.
case "$shell" in
bash)
case "$KERNEL" in
Darwin)
# macOS Terminal.app (and iTerm2.app) always launches login
# shells, i.e. for all windows and tabs, so use the login
# initialization file instead of the interactive one
# because Bash (unlike zsh) only reads one or the other.
# Although it's common practice for users to source their
# bashrc from their bash_profile, we can't rely on this.
#
# The order and conditions on files here follows bash(1) §
# INVOCATION:
#
# After reading [/etc/profile], [bash] looks for
# ~/.bash_profile, ~/.bash_login, and ~/.profile, in that
# order, and reads and executes commands from the first one
# that exists and is readable.
#
if [[ -r ~/.bash_profile ]]; then
# shellcheck disable=SC2088
echo "~/.bash_profile"
elif [[ -r ~/.bash_login ]]; then
# shellcheck disable=SC2088
echo "~/.bash_login"
elif [[ -r ~/.profile ]]; then
# shellcheck disable=SC2088
echo "~/.profile"
else
# If none exist, then we fallback to ~/.bash_profile
# and expect it to be created by our suggested shell
# init commands.
#
# shellcheck disable=SC2088
echo "~/.bash_profile"
fi;;
*)
# shellcheck disable=SC2088
echo "~/.bashrc";;
esac;;
zsh)
echo "${ZDOTDIR:-~}/.zshrc";;
sh)
# sh, whether a historic version or bash in sh-compat mode, doesn't
# use any default startup file for interactive shells. It only
# uses a default startup file for login shells. We'll have to
# assume here that anyone using sh as their default shell will also
# be using login shells appropriately. On macOS, at least, common
# terminal emulators spawn login shells (see commentary above).
# -trs, 26 September 2023
# shellcheck disable=SC2088
echo "~/.profile";;
*)
# A decent guess?
#
# shellcheck disable=SC2088
echo "~/.${shell}rc";;
esac
}
debug() {
[[ -n "${DEBUG:-}" ]]
}
if-debug() {
if debug; then
echo "$@"
fi
}
log() {
echo "--> $*"
}
die() {
echo "ERROR:" "$@" >&2
exit 1
}
main "$@"