name: Build gradle and/or publish (reusable workflow)

on:
  workflow_call:
    inputs:
      version:
        description: "release version"
        required: false
        type: string
      publish:
        description: "Specifies whether to publish the artifacts (defaults to false)"
        required: false
        type: boolean
        default: false

jobs:
  gradle:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # macos-latest disabled until all multiplatform modules set up properly
        # currently configured on workflow dispatch as a separate workflow (gradle-macos)
        os: [ ubuntu-latest ]
    steps:
      - name: Free Disk Space (Ubuntu)
        if: ${{ runner.os == 'linux' }}
        uses: jlumbroso/free-disk-space@v1.3.1
        with:
          tool-cache: false
          docker-images: false

      - name: Checkout all repos with ref fallback
        uses: walt-id/waltid-identity/.github/actions/checkout-repos@29f904ce0de831e02fe64507ebe284b02e66b50e
        with:
          token: ${{ secrets.GH_ACCESS_TOKEN }}

      - name: Configure gradle
        uses: walt-id/waltid-identity/.github/actions/gradle-setup-action@a4a92e12ee9b7396ee9a22083ff21b3277c627d6

      - name: Clean and build with all tests
        if: ${{ runner.os != 'macos' }}
        run: ./gradlew clean build cleanAllTests allTests --rerun-tasks --no-daemon --parallel --max-workers=3 --continue
        env:
          MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}

      - name: Build iOS Simulator Framework (ARM64)
        if: ${{ runner.os == 'macos'}}
        uses: walt-id/waltid-identity/.github/actions/gradle-ios@d3aa55fb102dadb48032778d6e1df9a8f9155261
        with:
          waltid-directory: 'waltid-identity'

      - name: Set version
        if: ${{ inputs.version != '' && runner.os == 'linux' }}
        run: |
          sed -i "s/1.0.0-SNAPSHOT/${{ inputs.version }}/g" build.gradle.kts

      - name: Publish
        if: ${{ inputs.publish && runner.os == 'linux' }}
        env:
          MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
        run: ./gradlew publish --no-daemon