Last active
September 13, 2021 15:36
-
-
Save subfuzion/8125ff0c510945e177e869e024c1fba0 to your computer and use it in GitHub Desktop.
Install specific Dart releases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
VERSION="$1" | |
CHANNEL="${CHANNEL:-dev}" | |
PLATFORM="${PLATFORM:-macos}" | |
ARCH="${ARCH:-x64}" | |
BASEURL="https://storage.googleapis.com/dart-archive/channels" | |
DARTSDK="dartsdk-$PLATFORM-$ARCH-release.zip" | |
DARTDOCS="dartdocs-gen-api.zip" | |
function err() { | |
echo "Error:" "$@" | |
echo "Try to download a valid version from: https://dart.dev/tools/sdk/archive," | |
echo "or run command with no arguments and let it download the latest" | |
exit 1 | |
} | |
function get_latest() { | |
local version=$(curl -s "$BASEURL/$CHANNEL/release/latest/VERSION" \ | |
| grep '"version":' \ | |
| sed -E 's/.*"([^"]+)".*/\1/' \ | |
| grep '^[[:digit:]].*' \ | |
| head -n1) | |
# echo "Checking: $BASEURL/$CHANNEL/release/latest/VERSION" | |
echo "$version" | |
} | |
function download_sdk() { | |
local version="$1" | |
echo "download sdk: $DARTSDK, version: $version" | |
local url="$BASEURL/$CHANNEL/release/$version/sdk/$DARTSDK" | |
echo $url | |
status=$(curl -O -L "$url" -w "%{http_code}") | |
(( status != 200 )) && err "HTTP status code: $status" | |
rm -rf dartsdk | |
unzip "$DARTSDK" -d dartsdk && rm "$DARTSDK" | |
} | |
function download_docs() { | |
local version="$1" | |
echo "download docs: $DARTDOCS" | |
curl -O -L "$BASEURL/$CHANNEL/release/$version/api-docs/$DARTDOCS" | |
rm -rf dartdocs | |
unzip -o "$DARTDOCS" -d dartdocs && rm "$DARTDOCS" | |
command -v open >/dev/null && [ -f dartdocs/gen-dartdocs/index.html ] && open "dartdocs/gen-dartdocs/index.html" | |
} | |
function download() { | |
local version="${1:-$(get_latest)}" | |
echo $version | |
download_sdk "$version" | |
download_docs "$version" | |
} | |
function main() { | |
local target="${1:-$(get_latest)}" | |
local current=$(dart --version 2>&1 | cut -d ' ' -f 4) | |
# or: local current=$(dart --version 2>&1 | sed -n 's/^[^[:digit:]]*\([^[:space:]]*\).*$/\1/p') | |
if [[ "$current" == "$target" ]]; then | |
echo "Latest version already installed: $current" | |
else | |
download "$target" | |
[[ -n "$current" ]] \ | |
&& echo "Upgraded Dart from previous version: $current to: $target" \ | |
|| echo "Installed Dart version: $target" | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment