-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrust-install.sh
More file actions
executable file
·328 lines (292 loc) · 11.8 KB
/
rust-install.sh
File metadata and controls
executable file
·328 lines (292 loc) · 11.8 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env bash
set -eou pipefail
TMPDIR=$(mktemp -d)
INPUT_REPOSITORY=$(basename "$INPUT_REPOSITORY")
if [[ "$INPUT_VERSION" == "latest" ]]; then
echo "Downloading the latest release"
# Set the latest release version
VERSION=$(curl --silent -H "Authorization: token $INPUT_TOKEN" "https://api.github.com/repos/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/latest" | jq -r '.tag_name')
# If the version is in the format like "v3", find the latest semver
if [[ $VERSION =~ ^v[0-9]+$ ]]; then
# Get all releases and sort them semantically
VERSION=$(curl --silent -H "Authorization: token $INPUT_TOKEN" "https://api.github.com/repos/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases" | jq -r '
[.[] | .tag_name] |
map(select(test("^v[0-9]"))) |
sort_by(. | ltrimstr("v") | split(".") | map(tonumber? // 0)) |
reverse |
.[0]
')
fi
else
echo "Downloading version $INPUT_VERSION"
VERSION="$INPUT_VERSION"
fi
echo "VERSION: $VERSION"
echo "INPUT_ADD_PREFIX_TO_VERSION: $INPUT_ADD_PREFIX_TO_VERSION"
NAME_VERSION="${VERSION#"v"}"
if [ "$INPUT_ADD_PREFIX_TO_VERSION" == "true" ]; then
echo "Adding v to the version"
NAME_VERSION="v$NAME_VERSION"
fi
# Determine the operating system and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
echo "OS: $OS"
echo "ARCH: $ARCH"
# Function to try downloading with different filename patterns
function try_download {
local base_url="https://github.com/$INPUT_REPOSITORY_OWNER/$INPUT_REPOSITORY/releases/download/${VERSION}"
local patterns=("$@")
for pattern in "${patterns[@]}"; do
local filename="$pattern"
local url="$base_url/$filename"
echo "Checking for file: $filename"
# Check if file exists using HEAD request (no download)
if curl --fail --silent -H "Authorization: token $INPUT_TOKEN" --head "$url" > /dev/null; then
echo "Found file: $filename"
FILENAME="$filename"
URL="$url"
# Try to find checksum file
local sha256sum_file="${filename}.sha256sum"
local sha512_file="${filename}.sha512"
local sha256sum_url="$base_url/$sha256sum_file"
local sha512_url="$base_url/$sha512_file"
# Try sha256sum first, then sha512
if curl --fail --silent -H "Authorization: token $INPUT_TOKEN" --head "$sha256sum_url" > /dev/null; then
echo "Found checksum file: $sha256sum_file"
SHA256SUM_FILE="$sha256sum_file"
SHA256SUM_URL="$sha256sum_url"
CHECKSUM_TYPE="sha256sum"
elif curl --fail --silent -H "Authorization: token $INPUT_TOKEN" --head "$sha512_url" > /dev/null; then
echo "Found checksum file: $sha512_file"
SHA256SUM_FILE="$sha512_file"
SHA256SUM_URL="$sha512_url"
CHECKSUM_TYPE="sha512"
else
echo "Warning: No checksum file found for $filename"
SHA256SUM_FILE=""
SHA256SUM_URL=""
CHECKSUM_TYPE="none"
fi
return 0
fi
done
echo "No matching files found for any pattern"
return 1
}
# Define filename patterns for each platform
if [[ $OS == "darwin" ]]; then
if [[ $ARCH == "arm64" || $ARCH == "aarch64" ]]; then
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-aarch64-apple-darwin.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_aarch64-apple-darwin.zip"
"${INPUT_REPOSITORY}-${NAME_VERSION}-aarch64-apple-darwin.zip"
# Fallback to x86_64
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-apple-darwin.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-apple-darwin.zip"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-apple-darwin.zip"
)
else
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-apple-darwin.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-apple-darwin.zip"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-apple-darwin.zip"
)
fi
elif [[ $OS == "linux" ]]; then
if [[ $ARCH == "x86_64" ]]; then
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.zst"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.zst"
)
elif [[ $ARCH == "aarch64" || $ARCH == "arm64" ]]; then
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-aarch64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_aarch64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-aarch64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_aarch64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-aarch64-unknown-linux-musl.tar.zst"
"${INPUT_REPOSITORY}_${NAME_VERSION}_aarch64-unknown-linux-musl.tar.zst"
# Fallback to x86_64
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.zst"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.zst"
)
elif [[ $ARCH == "i686" || $ARCH == "i386" ]]; then
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-i686-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_i686-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-i686-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_i686-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-i686-unknown-linux-musl.tar.zst"
"${INPUT_REPOSITORY}_${NAME_VERSION}_i686-unknown-linux-musl.tar.zst"
# Fallback to x86_64
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.gz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.xz"
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-unknown-linux-musl.tar.zst"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-unknown-linux-musl.tar.zst"
)
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
elif [[ $OS == *"mingw64"* ]]; then
if [[ $ARCH == "x86_64" ]]; then
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-pc-windows-gnu.zip"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-pc-windows-gnu.zip"
)
elif [[ $ARCH == "aarch64" || $ARCH == "arm64" ]]; then
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-aarch64-pc-windows-msvc.zip"
"${INPUT_REPOSITORY}_${NAME_VERSION}_aarch64-pc-windows-msvc.zip"
# Fallback to x86_64
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-pc-windows-gnu.zip"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-pc-windows-gnu.zip"
)
elif [[ $ARCH == "i686" || $ARCH == "i386" ]]; then
patterns=(
"${INPUT_REPOSITORY}-${NAME_VERSION}-i686-pc-windows-msvc.zip"
"${INPUT_REPOSITORY}_${NAME_VERSION}_i686-pc-windows-msvc.zip"
# Fallback to x86_64
"${INPUT_REPOSITORY}-${NAME_VERSION}-x86_64-pc-windows-gnu.zip"
"${INPUT_REPOSITORY}_${NAME_VERSION}_x86_64-pc-windows-gnu.zip"
)
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
else
echo "Unsupported operating system: $OS"
exit 1
fi
# Try to find a matching file
if ! try_download "${patterns[@]}"; then
echo "Failed to find any matching files for $OS/$ARCH"
exit 1
fi
OUTPUT_FILE="$TMPDIR/$FILENAME"
SHA256SUM_OUTPUT_FILE="$TMPDIR/$SHA256SUM_FILE"
function download {
local output=$1
local url=$2
for i in $(seq 1 5); do
curl --fail --silent -H "Authorization: token $INPUT_TOKEN" --location --output "$output" "$url" && break
sleep 10
echo "$i retries"
done
}
# Download the binary and its checksum file to the temporary directory
echo "Downloading $URL"
download "$OUTPUT_FILE" "$URL"
if [[ -n "$SHA256SUM_URL" ]]; then
echo "Downloading $SHA256SUM_URL"
download "$SHA256SUM_OUTPUT_FILE" "$SHA256SUM_URL"
else
echo "No checksum file to download"
fi
# Verify the checksum
if [[ "$CHECKSUM_TYPE" == "sha256sum" ]]; then
EXPECTED=$(grep "$FILENAME" "$SHA256SUM_OUTPUT_FILE" | awk '{print $1}')
elif [[ "$CHECKSUM_TYPE" == "sha512" ]]; then
EXPECTED=$(grep "$FILENAME" "$SHA256SUM_OUTPUT_FILE" | awk '{print $1}')
else
echo "No checksum file found, skipping verification."
EXPECTED=""
fi
if [[ -n "$EXPECTED" ]]; then
if [[ $OS == "darwin" ]]; then
if [[ "$CHECKSUM_TYPE" == "sha512" ]]; then
ACTUAL=$(shasum -a 512 "$OUTPUT_FILE" | awk '{print $1}')
else
ACTUAL=$(shasum -a 256 "$OUTPUT_FILE" | awk '{print $1}')
fi
elif [[ $OS == "linux" ]]; then
if [[ "$CHECKSUM_TYPE" == "sha512" ]]; then
ACTUAL=$(sha512sum "$OUTPUT_FILE" | awk '{print $1}')
else
ACTUAL=$(sha256sum "$OUTPUT_FILE" | awk '{print $1}')
fi
elif [[ $OS == *"mingw64"* ]]; then
if [[ "$CHECKSUM_TYPE" == "sha512" ]]; then
ACTUAL=$(sha512sum "$OUTPUT_FILE" | awk '{print $1}')
else
ACTUAL=$(sha256sum "$OUTPUT_FILE" | awk '{print $1}')
fi
else
echo "Unsupported operating system: $OS"
exit 1
fi
if [[ "$EXPECTED" != "$ACTUAL" ]]; then
echo "Checksum verification failed"
echo "Expected: $EXPECTED"
echo "Actual: $ACTUAL"
exit 1
fi
echo "Checksum verification passed"
else
echo "Skipping checksum verification"
fi
# Extract the binary in the temporary directory to a subdirectory named after the repository
if [[ $FILENAME == *.zip ]]; then
unzip "$OUTPUT_FILE" -d "$TMPDIR"
elif [[ $FILENAME == *.tar.gz ]]; then
tar -xzf "$OUTPUT_FILE" -C "$TMPDIR"
elif [[ $FILENAME == *.tar.xz ]]; then
tar -xf "$OUTPUT_FILE" -C "$TMPDIR"
elif [[ $FILENAME == *.tar.zst ]]; then
# Try tar with --zstd flag first, fallback to zstd pipe if not supported
if tar --help 2>&1 | grep -q -- --zstd; then
tar --zstd -xf "$OUTPUT_FILE" -C "$TMPDIR"
else
zstd -dc "$OUTPUT_FILE" | tar -xf - -C "$TMPDIR"
fi
else
echo "Unsupported file format: $FILENAME"
exit 1
fi
# Try to find the binary with the repository name
BINARY_PATH=""
# First, try to find the binary with the exact repository name
if [[ -f "$TMPDIR/$INPUT_REPOSITORY" ]]; then
BINARY_PATH="$TMPDIR/$INPUT_REPOSITORY"
elif [[ -f "$TMPDIR/$INPUT_REPOSITORY.exe" ]]; then
BINARY_PATH="$TMPDIR/$INPUT_REPOSITORY.exe"
else
# Search recursively for the binary with repository name
if [[ $OS == "darwin" ]]; then
# macOS compatible find command
BINARY_PATH=$(find "$TMPDIR" -name "$INPUT_REPOSITORY" -type f -perm +111 | head -n 1)
else
# Linux and other systems
BINARY_PATH=$(find "$TMPDIR" -name "$INPUT_REPOSITORY" -type f -executable | head -n 1)
fi
# If not found, try with .exe extension
if [[ -z "$BINARY_PATH" ]]; then
if [[ $OS == "darwin" ]]; then
BINARY_PATH=$(find "$TMPDIR" -name "$INPUT_REPOSITORY.exe" -type f -perm +111 | head -n 1)
else
BINARY_PATH=$(find "$TMPDIR" -name "$INPUT_REPOSITORY.exe" -type f -executable | head -n 1)
fi
fi
fi
if [[ -z "$BINARY_PATH" ]]; then
echo "Error: Could not find executable binary '$INPUT_REPOSITORY' after extraction"
echo "Contents of $TMPDIR:"
find "$TMPDIR" -type f
exit 1
fi
echo "Found binary: $BINARY_PATH"
# Make the binary executable
chmod +x "$BINARY_PATH"
# Return the binary path
echo "binary_path=$BINARY_PATH" >> "$GITHUB_OUTPUT"