@@ -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}
0 commit comments