name: Build

on:
  push:
    branches:
      - release
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
  pull_request:
    branches:
      - release
  schedule:
    - cron: '0 2 * * *'  # Run at 2 AM UTC every day

jobs:
  buildForAllSupportedPlatforms:
    name: Build for ${{ matrix.targetPlatform }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        targetPlatform:
          - StandaloneOSX # Build a macOS standalone (Intel 64-bit).
          - StandaloneWindows64 # Build a Windows 64-bit standalone.
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          lfs: false
      - uses: actions/cache@v4
        with:
          path: Library
          key: Library-${{ matrix.targetPlatform }}
          restore-keys: Library-
      - if: matrix.targetPlatform == 'Android'
        uses: jlumbroso/free-disk-space@v1.3.1
      - uses: game-ci/unity-builder@v4
        env:
          UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
          UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
          UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
        with:
          buildName: micromissiles-${{ github.ref_name }}-${{ matrix.targetPlatform }}
          versioning: Semantic
          targetPlatform: ${{ matrix.targetPlatform }}
      - name: Copy Tools Directory
        run: |
          mkdir -p build/${{ matrix.targetPlatform }}/Tools
          cp -r Tools/ build/${{ matrix.targetPlatform }}/Tools/
      - if: matrix.targetPlatform == 'StandaloneWindows64'
        run: cd build/${{ matrix.targetPlatform }} && sudo zip -r ../build-${{ matrix.targetPlatform }}.zip * && cd -
      - if: matrix.targetPlatform == 'StandaloneWindows64'
        uses: actions/upload-artifact@v4
        with:
          name: Build-${{ matrix.targetPlatform }}
          path: build/build-${{ matrix.targetPlatform }}.zip
      - if: matrix.targetPlatform == 'StandaloneOSX'
        run: sudo tar -czvf build/build-${{ matrix.targetPlatform }}.tar.gz -C build/${{ matrix.targetPlatform }} .
      - if: matrix.targetPlatform == 'StandaloneOSX'
        uses: actions/upload-artifact@v4
        with:
          name: Build-${{ matrix.targetPlatform }}
          path: build/build-${{ matrix.targetPlatform }}.tar.gz
