@@ -19,6 +19,8 @@ function New-NugetPackage {
19
19
)
20
20
Set-StrictMode - Off
21
21
22
+ Write-Verbose " Calling New-NugetPackage"
23
+
22
24
if (-Not (Test-Path - Path $NuspecPath - PathType Leaf)) {
23
25
throw " A nuspec file does not exist at $NuspecPath , provide valid path to a .nuspec"
24
26
}
@@ -27,89 +29,96 @@ function New-NugetPackage {
27
29
throw " NugetPackageRoot $NugetPackageRoot does not exist"
28
30
}
29
31
32
+ $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
30
33
31
34
if ($PSCmdlet.ParameterSetName -eq " UseNuget" ) {
32
35
if (-Not (Test-Path - Path $NuGetExePath )) {
33
36
throw " Nuget.exe does not exist at $NugetExePath , provide a valid path to nuget.exe"
34
37
}
38
+ $ProcessName = $NugetExePath
35
39
36
40
$ArgumentList = @ (" pack" )
37
41
$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
56
45
}
46
+ else {
47
+ # use Dotnet CLI
57
48
58
- if ($PSCmdlet.ParameterSetName -eq " UseDotnetCli" ) {
59
49
# perform dotnet pack using a temporary project file.
60
- $dotnetCliPath = (Get-Command - Name " dotnet" ).Source
50
+ $ProcessName = (Get-Command - Name " dotnet" ).Source
61
51
$tempPath = Join-Path - Path ([System.IO.Path ]::GetTempPath()) - ChildPath ([System.Guid ]::NewGuid()).Guid
62
52
New-Item - ItemType Directory - Path $tempPath - Force | Out-Null
63
53
64
54
$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>
73
63
"@
74
64
$projectFile = New-Item - ItemType File - Path $tempPath - Name " Temp.csproj"
75
65
Set-Content - Value $CsprojContent - Path $projectFile
76
66
77
- # execution
78
-
79
67
$ArgumentList = @ (" pack" )
80
68
$ArgumentList += " `" $projectFile `" "
81
69
$ArgumentList += " /p:NuspecFile=`" $NuspecPath `" "
82
70
$ArgumentList += " --output `" $OutputPath `" "
71
+ }
83
72
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
+ }
90
95
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 ()
95
99
96
- if (Test-Path - Path $tempPath ) {
97
- Remove-Item - Path $tempPath - Force - Recurse
98
- }
100
+ $stdOut = $outputLines -join " `n "
99
101
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
+ }
104
107
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 "
105
117
}
106
118
107
- $stdOut = $process.StandardOutput.ReadToEnd ()
108
119
$stdOut -match " Successfully created package '(.*.nupkg)'" | Out-Null
109
120
$nupkgFullFile = $matches [1 ]
110
121
111
- $stdOut = $process.StandardOutput.ReadToEnd ()
112
-
113
- Write-Verbose - Message $stdOut
122
+ Write-Verbose " Created Nuget Package $nupkgFullFile "
114
123
Write-Output $nupkgFullFile
115
124
}
0 commit comments