1
0
Fork 0
mirror of https://github.com/subosito/flutter-action.git synced 2024-08-16 10:19:50 +02:00
flutter/setup.sh

257 lines
6.9 KiB
Bash
Raw Permalink Normal View History

2022-01-06 08:40:25 +01:00
#!/bin/bash
2022-07-25 13:58:09 +02:00
check_command() {
command -v "$1" >/dev/null 2>&1
}
if ! check_command jq; then
echo "jq not found, please install it, https://stedolan.github.io/jq/download/"
exit 1
fi
2022-01-06 08:51:59 +01:00
OS_NAME=$(echo "$RUNNER_OS" | awk '{print tolower($0)}')
2022-01-06 09:38:04 +01:00
MANIFEST_BASE_URL="https://storage.googleapis.com/flutter_infra_release/releases"
2022-07-24 14:30:59 +02:00
MANIFEST_URL="$MANIFEST_BASE_URL/releases_$OS_NAME.json"
2022-07-26 12:47:54 +02:00
MANIFEST_TEST_FIXTURE="$(dirname -- "${BASH_SOURCE[0]}")/test/releases_$OS_NAME.json"
2022-01-06 08:40:25 +01:00
2022-07-25 13:58:09 +02:00
legacy_wildcard_version() {
if [[ $1 == any ]]; then
jq --arg version "$2" '.releases | map(select(.version | startswith($version) )) | first'
2022-07-24 14:30:59 +02:00
else
2022-07-25 13:58:09 +02:00
jq --arg channel "$1" --arg version "$2" '.releases | map(select(.channel==$channel) | select(.version | startswith($version) )) | first'
2022-07-24 14:30:59 +02:00
fi
2022-01-06 08:40:25 +01:00
}
wildcard_version() {
2022-07-25 14:15:22 +02:00
if [[ $1 == any ]]; then
2022-07-25 13:58:09 +02:00
jq --arg version "$2" --arg arch "$3" '.releases | map(select(.version | startswith($version)) | select(.dart_sdk_arch == null or .dart_sdk_arch == $arch)) | first'
2022-07-24 14:30:59 +02:00
else
2022-07-25 13:58:09 +02:00
jq --arg channel "$1" --arg version "$2" --arg arch "$3" '.releases | map(select(.channel==$channel) | select(.version | startswith($version) ) | select(.dart_sdk_arch == null or .dart_sdk_arch == $arch)) | first'
2022-07-24 14:30:59 +02:00
fi
2022-01-06 08:40:25 +01:00
}
get_version() {
2022-07-25 13:58:09 +02:00
if [[ "$1" == any && "$2" == any ]]; then # latest_version
jq --arg arch "$3" '.releases | map(select(.dart_sdk_arch == null or .dart_sdk_arch == $arch)) | first'
elif [[ "$2" == any ]]; then # latest channel version
jq --arg channel "$1" --arg arch "$3" '.releases | map(select(.channel==$channel) | select(.dart_sdk_arch == null or .dart_sdk_arch == $arch)) | first'
2022-07-24 14:30:59 +02:00
else
2022-07-25 13:58:09 +02:00
wildcard_version "$1" "$2" "$3"
2022-07-24 14:30:59 +02:00
fi
2022-01-06 08:40:25 +01:00
}
2022-07-25 13:58:09 +02:00
normalize_version() {
if [[ "$1" == *.x ]]; then
echo "${1/.x/}"
else
echo "$1"
2022-07-24 14:30:59 +02:00
fi
2022-07-25 13:58:09 +02:00
}
2022-07-24 14:19:31 +02:00
2022-07-25 13:58:09 +02:00
not_found_error() {
echo "Unable to determine Flutter version for channel: $1 version: $2 architecture: $3"
}
2022-07-24 14:19:31 +02:00
2022-07-25 13:58:09 +02:00
transform_path() {
if [[ "$OS_NAME" == windows ]]; then
echo "$1" | sed -e 's/^\///' -e 's/\//\\/g'
2022-07-24 14:30:59 +02:00
else
2022-07-25 13:58:09 +02:00
echo "$1"
2022-07-24 14:30:59 +02:00
fi
2022-01-06 08:40:25 +01:00
}
2022-01-06 09:38:04 +01:00
download_archive() {
2022-07-24 14:30:59 +02:00
archive_url="$MANIFEST_BASE_URL/$1"
archive_name=$(basename "$1")
archive_local="$RUNNER_TEMP/$archive_name"
curl --connect-timeout 15 --retry 5 "$archive_url" >"$archive_local"
# Create the target folder
mkdir -p "$2"
2022-07-25 13:58:09 +02:00
if [[ "$archive_name" == *zip ]]; then
2022-07-24 14:30:59 +02:00
unzip -q -o "$archive_local" -d "$RUNNER_TEMP"
# Remove the folder again so that the move command can do a simple rename
# instead of moving the content into the target folder.
# This is a little bit of a hack since the "mv --no-target-directory"
# linux option is not available here
rm -r "$2"
mv "$RUNNER_TEMP"/flutter "$2"
else
tar xf "$archive_local" -C "$2" --strip-components=1
fi
rm "$archive_local"
2022-01-06 09:38:04 +01:00
}
2022-03-31 07:45:08 +02:00
CACHE_PATH=""
2022-07-24 14:19:31 +02:00
CACHE_KEY=""
PRINT_MODE=""
2022-07-26 12:47:54 +02:00
USE_TEST_FIXTURE=false
ARCH=""
VERSION=""
2022-01-13 11:10:55 +01:00
while getopts 'tc:k:p:a:n:' flag; do
2022-07-24 14:30:59 +02:00
case "$flag" in
c) CACHE_PATH="$OPTARG" ;;
k) CACHE_KEY="$OPTARG" ;;
p) PRINT_MODE="$OPTARG" ;;
2022-07-26 12:47:54 +02:00
t) USE_TEST_FIXTURE=true ;;
a) ARCH="$(echo "$OPTARG" | awk '{print tolower($0)}')" ;;
n) VERSION="$OPTARG" ;;
2022-07-24 14:30:59 +02:00
?) exit 2 ;;
esac
2022-01-13 11:10:55 +01:00
done
2022-07-24 14:30:59 +02:00
ARR_CHANNEL=("${@:$OPTIND:1}")
CHANNEL="${ARR_CHANNEL[0]}"
2022-01-06 08:40:25 +01:00
2022-07-24 14:19:31 +02:00
[[ -z $CHANNEL ]] && CHANNEL=stable
[[ -z $VERSION ]] && VERSION=any
[[ -z $ARCH ]] && ARCH=x64
2022-07-26 07:08:35 +02:00
[[ -z $CACHE_PATH ]] && CACHE_PATH="$RUNNER_TEMP/flutter/:channel:-:version:-:arch:"
[[ -z $CACHE_KEY ]] && CACHE_KEY="flutter-:os:-:channel:-:version:-:arch:-:hash:"
2022-07-24 14:19:31 +02:00
2022-07-25 13:58:09 +02:00
RELEASE_MANIFEST=""
VERSION_MANIFEST=""
get_version_manifest() {
2022-07-25 14:15:22 +02:00
version_normalized=$(normalize_version "$VERSION")
version_manifest=$(echo "$RELEASE_MANIFEST" | get_version "$CHANNEL" "$version_normalized" "$ARCH")
2022-07-25 13:58:09 +02:00
if [[ "$version_manifest" == null ]]; then
2022-07-25 14:15:22 +02:00
version_manifest=$(echo "$RELEASE_MANIFEST" | legacy_wildcard_version "$CHANNEL" "v$version_normalized")
2022-07-25 13:58:09 +02:00
fi
version_arch=$(echo "$version_manifest" | jq -r '.dart_sdk_arch')
2022-07-25 14:15:22 +02:00
2022-07-25 13:58:09 +02:00
if [[ "$version_arch" == null ]]; then
if [[ "$ARCH" == x64 ]]; then
echo "$version_manifest" | jq --arg dart_sdk_arch x64 '.+={dart_sdk_arch:$dart_sdk_arch}'
else
echo ""
fi
else
echo "$version_manifest"
fi
}
expand_key() {
version_channel=$(echo "$VERSION_MANIFEST" | jq -r '.channel')
version_version=$(echo "$VERSION_MANIFEST" | jq -r '.version')
version_arch=$(echo "$VERSION_MANIFEST" | jq -r '.dart_sdk_arch')
version_hash=$(echo "$VERSION_MANIFEST" | jq -r '.hash')
version_sha_256=$(echo "$VERSION_MANIFEST" | jq -r '.sha256')
expanded_key="${1/:channel:/$version_channel}"
expanded_key="${expanded_key/:version:/$version_version}"
expanded_key="${expanded_key/:arch:/$version_arch}"
expanded_key="${expanded_key/:hash:/$version_hash}"
expanded_key="${expanded_key/:sha256:/$version_sha_256}"
expanded_key="${expanded_key/:os:/$OS_NAME}"
echo "$expanded_key"
}
2022-07-25 14:47:52 +02:00
print_version() {
version_debug=$(echo "$VERSION_MANIFEST" | jq -j '.channel,":",.version,":",.dart_sdk_arch')
echo "$CHANNEL:$VERSION:$ARCH|$version_debug"
}
2022-07-24 14:19:31 +02:00
if [[ -n "$PRINT_MODE" ]]; then
2022-07-24 14:30:59 +02:00
if [[ "$CHANNEL" == master ]]; then
if [[ "$PRINT_MODE" == version ]]; then
echo "master:master:$ARCH|master:master:$ARCH"
exit 0
fi
2022-07-26 07:08:35 +02:00
VERSION_MANIFEST="{\"channel\":\"$CHANNEL\",\"version\":\"$CHANNEL\",\"dart_sdk_arch\":\"$ARCH\",\"hash\":\"$CHANNEL\",\"sha256\":\"$CHANNEL\"}"
2022-07-24 14:30:59 +02:00
if [[ "$PRINT_MODE" == cache-key ]]; then
2022-07-26 07:08:35 +02:00
expanded_key=$(expand_key "$CACHE_KEY")
echo "$expanded_key"
exit 0
fi
if [[ "$PRINT_MODE" == cache-path ]]; then
cache_path=$(transform_path "$CACHE_PATH")
expanded_path=$(expand_key "$cache_path")
2022-07-24 14:30:59 +02:00
2022-07-26 07:08:35 +02:00
echo "$expanded_path"
2022-07-24 14:30:59 +02:00
exit 0
fi
exit 1
fi
2022-07-26 12:47:54 +02:00
if [[ "$USE_TEST_FIXTURE" == true ]]; then
RELEASE_MANIFEST=$(cat "$MANIFEST_TEST_FIXTURE")
else
RELEASE_MANIFEST=$(curl --silent --connect-timeout 15 --retry 5 "$MANIFEST_URL")
fi
2022-07-25 13:58:09 +02:00
VERSION_MANIFEST=$(get_version_manifest)
2022-07-24 14:30:59 +02:00
2022-07-25 13:58:09 +02:00
if [[ -z "$VERSION_MANIFEST" ]]; then
2022-07-24 14:30:59 +02:00
not_found_error "$CHANNEL" "$VERSION" "$ARCH"
exit 1
fi
if [[ "$PRINT_MODE" == version ]]; then
2022-07-25 14:47:52 +02:00
print_version
exit 0
2022-07-24 14:30:59 +02:00
fi
if [[ "$PRINT_MODE" == cache-key ]]; then
2022-07-26 07:08:35 +02:00
expanded_key=$(expand_key "$CACHE_KEY")
echo "$expanded_key"
exit 0
fi
if [[ "$PRINT_MODE" == cache-path ]]; then
cache_path=$(transform_path "$CACHE_PATH")
expanded_path=$(expand_key "$cache_path")
2022-07-24 14:30:59 +02:00
2022-07-26 07:08:35 +02:00
echo "$expanded_path"
2022-07-24 14:30:59 +02:00
exit 0
fi
exit 1
2022-07-24 14:19:31 +02:00
fi
CACHE_PATH=$(transform_path "$CACHE_PATH")
SDK_CACHE=$(expand_key "$CACHE_PATH")
2022-07-24 14:30:59 +02:00
PUB_CACHE=$(expand_key "$SDK_CACHE/.pub-cache")
if [[ ! -x "$SDK_CACHE/bin/flutter" ]]; then
if [[ $CHANNEL == master ]]; then
git clone -b master https://github.com/flutter/flutter.git "$SDK_CACHE"
else
RELEASE_MANIFEST=$(curl --silent --connect-timeout 15 --retry 5 "$MANIFEST_URL")
2022-07-25 13:58:09 +02:00
VERSION_MANIFEST=$(get_version_manifest)
if [[ -z "$VERSION_MANIFEST" ]]; then
2022-07-24 14:30:59 +02:00
not_found_error "$CHANNEL" "$VERSION" "$ARCH"
exit 1
fi
2022-07-25 13:58:09 +02:00
2022-07-24 14:30:59 +02:00
ARCHIVE_PATH=$(echo "$VERSION_MANIFEST" | jq -r '.archive')
download_archive "$ARCHIVE_PATH" "$SDK_CACHE"
2022-07-25 14:47:52 +02:00
print_version
2022-07-24 14:30:59 +02:00
fi
2022-01-07 05:47:25 +01:00
fi
2022-07-24 14:30:59 +02:00
{
echo "FLUTTER_ROOT=$SDK_CACHE"
echo "PUB_CACHE=$PUB_CACHE"
} >>"$GITHUB_ENV"
2022-01-06 09:48:07 +01:00
2022-07-24 14:30:59 +02:00
{
echo "$SDK_CACHE/bin"
echo "$SDK_CACHE/bin/cache/dart-sdk/bin"
echo "$PUB_CACHE/bin"
} >>"$GITHUB_PATH"