Skip to content

Commit a2ec50c

Browse files
authored
Merge pull request #376 from per1234/sync-install-script
Fix installation script bugs affecting Apple M1 users
2 parents ed64d7a + 0e9c612 commit a2ec50c

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

Diff for: etc/install.sh

+60-37
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
@@ -70,6 +80,7 @@ initDownloadTool() {
7080
echo "Using $DOWNLOAD_TOOL as download tool"
7181
}
7282

83+
# checkLatestVersion() sets the CHECKLATESTVERSION_TAG variable to the latest version
7384
checkLatestVersion() {
7485
# Use the GitHub releases webpage to find the latest version for this project
7586
# so we don't get rate-limited.
@@ -84,26 +95,6 @@ checkLatestVersion() {
8495
echo "Cannot determine latest tag."
8596
exit 1
8697
fi
87-
eval "$1='$CHECKLATESTVERSION_TAG'"
88-
}
89-
90-
get() {
91-
GET_URL="$2"
92-
echo "Getting $GET_URL"
93-
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
94-
GET_HTTP_RESPONSE=$(curl -sL --write-out 'HTTPSTATUS:%{http_code}' "$GET_URL")
95-
GET_HTTP_STATUS_CODE=$(echo "$GET_HTTP_RESPONSE" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
96-
GET_BODY=$(echo "$GET_HTTP_RESPONSE" | sed -e 's/HTTPSTATUS\:.*//g')
97-
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
98-
TMP_FILE=$(mktemp)
99-
GET_BODY=$(wget --server-response --content-on-error -q -O - "$GET_URL" 2>"$TMP_FILE" || true)
100-
GET_HTTP_STATUS_CODE=$(awk '/^ HTTP/{print $2}' "$TMP_FILE")
101-
fi
102-
if [ "$GET_HTTP_STATUS_CODE" != 200 ]; then
103-
echo "Request failed with HTTP status code $GET_HTTP_STATUS_CODE"
104-
fail "Body: $GET_BODY"
105-
fi
106-
eval "$1='$GET_BODY'"
10798
}
10899

109100
getFile() {
@@ -120,44 +111,75 @@ getFile() {
120111

121112
downloadFile() {
122113
if [ -z "$1" ]; then
123-
checkLatestVersion TAG
114+
checkLatestVersion
115+
TAG="$CHECKLATESTVERSION_TAG"
124116
else
125117
TAG=$1
126118
fi
127119
# arduino-lint_0.4.0-rc1_Linux_64bit.[tar.gz, zip]
120+
APPLICATION_DIST_PREFIX="${PROJECT_NAME}_${TAG}_"
128121
if [ "$OS" = "Windows" ]; then
129-
APPLICATION_DIST="${PROJECT_NAME}_${TAG}_${OS}_${ARCH}.zip"
122+
APPLICATION_DIST_EXTENSION=".zip"
130123
else
131-
APPLICATION_DIST="${PROJECT_NAME}_${TAG}_${OS}_${ARCH}.tar.gz"
124+
APPLICATION_DIST_EXTENSION=".tar.gz"
132125
fi
126+
APPLICATION_DIST="${APPLICATION_DIST_PREFIX}${OS}_${ARCH}${APPLICATION_DIST_EXTENSION}"
133127

134128
# Support specifying nightly build versions (e.g., "nightly-latest") via the script argument.
135129
case "$TAG" in
136130
nightly*)
137-
DOWNLOAD_URL="https://downloads.arduino.cc/${PROJECT_NAME}/nightly/${APPLICATION_DIST}"
131+
DOWNLOAD_URL_PREFIX="https://downloads.arduino.cc/${PROJECT_NAME}/nightly/"
138132
;;
139133
*)
140-
DOWNLOAD_URL="https://downloads.arduino.cc/${PROJECT_NAME}/${APPLICATION_DIST}"
134+
DOWNLOAD_URL_PREFIX="https://downloads.arduino.cc/${PROJECT_NAME}/"
141135
;;
142136
esac
137+
DOWNLOAD_URL="${DOWNLOAD_URL_PREFIX}${APPLICATION_DIST}"
143138

144139
INSTALLATION_TMP_FILE="/tmp/$APPLICATION_DIST"
145140
echo "Downloading $DOWNLOAD_URL"
146141
httpStatusCode=$(getFile "$DOWNLOAD_URL" "$INSTALLATION_TMP_FILE")
147142
if [ "$httpStatusCode" -ne 200 ]; then
148-
echo "Did not find a release for your system: $OS $ARCH"
149-
echo "Trying to find a release using the GitHub API."
150-
LATEST_RELEASE_URL="https://api.github.com/repos/${PROJECT_OWNER}/$PROJECT_NAME/releases/tags/$TAG"
151-
echo "LATEST_RELEASE_URL=$LATEST_RELEASE_URL"
152-
get LATEST_RELEASE_JSON "$LATEST_RELEASE_URL"
153-
# || true forces this command to not catch error if grep does not find anything
154-
DOWNLOAD_URL=$(echo "$LATEST_RELEASE_JSON" | grep 'browser_' | cut -d\" -f4 | grep "$APPLICATION_DIST") || true
155-
if [ -z "$DOWNLOAD_URL" ]; then
156-
echo "Sorry, we dont have a dist for your system: $OS $ARCH"
157-
fail "You can request one here: https://github.com/${PROJECT_OWNER}/$PROJECT_NAME/issues"
158-
else
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}"
159147
echo "Downloading $DOWNLOAD_URL"
160-
getFile "$DOWNLOAD_URL" "$INSTALLATION_TMP_FILE"
148+
httpStatusCode=$(getFile "$DOWNLOAD_URL" "$INSTALLATION_TMP_FILE")
149+
fi
150+
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
161183
fi
162184
fi
163185
}
@@ -214,6 +236,7 @@ initDestination
214236
set -e
215237
initArch
216238
initOS
239+
initFallbackArch
217240
initDownloadTool
218241
downloadFile "$1"
219242
installFile

0 commit comments

Comments
 (0)