Last active
September 26, 2023 19:26
-
-
Save milo-minderbinder/03be468b1fc56ce28934f2c2af522595 to your computer and use it in GitHub Desktop.
Creates a Gradle wrapper in $PWD for the latest Gradle version (optionally, latest for the given major version, e.g. `./create-gradle-wrapper.bash 4` will fetch the latest Gradle 4.X wrapper).
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
#!/usr/bin/env bash | |
set -o errexit -o errtrace -o noclobber -o nounset -o pipefail | |
trap 'e=$?; if [ "$e" -ne "0" ]; then printf "LINE %s: exit %s <- %s%s\\n" "$BASH_LINENO" "$e" "${BASH_COMMAND}" "$(printf " <- %s" "${FUNCNAME[@]:-main}")" 1>&2; fi' EXIT | |
PROGNAME="${0##*/}" | |
contains_value() { | |
local value | |
value="$1" | |
shift | |
for arg in "$@"; do | |
if [ "$value" == "$arg" ]; then | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
get_versions() { | |
local all_versions_data | |
local versions | |
all_versions_data="$(curl -s https://services.gradle.org/versions/all)" | |
versions="$(printf '%s' "${all_versions_data}" | \ | |
jq -r 'map(select([.snapshot, .nightly, .releaseNightly, .activeRc, .broken, (.rcFor != ""), (.milestoneFor != ""), (.version | test("milestone|rc"; "i")) | not] | all) | .version) | join("\n")' | sort -V)" | |
printf '%s\n' "${versions}" | |
} | |
get_latest_version() { | |
local selected_version | |
local versions | |
versions="$(get_versions | sort -rV)" | |
if [[ $# -eq 1 ]]; then | |
versions="$(printf '%s\n' "$versions" | grep -E "^$1(\..+)?$")" | |
selected_version="$(printf '%s\n' "$versions" | head -n 1)" | |
else | |
selected_version="$(curl -s 'https://services.gradle.org/versions/current' | jq -r '.version')" | |
fi | |
1>&2 printf 'selected version: %s\n' "$selected_version" | |
1>&2 printf 'available versions:\n%s\n' "$versions" | |
printf '%s\n' "$selected_version" | |
} | |
usage() { | |
>&2 cat <<EOF | |
NAME | |
${PROGNAME} -- Utility to create a Gradle wrapper for the latest release of Gradle. | |
SYNOPSIS | |
${PROGNAME} [-h] -l [<MAJOR VERSION>[.<MINOR VERSION>]] | |
DESCRIPTION | |
${PROGNAME} is a utility to create a Gradle wrapper for the latest release of Gradle, | |
with latest version optionally filtered for the major or minor release version if provided. | |
The options are as follows: | |
-h print this help and exit | |
-l list matching version without creating Gradle wrapper | |
EXAMPLES | |
Create a wrapper for the latest Gradle release version: | |
>>> ${PROGNAME} | |
Alternatively, you can print the latest Gradle release version without creating a wrapper: | |
>>> ${PROGNAME} -l | |
Or you can get the latest 6.X release with: | |
>>> ${PROGNAME} -l 6 | |
You can also specify the minor version, too: | |
>>> ${PROGNAME} -l 7.1 | |
EOF | |
} | |
LIST_ONLY="" | |
# options which must be given | |
REQUIRED_OPTS=() | |
# tracks which options have been provided | |
PROVIDED_OPTS=() | |
# options which may be given more than once | |
ADDITIVE_OPTS=() | |
# minimum number of positional arguments allowed (ignored if empty) | |
MIN_POSITIONAL_ARGS="" | |
# maximum number of positional arguments allowed (ignored if empty) | |
MAX_POSITIONAL_ARGS="1" | |
while getopts 'hl' opt; do | |
if ! contains_value "$opt" "${ADDITIVE_OPTS[@]:-}" && contains_value "$opt" "${PROVIDED_OPTS[@]:-}"; then | |
>&2 printf '%s: option cannot be given more than once -- %s\n' "$0" "$opt" | |
usage | |
exit 1 | |
fi | |
case "$opt" in | |
h) | |
usage | |
exit 0 | |
;; | |
l) | |
LIST_ONLY="y" | |
;; | |
*) | |
usage | |
exit 1 | |
;; | |
esac | |
PROVIDED_OPTS+=("$opt") | |
done | |
shift $((OPTIND - 1)) | |
if [ -n "$MIN_POSITIONAL_ARGS" ] && [ "$#" -lt "$MIN_POSITIONAL_ARGS" ]; then | |
>&2 printf '%s: at least %d positional argument(s) are needed but got %d -- %s\n' "$0" "$MIN_POSITIONAL_ARGS" "$#" "$(printf "'%s' " "$@")" | |
usage | |
exit 1 | |
fi | |
if [ -n "$MAX_POSITIONAL_ARGS" ] && [ "$#" -gt "$MAX_POSITIONAL_ARGS" ]; then | |
>&2 printf '%s: up to %d positional argument(s) are allowed but got %d -- %s\n' "$0" "$MAX_POSITIONAL_ARGS" "$#" "$(printf "'%s' " "$@")" | |
usage | |
exit 1 | |
fi | |
MISSING_OPTS=() | |
if [ "${#REQUIRED_OPTS[@]}" -gt "0" ]; then | |
for opt in "${REQUIRED_OPTS[@]}"; do | |
if ! contains_value "$opt" "${PROVIDED_OPTS[@]:-}"; then | |
MISSING_OPTS+=("${opt}") | |
fi | |
done | |
if [ "${#MISSING_OPTS[@]}" -gt "0" ]; then | |
>&2 printf '%s: missing required options -- %s\n' "$0" "${MISSING_OPTS[*]}" | |
usage | |
exit 1 | |
fi | |
fi | |
GRADLE_VERSION="$(get_latest_version "$@")" | |
printf 'using Gradle %s (see https://gradle.org/releases/)\n' "${GRADLE_VERSION}" | |
if [ -z "${LIST_ONLY}" ]; then | |
TMP_DIR="$(mktemp -d "${0##*/}.XXXXXX")" || exit 1 | |
printf 'TMP_DIR: %s\n' "${TMP_DIR}" 1>&2 | |
for fpath in "build.gradle" "settings.gradle" "buildSrc"; do | |
if [ -e "$fpath" ]; then | |
mv -v "$fpath" "$TMP_DIR/$fpath.bak" | |
fi | |
done | |
printf "rootProject.name = '$(basename "$(pwd)")'\n" > "settings.gradle" | |
gradle --info --full-stacktrace --no-daemon --no-watch-fs wrapper --gradle-version "${GRADLE_VERSION}" --distribution-type all | |
for fpath in "build.gradle" "settings.gradle" "buildSrc"; do | |
if [ -e "$TMP_DIR/$fpath.bak" ]; then | |
mv -vf "$TMP_DIR/$fpath.bak" "$fpath" | |
fi | |
done | |
rm -vfr "${TMP_DIR}" | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment