name: Build NuGet Packages

on:
  workflow_call:
    outputs:
      # Output the NuGet version suffix so that the caller workflow can have the correct package version.
      nuget-version-suffix:
        description: "The NuGet version suffix to build the packages"
        value: ${{ jobs.build-nuget-packages.outputs.nuget-version-suffix }}

permissions:
  contents: read

env:
  NUGET_PACKAGES: ${{ github.workspace }}/packages
  DOTNET_CLI_TELEMETRY_OPTOUT: 1

jobs:
  build-nuget-packages:
    # In principle this job "needs: [ build, build-container ]", however, that would cause the build and build-container
    # jobs to run twice, which is not what we want. Instead, we let the "caller" job handle the dependencies.
    outputs:
      nuget-version-suffix: ${{ steps.set-nuget-version-suffix.outputs.suffix }}
    runs-on: windows-2022
    steps:

      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag: v4.2.2
        with:
          fetch-depth: 0 # fetching all, needed to correctly calculate version

      - name: Setup .NET
        uses: actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # tag: v4.3.1
        with:
          dotnet-version: | 
            8.0.412
            9.0.302

      - name: Check for NuGet packages cache
        uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # tag: v4.2.3
        id: nuget-cache
        with:
          key: ${{ hashFiles('**/Directory.packages.props', './build/LibraryVersions.g.cs', '**/packages.config' ) }}
          path: ${{ env.NUGET_PACKAGES }}

      - name: Download Windows Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-windows-2022
          path: bin/ci-artifacts/bin-windows-2022

      - name: Download Ubuntu x64 Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-ubuntu-22.04
          path: bin/ci-artifacts/bin-ubuntu-22.04

      - name: Download Ubuntu arm64 Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-ubuntu-22.04-arm
          path: bin/ci-artifacts/bin-ubuntu-22.04-arm

      - name: Download Alpine x64 Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-alpine-x64
          path: bin/ci-artifacts/bin-alpine-x64

      - name: Download Alpine arm64 Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-alpine-arm64
          path: bin/ci-artifacts/bin-alpine-arm64

      - name: Download Mac-OS Artifacts from build job
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # tag: v4.3.0
        with:
          name: bin-macos-13
          path: bin/ci-artifacts/bin-macos-13

      - name: Build NuGet packages
        run: ./build.cmd BuildNuGetPackages

      - name: Install dotnet-validate
        run: dotnet tool install --global dotnet-validate --version 0.0.1-preview.304

      - name: Install Meziantou.Framework.NuGetPackageValidation.Tool
        run: dotnet tool install --global Meziantou.Framework.NuGetPackageValidation.Tool --version 1.0.17

      - name: Validate nuget packages
        shell: pwsh
        run: |
          foreach ($file in (Get-ChildItem bin/nuget-artifacts//*.nupkg)) {
            dotnet validate package local $($file)
            if (-not ($LASTEXITCODE -eq 0)) {
              throw "dotnet validate failed for $($file)";
            }
            meziantou.validate-nuget-package $($file)
            if (-not ($LASTEXITCODE -eq 0)) {
              throw "meziantou.validate-nuget-package validate failed for $($file)";
            }
          }
        
      - name: Upload Nuget Artifacts
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # tag: v4.6.2
        with:
          name: bin-nuget-packages
          path: bin/nuget-artifacts/
