Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

fix new-nugetpackage to cope with more output from child process #479

Merged
merged 2 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 60 additions & 51 deletions src/PowerShellGet/private/functions/New-NugetPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -27,89 +29,96 @@ 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"

$tempPath = $null
}
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 = @"
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>NotUsed</AssemblyName>
<Description>Temp project used for creating nupkg file.</Description>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>true</IsPackable>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>NotUsed</AssemblyName>
<Description>Temp project used for creating nupkg file.</Description>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>true</IsPackable>
</PropertyGroup>
</Project>
"@
$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 = @()
Write-Verbose "$ProcessName output:"
while (! $process.HasExited) {
$output = $process.StandardOutput.ReadLine()
Write-Verbose "`t$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 (($tempPath -ne $null) -and (Test-Path -Path $tempPath)) {
Remove-Item -Path $tempPath -Force -Recurse
}

if (-Not ($process.ExitCode -eq 0 )) {
# 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 = $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
}
2 changes: 2 additions & 0 deletions src/PowerShellGet/private/functions/New-NuspecFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function Publish-PSArtifactUtility {
$Exclude
)

Write-Verbose "Calling Publish-PSArtifactUtility"
Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -BootstrapNuGetExe

$PSArtifactType = $script:PSArtifactTypeModule
Expand Down