name: 'Setup Java, Rust, and Dependency Cache'
description: "Configures the build environment and caches Gradle, dependencies, and build outputs."
runs:
  using: "composite"
  steps:
    - name: Set Env
      shell: bash
      run: |
        echo "home=${HOME}" >> "$GITHUB_ENV"
    - name: Set up Java
      uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9
      with:
        distribution: 'temurin'
        java-version: 17
    - name: Setup Android NDK
      shell: bash
      run: |
        ANDROID_ROOT=/usr/local/lib/android
        ANDROID_SDK_ROOT=${ANDROID_ROOT}/sdk
        SDKMANAGER=${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager
        echo "y" | $SDKMANAGER "ndk;26.1.10909125"
    - name: Setup Rust
      shell: bash
      run: |
        # Rust 1.68+ is not compatible yet https://github.com/zcash/zcash-android-wallet-sdk/issues/958
        rustup install 1.67.1
        rustup default 1.67.1
        rustup target add armv7-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android
    - name: Configure Gradle
      shell: bash
      run: |
        mkdir ~/.gradle
        
        echo "GRADLE_BUILD_CACHE_DAYS=7" >> ~/.gradle/gradle.properties
    - name: Gradle Binary Cache Restore
      id: gradle-binary-cache-restore
      uses: actions/cache/restore@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
      env:
        SEGMENT_DOWNLOAD_TIMEOUT_MINS: "3"
      with:
        path: |
          ~/.gradle/notifications
          ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-binary-${{ hashFiles(format('{0}{1}', github.workspace, '/gradle/wrapper/gradle-wrapper.properties')) }}
    - name: Gradle Dependency Cache Restore
      id: gradle-dependency-cache-restore
      uses: actions/cache/restore@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
      env:
        SEGMENT_DOWNLOAD_TIMEOUT_MINS: "3"
      with:
        path: |
          ~/.gradle/caches/modules-2
        key: ${{ runner.os }}-gradle-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/gradle.properties')) }}
        restore-keys: |
          ${{ runner.os }}-gradle-deps-
    - name: Gradle Build Cache Restore
      id: gradle-build-cache-restore
      uses: actions/cache/restore@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
      env:
        SEGMENT_DOWNLOAD_TIMEOUT_MINS: "3"
      with:
        path: |
          ~/.gradle/caches/build-cache-1
          ~/.gradle/caches/transforms-3
          .gradle/configuration-cache
        key: ${{ runner.os }}-gradle-build-${{ github.sha }}
        restore-keys: |
          ${{ runner.os }}-gradle-build-
    - name: Rust Cache Restore
      id: rust-cache-restore
      uses: actions/cache/restore@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
      env:
        SEGMENT_DOWNLOAD_TIMEOUT_MINS: "3"
      with:
        path: |
          sdk-lib/target
          ~/.cargo
        key: ${{ runner.os }}-rust-${{ hashFiles(format('{0}{1}', github.workspace, '/sdk-lib/Cargo.lock'), format('{0}{1}', github.workspace, '/sdk-lib/Cargo.toml'), format('{0}{1}', github.workspace, '/sdk-lib/build.gradle.kts'), format('{0}{1}', github.workspace, '/gradle.properties')) }}

    - name: Gradle Binary Download
      if: steps.gradle-binary-cache-restore.outputs.cache-hit != 'true'
      shell: bash
      run: |
        ./gradlew --version
    - name: Gradle Dependency Download
      if: steps.gradle-dependency-cache-restore.outputs.cache-hit != 'true'
      shell: bash
      run: |
        ./gradlew dependencies :sdk-lib:dependencies :demo-app:dependencies
    - name: Gradle Build
      if: github.event.pull_request.head.sha == '' && (steps.gradle-binary-cache-restore.outputs.cache-hit != 'true' || steps.gradle-dependency-cache-restore.outputs.cache-hit != 'true' || steps.rust-cache-restore.outputs.cache-hit != 'true' || steps.gradle-build-cache-restore.outputs.cache-hit != 'true')
      env:
        ORG_GRADLE_PROJECT_IS_MINIFY_APP_ENABLED: "false"
      shell: bash
      run: |
        ./gradlew assemble assembleAndroidTest

    - name: Gradle Binary Cache Save
      id: gradle-binary-cache-save
      if: github.event.pull_request.head.sha == '' && steps.gradle-binary-cache-restore.outputs.cache-hit != 'true'
      uses: actions/cache/save@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
      env:
        SEGMENT_DOWNLOAD_TIMEOUT_MINS: "3"
      with:
        path: |
          ~/.gradle/notifications
          ~/.gradle/wrapper
        key: steps.gradle-binary-cache-restore.key
    - name: Gradle Dependency Cache Save
      id: gradle-dependency-cache-save
      if: github.event.pull_request.head.sha == '' && steps.gradle-dependency-cache-restore.outputs.cache-hit != 'true'
      uses: actions/cache/save@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
      with:
        path: |
          ~/.gradle/caches/modules-2
        key: ${{ steps.gradle-dependency-cache-restore.key }}
    - name: Rust Cache Save
      id: rust-cache-save
      uses: actions/cache/save@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9
      env:
        SEGMENT_DOWNLOAD_TIMEOUT_MINS: "3"
      with:
        path: |
          sdk-lib/target
          ~/.cargo
        key: ${{ steps.rust-cache-restore.key }}
