name: Benchmark Builder
# Manually build a benchmark on a tag

on:
  # Dispatch or Manual triggers
  workflow_dispatch:

#  push:
#    branches:
#      - dev-benchmark

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  prepare-build:
    runs-on: 'ubuntu-24.04'
    outputs:
      VERSION_TAG: ${{ steps.get_tag.outputs.VERSION_TAG }}
      ARTIFACTS_PREFIX: benchmark/app
      TAG_NAME: benchmark-${{ steps.tag_name.outputs.TAG_NAME }}
    steps:
      - name: Get tag name (${{ github.ref }})
        id: get_tag
        run: |
          version_tag=$(echo ${GITHUB_REF/refs\/tags\//} | sed -r 's/^refs\/(heads|tags)\///' | sed -r 's/[-\/]+/_/g')
          echo "VERSION_TAG=$version_tag"
          echo "VERSION_TAG=$version_tag" >> $GITHUB_OUTPUT

      - name: Clone Repository (${{ steps.get_tag.outputs.VERSION_TAG }})
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          fetch-depth: 0

      - name: Prepare env
        id: prepare_env
        run: |
          commit_count=$(git rev-list --count HEAD)
          echo "COMMIT_COUNT=$commit_count"
          echo "COMMIT_COUNT=$commit_count" >> $GITHUB_OUTPUT

      - name: Tag name
        id: tag_name
        run: |
          if [ "${{ steps.get_tag.outputs.VERSION_TAG }}" = "master" ]; then
            tag_name=${{ steps.get_tag.outputs.VERSION_TAG }}-r${{ steps.prepare_env.outputs.COMMIT_COUNT }}
            else
            tag_name=${{ steps.get_tag.outputs.VERSION_TAG }}
          fi

          echo "TAG_NAME=$tag_name"
          echo "TAG_NAME=$tag_name" >> $GITHUB_OUTPUT

  build-app:
    runs-on: 'ubuntu-24.04'
    needs: prepare-build
    steps:
      - name: Clone Repository (${{ needs.prepare-build.outputs.TAG_NAME }})
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          fetch-depth: 0

      - name: Set up JDK
        uses: actions/setup-java@1bcf9fb12cf4aa7d266a90ae39939e61372fe520 # v5.4.0
        with:
          java-version: 17
          distribution: temurin

      - name: Set up gradle
        uses: gradle/actions/setup-gradle@0723195856401067f7a2779048b490ace7a47d7c # v5

      - name: Check code format
        run: ./gradlew spotlessCheck

      - name: Build app
        run: ./gradlew assembleBenchmark

      - name: Run unit tests
        run: ./gradlew testReleaseUnitTest

      - name: Sign APK
        uses: r0adkll/sign-android-release@f30bdd30588842ac76044ecdbd4b6d0e3e813478
        with:
          releaseDirectory: app/build/outputs/apk/benchmark
          signingKeyBase64: ${{ secrets.SIGNING_KEY }}
          alias: ${{ secrets.ALIAS }}
          keyStorePassword: ${{ secrets.KEY_STORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD }}
        env:
          BUILD_TOOLS_VERSION: '35.0.1'

      - name: Get SHA & clean up build artifacts
        id: get_sha
        run: |
          set -e
          mv app/build/outputs/apk/${{ needs.prepare-build.outputs.ARTIFACTS_PREFIX }}-universal-benchmark-signed.apk Komikku-${{ needs.prepare-build.outputs.TAG_NAME }}.apk
          mv app/build/outputs/apk/${{ needs.prepare-build.outputs.ARTIFACTS_PREFIX }}-arm64-v8a-benchmark-signed.apk Komikku-arm64-v8a-${{ needs.prepare-build.outputs.TAG_NAME }}.apk
          mv app/build/outputs/apk/${{ needs.prepare-build.outputs.ARTIFACTS_PREFIX }}-armeabi-v7a-benchmark-signed.apk Komikku-armeabi-v7a-${{ needs.prepare-build.outputs.TAG_NAME }}.apk
          mv app/build/outputs/apk/${{ needs.prepare-build.outputs.ARTIFACTS_PREFIX }}-x86-benchmark-signed.apk Komikku-x86-${{ needs.prepare-build.outputs.TAG_NAME }}.apk
          mv app/build/outputs/apk/${{ needs.prepare-build.outputs.ARTIFACTS_PREFIX }}-x86_64-benchmark-signed.apk Komikku-x86_64-${{ needs.prepare-build.outputs.TAG_NAME }}.apk

      - name: Upload artifacts
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          path: "**/*.apk"
          retention-days: 30
