|
| 1 | +function Publish-NugetPackage { |
| 2 | + [CmdletBinding()] |
| 3 | + Param( |
| 4 | + [Parameter(Mandatory = $true)] |
| 5 | + [string]$NupkgPath, |
| 6 | + |
| 7 | + [Parameter(Mandatory = $true)] |
| 8 | + [string]$Destination, |
| 9 | + |
| 10 | + [Parameter(Mandatory = $true)] |
| 11 | + [string]$NugetApiKey, |
| 12 | + |
| 13 | + [Parameter(ParameterSetName = "UseNuget")] |
| 14 | + [string]$NugetExePath, |
| 15 | + |
| 16 | + [Parameter(ParameterSetName = "UseDotnetCli")] |
| 17 | + [switch]$UseDotnetCli |
| 18 | + ) |
| 19 | + |
| 20 | + $Destination = $Destination.TrimEnd("\") |
| 21 | + |
| 22 | + if ($PSCmdlet.ParameterSetName -eq "UseNuget") { |
| 23 | + $ArgumentList = @('push') |
| 24 | + $ArgumentList += "`"$NupkgPath`"" |
| 25 | + $ArgumentList += @('-source', "`"$Destination`"") |
| 26 | + $ArgumentList += @('-apikey', "`"$NugetApiKey`"") |
| 27 | + $ArgumentList += '-NonInteractive' |
| 28 | + |
| 29 | + #use processstartinfo and process objects here as it allows stderr redirection in memory rather than file. |
| 30 | + $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo |
| 31 | + $processStartInfo.FileName = $NugetExePath |
| 32 | + $processStartInfo.RedirectStandardError = $true |
| 33 | + $processStartInfo.RedirectStandardOutput = $true |
| 34 | + $processStartInfo.UseShellExecute = $false |
| 35 | + $processStartInfo.Arguments = $ArgumentList |
| 36 | + |
| 37 | + $process = New-Object System.Diagnostics.Process |
| 38 | + $process.StartInfo = $processStartInfo |
| 39 | + $process.Start() | Out-Null |
| 40 | + $process.WaitForExit() |
| 41 | + |
| 42 | + if (-Not ($process.ExitCode -eq 0 )) { |
| 43 | + $stdErr = $process.StandardError.ReadToEnd() |
| 44 | + throw "nuget.exe failed to push $stdErr" |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if ($PSCmdlet.ParameterSetName -eq "UseDotnetCli") { |
| 49 | + #perform dotnet pack using a temporary project file. |
| 50 | + $dotnetCliPath = (Get-Command -Name "dotnet").Source |
| 51 | + |
| 52 | + $ArgumentList = @('nuget') |
| 53 | + $ArgumentList += 'push' |
| 54 | + $ArgumentList += "`"$NupkgPath`"" |
| 55 | + $ArgumentList += @('--source', "`"$Destination`"") |
| 56 | + $ArgumentList += @('--api-key', "`"$NugetApiKey`"") |
| 57 | + |
| 58 | + #use processstartinfo and process objects here as it allows stdout redirection in memory rather than file. |
| 59 | + $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo |
| 60 | + $processStartInfo.FileName = $dotnetCliPath |
| 61 | + $processStartInfo.RedirectStandardError = $true |
| 62 | + $processStartInfo.RedirectStandardOutput = $true |
| 63 | + $processStartInfo.UseShellExecute = $false |
| 64 | + $processStartInfo.Arguments = $ArgumentList |
| 65 | + |
| 66 | + $process = New-Object System.Diagnostics.Process |
| 67 | + $process.StartInfo = $processStartInfo |
| 68 | + $process.Start() | Out-Null |
| 69 | + $process.WaitForExit() |
| 70 | + |
| 71 | + if (-Not ($process.ExitCode -eq 0)) { |
| 72 | + $stdOut = $process.StandardOutput.ReadToEnd() |
| 73 | + throw "dotnet cli failed to nuget push $stdOut" |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + $stdOut = $process.StandardOutput.ReadToEnd() |
| 78 | + Write-Verbose -Message $stdOut |
| 79 | +} |
0 commit comments