Skip to content

Commit 3c5897d

Browse files
authored
Merge pull request #223 from per1234/install-script-m1-support
Fallback to x86-64 release when macOS ARM 64-bit build not available
2 parents 9b93581 + d8c59e3 commit 3c5897d

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

other/installation-script/install.sh

+56-28
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ initArch() {
4040
armv6*) ARCH="ARMv6" ;;
4141
armv7*) ARCH="ARMv7" ;;
4242
aarch64) ARCH="ARM64" ;;
43+
arm64) ARCH="ARM64" ;;
4344
x86) ARCH="32bit" ;;
4445
x86_64) ARCH="64bit" ;;
4546
i686) ARCH="32bit" ;;
@@ -48,6 +49,15 @@ initArch() {
4849
echo "ARCH=$ARCH"
4950
}
5051

52+
initFallbackArch() {
53+
case "${OS}_${ARCH}" in
54+
macOS_ARM64)
55+
# Rosetta 2 allows applications built for x86-64 hosts to run on the ARM 64-bit M1 processor
56+
FALLBACK_ARCH='64bit'
57+
;;
58+
esac
59+
}
60+
5161
initOS() {
5262
OS=$(uname -s)
5363
case "$OS" in
@@ -107,52 +117,69 @@ downloadFile() {
107117
TAG=$1
108118
fi
109119
# arduino-lint_0.4.0-rc1_Linux_64bit.[tar.gz, zip]
120+
APPLICATION_DIST_PREFIX="${PROJECT_NAME}_${TAG}_"
110121
if [ "$OS" = "Windows" ]; then
111-
APPLICATION_DIST="${PROJECT_NAME}_${TAG}_${OS}_${ARCH}.zip"
122+
APPLICATION_DIST_EXTENSION=".zip"
112123
else
113-
APPLICATION_DIST="${PROJECT_NAME}_${TAG}_${OS}_${ARCH}.tar.gz"
124+
APPLICATION_DIST_EXTENSION=".tar.gz"
114125
fi
126+
APPLICATION_DIST="${APPLICATION_DIST_PREFIX}${OS}_${ARCH}${APPLICATION_DIST_EXTENSION}"
115127

116128
# Support specifying nightly build versions (e.g., "nightly-latest") via the script argument.
117129
case "$TAG" in
118130
nightly*)
119-
DOWNLOAD_URL="https://downloads.arduino.cc/${PROJECT_NAME}/nightly/${APPLICATION_DIST}"
131+
DOWNLOAD_URL_PREFIX="https://downloads.arduino.cc/${PROJECT_NAME}/nightly/"
120132
;;
121133
*)
122-
DOWNLOAD_URL="https://downloads.arduino.cc/${PROJECT_NAME}/${APPLICATION_DIST}"
134+
DOWNLOAD_URL_PREFIX="https://downloads.arduino.cc/${PROJECT_NAME}/"
123135
;;
124136
esac
137+
DOWNLOAD_URL="${DOWNLOAD_URL_PREFIX}${APPLICATION_DIST}"
125138

126139
INSTALLATION_TMP_FILE="/tmp/$APPLICATION_DIST"
127140
echo "Downloading $DOWNLOAD_URL"
128141
httpStatusCode=$(getFile "$DOWNLOAD_URL" "$INSTALLATION_TMP_FILE")
129142
if [ "$httpStatusCode" -ne 200 ]; then
130-
echo "Did not find a release for your system: $OS $ARCH"
131-
echo "Trying to find a release using the GitHub API."
132-
133-
LATEST_RELEASE_URL="https://api.github.com/repos/${PROJECT_OWNER}/$PROJECT_NAME/releases/tags/$TAG"
134-
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
135-
HTTP_RESPONSE=$(curl -sL --write-out 'HTTPSTATUS:%{http_code}' "$LATEST_RELEASE_URL")
136-
HTTP_STATUS_CODE=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
137-
BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
138-
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
139-
TMP_FILE=$(mktemp)
140-
BODY=$(wget --server-response --content-on-error -q -O - "$LATEST_RELEASE_URL" 2>"$TMP_FILE" || true)
141-
HTTP_STATUS_CODE=$(awk '/^ HTTP/{print $2}' "$TMP_FILE")
142-
fi
143-
if [ "$HTTP_STATUS_CODE" != 200 ]; then
144-
echo "Request failed with HTTP status code $HTTP_STATUS_CODE"
145-
fail "Body: $BODY"
143+
if [ -n "$FALLBACK_ARCH" ]; then
144+
echo "$OS $ARCH release not currently available. Checking for alternative $OS $FALLBACK_ARCH release for your system."
145+
FALLBACK_APPLICATION_DIST="${APPLICATION_DIST_PREFIX}${OS}_${FALLBACK_ARCH}${APPLICATION_DIST_EXTENSION}"
146+
DOWNLOAD_URL="${DOWNLOAD_URL_PREFIX}${FALLBACK_APPLICATION_DIST}"
147+
echo "Downloading $DOWNLOAD_URL"
148+
httpStatusCode=$(getFile "$DOWNLOAD_URL" "$INSTALLATION_TMP_FILE")
146149
fi
147150

148-
# || true forces this command to not catch error if grep does not find anything
149-
DOWNLOAD_URL=$(echo "$BODY" | grep 'browser_' | cut -d\" -f4 | grep "$APPLICATION_DIST") || true
150-
if [ -z "$DOWNLOAD_URL" ]; then
151-
echo "Sorry, we dont have a dist for your system: $OS $ARCH"
152-
fail "You can request one here: https://github.com/${PROJECT_OWNER}/$PROJECT_NAME/issues"
153-
else
154-
echo "Downloading $DOWNLOAD_URL"
155-
getFile "$DOWNLOAD_URL" "$INSTALLATION_TMP_FILE"
151+
if [ "$httpStatusCode" -ne 200 ]; then
152+
echo "Did not find a release for your system: $OS $ARCH"
153+
echo "Trying to find a release using the GitHub API."
154+
155+
LATEST_RELEASE_URL="https://api.github.com/repos/${PROJECT_OWNER}/$PROJECT_NAME/releases/tags/$TAG"
156+
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
157+
HTTP_RESPONSE=$(curl -sL --write-out 'HTTPSTATUS:%{http_code}' "$LATEST_RELEASE_URL")
158+
HTTP_STATUS_CODE=$(echo "$HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
159+
BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
160+
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
161+
TMP_FILE=$(mktemp)
162+
BODY=$(wget --server-response --content-on-error -q -O - "$LATEST_RELEASE_URL" 2>"$TMP_FILE" || true)
163+
HTTP_STATUS_CODE=$(awk '/^ HTTP/{print $2}' "$TMP_FILE")
164+
fi
165+
if [ "$HTTP_STATUS_CODE" != 200 ]; then
166+
echo "Request failed with HTTP status code $HTTP_STATUS_CODE"
167+
fail "Body: $BODY"
168+
fi
169+
170+
# || true forces this command to not catch error if grep does not find anything
171+
DOWNLOAD_URL=$(echo "$BODY" | grep 'browser_' | cut -d\" -f4 | grep "$APPLICATION_DIST") || true
172+
if [ -z "$DOWNLOAD_URL" ]; then
173+
DOWNLOAD_URL=$(echo "$BODY" | grep 'browser_' | cut -d\" -f4 | grep "$FALLBACK_APPLICATION_DIST") || true
174+
fi
175+
176+
if [ -z "$DOWNLOAD_URL" ]; then
177+
echo "Sorry, we dont have a dist for your system: $OS $ARCH"
178+
fail "You can request one here: https://github.com/${PROJECT_OWNER}/$PROJECT_NAME/issues"
179+
else
180+
echo "Downloading $DOWNLOAD_URL"
181+
getFile "$DOWNLOAD_URL" "$INSTALLATION_TMP_FILE"
182+
fi
156183
fi
157184
fi
158185
}
@@ -209,6 +236,7 @@ initDestination
209236
set -e
210237
initArch
211238
initOS
239+
initFallbackArch
212240
initDownloadTool
213241
downloadFile "$1"
214242
installFile

0 commit comments

Comments
 (0)