name: 'Continuous integration'

on:
  workflow_dispatch:
    inputs:
      publish_release:
        description: If this build should publish nuget packages
        required: true
        type: boolean
      version_suffix:
        description: Suffix of the version number. Can be used to create a preview package.
        required: false
        type: string
  push:
    branches:
      - main
    paths-ignore:
      - '**.md'
  pull_request:

env:
  configuration: Release
  publish_release: ${{ github.event.inputs.publish_release }}
  version_suffix: ${{ github.event.inputs.version_suffix }}

jobs:
  build_macos:
    name: Build (MacOS)
    runs-on: macos-12
    if: github.event.inputs.publish_release != 'true'

    defaults:
      run:
        shell: bash

    steps:
      - name: Install dependencies
        run: brew install cairo gdk-pixbuf gobject-introspection meson

      - name: Prepare git
        run: git config --global core.autocrlf false
        shell: bash

      - name: Checkout with submodules
        uses: actions/checkout@v4
        with:
          submodules: 'true'

      - name: Create Gir.Core
        uses: ./.github/actions/create
        with:
          configuration: ${{ env.configuration }}
          source_directory: './src'
          shell: bash
  
  build_windows:
    name: Build (Windows)
    runs-on: windows-latest
    if: github.event.inputs.publish_release != 'true'
    
    defaults:
      run:
        shell: msys2 {0}
    
    steps:
      - name: Install dependencies
        uses: msys2/setup-msys2@v2
        with:
          path-type: inherit # Inherit the path so that dotnet can be found
          update: true
          install: >-
            mingw-w64-x86_64-cairo
            mingw-w64-x86_64-gcc
            mingw-w64-x86_64-gdk-pixbuf2
            mingw-w64-x86_64-gobject-introspection
            mingw-w64-x86_64-meson

      - name: Prepare git
        run: git config --global core.autocrlf false
        shell: bash

      - name: Checkout with submodules
        uses: actions/checkout@v4
        with:
          submodules: 'true'

      - name: Create Gir.Core
        uses: ./.github/actions/create
        with:
          configuration: ${{ env.configuration }}
          source_directory: './src'
          shell: msys2 {0}

  build_linux:
    name: Build (Linux)
    runs-on: ubuntu-latest
    container:
      image: fedora:latest
      env:
        DOTNET_SYSTEM_GLOBALIZATION_INVARIANT: 1
    
    defaults:
      run:
        shell: bash
    
    steps:
      - name: Install dependencies
        run: sudo dnf -y upgrade && sudo dnf -y install meson gobject-introspection-devel git gcc glib2-devel gdk-pixbuf2 gdk-pixbuf2-modules cairo-gobject

      - name: Prepare git
        run: git config --global core.autocrlf false
        shell: bash

      - name: Checkout with submodules
        uses: actions/checkout@v4
        with:
          submodules: 'true'

      - name: Create Gir.Core
        uses: ./.github/actions/create
        with:
          configuration: ${{ env.configuration }}
          source_directory: './src'
          shell: bash

      - name: Verify code format
        run: dotnet format GirCore.sln --no-restore --verify-no-changes --exclude *.Generated.cs
        working-directory: './src'

      - name: Run integration tests
        run: dotnet test --no-restore -c $configuration --filter TestCategory=IntegrationTest
        working-directory: './src'

      - name: Pack release version
        if: env.publish_release == 'true'
        run: dotnet pack --no-build --nologo -c $configuration --version-suffix "$version_suffix" -o ../Nuget
        working-directory: './src'

      - name: Publish to nuget org
        if: env.publish_release == 'true'
        run: dotnet nuget push "*.nupkg" -k ${{ secrets.NUGET_API_KEY }} -s nuget.org
        working-directory: './Nuget'