From a14049c5241e75f52270d292c026d67dac173a92 Mon Sep 17 00:00:00 2001 From: Paul Shamus Date: Wed, 28 Jun 2023 20:48:08 -0700 Subject: [PATCH] feat: Add support for publishing NuGet package with .NET SDK --- .build/tasks/release.module.build.ps1 | 17 ++++++++-- CHANGELOG.md | 6 +++- .../Unit/tasks/release.module.build.Tests.ps1 | 31 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/.build/tasks/release.module.build.ps1 b/.build/tasks/release.module.build.ps1 index f8646699..9e2c8e42 100644 --- a/.build/tasks/release.module.build.ps1 +++ b/.build/tasks/release.module.build.ps1 @@ -207,7 +207,7 @@ task Create_changelog_release_output { } # Synopsis: Publish Nuget package to a gallery. -task publish_nupkg_to_gallery -if ($GalleryApiToken -and (Get-Command -Name 'nuget' -ErrorAction 'SilentlyContinue')) { +task publish_nupkg_to_gallery -if ($GalleryApiToken -and ((Get-Command -Name 'nuget' -ErrorAction 'SilentlyContinue') -or (Get-Command -Name 'dotnet' -ErrorAction 'SilentlyContinue'))) { # Get the vales for task variables, see https://github.com/gaelcolas/Sampler#task-variables. . Set-SamplerTaskVariable @@ -223,7 +223,20 @@ task publish_nupkg_to_gallery -if ($GalleryApiToken -and (Get-Command -Name 'nug Write-Build DarkGray "About to release $PackageToRelease" if (-not $SkipPublish) { - $response = &nuget push $PackageToRelease -source $nugetPublishSource -ApiKey $GalleryApiToken + # Determine whether to use nuget or .NET SDK nuget + if (Get-Command -Name 'nuget' -ErrorAction 'SilentlyContinue') + { + Write-Build DarkGray 'Publishing package with nuget' + $command = 'nuget' + $nugetArgs = @('push', $PackageToRelease, '-Source', $nugetPublishSource, '-ApiKey', $GalleryApiToken) + } + else + { + Write-Build DarkGray 'Publishing package with .NET SDK nuget' + $command = 'dotnet' + $nugetArgs = @('nuget', 'push', $PackageToRelease, '--source', $nugetPublishSource, '--api-key', $GalleryApiToken) + } + $response = & $command $nugetArgs } Write-Build Green "Response = " + $response diff --git a/CHANGELOG.md b/CHANGELOG.md index 8147717a..68605058 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Task `publish_nupkg_to_gallery` + - Add support for publishing a NuGet package to a gallery using the .NET SDK in addition to using nuget.exe. Fixes [#433](https://github.com/gaelcolas/Sampler/issues/433) + ### Fixed - Fix unit tests that was wrongly written and failed on Pester 5.5. @@ -118,7 +123,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Script `Set-SamplerTaskVariable.ps1` - Added debug output of PSModulePath - ## [0.116.1] - 2023-01-09 ### Fixed diff --git a/tests/Unit/tasks/release.module.build.Tests.ps1 b/tests/Unit/tasks/release.module.build.Tests.ps1 index b0f4630d..ffba032e 100644 --- a/tests/Unit/tasks/release.module.build.Tests.ps1 +++ b/tests/Unit/tasks/release.module.build.Tests.ps1 @@ -280,6 +280,37 @@ Describe 'publish_nupkg_to_gallery' { Should -Invoke -CommandName nuget -Exactly -Times 1 -Scope It } } + + Context 'When publish Nuget package with .NET SDK' { + BeforeAll { + # Stub for executable dotnet + function dotnet {} + + Mock -CommandName dotnet -MockWith { + return '0' + } + + Mock -CommandName Get-Command -ParameterFilter { + $Name -eq 'nuget' -and $ErrorAction -eq 'SilentlyContinue' + } -MockWith { + $null + } + + Mock -CommandName Get-ChildItem -ParameterFilter { + $Path -match '\.nupkg' + } -MockWith { + return $TestDrive + } + } + + It 'Should run the build task without throwing' { + { + Invoke-Build -Task 'publish_nupkg_to_gallery' -File $taskAlias.Definition @mockTaskParameters + } | Should -Not -Throw + + Should -Invoke -CommandName dotnet -Exactly -Times 1 -Scope It + } + } } Describe 'package_module_nupkg' {