Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·185 lines (163 loc) · 5.23 KB
/
Copy pathinstall
File metadata and controls
executable file
·185 lines (163 loc) · 5.23 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
#!/usr/bin/env bash
# Inspired on official babashka install script https://github.com/babashka/babashka/blob/master/install
set -euo pipefail
version=""
checksum=""
default_install_dir="/usr/local/bin"
install_dir="$default_install_dir"
download_dir=""
print_help() {
echo "Installs latest (or specific) version of eca. Installation directory defaults to $default_install_dir."
echo -e
echo "Usage:"
echo "install [--dir <dir>] [--download-dir <download-dir>] [--version <version> or \"nightly\"] [--checksum <checksum>]"
echo -e
echo "Defaults:"
echo " * Installation directory: ${default_install_dir}"
echo " * Download directory: temporary"
if [[ -z "$checksum" ]]; then
echo " * Checksum: no"
else
echo " * Checksum: ${checksum}"
fi
echo " * Version: <Latest release on github>"
echo -e
echo "Requires curl and unzip (plus sha256sum or shasum when using --checksum)."
exit 1
}
# Prints an error (and an optional hint) to stderr and exits.
error() {
>&2 echo "Error: $1"
if [[ -n "${2:-}" ]]; then
>&2 echo "$2"
fi
exit 1
}
has() {
command -v "$1" >/dev/null 2>&1
}
file_sha256() {
if has shasum; then
shasum -a 256 < "$1" | cut -d' ' -f1
else
sha256sum < "$1" | cut -d' ' -f1
fi
}
while [[ $# -gt 0 ]]
do
key="$1"
case "$key" in
--dir)
install_dir="$2"
shift
shift
;;
--download-dir)
download_dir="$2"
shift
shift
;;
--version)
version="$2"
shift
shift
;;
--checksum)
checksum="$2"
shift
shift
;;
--static)
# Retro compatible, always static if available for that arch.
shift
;;
*) # unknown option
print_help
;;
esac
done
if [[ -z "$download_dir" ]]; then
download_dir="$(mktemp -d)"
trap 'rm -rf "$download_dir"' EXIT
fi
if [[ "$checksum" != "" ]] && [[ "$version" == "" ]]; then
error "options --checksum and --version should be provided together!"
fi
# Check required commands upfront, otherwise `set -e` would make the script
# exit halfway through without saying why when one of them is missing.
missing=""
for cmd in curl unzip; do
has "$cmd" || missing="$missing $cmd"
done
if [[ -n "$checksum" ]] && ! has shasum && ! has sha256sum; then
missing="$missing sha256sum"
fi
if [[ -n "$missing" ]]; then
error "required command(s) not found:$missing" \
"Please install the missing command(s) using your system package manager and run this script again."
fi
case "$(uname -s)" in
Linux*) platform=linux;;
Darwin*) platform=macos;;
*) error "unsupported OS: $(uname -s)" \
"Please download eca manually from https://github.com/editor-code-assistant/eca/releases";;
esac
case "$(uname -m)" in
aarch64) arch=aarch64;;
arm64) arch=aarch64;;
*) arch=amd64;;
esac
if [[ "$version" == "" ]]; then
version_url="https://raw.githubusercontent.com/editor-code-assistant/eca/master/resources/ECA_VERSION"
if ! version="$(curl -fsSL "$version_url")" || [[ -z "$version" ]]; then
error "failed to fetch the latest eca version from $version_url" \
"Check your internet connection or pass a specific version with --version."
fi
fi
ext="zip"
case "$platform-$arch" in
linux-amd64) filename="eca-native-static-$platform-$arch."$ext
;;
*) filename="eca-native-$platform-$arch."$ext
;;
esac
if [[ "$version" == "nightly" ]]; then
download_url="https://github.com/editor-code-assistant/eca-dev-builds/releases/latest/download/$filename"
else
download_url="https://github.com/editor-code-assistant/eca/releases/download/$version/$filename"
fi
# No subshell here on purpose: bash 3.2 (macOS default) doesn't stop on a failing
# subshell even with `set -e`, which would let the script continue after an error.
mkdir -p "$download_dir"
archive="$download_dir/$filename"
echo -e "Downloading $download_url to $download_dir"
if ! curl -fsSL -o "$archive" "$download_url"; then
rm -f "$archive"
error "failed to download $download_url" \
"Check your internet connection and that eca version '$version' is available for $platform-$arch."
fi
if [[ -n "$checksum" ]]; then
actual_checksum="$(file_sha256 "$archive")"
expected_checksum="$(echo "$checksum" | tr '[:upper:]' '[:lower:]')"
if [[ "$actual_checksum" != "$expected_checksum" ]]; then
>&2 echo "Error: failed checksum on $filename"
>&2 echo "Got: $actual_checksum"
>&2 echo "Expected: $expected_checksum"
exit 1
fi
fi
unzip -qqo "$archive" -d "$download_dir"
rm -f "$archive"
if [[ "$download_dir" != "$install_dir" ]]
then
mkdir -p "$install_dir"
if [ -f "$install_dir/eca" ]; then
echo "Moving $install_dir/eca to $install_dir/eca.old"
mv -f "$install_dir/eca" "$install_dir/eca.old"
fi
mv -f "$download_dir/eca" "$install_dir/eca"
fi
if [[ "$version" == "nightly" ]]; then
chmod +x "$install_dir/eca"
fi
echo "Successfully installed eca in $install_dir"