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

Commit 7de99ee

Browse files
authored
fix new-nugetpackage to cope with more output from child process (#479)
* fix new-nugetpackage to cope with more output from child process * address PR feedback
1 parent 23797a9 commit 7de99ee

File tree

4 files changed

+64
-51
lines changed

4 files changed

+64
-51
lines changed

src/PowerShellGet/private/functions/New-NugetPackage.ps1

Lines changed: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ function New-NugetPackage {
1919
)
2020
Set-StrictMode -Off
2121

22+
Write-Verbose "Calling New-NugetPackage"
23+
2224
if (-Not(Test-Path -Path $NuspecPath -PathType Leaf)) {
2325
throw "A nuspec file does not exist at $NuspecPath, provide valid path to a .nuspec"
2426
}
@@ -27,89 +29,96 @@ function New-NugetPackage {
2729
throw "NugetPackageRoot $NugetPackageRoot does not exist"
2830
}
2931

32+
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
3033

3134
if ($PSCmdlet.ParameterSetName -eq "UseNuget") {
3235
if (-Not(Test-Path -Path $NuGetExePath)) {
3336
throw "Nuget.exe does not exist at $NugetExePath, provide a valid path to nuget.exe"
3437
}
38+
$ProcessName = $NugetExePath
3539

3640
$ArgumentList = @("pack")
3741
$ArgumentList += "`"$NuspecPath`""
38-
$ArgumentList += "-outputdirectory `"$OutputPath`""
39-
40-
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
41-
$processStartInfo.FileName = $NugetExePath
42-
$processStartInfo.RedirectStandardError = $true
43-
$processStartInfo.RedirectStandardOutput = $true
44-
$processStartInfo.UseShellExecute = $false
45-
$processStartInfo.Arguments = $ArgumentList
46-
47-
$process = New-Object System.Diagnostics.Process
48-
$process.StartInfo = $processStartInfo
49-
$process.Start() | Out-Null
50-
$process.WaitForExit()
51-
52-
if (-Not ($process.ExitCode -eq 0 )) {
53-
$stdErr = $process.StandardError.ReadToEnd()
54-
throw "nuget.exe failed to pack $stdErr"
55-
}
42+
$ArgumentList += "-outputdirectory `"$OutputPath`" -noninteractive"
43+
44+
$tempPath = $null
5645
}
46+
else {
47+
# use Dotnet CLI
5748

58-
if ($PSCmdlet.ParameterSetName -eq "UseDotnetCli") {
5949
#perform dotnet pack using a temporary project file.
60-
$dotnetCliPath = (Get-Command -Name "dotnet").Source
50+
$ProcessName = (Get-Command -Name "dotnet").Source
6151
$tempPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.Guid]::NewGuid()).Guid
6252
New-Item -ItemType Directory -Path $tempPath -Force | Out-Null
6353

6454
$CsprojContent = @"
65-
<Project Sdk="Microsoft.NET.Sdk">
66-
<PropertyGroup>
67-
<AssemblyName>NotUsed</AssemblyName>
68-
<Description>Temp project used for creating nupkg file.</Description>
69-
<TargetFramework>netcoreapp2.0</TargetFramework>
70-
<IsPackable>true</IsPackable>
71-
</PropertyGroup>
72-
</Project>
55+
<Project Sdk="Microsoft.NET.Sdk">
56+
<PropertyGroup>
57+
<AssemblyName>NotUsed</AssemblyName>
58+
<Description>Temp project used for creating nupkg file.</Description>
59+
<TargetFramework>netcoreapp2.0</TargetFramework>
60+
<IsPackable>true</IsPackable>
61+
</PropertyGroup>
62+
</Project>
7363
"@
7464
$projectFile = New-Item -ItemType File -Path $tempPath -Name "Temp.csproj"
7565
Set-Content -Value $CsprojContent -Path $projectFile
7666

77-
#execution
78-
7967
$ArgumentList = @("pack")
8068
$ArgumentList += "`"$projectFile`""
8169
$ArgumentList += "/p:NuspecFile=`"$NuspecPath`""
8270
$ArgumentList += "--output `"$OutputPath`""
71+
}
8372

84-
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
85-
$processStartInfo.FileName = $dotnetCliPath
86-
$processStartInfo.RedirectStandardError = $true
87-
$processStartInfo.RedirectStandardOutput = $true
88-
$processStartInfo.UseShellExecute = $false
89-
$processStartInfo.Arguments = $ArgumentList
73+
# run the packing program
74+
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
75+
$processStartInfo.FileName = $ProcessName
76+
$processStartInfo.Arguments = $ArgumentList
77+
$processStartInfo.RedirectStandardError = $true
78+
$processStartInfo.RedirectStandardOutput = $true
79+
$processStartInfo.UseShellExecute = $false
80+
81+
Write-Verbose "Calling $ProcessName $($ArgumentList -join ' ')"
82+
$process = New-Object System.Diagnostics.Process
83+
$process.StartInfo = $processStartInfo
84+
85+
$process.Start() | Out-Null
86+
87+
# read output incrementally, it'll block if it writes too much
88+
$outputLines = @()
89+
Write-Verbose "$ProcessName output:"
90+
while (! $process.HasExited) {
91+
$output = $process.StandardOutput.ReadLine()
92+
Write-Verbose "`t$output"
93+
$outputLines += $output
94+
}
9095

91-
$process = New-Object System.Diagnostics.Process
92-
$process.StartInfo = $processStartInfo
93-
$process.Start() | Out-Null
94-
$process.WaitForExit()
96+
# get any remaining output
97+
$process.WaitForExit()
98+
$outputLines += $process.StandardOutput.ReadToEnd()
9599

96-
if (Test-Path -Path $tempPath) {
97-
Remove-Item -Path $tempPath -Force -Recurse
98-
}
100+
$stdOut = $outputLines -join "`n"
99101

100-
if (-Not ($process.ExitCode -eq 0 )) {
101-
$stdOut = $process.StandardOutput.ReadToEnd()
102-
throw "dotnet cli failed to pack $stdOut"
103-
}
102+
Write-Verbose "finished running $($processStartInfo.FileName) with exit code $($process.ExitCode)"
103+
104+
if (($tempPath -ne $null) -and (Test-Path -Path $tempPath)) {
105+
Remove-Item -Path $tempPath -Force -Recurse
106+
}
104107

108+
if (-Not ($process.ExitCode -eq 0 )) {
109+
# nuget writes errors to stdErr, dotnet writes them to stdOut
110+
if ($UseDotnetCli) {
111+
$errors = $stdOut
112+
}
113+
else {
114+
$errors = $process.StandardError.ReadToEnd()
115+
}
116+
throw "$ProcessName failed to pack: error $errors"
105117
}
106118

107-
$stdOut = $process.StandardOutput.ReadToEnd()
108119
$stdOut -match "Successfully created package '(.*.nupkg)'" | Out-Null
109120
$nupkgFullFile = $matches[1]
110121

111-
$stdOut = $process.StandardOutput.ReadToEnd()
112-
113-
Write-Verbose -Message $stdOut
122+
Write-Verbose "Created Nuget Package $nupkgFullFile"
114123
Write-Output $nupkgFullFile
115124
}

src/PowerShellGet/private/functions/New-NuspecFile.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ function New-NuspecFile {
4949
)
5050
Set-StrictMode -Off
5151

52+
Write-Verbose "Calling New-NuspecFile"
53+
5254
$nameSpaceUri = "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"
5355
[xml]$xml = New-Object System.Xml.XmlDocument
5456

src/PowerShellGet/private/functions/Publish-NugetPackage.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function Publish-NugetPackage {
1818
)
1919
Set-StrictMode -Off
2020

21+
Write-Verbose "Calling Publish-NugetPackage -NupkgPath $NupkgPath -Destination $Destination -NugetExePath $NugetExePath -UseDotnetCli:$UseDotnetCli"
2122
$Destination = $Destination.TrimEnd("\")
2223

2324
if ($PSCmdlet.ParameterSetName -eq "UseNuget") {

src/PowerShellGet/private/functions/Publish-PSArtifactUtility.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function Publish-PSArtifactUtility {
7474
$Exclude
7575
)
7676

77+
Write-Verbose "Calling Publish-PSArtifactUtility"
7778
Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -BootstrapNuGetExe
7879

7980
$PSArtifactType = $script:PSArtifactTypeModule

0 commit comments

Comments
 (0)