Skip to content

Commit

Permalink
feat: Add support for publishing NuGet package with .NET SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Shamus authored and gaelcolas committed Jul 1, 2023
1 parent 73c8777 commit a14049c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
17 changes: 15 additions & 2 deletions .build/tasks/release.module.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/tasks/release.module.build.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down

0 comments on commit a14049c

Please sign in to comment.