on:
  push:
    branches:
    - main
  pull_request:
    branches:
    - main

permissions:
  contents: read
  packages: read

name: main
jobs:
  version:
    name: Set version
    runs-on: ubuntu-24.04
    outputs:
      version: ${{steps.version.outputs.version}}
      commit: ${{steps.version.outputs.commit}}
      publish: ${{steps.version.outputs.publish}}

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          fetch-tags: true
          persist-credentials: false

      - name: Set version
        id: version
        run: |
          version=$(git describe --exact-match --tags HEAD 2> /dev/null || true)
          publish=false
          if [ -z "$version" ]; then
              version=$(git log -1 --date=format:"0.%Y.%m%d.%H%M" --format="%ad")
          else
              publish=true
          fi
          commit=$(git rev-parse HEAD)

          echo "version=$version" >> $GITHUB_OUTPUT
          echo "version=$version"
          echo "commit=$commit" >> $GITHUB_OUTPUT
          echo "commit=$commit"
          echo "publish=$publish" >> $GITHUB_OUTPUT
          echo "publish=$publish"

  linux_x64:
    name: Linux (Q8/Q16/Q16-HDRI, x64)
    container:
      image: mcr.microsoft.com/dotnet/sdk:9.0
    runs-on: ubuntu-24.04

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: build/linux-x64/install.dependencies.sh

      - uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7 #v5.2.0
        with:
          dotnet-version: 8.x

      - name: Install Magick.Native
        run: build/shared/install.Magick.Native.sh "dlemstra" "${{ secrets.GITHUB_TOKEN }}" linux x64

      - name: Build
        run: build/shared/build.Magick.NET.sh x64

      - name: Test
        run: build/shared/test.Magick.NET.sh x64

      - name: Build OpenMP
        run: build/shared/build.Magick.NET.sh x64 OpenMP

      - name: Test OpenMP
        run: build/shared/test.Magick.NET.sh x64 OpenMP

  linux_musl_x64:
    name: Linux musl (Q8/Q16/Q16-HDRI, x64)
    container:
      image: mcr.microsoft.com/dotnet/sdk:9.0-alpine
    runs-on: ubuntu-24.04

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: build/linux-musl-x64/install.dependencies.sh

      - uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7 #v5.2.0
        with:
          dotnet-version: 8.x

      - name: Install Magick.Native
        run: build/shared/install.Magick.Native.sh "dlemstra" "${{ secrets.GITHUB_TOKEN }}" linux-musl x64

      - name: Build
        run: build/shared/build.Magick.NET.sh x64

      - name: Test
        run: build/shared/test.Magick.NET.sh x64

      - name: Build OpenMP
        run: build/shared/build.Magick.NET.sh x64 OpenMP

      - name: Test OpenMP
        run: build/shared/test.Magick.NET.sh x64 OpenMP

  linux_arm64:
    name: Linux (Q8/Q16/Q16-HDRI, arm64)
    runs-on: ubuntu-24.04-arm

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: build/linux-arm64/install.dependencies.sh

      - uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7 #v5.2.0
        with:
          dotnet-version: | 
            8.x
            9.x

      - name: Install Magick.Native
        run: build/shared/install.Magick.Native.sh "dlemstra" "${{ secrets.GITHUB_TOKEN }}" linux arm64

      - name: Build
        run: build/shared/build.Magick.NET.sh arm64

      - name: Test
        run: build/shared/test.Magick.NET.sh arm64

      - name: Build OpenMP
        run: build/shared/build.Magick.NET.sh arm64 OpenMP

      - name: Test OpenMP
        run: build/shared/test.Magick.NET.sh arm64 OpenMP

  macos_x64:
    name: MacOS (Q8/Q16/Q16-HDRI, x64)
    runs-on: macos-15-intel

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: build/macos-x64/install.dependencies.sh

      - uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7 #v5.2.0
        with:
          dotnet-version: 8.x

      - name: Install Magick.Native
        run: build/shared/install.Magick.Native.sh "dlemstra" "${{ secrets.GITHUB_TOKEN }}" macos x64

      - name: Build
        run: build/shared/build.Magick.NET.sh x64

      - name: Test
        run: build/shared/test.Magick.NET.sh x64

  macos_arm64:
    name: MacOS (Q8/Q16/Q16-HDRI, arm64)
    runs-on: macos-14

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: build/macos-arm64/install.dependencies.sh

      - uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7 #v5.2.0
        with:
          dotnet-version: 8.x

      - name: Install Magick.Native
        run: build/shared/install.Magick.Native.sh "dlemstra" "${{ secrets.GITHUB_TOKEN }}" macos arm64

      - name: Build
        run: build/shared/build.Magick.NET.sh arm64

      - name: Test
        run: build/shared/test.Magick.NET.sh arm64

  windows_x64:
    name: Windows (${{matrix.quantumName}}, ${{matrix.platformName}})
    runs-on: windows-2022
    needs:
      - version

    strategy:
      fail-fast: false
      matrix:
        quantumName: [Q8, Q8-OpenMP, Q16, Q16-OpenMP, Q16-HDRI, Q16-HDRI-OpenMP]
        platformName: [x86, x64, Any CPU]
        exclude:
        - quantumName: Q8-OpenMP
          platformName: x86
        - quantumName: Q16-OpenMP
          platformName: x86
        - quantumName: Q16-HDRI-OpenMP
          platformName: x86
        - quantumName: Q8-OpenMP
          platformName: Any CPU
        - quantumName: Q16-OpenMP
          platformName: Any CPU
        - quantumName: Q16-HDRI-OpenMP
          platformName: Any CPU

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: ./install.dependencies.cmd
        working-directory: build/windows

      - name: Install NuGet
        run: ./install.nuget.cmd
        working-directory: build/windows

      - name: Create nuget.config
        run: ./create-nuget-config.cmd "dlemstra" "${{ secrets.GITHUB_TOKEN }}"
        working-directory: src/Magick.Native

      - name: Install Magick.Native
        run: ./install.cmd
        working-directory: src/Magick.Native

      - name: Build Magick.NET (Test)
        run: ./build.Magick.NET.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}" Test
        working-directory: build/windows

      - name: Test Magick.NET
        run: ./test.Magick.NET.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}"
        working-directory: build/windows

      - name: Build Magick.NET (Release)
        run: ./build.Magick.NET.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}" Release
        working-directory: build/windows

      - name: Create NuGet package
        run: ./publish.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}" "${{ needs.version.outputs.version }}" "${{ needs.version.outputs.commit }}"
        working-directory: publish

      - name: Upload library
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a #v7.0.1
        with:
          name: Magick.NET-${{ matrix.quantumName }}-${{ matrix.platformName }}
          path: publish/output

  windows_arm64:
    name: Windows (${{matrix.quantumName}}, ${{matrix.platformName}})
    #runs-on: windows-11-arm
    runs-on: windows-2022
    needs:
      - version

    strategy:
      fail-fast: false
      matrix:
        quantumName: [Q8, Q8-OpenMP, Q16, Q16-OpenMP, Q16-HDRI, Q16-HDRI-OpenMP]
        platformName: [arm64]

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: ./install.dependencies.cmd
        working-directory: build/windows

      - name: Install NuGet
        run: ./install.nuget.cmd
        working-directory: build/windows

      - name: Create nuget.config
        run: ./create-nuget-config.cmd "dlemstra" "${{ secrets.GITHUB_TOKEN }}"
        working-directory: src/Magick.Native

      - name: Install Magick.Native
        run: ./install.cmd
        working-directory: src/Magick.Native

      # - name: Build Magick.NET (Test)
      #   run: ./build.Magick.NET.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}" Test
      #   working-directory: build/windows

      # - name: Test Magick.NET
      #   run: ./test.Magick.NET.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}"
      #   working-directory: build/windows

      - name: Build Magick.NET (Release)
        run: ./build.Magick.NET.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}" Release
        working-directory: build/windows

      - name: Create NuGet package
        run: ./publish.cmd ${{ matrix.quantumName }} "${{ matrix.platformName }}" "${{ needs.version.outputs.version }}" "${{ needs.version.outputs.commit }}"
        working-directory: publish

      - name: Upload library
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a #v7.0.1
        with:
          name: Magick.NET-${{ matrix.quantumName }}-${{ matrix.platformName }}
          path: publish/output

  libraries:
    name: Library
    runs-on: windows-2022
    needs:
      - version

    strategy:
      fail-fast: false
      matrix:
        libraryName: [Core, AvaloniaMediaImaging, SystemDrawing, SystemWindowsMedia]

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install dependencies
        run: ./install.dependencies.cmd
        working-directory: build/windows

      - name: Install NuGet
        run: ./install.nuget.cmd
        working-directory: build/windows

      - name: Create nuget.config
        run: ./create-nuget-config.cmd "dlemstra" "${{ secrets.GITHUB_TOKEN }}"
        working-directory: src/Magick.Native

      - name: Install Magick.Native
        run: ./install.cmd
        working-directory: src/Magick.Native

      - name: Build Magick.NET.${{ matrix.libraryName }} (Release)
        run: ./build.Magick.NET.cmd "Q8" "Any CPU" Release
        working-directory: build/windows

      - name: Create NuGet package
        run: ./publish.library.cmd "Magick.NET.${{ matrix.libraryName }}" "${{ needs.version.outputs.version }}" "${{ needs.version.outputs.commit }}"
        working-directory: publish

      - name: Upload library
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a #v7.0.1
        with:
          name: Magick.NET.${{ matrix.libraryName }}
          path: publish/output

  publish:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    name: Publish packages to NuGet (tagged releases only)
    runs-on: windows-2022
    environment: nuget
    needs:
      - libraries
      - linux_arm64
      - linux_musl_x64
      - linux_x64
      - macos_arm64
      - macos_x64 
      - version
      - windows_arm64
      - windows_x64

    permissions:
      id-token: write
      contents: read
      packages: read

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
        with:
          persist-credentials: false

      - name: Install NuGet
        run: ./install.nuget.cmd
        working-directory: build/windows

      - name: Download artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c #v8.0.1
        with:
          path: packages
          merge-multiple: true

      - name: Sign executables and libraries
        uses: ImageMagick/code-signing-action@11c51f7659405bee4dd3529748e1501a0d388911 #v1.0.1
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
          directory: ${{ github.workspace }}\packages
          files: '*.nupkg'

      - name: Print public key
        run: |
          pip3 install asn1crypto
          python3 ./build/windows/print.publickey.py

      - name: NuGet login
        uses: NuGet/login@d22cc5f58ff5b88bf9bd452535b4335137e24544 #v1
        id: login
        with:
          user: dlemstra

      - name: Publish NuGet packages
        if: needs.version.outputs.publish == 'true'
        env:
          NUGET_API_KEY: ${{ steps.login.outputs.NUGET_API_KEY }}
        run: ../publish/push.cmd
        working-directory: packages
