blob: b400ac459a1bcd4470935268bbd4de6fefc47ea8 [file] [log] [blame]
Omar Ismail7fd325e2024-09-18 17:23:33 +01001#!/bin/bash
2
3set -e
4
5# Check if the user has provided the version as an argument
6if [ -z "$1" ]; then
7 echo "Error: No version provided. Usage: $0 <gradle-version>"
8 exit 1
9fi
10
11VERSION="$1"
12DEST_DIR="../../tools/external/gradle"
13WRAPPER_FILES=("gradle/wrapper/gradle-wrapper.properties" "playground-common/gradle/wrapper/gradle-wrapper.properties")
14
15BASE_URL="https://services.gradle.org/distributions"
16ZIP_FILE="gradle-${VERSION}-bin.zip"
17SHA_FILE="${ZIP_FILE}.sha256"
18
19# Function to check if a URL is valid by checking the HTTP status code
20check_url() {
21 local url="$1"
22
23 echo "Checking URL: $url"
24
25 http_status=$(curl -L --silent --head --write-out "%{http_code}" --output /dev/null "$url")
26
27 if [ "$http_status" -ne 200 ]; then
28 echo "Error: URL returned status code $http_status. The file doesn't exist at: $url"
29 exit 1
30 else
31 echo "URL is valid: $url"
32 fi
33}
34
35check_url "$BASE_URL/$ZIP_FILE"
36check_url "$BASE_URL/$SHA_FILE"
37
38echo "Cleaning destination directory: $DEST_DIR"
39rm -rf "$DEST_DIR"/*
40mkdir -p "$DEST_DIR"
41
42echo "Downloading Gradle ${VERSION}..."
43curl -Lo "$DEST_DIR/$ZIP_FILE" "$BASE_URL/$ZIP_FILE"
44curl -Lo "$DEST_DIR/$SHA_FILE" "$BASE_URL/$SHA_FILE"
45
46GRADLE_SHA256SUM=$(cat "$DEST_DIR/$SHA_FILE")
47
48echo "Downloaded Gradle ${VERSION} with SHA256: $GRADLE_SHA256SUM"
49
50update_gradle_wrapper_properties() {
51 local file="$1"
52 echo "Updating $file..."
53
54 if [ "$(uname)" = "Darwin" ]; then
55 sed -i '' "
56 s|distributionUrl=.*tools/external/gradle/.*|distributionUrl=../../../../tools/external/gradle/${ZIP_FILE}|;
57 s|distributionUrl=https\\\://services.gradle.org/distributions/.*|distributionUrl=https\\\://services.gradle.org/distributions/${ZIP_FILE}|;
58 s|distributionSha256Sum=.*|distributionSha256Sum=${GRADLE_SHA256SUM}|
59 " "$file"
60 else
61 sed -i "
62 s|distributionUrl=.*tools/external/gradle/.*|distributionUrl=../../../../tools/external/gradle/${ZIP_FILE}|;
63 s|distributionUrl=https\\\://services.gradle.org/distributions/.*|distributionUrl=https\\\://services.gradle.org/distributions/${ZIP_FILE}|;
64 s|distributionSha256Sum=.*|distributionSha256Sum=${GRADLE_SHA256SUM}|
65 " "$file"
66 fi
67
68 echo "Updated $file."
69}
70
71for file in "${WRAPPER_FILES[@]}"; do
72 update_gradle_wrapper_properties "$file"
73done
74
75echo "Gradle binary downloaded, and the wrapper properties updated successfully!"
76
77echo "Testing the setup with './gradlew bOS --dry-run'..."
78if ./gradlew bOS --dry-run; then
79 echo "Download and setup successful!"
80 echo "You can now upload changes in $(pwd) and $DEST_DIR to Gerrit!"
81fi