Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions .github/scripts/gradle-distribution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env bash
# The Gradle wrapper pin, gradle/wrapper/gradle-distribution.sha256, and the hash behind it.
# The wrapper checks distributionSha256Sum when it downloads the distribution, then deletes the
# zip; the pin lets gradle.sh check a distribution already unpacked in the Gradle user home, and
# the wrapper jar and start scripts, before every run.
#
# bash .github/scripts/gradle-distribution.sh pin [<zip>]
# After any change to the wrapper files: downloads distributionUrl (or takes <zip>), refuses
# it unless its sha256 equals distributionSha256Sum, unpacks it, runs its own wrapper task in
# an empty project and refuses unless the committed gradle-wrapper.properties,
# gradle-wrapper.jar, gradlew and gradlew.bat are byte-identical to what it generates, then
# writes the pin. Review the diff and commit the pin with the wrapper files.
# bash .github/scripts/gradle-distribution.sh sum <file>
# Prints the sha256 of <file> (used by gradle.sh for the pinned wrapper files).
# bash .github/scripts/gradle-distribution.sh hash <dir>
# Prints the sha256 of the listing of <dir>: every directory, and every file with its own
# sha256, sorted by path. Symbolic links, other special files and names with control
# characters or backslashes are refused.
set -euo pipefail

fail() { echo "::error::$1" >&2; exit 1; }
absolute() { case "$1" in /*) printf '%s' "$1" ;; *) printf '%s/%s' "$PWD" "$1" ;; esac; }
arg2=""
if [ $# -ge 2 ]; then arg2="$(absolute "$2")"; fi
if command -v sha256sum > /dev/null 2>&1; then sha256=(sha256sum); else sha256=(shasum -a 256); fi

tree_hash() {
local dir="$1"
[ -d "$dir" ] && [ ! -L "$dir" ] || fail "$dir is not a directory"
(
cd "$dir"
# find exits non-zero when it cannot read a folder; the listing is then refused.
find . > /dev/null || fail "$dir holds a folder that cannot be read"
[ -z "$(find . ! -type f ! -type d -print | head -1)" ] ||
fail "$dir holds an entry that is neither a file nor a directory"
[ -z "$(find . \( -name '*[[:cntrl:]]*' -o -name '*\\*' \) -print | head -1)" ] ||
fail "$dir holds a name with a control character or a backslash"
{
find . -mindepth 1 -type d -print | LC_ALL=C sort | sed 's/^/directory /'
find . -type f -print0 | LC_ALL=C sort -z | xargs -0 -r "${sha256[@]}"
} | "${sha256[@]}" | cut -d' ' -f1
)
}

wrapper=gradle/wrapper/gradle-wrapper.properties
pin_file=gradle/wrapper/gradle-distribution.sha256
# The properties file is compared, not pinned: gradle.sh checks its content itself.
wrapper_files=("$wrapper" gradle/wrapper/gradle-wrapper.jar gradlew gradlew.bat)

case "${1:-}" in
hash)
[ $# -eq 2 ] || fail "usage: gradle-distribution.sh hash <dir>"
tree_hash "$arg2"
;;
sum)
[ $# -eq 2 ] || fail "usage: gradle-distribution.sh sum <file>"
[ -f "$arg2" ] && [ -r "$arg2" ] || fail "$arg2 cannot be read"
"${sha256[@]}" "$arg2" | cut -d' ' -f1
;;
pin)
[ $# -le 2 ] || fail "usage: gradle-distribution.sh pin [<zip>]"
cd "$(git rev-parse --show-toplevel)"
url="$(sed -n 's/^distributionUrl=//p' "$wrapper" | sed 's#\\:#:#')"
sum="$(sed -n 's/^distributionSha256Sum=//p' "$wrapper")"
[[ "$url" =~ ^https://services\.gradle\.org/distributions/(gradle-([0-9]+(\.[0-9]+)+)-(all|bin))\.zip$ ]] ||
fail "$wrapper: distributionUrl is not an official distribution"
name="${BASH_REMATCH[1]}"
version="${BASH_REMATCH[2]}"
type="${BASH_REMATCH[4]}"
top="gradle-$version"
[[ "$sum" =~ ^[0-9a-f]{64}$ ]] || fail "$wrapper: distributionSha256Sum is missing"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
zip="$arg2"
if [ -z "$zip" ]; then
zip="$work/$name.zip"
curl -fsSL --proto '=https' --proto-redir '=https' --tlsv1.2 -o "$zip" "$url"
fi
[ "$("${sha256[@]}" "$zip" | cut -d' ' -f1)" = "$sum" ] || fail "$zip does not match distributionSha256Sum"
mkdir "$work/unpacked"
unzip -q "$zip" -d "$work/unpacked"
[ "$(ls -A "$work/unpacked")" = "$top" ] || fail "$zip does not hold exactly one $top directory"
tree="$(tree_hash "$work/unpacked/$top")"
# The committed wrapper files are the ones this release generates.
generated="$work/generated"
mkdir "$generated"
: > "$generated/settings.gradle"
(cd "$generated" && GRADLE_USER_HOME="$work/home" "$work/unpacked/$top/bin/gradle" --no-daemon --quiet \
--offline wrapper --gradle-version "$version" --distribution-type "$type" \
--gradle-distribution-sha256-sum "$sum") || fail "Gradle $version could not generate the wrapper files"
for file in "${wrapper_files[@]}"; do
cmp -s "$generated/$file" "$file" ||
fail "$file is not the file Gradle $version generates: run ./gradlew wrapper --gradle-version $version --distribution-type $type --gradle-distribution-sha256-sum $sum (twice)"
done
{
printf '%s %s\n' "$tree" "$name"
for file in "${wrapper_files[@]:1}"; do
printf '%s %s\n' "$("${sha256[@]}" "$file" | cut -d' ' -f1)" "$file"
done
} > "$pin_file"
echo "$pin_file updated for $name; review 'git diff $pin_file'"
;;
*)
fail "usage: gradle-distribution.sh pin [<zip>] | hash <dir> | sum <file>"
;;
esac
Loading
Loading