name: Pull Request Check

on:
  pull_request:
    branches: [ main ]
    paths-ignore:
      - '**.md'
      - '**.txt'
      - '.github/**'
      - '!.github/workflows/**'

jobs:
  build:
    name: Build CI (Pull Request)
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3.2.0
        with:
          submodules: 'recursive'
          fetch-depth: 0

      - name: set up JDK 17
        uses: actions/setup-java@v3.9.0
        with:
          java-version: '17'
          distribution: 'adopt'
          cache: gradle
      - name: Install ccache
        run: |
          sudo apt install -y ccache
          ccache -o max_size=1G
          ccache -o hash_dir=false
          ccache -o hard_link=true
          ccache -o compiler_check='%compiler% -dumpmachine; %compiler% -dumpversion'
          ccache -zp
      - name: Cache Native Build
        uses: actions/cache@v3.2.2
        with:
          path: |
            ~/.cache/ccache
            ~/.ccache
            app/build/.lto-cache
          key: native-cache-${{ github.sha }}
          restore-keys: native-cache-

      - name: Setup cmake
        run: |
          echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install "cmake;3.22.1" --sdk_root=${ANDROID_SDK_ROOT} &> /dev/null
          echo "sdk.dir=${ANDROID_HOME}" > local.properties
      - name: Cache Gradle Dependencies
        uses: actions/cache@v3.2.2
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
            !~/.gradle/caches/build-cache-*
          key: gradle-deps-core-${{ hashFiles('**/build.gradle.kts') }}
          restore-keys: |
            gradle-deps
      - name: Cache Gradle Build
        uses: actions/cache@v3.2.2
        with:
          path: |
            ~/.gradle/caches/build-cache-*
            ~/.gradle/buildOutputCleanup/cache.properties
          key: gradle-builds-core-${{ github.sha }}
          restore-keys: |
            gradle-builds
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build with Gradle
        run: |
          [ $(du -s ~/.gradle/wrapper | awk '{ print $1 }') -gt 250000 ] && rm -rf ~/.gradle/wrapper/* || true
          find ~/.gradle/caches -exec touch -d "2 days ago" {} + || true
          echo 'org.gradle.caching=true' >> gradle.properties
          echo 'org.gradle.parallel=true' >> gradle.properties
          echo 'org.gradle.vfs.watch=true' >> gradle.properties
          ./gradlew :app:packageDebug
          ccache -s
