|
| 1 | +function New-NugetPackage { |
| 2 | + [CmdletBinding()] |
| 3 | + Param( |
| 4 | + [Parameter(Mandatory = $true)] |
| 5 | + [string]$NuspecPath, |
| 6 | + |
| 7 | + [Parameter(Mandatory = $true)] |
| 8 | + [string]$NugetPackageRoot, |
| 9 | + |
| 10 | + [Parameter()] |
| 11 | + [string]$OutputPath = $NugetPackageRoot, |
| 12 | + |
| 13 | + [Parameter(ParameterSetName = "UseNuget")] |
| 14 | + [string]$NugetExePath, |
| 15 | + |
| 16 | + [Parameter(ParameterSetName = "UseDotnetCli")] |
| 17 | + [switch]$UseDotnetCli |
| 18 | + |
| 19 | + ) |
| 20 | + |
| 21 | + if (-Not(Test-Path -Path $NuspecPath -PathType Leaf)) { |
| 22 | + throw "A nuspec file does not exist at $NuspecPath, provide valid path to a .nuspec" |
| 23 | + } |
| 24 | + |
| 25 | + if (-Not(Test-Path -Path $NugetPackageRoot)) { |
| 26 | + throw "NugetPackageRoot $NugetPackageRoot does not exist" |
| 27 | + } |
| 28 | + |
| 29 | + if ($PSCmdlet.ParameterSetName -eq "UseNuget") { |
| 30 | + if (-Not(Test-Path -Path $NuGetExePath)) { |
| 31 | + throw "Nuget.exe does not exist at $NugetExePath, provide a valid path to nuget.exe" |
| 32 | + } |
| 33 | + |
| 34 | + $ArgumentList = @("pack") |
| 35 | + $ArgumentList += "`"$NuspecPath`"" |
| 36 | + $ArgumentList += "-outputdirectory `"$OutputPath`"" |
| 37 | + |
| 38 | + $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo |
| 39 | + $processStartInfo.FileName = $NugetExePath |
| 40 | + $processStartInfo.RedirectStandardError = $true |
| 41 | + $processStartInfo.RedirectStandardOutput = $true |
| 42 | + $processStartInfo.UseShellExecute = $false |
| 43 | + $processStartInfo.Arguments = $ArgumentList |
| 44 | + |
| 45 | + $process = New-Object System.Diagnostics.Process |
| 46 | + $process.StartInfo = $processStartInfo |
| 47 | + $process.Start() | Out-Null |
| 48 | + $process.WaitForExit() |
| 49 | + |
| 50 | + if (-Not ($process.ExitCode -eq 0 )) { |
| 51 | + $stdErr = $process.StandardError.ReadToEnd() |
| 52 | + throw "nuget.exe failed to pack $stdErr" |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if ($PSCmdlet.ParameterSetName -eq "UseDotnetCli") { |
| 57 | + #perform dotnet pack using a temporary project file. |
| 58 | + $dotnetCliPath = (Get-Command -Name "dotnet").Source |
| 59 | + $tempPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.Guid]::NewGuid()).Guid |
| 60 | + New-Item -ItemType Directory -Path $tempPath -Force | Out-Null |
| 61 | + |
| 62 | + $CsprojContent = @" |
| 63 | + <Project Sdk="Microsoft.NET.Sdk"> |
| 64 | + <PropertyGroup> |
| 65 | + <AssemblyName>NotUsed</AssemblyName> |
| 66 | + <Description>Temp project used for creating nupkg file.</Description> |
| 67 | + <TargetFramework>netcoreapp2.0</TargetFramework> |
| 68 | + <IsPackable>true</IsPackable> |
| 69 | + </PropertyGroup> |
| 70 | + </Project> |
| 71 | +"@ |
| 72 | + $projectFile = New-Item -ItemType File -Path $tempPath -Name "Temp.csproj" |
| 73 | + Set-Content -Value $CsprojContent -Path $projectFile |
| 74 | + |
| 75 | + #execution |
| 76 | + |
| 77 | + $ArgumentList = @("pack") |
| 78 | + $ArgumentList += "`"$projectFile`"" |
| 79 | + $ArgumentList += "/p:NuspecFile=`"$NuspecPath`"" |
| 80 | + $ArgumentList += "--output `"$OutputPath`"" |
| 81 | + |
| 82 | + $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo |
| 83 | + $processStartInfo.FileName = $dotnetCliPath |
| 84 | + $processStartInfo.RedirectStandardError = $true |
| 85 | + $processStartInfo.RedirectStandardOutput = $true |
| 86 | + $processStartInfo.UseShellExecute = $false |
| 87 | + $processStartInfo.Arguments = $ArgumentList |
| 88 | + |
| 89 | + $process = New-Object System.Diagnostics.Process |
| 90 | + $process.StartInfo = $processStartInfo |
| 91 | + $process.Start() | Out-Null |
| 92 | + $process.WaitForExit() |
| 93 | + |
| 94 | + if (-Not ($process.ExitCode -eq 0 )) { |
| 95 | + if (Test-Path -Path $tempPath) { |
| 96 | + Remove-Item -Path $tempPath -Force -Recurse |
| 97 | + } |
| 98 | + $stdOut = $process.StandardOut.ReadToEnd() |
| 99 | + throw "dotnet cli failed to pack $stdOut" |
| 100 | + } |
| 101 | + |
| 102 | + if (Test-Path -Path $tempPath) { |
| 103 | + Remove-Item -Path $tempPath -Force -Recurse |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + $stdOut = $process.StandardOutput.ReadLine() |
| 108 | + |
| 109 | + Write-Verbose -Message $stdOut |
| 110 | +} |
0 commit comments