From a6957d0f0baaea59454897b3285df9d8555a6a88 Mon Sep 17 00:00:00 2001 From: edwin young Date: Wed, 15 May 2019 09:51:31 -0700 Subject: [PATCH 1/2] fix new-nugetpackage to cope with more output from child process --- .../private/functions/New-NugetPackage.ps1 | 98 +++++++++---------- .../private/functions/New-NuspecFile.ps1 | 2 + .../functions/Publish-NugetPackage.ps1 | 1 + .../functions/Publish-PSArtifactUtility.ps1 | 1 + 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/PowerShellGet/private/functions/New-NugetPackage.ps1 b/src/PowerShellGet/private/functions/New-NugetPackage.ps1 index 16cd6502..b87fb5ec 100644 --- a/src/PowerShellGet/private/functions/New-NugetPackage.ps1 +++ b/src/PowerShellGet/private/functions/New-NugetPackage.ps1 @@ -19,6 +19,8 @@ function New-NugetPackage { ) Set-StrictMode -Off + Write-Verbose "Calling New-NugetPackage" + if (-Not(Test-Path -Path $NuspecPath -PathType Leaf)) { throw "A nuspec file does not exist at $NuspecPath, provide valid path to a .nuspec" } @@ -27,89 +29,83 @@ function New-NugetPackage { throw "NugetPackageRoot $NugetPackageRoot does not exist" } + $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo if ($PSCmdlet.ParameterSetName -eq "UseNuget") { if (-Not(Test-Path -Path $NuGetExePath)) { throw "Nuget.exe does not exist at $NugetExePath, provide a valid path to nuget.exe" } + $ProcessName = $NugetExePath $ArgumentList = @("pack") $ArgumentList += "`"$NuspecPath`"" - $ArgumentList += "-outputdirectory `"$OutputPath`"" - - $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo - $processStartInfo.FileName = $NugetExePath - $processStartInfo.RedirectStandardError = $true - $processStartInfo.RedirectStandardOutput = $true - $processStartInfo.UseShellExecute = $false - $processStartInfo.Arguments = $ArgumentList - - $process = New-Object System.Diagnostics.Process - $process.StartInfo = $processStartInfo - $process.Start() | Out-Null - $process.WaitForExit() - - if (-Not ($process.ExitCode -eq 0 )) { - $stdErr = $process.StandardError.ReadToEnd() - throw "nuget.exe failed to pack $stdErr" - } + $ArgumentList += "-outputdirectory `"$OutputPath`" -noninteractive" } + else { + # use Dotnet CLI - if ($PSCmdlet.ParameterSetName -eq "UseDotnetCli") { #perform dotnet pack using a temporary project file. - $dotnetCliPath = (Get-Command -Name "dotnet").Source + $ProcessName = (Get-Command -Name "dotnet").Source $tempPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.Guid]::NewGuid()).Guid New-Item -ItemType Directory -Path $tempPath -Force | Out-Null $CsprojContent = @" - - - NotUsed - Temp project used for creating nupkg file. - netcoreapp2.0 - true - - + + + NotUsed + Temp project used for creating nupkg file. + netcoreapp2.0 + true + + "@ $projectFile = New-Item -ItemType File -Path $tempPath -Name "Temp.csproj" Set-Content -Value $CsprojContent -Path $projectFile - #execution - $ArgumentList = @("pack") $ArgumentList += "`"$projectFile`"" $ArgumentList += "/p:NuspecFile=`"$NuspecPath`"" $ArgumentList += "--output `"$OutputPath`"" + } - $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo - $processStartInfo.FileName = $dotnetCliPath - $processStartInfo.RedirectStandardError = $true - $processStartInfo.RedirectStandardOutput = $true - $processStartInfo.UseShellExecute = $false - $processStartInfo.Arguments = $ArgumentList + # run the packing program + $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo + $processStartInfo.FileName = $ProcessName + $processStartInfo.Arguments = $ArgumentList + $processStartInfo.RedirectStandardError = $true + $processStartInfo.RedirectStandardOutput = $true + $processStartInfo.UseShellExecute = $false + + Write-Verbose "Calling $ProcessName $($ArgumentList -join ' ')" + $process = New-Object System.Diagnostics.Process + $process.StartInfo = $processStartInfo + + $process.Start() | Out-Null + + # read output incrementally, it'll block if it writes too much + $outputLines = @() + while (! $process.HasExited) { + $output = $process.StandardOutput.ReadLine() + Write-Verbose "Nuget output: $output" + $outputLines += $output + } - $process = New-Object System.Diagnostics.Process - $process.StartInfo = $processStartInfo - $process.Start() | Out-Null - $process.WaitForExit() + # get any remaining output + $process.WaitForExit() + $outputLines += $process.StandardOutput.ReadToEnd() - if (Test-Path -Path $tempPath) { - Remove-Item -Path $tempPath -Force -Recurse - } + $stdOut = $outputLines -join "`n" - if (-Not ($process.ExitCode -eq 0 )) { - $stdOut = $process.StandardOutput.ReadToEnd() - throw "dotnet cli failed to pack $stdOut" - } + Write-Verbose "finished running $($processStartInfo.FileName) with exit code $($process.ExitCode)" + if (-Not ($process.ExitCode -eq 0 )) { + $stdErr = $process.StandardError.ReadToEnd() + throw "$ProcessName failed to pack: error $stdErr" } - $stdOut = $process.StandardOutput.ReadToEnd() $stdOut -match "Successfully created package '(.*.nupkg)'" | Out-Null $nupkgFullFile = $matches[1] - $stdOut = $process.StandardOutput.ReadToEnd() - - Write-Verbose -Message $stdOut + Write-Verbose "Created Nuget Package $nupkgFullFile" Write-Output $nupkgFullFile } diff --git a/src/PowerShellGet/private/functions/New-NuspecFile.ps1 b/src/PowerShellGet/private/functions/New-NuspecFile.ps1 index e071d20e..1cc17497 100644 --- a/src/PowerShellGet/private/functions/New-NuspecFile.ps1 +++ b/src/PowerShellGet/private/functions/New-NuspecFile.ps1 @@ -49,6 +49,8 @@ function New-NuspecFile { ) Set-StrictMode -Off + Write-Verbose "Calling New-NuspecFile" + $nameSpaceUri = "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd" [xml]$xml = New-Object System.Xml.XmlDocument diff --git a/src/PowerShellGet/private/functions/Publish-NugetPackage.ps1 b/src/PowerShellGet/private/functions/Publish-NugetPackage.ps1 index d410bd30..d470dba4 100644 --- a/src/PowerShellGet/private/functions/Publish-NugetPackage.ps1 +++ b/src/PowerShellGet/private/functions/Publish-NugetPackage.ps1 @@ -18,6 +18,7 @@ function Publish-NugetPackage { ) Set-StrictMode -Off + Write-Verbose "Calling Publish-NugetPackage -NupkgPath $NupkgPath -Destination $Destination -NugetExePath $NugetExePath -UseDotnetCli:$UseDotnetCli" $Destination = $Destination.TrimEnd("\") if ($PSCmdlet.ParameterSetName -eq "UseNuget") { diff --git a/src/PowerShellGet/private/functions/Publish-PSArtifactUtility.ps1 b/src/PowerShellGet/private/functions/Publish-PSArtifactUtility.ps1 index 504ae5c0..33c27453 100644 --- a/src/PowerShellGet/private/functions/Publish-PSArtifactUtility.ps1 +++ b/src/PowerShellGet/private/functions/Publish-PSArtifactUtility.ps1 @@ -70,6 +70,7 @@ function Publish-PSArtifactUtility { $Exclude ) + Write-Verbose "Calling Publish-PSArtifactUtility" Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -BootstrapNuGetExe $PSArtifactType = $script:PSArtifactTypeModule From 24b237fdaecd562f95bdb84926be8ee1013752fd Mon Sep 17 00:00:00 2001 From: edwin young Date: Thu, 16 May 2019 21:39:51 -0700 Subject: [PATCH 2/2] address PR feedback --- .../private/functions/New-NugetPackage.ps1 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/PowerShellGet/private/functions/New-NugetPackage.ps1 b/src/PowerShellGet/private/functions/New-NugetPackage.ps1 index b87fb5ec..c4d773ab 100644 --- a/src/PowerShellGet/private/functions/New-NugetPackage.ps1 +++ b/src/PowerShellGet/private/functions/New-NugetPackage.ps1 @@ -40,6 +40,8 @@ function New-NugetPackage { $ArgumentList = @("pack") $ArgumentList += "`"$NuspecPath`"" $ArgumentList += "-outputdirectory `"$OutputPath`" -noninteractive" + + $tempPath = $null } else { # use Dotnet CLI @@ -84,9 +86,10 @@ function New-NugetPackage { # read output incrementally, it'll block if it writes too much $outputLines = @() + Write-Verbose "$ProcessName output:" while (! $process.HasExited) { $output = $process.StandardOutput.ReadLine() - Write-Verbose "Nuget output: $output" + Write-Verbose "`t$output" $outputLines += $output } @@ -98,9 +101,19 @@ function New-NugetPackage { Write-Verbose "finished running $($processStartInfo.FileName) with exit code $($process.ExitCode)" + if (($tempPath -ne $null) -and (Test-Path -Path $tempPath)) { + Remove-Item -Path $tempPath -Force -Recurse + } + if (-Not ($process.ExitCode -eq 0 )) { - $stdErr = $process.StandardError.ReadToEnd() - throw "$ProcessName failed to pack: error $stdErr" + # nuget writes errors to stdErr, dotnet writes them to stdOut + if ($UseDotnetCli) { + $errors = $stdOut + } + else { + $errors = $process.StandardError.ReadToEnd() + } + throw "$ProcessName failed to pack: error $errors" } $stdOut -match "Successfully created package '(.*.nupkg)'" | Out-Null