-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
install.sh
executable file
·208 lines (171 loc) · 4.74 KB
/
install.sh
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
#!/usr/bin/env bash
# Copyright 2024 Science project contributors.
# Licensed under the Apache License, Version 2.0 (see LICENSE).
set -euo pipefail
COLOR_RED="\x1b[31m"
COLOR_GREEN="\x1b[32m"
COLOR_YELLOW="\x1b[33m"
COLOR_RESET="\x1b[0m"
function log() {
echo -e "$@" 1>&2
}
function die() {
(($# > 0)) && log "${COLOR_RED}$*${COLOR_RESET}"
exit 1
}
function green() {
(($# > 0)) && log "${COLOR_GREEN}$*${COLOR_RESET}"
}
function warn() {
(($# > 0)) && log "${COLOR_YELLOW}$*${COLOR_RESET}"
}
function ensure_cmd() {
local cmd="$1"
command -v "$cmd" > /dev/null || die "This script requires the ${cmd} binary to be on \$PATH."
}
ISSUES_URL="https://github.com/a-scie/lift/issues"
_GC=()
ensure_cmd rm
function gc() {
if (($# > 0)); then
_GC+=("$@")
else
# Check if $_GC has members to avoid "unbound variable" warnings if gc w/ arguments is never
# called.
if ! [ ${#_GC[@]} -eq 0 ]; then
rm -rf "${_GC[@]}"
fi
fi
}
trap gc EXIT
ensure_cmd uname
function determine_os() {
local os
os="$(uname -s)"
if [[ "${os}" =~ [Ll]inux ]]; then
echo linux
elif [[ "${os}" =~ [Dd]arwin ]]; then
echo macos
elif [[ "${os}" =~ [Ww]indow|[Mm][Ii][Nn][Gg] ]]; then
# Powershell reports something like: Windows_NT
# Git bash reports something like: MINGW64_NT-10.0-22621
echo windows
else
die "Science is not supported on this OS (${os}). Please reach out at ${ISSUES_URL} for help."
fi
}
OS="$(determine_os)"
function determine_arch() {
# Map platform architecture to released binary architecture.
read_arch="$(uname -m)"
case "$read_arch" in
x86_64*) echo "x86_64" ;;
amd64*) echo "x86_64" ;;
arm64*) echo "aarch64" ;;
aarch64*) echo "aarch64" ;;
*) die "unknown arch: ${read_arch}" ;;
esac
}
ensure_cmd basename
ensure_cmd curl
function fetch() {
local url="$1"
local dest_dir="$2"
local dest
dest="${dest_dir}/$(basename "${url}")"
# N.B. Curl is included on Windows 10+:
# https://devblogs.microsoft.com/commandline/tar-and-curl-come-to-windows/
curl --proto '=https' --tlsv1.2 -SfL --progress-bar -o "${dest}" "${url}"
}
ensure_cmd $([[ "${OS}" == "macos" ]] && echo "shasum" || echo "sha256sum")
function sha256() {
if [[ "${OS}" == "macos" ]]; then
shasum --algorithm 256 "$@"
else
sha256sum "$@"
fi
}
ensure_cmd dirname
ensure_cmd mktemp
ensure_cmd install
ensure_cmd tr
ensure_cmd mv
function install_from_url() {
local url="$1"
local dest="$2"
local workdir
workdir="$(mktemp -d)"
gc "${workdir}"
fetch "${url}.sha256" "${workdir}"
fetch "${url}" "${workdir}" && green "Download completed successfully"
(
cd "${workdir}"
if [[ "${OS}" == "windows" ]]; then
# N.B. Windows sha256sum is sensitive to trailing \r in files.
cat *.sha256 | tr -d "\r" > out.sanitized
mv -f out.sanitized *.sha256
fi
sha256 -c --status ./*.sha256 &&
green "Download matched it's expected sha256 fingerprint, proceeding" ||
die "Download from ${url} did not match the fingerprint at ${url}.sha256"
)
rm "${workdir}/"*.sha256
if [[ "${OS}" == "macos" ]]; then
mkdir -p "$(dirname "${dest}")"
install -m 755 "${workdir}/"* "${dest}"
else
install -D -m 755 "${workdir}/"* "${dest}"
fi
green "Installed ${url} to ${dest}"
}
ensure_cmd cat
function usage() {
cat << __EOF__
Usage: $0
Installs the \`science\` binary.
-h | --help: Print this help message.
-d | --bin-dir:
The directory to install the science binary in, "~/.local/bin" by default.
-V | --version:
The version of the science binary to install, the latest version by default.
The available versions can be seen at:
https://github.com/a-scie/lift/releases
__EOF__
}
INSTALL_PREFIX="${HOME}/.local/bin"
VERSION="latest/download"
# Parse arguments.
while (($# > 0)); do
case "$1" in
--help | -h)
usage
exit 0
;;
--bin-dir | -d)
INSTALL_PREFIX="$2"
shift
;;
--version | -V)
VERSION="download/v${2}"
shift
;;
*)
usage
die "Unexpected argument: ${1}\n"
;;
esac
shift
done
ARCH="$(determine_arch)"
DIRSEP=$([[ "${OS}" == "windows" ]] && echo "\\" || echo "/")
EXE_EXT=$([[ "${OS}" == "windows" ]] && echo ".exe" || echo "")
INSTALL_DEST="${INSTALL_PREFIX}${DIRSEP}science${EXE_EXT}"
DL_URL="https://github.com/a-scie/lift/releases/${VERSION}/science-fat-${OS}-${ARCH}${EXE_EXT}"
green "Download URL is: ${DL_URL}"
install_from_url "${DL_URL}" "${INSTALL_DEST}"
# Warn if the install prefix is not on $PATH.
if ! [[ ":$PATH:" == *":${INSTALL_PREFIX}:"* ]]; then
warn "WARNING: ${INSTALL_PREFIX} is not detected on \$PATH"
warn "You'll either need to invoke ${INSTALL_DEST} explicitly or else add ${INSTALL_PREFIX} \
to your shell's PATH."
fi