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@1df8dbefe2a8cbc99770194893dd902763bee34b
      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;22.1.7171670"
    - name: Setup Rust
      shell: bash
      run: |
        rustup target add armv7-linux-androideabi aarch64-linux-android i686-linux-android x86_64-linux-android
    - name: Disable Gradle Daemon
      shell: bash
      run: |
        mkdir ~/.gradle

        echo "org.gradle.daemon=false" >> ~/.gradle/gradle.properties
    - name: Gradle Wrapper Cache
      id: gradle-wrapper-cache
      uses: actions/cache@58c146cc91c5b9e778e71775dfe9bf1442ad9a12
      with:
        path: ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-wrapper-${{ hashFiles(format('{0}{1}', github.workspace, '/gradle/wrapper/gradle-wrapper.properties')) }}
    - name: Gradle Dependency Cache
      id: gradle-dependency-cache
      uses: actions/cache@58c146cc91c5b9e778e71775dfe9bf1442ad9a12
      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-
# The build cache is doing two things:
# 1. Relying on the most recent main branch build to provide a partial cache for both main branch and PR builds.
# 2. Relying on the sha for an exact match so that the prime_cache job is re-used by all dependent jobs in a single workflow run
    - name: Gradle Build Cache
      id: gradle-build-cache
      uses: actions/cache@58c146cc91c5b9e778e71775dfe9bf1442ad9a12
      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
      id: rust-cache
      uses: actions/cache@58c146cc91c5b9e778e71775dfe9bf1442ad9a12
      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: Download Gradle
      if: steps.gradle-wrapper-cache.outputs.cache-hit != 'true'
      shell: bash
      run: |
        ./gradlew --version
    - name: Download Gradle Dependencies
      if: steps.gradle-dependency-cache.outputs.cache-hit != 'true'
      shell: bash
      run: |
        ./gradlew dependencies :sdk-lib:dependencies :demo-app:dependencies
    - name: Compile
      if: steps.gradle-build-cache.outputs.cache-hit != 'true'
      shell: bash
      env:
        ORG_GRADLE_PROJECT_IS_MINIFY_APP_ENABLED: "false"
      run: |
        ./gradlew assemble assembleAndroidTest
