name: Prepare Gradle Build
description: 'Prepares a Gradle build. Sets up Java and Gradle and configures Gradle properties'
inputs:
  develocity-access-key:
    description: 'Access key for authentication with ge.spring.io'
    required: false
  java-distribution:
    description: 'Java distribution to use'
    required: false
    default: 'liberica'
  java-early-access:
    description: 'Whether the Java version is in early access. When true, forces java-distribution to temurin'
    required: false
    default: 'false'
  java-toolchain:
    description: 'Whether a Java toolchain should be used'
    required: false
    default: 'false'
  java-version:
    description: 'Java version to use for the build'
    required: false
    default: '17'
runs:
  using: composite
  steps:
    - name: Set Up Java
      uses: actions/setup-java@v4
      with:
        distribution: ${{ inputs.java-early-access == 'true' && 'temurin' || (inputs.java-distribution || 'liberica') }}
        java-version: |
          ${{ inputs.java-early-access == 'true' && format('{0}-ea', inputs.java-version) || inputs.java-version }}
          ${{ inputs.java-toolchain == 'true' && '17' || '' }}
    - name: Set Up Gradle
      uses: gradle/actions/setup-gradle@d156388eb19639ec20ade50009f3d199ce1e2808 # v4.1.0
      with:
        cache-read-only: false
        develocity-access-key: ${{ inputs.develocity-access-key }}
    - name: Configure Gradle Properties
      shell: bash
      run: |
        mkdir -p $HOME/.gradle
        echo 'systemProp.user.name=spring-builds+github' >> $HOME/.gradle/gradle.properties
        echo 'systemProp.org.gradle.internal.launcher.welcomeMessageEnabled=false' >> $HOME/.gradle/gradle.properties
        echo 'org.gradle.daemon=false' >> $HOME/.gradle/gradle.properties
        echo 'org.gradle.daemon=4' >> $HOME/.gradle/gradle.properties
    - name: Configure Toolchain Properties
      if: ${{ inputs.java-toolchain == 'true' }}
      shell: bash
      run: |
        echo toolchainVersion=${{ inputs.java-version }} >> $HOME/.gradle/gradle.properties
        echo systemProp.org.gradle.java.installations.auto-detect=false >> $HOME/.gradle/gradle.properties
        echo systemProp.org.gradle.java.installations.auto-download=false >> $HOME/.gradle/gradle.properties
        echo systemProp.org.gradle.java.installations.paths=${{ format('$JAVA_HOME_{0}_X64', inputs.java-version) }} >> $HOME/.gradle/gradle.properties
