name: build-and-release

on:
  push:
    branches: [main, develop]
    tags: ["v*"]
  pull_request:
    branches: [main, develop]

permissions:
  contents: read

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

env:
  GODOT_VERSION: "4.3"
  PROJECT_PATH: game
  EXPORT_PATH: build

jobs:
  validate:
    name: Validate project
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4

      - name: Check formatting
        run: |
          find "$PROJECT_PATH" -name "*.gd" -print0 |
            xargs -0 gdformat --check

      - name: Scan assets for oversized files
        run: |
          find "$PROJECT_PATH/assets" -type f -size +50M -print |
            tee oversized-assets.txt
          test ! -s oversized-assets.txt

  test:
    name: Run tests
    needs: validate
    runs-on: ubuntu-latest
    timeout-minutes: 20
    container:
      image: barichello/godot-ci:${{ env.GODOT_VERSION }}
    steps:
      - uses: actions/checkout@v4

      - name: Import project
        run: godot --headless --editor --path "$PROJECT_PATH" --quit

      - name: Run unit tests
        run: |
          godot --headless \
            --path "$PROJECT_PATH" \
            -s addons/gut/gut_cmdln.gd \
            -gdir=res://tests \
            -gexit

  export:
    name: Export ${{ matrix.preset }}
    needs: test
    runs-on: ubuntu-latest
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        include:
          - preset: Windows Desktop
            artifact: starfall-windows
            output: Starfall.exe
          - preset: Linux
            artifact: starfall-linux
            output: Starfall.x86_64
          - preset: Web
            artifact: starfall-web
            output: index.html
    container:
      image: barichello/godot-ci:${{ env.GODOT_VERSION }}
    steps:
      - uses: actions/checkout@v4

      - name: Import project
        run: godot --headless --editor --path "$PROJECT_PATH" --quit

      - name: Export game
        run: |
          mkdir -p "$EXPORT_PATH/${{ matrix.artifact }}"
          godot --headless \
            --path "$PROJECT_PATH" \
            --export-release "${{ matrix.preset }}" \
            "../$EXPORT_PATH/${{ matrix.artifact }}/${{ matrix.output }}"

      - name: Upload build
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: ${{ env.EXPORT_PATH }}/${{ matrix.artifact }}
          if-no-files-found: error
          retention-days: 14

  release:
    name: Publish tagged release
    if: startsWith(github.ref, 'refs/tags/v')
    needs: export
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download builds
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Package builds
        run: |
          for directory in artifacts/*; do
            archive="$(basename "$directory").zip"
            cd "$directory"
            zip -r "../../$archive" .
            cd ../..
          done

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: "*.zip"