name: Build
on:
  push:
    branches: master
  pull_request:
    branches: '*'
    paths-ignore:
      - 'README.md'
      - '.github/ISSUE_TEMPLATE/**'
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      pkgversion: ${{ steps.version.outputs.pkgversion }}
    steps:
      - name: checkout repository
        uses: actions/checkout@v3

      - name: setup dependencies
        run: |
          sudo apt-get update
          sudo apt-get install \
            libasound2-dev \
            libx11-dev \
            libxrandr-dev \
            libxi-dev \
            libgl1-mesa-dev \
            libglu1-mesa-dev \
            libxcursor-dev \
            libxinerama-dev

      - name: setup dotnet
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 6.0.x

      - name: build and run tests
        run: dotnet test Raylib-cs.Tests -c Release

      - name: build examples desktop
        run: dotnet build Examples -c Release

      - name: check raylib version
        id: version
        shell: bash
        run: |
          echo "version=$(sed -n 's/.*<TargetRaylibTag>\(.*\)<\/TargetRaylibTag>.*/\1/p' Raylib-cs/Build.props)">> ${GITHUB_OUTPUT}
          echo "pkgversion=$(sed -n 's/.*<PackageVersion>\(.*\)<\/PackageVersion>.*/\1/p' Raylib-cs/Build.props)">> ${GITHUB_OUTPUT}

  build-android:
    runs-on: ubuntu-latest
    needs: build
    strategy:
      matrix:
        name: [arm, arm64, x86, x64]
        include:
          - name: arm
            arch: armeabi-v7a
            cflags: "-mfloat-abi=softfp -mfpu=vfpv3-d16"
          - name: arm64
            arch: arm64-v8a
            cflags: "-mfix-cortex-a53-835769"
          - name: x86
            arch: x86
          - name: x64
            arch: x86_64
    steps:
      - name: checkout repository
        uses: actions/checkout@v3

      - name: build raylib
        env:
          version: ${{ needs.build.outputs.version }}
        run: |
          curl -Lso raylib.zip https://github.com/raysan5/raylib/archive/refs/tags/${version}.zip
          unzip -qq raylib.zip
          pushd raylib-${version}; patch -p1 < ../Examples.Android/android.patch; popd
          cmake -S raylib-${version} \
            -B build \
            -D CMAKE_BUILD_TYPE=Release \
            -D BUILD_SHARED_LIBS=ON \
            -D BUILD_EXAMPLES=OFF \
            -D CMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
            -D PLATFORM=Android \
            -D ANDROID_ABI=${{ matrix.arch }} \
            -D ANDROID_PLATFORM=21 \
            -D CMAKE_C_FLAGS="${{ matrix.cflags }}"
          cmake --build build --config Release

      - name: upload build
        uses: actions/upload-artifact@v3
        with:
          name: android-${{ matrix.name }}
          path: build/raylib/libraylib.so
          if-no-files-found: error

  build-linux:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: setup dependencies
        run: |
          sudo apt-get update
          sudo apt-get install \
            libasound2-dev \
            libx11-dev \
            libxrandr-dev \
            libxi-dev \
            libgl1-mesa-dev \
            libglu1-mesa-dev \
            libxcursor-dev \
            libxinerama-dev

      - name: build raylib
        env:
          version: ${{ needs.build.outputs.version }}
        run: |
          curl -Lso raylib.zip https://github.com/raysan5/raylib/archive/refs/tags/${version}.zip
          unzip -qq raylib.zip
          cmake -S raylib-${version} \
            -B build \
            -D CMAKE_BUILD_TYPE=Release \
            -D BUILD_SHARED_LIBS=ON \
            -D BUILD_EXAMPLES=OFF
          cmake --build build --config Release

      - name: upload build
        uses: actions/upload-artifact@v3
        with:
          name: linux-x64
          path: build/raylib/libraylib.so
          if-no-files-found: error

  build-osx:
    runs-on: macos-latest
    needs: build
    strategy:
      matrix:
        name: [arm64, x64]
        include:
          - name: arm64
            arch: arm64
          - name: x64
            arch: x86_64
    steps:
      - name: build raylib
        env:
          version: ${{ needs.build.outputs.version }}
        run: |
          curl -Lso raylib.zip https://github.com/raysan5/raylib/archive/refs/tags/${version}.zip
          unzip -qq raylib.zip
          cmake -S raylib-${version} \
            -B build \
            -D CMAKE_BUILD_TYPE=Release \
            -D CMAKE_OSX_ARCHITECTURES=${{ matrix.arch }} \
            -D BUILD_SHARED_LIBS=ON \
            -D BUILD_EXAMPLES=OFF
          cmake --build build --config Release

      - name: upload build
        uses: actions/upload-artifact@v3
        with:
          name: osx-${{ matrix.name }}
          path: build/raylib/libraylib.dylib
          if-no-files-found: error

  build-windows:
    runs-on: windows-latest
    needs: build
    strategy:
      matrix:
        name: [x86, x64]
        include:
          - name: x86
            arch: win32
          - name: x64
            arch: x64
    steps:
      - name: build ${{ matrix.arch }}
        shell: bash
        env:
          version: ${{ needs.build.outputs.version }}
        run: |
          curl -Lso raylib.zip https://github.com/raysan5/raylib/archive/refs/tags/${version}.zip
          unzip -qq raylib.zip
          cmake -S raylib-${version} \
            -A ${{ matrix.arch }} \
            -B build \
            -D CMAKE_BUILD_TYPE=Release \
            -D BUILD_SHARED_LIBS=ON \
            -D BUILD_EXAMPLES=OFF
          cmake --build build --config Release

      - name: upload build
        uses: actions/upload-artifact@v3
        with:
          name: win-${{ matrix.name }}
          path: build/raylib/Release/raylib.dll
          if-no-files-found: error

  publish:
    runs-on: ubuntu-latest
    needs:
      - build
      - build-android
      - build-linux
      - build-osx
      - build-windows
    steps:
      - name: checkout repository
        uses: actions/checkout@v3

      - name: setup dependencies
        run: |
          sudo apt-get update
          sudo apt-get install \
            libasound2-dev \
            libx11-dev \
            libxrandr-dev \
            libxi-dev \
            libgl1-mesa-dev \
            libglu1-mesa-dev \
            libxcursor-dev \
            libxinerama-dev

      - uses: actions/download-artifact@v3
        with:
          name: android-arm
          path: Examples.Android/runtimes/android-arm/native

      - uses: actions/download-artifact@v3
        with:
          name: android-arm64
          path: Examples.Android/runtimes/android-arm64/native

      - uses: actions/download-artifact@v3
        with:
          name: android-x86
          path: Examples.Android/runtimes/android-x86/native

      - uses: actions/download-artifact@v3
        with:
          name: android-x64
          path: Examples.Android/runtimes/android-x64/native

      - uses: actions/download-artifact@v3
        with:
          name: linux-x64
          path: Raylib-cs/runtimes/linux-x64/native

      - uses: actions/download-artifact@v3
        with:
          name: osx-arm64
          path: Raylib-cs/runtimes/osx-arm64/native

      - uses: actions/download-artifact@v3
        with:
          name: osx-x64
          path: Raylib-cs/runtimes/osx-x64/native

      - uses: actions/download-artifact@v3
        with:
          name: win-x86
          path: Raylib-cs/runtimes/win-x86/native

      - uses: actions/download-artifact@v3
        with:
          name: win-x64
          path: Raylib-cs/runtimes/win-x64/native

      - name: setup dotnet
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 6.0.x

      #- name: restore workload
      #  run: dotnet workload restore

      #- name: build examples android
      #  run: dotnet build Examples.Android -c Release

      - name: create NuGet Package
        run: dotnet pack Raylib-cs -c Release --output nuget

      - name: upload NuGet Package As Artifact
        uses: actions/upload-artifact@v3
        with:
          path: nuget/*

      - name: upload NuGet Package As Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: nuget/*

      - name: publish to NuGet
        if: startsWith(github.ref, 'refs/tags/')
        run: |
          dotnet nuget push \
            nuget/* \
            --api-key ${{secrets.NUGET_API_KEY}}