Skip to content

Commit 6804e61

Browse files
committed
1 parent bc4c5ca commit 6804e61

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

install.sh

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
#!/bin/sh
2+
3+
# The original version of this script is licensed under the MIT license.
4+
# See https://github.com/Masterminds/glide/blob/master/LICENSE for more details
5+
# and copyright notice.
6+
7+
#
8+
# Usage:
9+
#
10+
# To install the latest version of the CLI:
11+
# ./install.sh
12+
#
13+
# To pin a specific release of the CLI:
14+
# ./install.sh 0.9.0
15+
#
16+
17+
PROJECT_NAME="arduino-cli"
18+
19+
# BINDIR represents the local bin location, defaults to ./bin.
20+
LBINDIR=""
21+
DEFAULT_BINDIR="$PWD/bin"
22+
23+
fail() {
24+
echo "$1"
25+
exit 1
26+
}
27+
28+
initDestination() {
29+
if [ -n "$BINDIR" ]; then
30+
if [ ! -d "$BINDIR" ]; then
31+
fail "$BINDIR "'($BINDIR)'" folder not found. Please create it before continuing."
32+
fi
33+
LBINDIR="$BINDIR"
34+
else
35+
if [ ! -d "$DEFAULT_BINDIR" ]; then
36+
mkdir "$DEFAULT_BINDIR"
37+
fi
38+
LBINDIR="$DEFAULT_BINDIR"
39+
fi
40+
echo "Installing in $LBINDIR"
41+
}
42+
43+
initArch() {
44+
ARCH=$(uname -m)
45+
case $ARCH in
46+
armv5*) ARCH="armv5";;
47+
armv6*) ARCH="ARMv6";;
48+
armv7*) ARCH="ARMv7";;
49+
aarch64) ARCH="ARM64";;
50+
x86) ARCH="32bit";;
51+
x86_64) ARCH="64bit";;
52+
i686) ARCH="32bit";;
53+
i386) ARCH="32bit";;
54+
esac
55+
echo "ARCH=$ARCH"
56+
}
57+
58+
initOS() {
59+
OS=$(uname -s)
60+
case "$OS" in
61+
Linux*) OS='Linux' ;;
62+
Darwin*) OS='macOS' ;;
63+
MINGW*) OS='Windows';;
64+
MSYS*) OS='Windows';;
65+
esac
66+
echo "OS=$OS"
67+
}
68+
69+
initDownloadTool() {
70+
if type "curl" > /dev/null; then
71+
DOWNLOAD_TOOL="curl"
72+
elif type "wget" > /dev/null; then
73+
DOWNLOAD_TOOL="wget"
74+
else
75+
fail "You need curl or wget as download tool. Please install it first before continuing"
76+
fi
77+
echo "Using $DOWNLOAD_TOOL as download tool"
78+
}
79+
80+
checkLatestVersion() {
81+
# Use the GitHub releases webpage to find the latest version for this project
82+
# so we don't get rate-limited.
83+
local tag
84+
local regex="[0-9][A-Za-z0-9\.-]*"
85+
local latest_url="https://github.com/arduino/arduino-cli/releases/latest"
86+
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
87+
tag=$(curl -SsL $latest_url | grep -o "<title>Release $regex · arduino/arduino-cli" | grep -o "$regex")
88+
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
89+
tag=$(wget -q -O - $latest_url | grep -o "<title>Release $regex · arduino/arduino-cli" | grep -o "$regex")
90+
fi
91+
if [ "x$tag" = "x" ]; then
92+
echo "Cannot determine latest tag."
93+
exit 1
94+
fi
95+
eval "$1='$tag'"
96+
}
97+
98+
get() {
99+
local url="$2"
100+
local body
101+
local httpStatusCode
102+
echo "Getting $url"
103+
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
104+
httpResponse=$(curl -sL --write-out HTTPSTATUS:%{http_code} "$url")
105+
httpStatusCode=$(echo $httpResponse | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
106+
body=$(echo "$httpResponse" | sed -e 's/HTTPSTATUS\:.*//g')
107+
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
108+
tmpFile=$(mktemp)
109+
body=$(wget --server-response --content-on-error -q -O - "$url" 2> $tmpFile || true)
110+
httpStatusCode=$(cat $tmpFile | awk '/^ HTTP/{print $2}')
111+
fi
112+
if [ "$httpStatusCode" != 200 ]; then
113+
echo "Request failed with HTTP status code $httpStatusCode"
114+
fail "Body: $body"
115+
fi
116+
eval "$1='$body'"
117+
}
118+
119+
getFile() {
120+
local url="$1"
121+
local filePath="$2"
122+
if [ "$DOWNLOAD_TOOL" = "curl" ]; then
123+
httpStatusCode=$(curl -s -w '%{http_code}' -L "$url" -o "$filePath")
124+
elif [ "$DOWNLOAD_TOOL" = "wget" ]; then
125+
body=$(wget --server-response --content-on-error -q -O "$filePath" "$url")
126+
httpStatusCode=$(cat $tmpFile | awk '/^ HTTP/{print $2}')
127+
fi
128+
echo "$httpStatusCode"
129+
}
130+
131+
downloadFile() {
132+
if [ -z $1 ]; then
133+
checkLatestVersion TAG
134+
else
135+
TAG=$1
136+
fi
137+
echo "TAG=$TAG"
138+
# arduino-cli_0.4.0-rc1_Linux_64bit.[tar.gz, zip]
139+
if [ "$OS" = "Windows" ]; then
140+
CLI_DIST="arduino-cli_${TAG}_${OS}_${ARCH}.zip"
141+
else
142+
CLI_DIST="arduino-cli_${TAG}_${OS}_${ARCH}.tar.gz"
143+
fi
144+
echo "CLI_DIST=$CLI_DIST"
145+
DOWNLOAD_URL="https://downloads.arduino.cc/arduino-cli/$CLI_DIST"
146+
CLI_TMP_FILE="/tmp/$CLI_DIST"
147+
echo "Downloading $DOWNLOAD_URL"
148+
httpStatusCode=$(getFile "$DOWNLOAD_URL" "$CLI_TMP_FILE")
149+
if [ "$httpStatusCode" -ne 200 ]; then
150+
echo "Did not find a release for your system: $OS $ARCH"
151+
echo "Trying to find a release using the GitHub API."
152+
LATEST_RELEASE_URL="https://api.github.com/repos/arduino/$PROJECT_NAME/releases/tags/$TAG"
153+
echo "LATEST_RELEASE_URL=$LATEST_RELEASE_URL"
154+
get LATEST_RELEASE_JSON $LATEST_RELEASE_URL
155+
# || true forces this command to not catch error if grep does not find anything
156+
DOWNLOAD_URL=$(echo "$LATEST_RELEASE_JSON" | grep 'browser_' | cut -d\" -f4 | grep "$CLI_DIST") || true
157+
if [ -z "$DOWNLOAD_URL" ]; then
158+
echo "Sorry, we dont have a dist for your system: $OS $ARCH"
159+
fail "You can request one here: https://github.com/Arduino/$PROJECT_NAME/issues"
160+
else
161+
echo "Downloading $DOWNLOAD_URL"
162+
getFile "$DOWNLOAD_URL" "$CLI_TMP_FILE"
163+
fi
164+
fi
165+
}
166+
167+
installFile() {
168+
CLI_TMP="/tmp/$PROJECT_NAME"
169+
mkdir -p "$CLI_TMP"
170+
if [ "$OS" = "Windows" ]; then
171+
unzip -d "$CLI_TMP" "$CLI_TMP_FILE"
172+
else
173+
tar xf "$CLI_TMP_FILE" -C "$CLI_TMP"
174+
fi
175+
CLI_TMP_BIN="$CLI_TMP/$PROJECT_NAME"
176+
cp "$CLI_TMP_BIN" "$LBINDIR"
177+
rm -rf $CLI_TMP
178+
rm -f $CLI_TMP_FILE
179+
}
180+
181+
bye() {
182+
result=$?
183+
if [ "$result" != "0" ]; then
184+
echo "Failed to install $PROJECT_NAME"
185+
fi
186+
exit $result
187+
}
188+
189+
testVersion() {
190+
set +e
191+
CLI="$(which $PROJECT_NAME)"
192+
if [ "$?" = "1" ]; then
193+
echo "$PROJECT_NAME not found. You might want to add "$LBINDIR" to your "'$PATH'
194+
else
195+
# Convert to resolved, absolute paths before comparison
196+
CLI_REALPATH="$(cd -- "$(dirname -- "$CLI")" && pwd -P)"
197+
LBINDIR_REALPATH="$(cd -- "$LBINDIR" && pwd -P)"
198+
if [ "$CLI_REALPATH" != "$LBINDIR_REALPATH" ]; then
199+
echo "An existing $PROJECT_NAME was found at $CLI. Please prepend "$LBINDIR" to your "'$PATH'" or remove the existing one."
200+
fi
201+
fi
202+
203+
set -e
204+
CLI_VERSION=$($LBINDIR/$PROJECT_NAME version)
205+
echo "$CLI_VERSION installed successfully in $LBINDIR"
206+
}
207+
208+
209+
# Execution
210+
211+
#Stop execution on any error
212+
trap "bye" EXIT
213+
initDestination
214+
set -e
215+
initArch
216+
initOS
217+
initDownloadTool
218+
downloadFile $1
219+
installFile
220+
testVersion

0 commit comments

Comments
 (0)