name: Continuous Integration (MSVC)

on:
  push:
    branches: [ master ]
    tags: ['*']
    paths-ignore: ['**.md']
  pull_request:
    branches: [ master ]
    paths-ignore: ['**.md']
  workflow_dispatch:

env:
  VCPKG_DIR: C:\vcpkg
  VCPKG_BINARY_SOURCES: "clear;nuget,GitHub,readwrite"

jobs:
  build:
    if: ${{ github.repository == 'fabiangreffrath/woof' }}
    name: MSVC ${{ matrix.config.arch }}
    runs-on: windows-latest

    strategy:
      matrix:
        config:
          - name: x64
            arch: x64

          - name: Win32
            arch: x86

    steps:
      - uses: actions/checkout@v4

      - name: Developer Command Prompt
        uses: ilammy/msvc-dev-cmd@v1
        with:
          # The version of the toolset is specified due to faulty version discovery.
          toolset: 14.39
          arch: ${{ matrix.config.arch }}

      - name: Setup vcpkg and NuGet
        shell: bash
        run: |
          set -euo pipefail

          cd "${{ env.VCPKG_DIR }}"
          git fetch --tags
          LATESTTAG=$(git describe --tags --abbrev=0)
          git checkout $LATESTTAG

          NUGET=$(vcpkg fetch nuget | tail -n 1)
          GH_PACKAGES_URL="https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"

          "$NUGET" sources add \
            -source "$GH_PACKAGES_URL" \
            -storepasswordincleartext \
            -name "GitHub" \
            -username "${{ github.repository_owner }}" \
            -password "${{ secrets.GITHUB_TOKEN }}"

          "$NUGET" setapikey "${{ secrets.GITHUB_TOKEN }}" \
            -source "$GH_PACKAGES_URL"

      - name: Configure
        run: |
          cmake -B build -G Ninja `
            -DCMAKE_BUILD_TYPE=Release -DENABLE_WERROR=ON -DENABLE_LTO=ON `
            -DCMAKE_TOOLCHAIN_FILE="${{ env.VCPKG_DIR }}/scripts/buildsystems/vcpkg.cmake" `
            -DVCPKG_TARGET_TRIPLET="${{ matrix.config.arch }}-windows-static-release" `
            -DVCPKG_OVERLAY_TRIPLETS="cmake/triplets" `
            -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded `
            -DCMAKE_IGNORE_PATH="C:/Strawberry/perl/bin;C:/Strawberry/c/lib"

      - name: Build
        run: cmake --build build --config "Release"

      - name: Test
        if: github.event_name == 'workflow_dispatch'
        run: |
          cd demotest
          pip install pyyaml joblib
          python demotest --jobs 4 --port ../build/src/woof

      - name: Package
        run: |
          cd build
          cpack

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: Win-${{ matrix.config.arch }}
          path: build/*.zip

      - name: Extract Version Number
        shell: bash
        run: echo "VERSION=${GITHUB_REF##*_}" >> $GITHUB_ENV

      - name: Release
        if: ${{ contains(github.ref, 'tags') }}
        uses: ncipollo/release-action@v1
        with:
          name: Woof! ${{ env.VERSION }}
          bodyFile: CHANGELOG.md
          allowUpdates: true
          artifacts: build/*.zip
