name: Build NuGet Package
on:
  push:
    tags: [ '[0-9]+.[0-9]+.[0-9]+' ]
  workflow_dispatch:
    inputs:
      version:
        description: 'The optional semantic version number. If not supplied the branch/tag will be used.'
        type: string
      no_publish:
          description: 'Prevent publishing the NuGet package. Just build it and then upload it as an artifact.'
          type: boolean
          default: false

jobs:
  package-windows-latest:
    runs-on: windows-2019
    steps:
    - name: Set version based on input
      if: ${{ inputs.version }}
      run: echo "RELEASE_VERSION=${{ inputs.version }}" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8
    - name: Set version based on ref
      if: ${{ !inputs.version }}
      run: echo "RELEASE_VERSION=$($env:GITHUB_REF -replace 'refs/.*/', '')" | Out-File -Append -FilePath $env:GITHUB_ENV -Encoding utf8
    - name: Print Version
      run: echo "NuGet version will be '${{ env.RELEASE_VERSION }}'"
    - name: Checkout
      uses: actions/checkout@v3
    - name: Setup NuGet.exe
      uses: nuget/setup-nuget@v2
    - name: Install meson
      run: pip install meson
    - name: "[Release_x86] Build & Install"
      env:
        CMAKE_GENERATOR: "Visual Studio 16 2019"
      uses: ashutoshvarma/action-cmake-build@master
      with:
        build-dir: ${{github.workspace}}/build
        source-dir: ${{github.workspace}}
        build-type: Release
        target: ALL_BUILD
        run-test: false
        configure-options: -DBUILD_SHARED_LIBS=ON -DCURL_ZLIB=OFF -A Win32
        install-build: true
        install-options: --prefix ${{github.workspace}}\install --config Release
    - name: "[Release_x86] Copy install files for Release_x86"
      run: xcopy /e /i /y ${{github.workspace}}\install ${{github.workspace}}\nuget\build\native\x86\Release && xcopy /e /i /y ${{github.workspace}}\install ${{github.workspace}}\nuget\build\native\Win32\Release
    - name: "[Release_x86] Clean build"
      run: rm -r -fo ${{github.workspace}}/build
    - name: "[Debug_x86] Build & Install"
      env:
        CMAKE_GENERATOR: "Visual Studio 16 2019"
      uses: ashutoshvarma/action-cmake-build@master
      with:
        build-dir: ${{github.workspace}}/build
        source-dir: ${{github.workspace}}
        build-type: Debug
        target: ALL_BUILD
        run-test: false
        configure-options: -DBUILD_SHARED_LIBS=ON -DCURL_ZLIB=OFF -A Win32
        install-build: true
        install-options: --prefix ${{github.workspace}}\install --config Debug
    - name: "[Debug_x86] Copy install files for Debug_x86"
      run: xcopy /e /i /y ${{github.workspace}}\install ${{github.workspace}}\nuget\build\native\x86\Debug && xcopy /e /i /y ${{github.workspace}}\install ${{github.workspace}}\nuget\build\native\Win32\Debug
    - name: "[Debug_x86] Clean build"
      run: rm -r -fo ${{github.workspace}}/build
    - name: "[Release_x64] Build & Install"
      env:
        CMAKE_GENERATOR: "Visual Studio 16 2019"
      uses: ashutoshvarma/action-cmake-build@master
      with:
        build-dir: ${{github.workspace}}/build
        source-dir: ${{github.workspace}}
        build-type: Release
        target: ALL_BUILD
        run-test: false
        configure-options: -DBUILD_SHARED_LIBS=ON -DCURL_ZLIB=OFF -A x64
        install-build: true
        install-options: --prefix ${{github.workspace}}\install --config Release
    - name: "[Release_x64] Copy install files for Release_x64"
      run: xcopy /e /i /y ${{github.workspace}}\install ${{github.workspace}}\nuget\build\native\x64\Release
    - name: "[Release_x64] Clean build"
      run: rm -r -fo ${{github.workspace}}/build
    - name: "[Debug_x64] Build & Install"
      env:
        CMAKE_GENERATOR: "Visual Studio 16 2019"
      uses: ashutoshvarma/action-cmake-build@master
      with:
        build-dir: ${{github.workspace}}/build
        source-dir: ${{github.workspace}}
        build-type: Debug
        target: ALL_BUILD
        run-test: false
        configure-options: -DBUILD_SHARED_LIBS=ON -DCURL_ZLIB=OFF -A x64
        install-build: true
        install-options: --prefix ${{github.workspace}}\install --config Debug
    - name: "[Debug_x64] Copy install files for Debug_x64"
      run: xcopy /e /i /y ${{github.workspace}}\install ${{github.workspace}}\nuget\build\native\x64\Debug
    - name: "Copy Readme.md"
      run: xcopy /y ${{github.workspace}}\README.md ${{github.workspace}}\nuget
    - name: "Create NuGet package"
      env: 
        VERSION: ${{ env.RELEASE_VERSION }}
        COMMIT_HASH: ${{ github.sha }}
      run: nuget pack ${{github.workspace}}\nuget\libcpr.nuspec -OutputDirectory ${{github.workspace}} -Properties "VERSION=$ENV:VERSION;COMMIT_HASH=$ENV:COMMIT_HASH"
    - name: "Upload artifact"
      uses: actions/upload-artifact@v4
      with:
        name: artifact-nuget
        path: ${{github.workspace}}\*.nupkg
    - name: "Publish package to NuGet.org"
      if: ${{ !inputs.no_publish }}
      env:
        NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
      run: nuget push ${{github.workspace}}\*.nupkg $ENV:NUGET_API_KEY -Source https://api.nuget.org/v3/index.json
