2018-07-08 17:34:17 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
bindir="$(dirname $0)"
|
|
|
|
basedir="$(dirname $bindir)"
|
|
|
|
# Check if GRADLE_VERSION_DIR/CACHEDIR is set from environment
|
|
|
|
if [ -z "$GRADLE_VERSION_DIR" ]; then
|
|
|
|
gradle_version_dir="${basedir}/versions"
|
|
|
|
else
|
|
|
|
gradle_version_dir="$GRADLE_VERSION_DIR"
|
|
|
|
fi
|
2021-03-05 00:02:21 +01:00
|
|
|
BUILDSERVER_CACHEDIR=/vagrant/cache
|
2018-07-08 17:34:17 +02:00
|
|
|
if [ -n "$CACHEDIR" ]; then
|
|
|
|
cachedir="$CACHEDIR"
|
2021-03-05 00:02:21 +01:00
|
|
|
elif [ -d $BUILDSERVER_CACHEDIR ]; then
|
|
|
|
cachedir=$BUILDSERVER_CACHEDIR
|
2018-07-08 17:34:17 +02:00
|
|
|
fi
|
|
|
|
args=("$@")
|
|
|
|
|
2024-06-05 20:06:25 +02:00
|
|
|
checksums_url="https://gitlab.com/fdroid/gradle-transparency-log/-/raw/master/checksums.json"
|
|
|
|
checksum_cache="$(mktemp -d)/gradle_checksums.json"
|
|
|
|
curl -Lo $checksum_cache $checksums_url
|
|
|
|
|
2018-07-08 17:34:17 +02:00
|
|
|
run_gradle() {
|
|
|
|
if [ ! -d "${gradle_version_dir}/${v_found}" ]; then
|
|
|
|
download_gradle ${v_found}
|
|
|
|
fi
|
2023-12-13 21:08:12 +01:00
|
|
|
# shellcheck disable=SC2145
|
2018-07-13 17:00:45 +02:00
|
|
|
echo "Running ${gradle_version_dir}/${v_found}/bin/gradle ${args[@]}"
|
2018-07-13 17:02:48 +02:00
|
|
|
"${gradle_version_dir}/${v_found}/bin/gradle" "${args[@]}"
|
|
|
|
exit $?
|
2018-07-08 17:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
download_gradle() {
|
|
|
|
URL="https://downloads.gradle.org/distributions/gradle-${1}-bin.zip"
|
|
|
|
shasum=$(get_sha $1)
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
echo "No hash for gradle version $1! Exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ -n "${cachedir}" ] && [ -e "${cachedir}/gradle-$1-bin.zip" ]; then
|
|
|
|
echo "Using cached ${cachedir}/gradle-$1-bin.zip ..."
|
|
|
|
gradle_zip="${cachedir}/gradle-$1-bin.zip"
|
|
|
|
else
|
|
|
|
echo "Downloading missing gradle version $1"
|
2021-03-05 00:02:21 +01:00
|
|
|
echo cachedir $cachedir
|
|
|
|
if [[ -n "${cachedir}" && ! -d "${cachedir}" ]]; then
|
|
|
|
mkdir -p "${cachedir}"
|
|
|
|
fi
|
|
|
|
if [[ -n "${cachedir}" && -d "${cachedir}" && -w "${cachedir}" ]]; then
|
2018-07-08 17:34:17 +02:00
|
|
|
tmpdir="${cachedir}"
|
|
|
|
else
|
|
|
|
tmpdir=$(mktemp -d)
|
|
|
|
fi
|
2022-10-30 23:38:26 +01:00
|
|
|
curl -o "${tmpdir}/gradle-$1-bin.zip" --silent --fail --show-error --location --retry 3 --retry-all-errors "${URL}"
|
2018-07-13 17:01:14 +02:00
|
|
|
gradle_zip="${tmpdir}/gradle-$1-bin.zip"
|
2018-07-08 17:34:17 +02:00
|
|
|
fi
|
|
|
|
echo "${shasum} ${gradle_zip}" | sha256sum -c -
|
|
|
|
if [ $? != 0 ]; then
|
|
|
|
echo "gradle download checksum mismatch! Exiting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
mkdir -p "${gradle_version_dir}/"
|
|
|
|
unzip -q -d "${gradle_version_dir}" "${gradle_zip}"
|
|
|
|
mv "${gradle_version_dir}/gradle-$1" "${gradle_version_dir}/${v_found}"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_sha() {
|
2024-06-05 20:06:25 +02:00
|
|
|
sha=$(sed -n "/gradle-${1}-bin/,+2p" $checksum_cache | sed -n 's/.*"sha256": "\(.*\)"/\1/p')
|
|
|
|
[[ -z $sha ]] && exit 1
|
|
|
|
echo $sha
|
2018-07-08 17:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
contains() {
|
2018-07-13 17:02:48 +02:00
|
|
|
local e
|
|
|
|
for e in $2; do
|
|
|
|
[[ $e == $1 ]] && return 0;
|
|
|
|
done
|
|
|
|
return 1
|
2018-07-08 17:34:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# key-value pairs of what gradle version (value) each gradle plugin version
|
|
|
|
# (key) should accept. plugin versions are actually prefixes and catch sub-
|
|
|
|
# versions as well. Pairs are taken from:
|
2022-05-25 04:39:36 +02:00
|
|
|
# https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
|
2024-01-31 16:08:04 +01:00
|
|
|
d_gradle_plugin_ver_k=(8.4 8.3 8.2 8.1 8.0 7.4 7.3 7.2.0 7.1 7.0 4.2 4.1 4.0 3.6 3.5 3.4 3.3 3.2 3.1 3.0 2.3 2.2 2.1.3 2.1 2.0)
|
2024-02-04 13:42:43 +01:00
|
|
|
d_plugin_min_gradle_v=(8.6 8.4 8.2 8.0 8.0 7.5 7.4 7.3.3 7.2 7.0.2 6.7.1 6.5 6.1.1 5.6.4 5.4.1 5.1.1 4.10.1 4.6 4.4 4.1 3.3 2.14.1 2.14.1 2.12 2.12 2.4 2.4 2.3 2.2.1 2.2.1 2.1 2.1 1.12 1.12 1.12 1.11 1.10 1.9 1.8 1.6 1.6 1.4 1.4)
|
2018-07-08 17:34:17 +02:00
|
|
|
|
|
|
|
# All gradle versions we know about
|
2024-06-05 20:06:25 +02:00
|
|
|
plugin_v=(sed -n -E 's/.*gradle-([0-9.]+)-bin.*/\1/p' $checksum_cache)
|
2018-07-08 17:34:17 +02:00
|
|
|
|
|
|
|
v_all=${plugin_v[@]}
|
|
|
|
|
2023-05-19 18:12:23 +02:00
|
|
|
# Earliest file takes priority
|
|
|
|
# Last key takes priority if there are duplicates (matching java.util.Properties)
|
2018-07-08 17:34:17 +02:00
|
|
|
for f in {.,..}/gradle/wrapper/gradle-wrapper.properties; do
|
2018-07-13 17:02:48 +02:00
|
|
|
[[ -f $f ]] || continue
|
2020-01-21 11:15:51 +01:00
|
|
|
while IFS='' read -r line || [ -n "$line" ]; do
|
2020-08-19 16:28:58 +02:00
|
|
|
line=$(printf "$line" | tr -d '\r') # strip Windows linefeeds
|
2019-09-22 07:56:39 +02:00
|
|
|
if [[ $line == 'distributionUrl='* ]]; then
|
|
|
|
wrapper_ver=${line#*/gradle-}
|
|
|
|
wrapper_ver=${wrapper_ver%-*.zip}
|
2018-07-13 17:02:48 +02:00
|
|
|
fi
|
|
|
|
done < $f
|
2023-05-19 18:12:23 +02:00
|
|
|
[[ -n $wrapper_ver ]] && break
|
2018-07-08 17:34:17 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -n $wrapper_ver ]]; then
|
2018-07-13 17:02:48 +02:00
|
|
|
v_found=$wrapper_ver
|
|
|
|
echo "Found $v_found via distributionUrl"
|
|
|
|
run_gradle
|
2018-07-08 17:34:17 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Earliest takes priority
|
2019-12-23 02:02:28 +01:00
|
|
|
for f in {.,..}/build.gradle{,.kts}; do
|
2018-07-13 17:02:48 +02:00
|
|
|
[[ -f $f ]] || continue
|
2020-01-21 11:15:51 +01:00
|
|
|
while IFS='' read -r line || [ -n "$line" ]; do
|
2020-08-19 16:28:58 +02:00
|
|
|
line=$(printf "$line" | tr -d '\r') # strip Windows linefeeds
|
2019-09-22 07:56:39 +02:00
|
|
|
if [[ -z "$plugin_pver" && $line == *'com.android.tools.build:gradle:'* ]]; then
|
|
|
|
plugin_pver=${line#*[\'\"]com.android.tools.build:gradle:}
|
|
|
|
plugin_pver=${plugin_pver%[\'\"]*}
|
|
|
|
elif [[ -z "$wrapper_ver" && $line == *'gradleVersion = '* ]]; then
|
|
|
|
wrapper_ver=${line#*gradleVersion*=*[\'\"]}
|
|
|
|
wrapper_ver=${wrapper_ver%[\'\"]*}
|
2018-07-13 17:02:48 +02:00
|
|
|
fi
|
|
|
|
done < $f
|
2018-07-08 17:34:17 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -n $wrapper_ver ]]; then
|
2018-07-13 17:02:48 +02:00
|
|
|
v_found=$wrapper_ver
|
|
|
|
echo "Found $v_found via gradleVersion"
|
|
|
|
run_gradle
|
2018-07-08 17:34:17 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n $plugin_pver ]]; then
|
2018-07-13 17:02:48 +02:00
|
|
|
i=0
|
|
|
|
match=false
|
2023-12-13 21:08:12 +01:00
|
|
|
for k in "${d_gradle_plugin_ver_k[@]}"; do
|
2018-07-13 17:02:48 +02:00
|
|
|
if [[ $plugin_pver == ${k}* ]]; then
|
2023-08-04 16:30:56 +02:00
|
|
|
plugin_ver=${d_plugin_min_gradle_v[$i]}
|
2018-07-13 17:02:48 +02:00
|
|
|
match=true
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
let i++
|
|
|
|
done
|
|
|
|
if $match; then
|
|
|
|
v_found=$plugin_ver
|
|
|
|
echo "Found $v_found via gradle plugin version $k"
|
|
|
|
fi
|
2018-07-08 17:34:17 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Find the highest version available
|
|
|
|
for v in ${plugin_v[*]}; do
|
2018-07-13 17:02:48 +02:00
|
|
|
if contains $v "${v_all[*]}"; then
|
|
|
|
v_def=$v
|
|
|
|
break
|
|
|
|
fi
|
2018-07-08 17:34:17 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -z $v_found ]]; then
|
2018-07-13 17:02:48 +02:00
|
|
|
echo "No suitable gradle version found - defaulting to $v_def"
|
|
|
|
v_found=$v_def
|
2018-07-08 17:34:17 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
run_gradle
|