name: Build

on:
  push:
    branches:
      - main

jobs:
  SmokeTests:
    name: ${{ matrix.name }} (${{ matrix.config }} ${{ matrix.options }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-22.04, macos-14, windows-2022]
        config: [Debug, Release]
        options: [Standard, Gargantua]
        include:
          - os: macos-14
            name: macOS
          - os: ubuntu-22.04
            name: Linux
          - os: windows-2022
            name: Windows
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Checkout exploragram
        uses: actions/checkout@v4
        with:
          repository: BrunoLevy/exploragram
          path: src/lib/exploragram

      - name: Checkout data
        uses: actions/checkout@v4
        with:
          repository: BrunoLevy/geogram.data
          path: tests/data

      - name: Python3
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: RobotFramework
        run: |
          pip3 install robotframework
          robot --version || true

################### Linux/MacOS ###############################################

      - name: Dependencies (Linux)
        if: runner.os == 'Linux'
        run: |
            sudo apt-get update
            sudo apt-get install xorg-dev

      - name: Configure (Linux/MacOS)
        if: runner.os != 'Windows'
        run: |
          cp cmake/options/CMakeOptions.txt.${{matrix.options}} CMakeOptions.txt
          ./configure.sh
          echo "platform="\
          `./configure.sh --show-platform`-${{ matrix.config }} >> $GITHUB_ENV

      - name: Build (Linux/MacOS)
        if: runner.os != 'Windows'
        run: |
          cd build/${{ env.platform }};\
          make -j2 2>&1 |tee ../../build_log.txt

      - name: Tests (Linux/MacOs)
        if: runner.os != 'Windows'
        run: |
          build/${{ env.platform }}/tests/runpybot.sh \
          --include=smoke --include=smoke_${{matrix.options}} tests/

################### Windows ##################################################

      - name: Configure (Windows)
        if: runner.os == 'Windows'
        run: |
          copy cmake/options/CMakeOptions.txt.${{matrix.options}} CMakeOptions.txt
          ./configure.bat

      - name: Build (Windows)
        if: runner.os == 'Windows'
        run: |
          cmake --build build/Windows --config ${{ matrix.config }} `
          | tee -filepath build_log.txt

      - name: Tests (Windows)
        if: runner.os == 'Windows'
        run: |
          build/Windows/tests/runpybot.bat --config=${{ matrix.config }} `
          --include=smoke --include=smoke_${{matrix.options}} tests/

##############################################################################

      - name: Publish tests results as artifacts
        uses: actions/upload-artifact@v4
        with:
          name: smoke-${{matrix.name}}-${{matrix.config}}-${{matrix.options}}
          if-no-files-found: ignore
          path: |
            report.html
            log.html
            build_log.txt
            TESTS_SUCCESS

      - name: "Notify tests results"
        uses: andstor/file-existence-action@v3
        with:
          files: "TESTS_SUCCESS"
          fail: true

##############################################################################

  Reports:
    runs-on: ubuntu-latest
    if: always()
    needs: SmokeTests
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Get artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Generate reports from artifacts
        run: |
          ./tools/generate_reports.sh

      - name: Publish reports on gh-pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: BrunoLevy/geogram.CI
          publish_dir: ./reports/smoke
          destination_dir: reports/smoke
#         force_orphan: true   # to clean history of gh-pages branch
