@@ -2,67 +2,67 @@ function New-NexusNugetComponent {
22 <#
33 . SYNOPSIS
44 Uploads a NuGet package to Nexus through the Components API
5-
5+
66 . DESCRIPTION
77 Uploads a NuGet package to Nexus through the Components API
8-
8+
99 . PARAMETER RepositoryName
10- The repository to upload too
11-
10+ The repository to upload to
11+
1212 . PARAMETER NuGetComponent
1313 The NuGet package to upload
1414
1515 . EXAMPLE
1616 New-NexusNugetComponent -RepositoryName ProdNuGet -NuGetComponent C:\temp\awesomepackage.0.1.0.nupkg
17-
18- . NOTES
19-
17+
18+ . EXAMPLE
19+ Get-ChildItem ~\Downloads\ -Filter *.nupkg | New-NexusNugetComponent -RepositoryName ChocolateyTest
2020 #>
2121 [CmdletBinding ()]
2222 Param (
2323 [Parameter (Mandatory )]
2424 [String ]
2525 $RepositoryName ,
2626
27- [Parameter (Mandatory )]
27+ [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName )]
28+ [Alias (" PSPath" )]
2829 [ValidateScript ( {
2930 Test-Path $_
3031 })]
3132 [String ]
3233 $NuGetComponent
3334 )
35+ begin {
36+ if (-not (' System.Net.Http.HttpClient' -as [type ])) {
37+ Add-Type - AssemblyName ' System.Net.Http'
38+ }
3439
35- process {
36-
37- $urislug = " /service/rest/v1/components?repository=$ ( $RepositoryName ) "
38- $UriBase = " $ ( $protocol ) ://$ ( $Hostname ) :$ ( $port ) $ ( $ContextPath ) "
39- $Uri = $UriBase + $UriSlug
40-
41- $boundary = [System.Guid ]::NewGuid().ToString();
42- $ContentType = ' application/octet-stream'
43- $FileStream = [System.IO.FileStream ]::new($NuGetComponent , [System.IO.FileMode ]::Open)
44- $FileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue ]::new(' form-data' )
45- $FileHeader.Name = ' nuget.asset'
46- $FileHeader.FileName = Split-Path - leaf $NuGetComponent
47- $FileContent = [System.Net.Http.StreamContent ]::new($FileStream )
48- $FileContent.Headers.ContentDisposition = $FileHeader
49- $FileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue ]::Parse($ContentType )
40+ $Uri = " $ ( $protocol ) ://$ ( $Hostname ) :$ ( $port ) $ ( $ContextPath ) /service/rest/v1/components?repository=$ ( $RepositoryName ) "
5041
51- $MultipartContent = [System.Net.Http.MultipartFormDataContent ]::new()
52- $MultipartContent.Add ($FileContent )
42+ $Client = [System.Net.Http.HttpClient ]::new()
43+ $Client.DefaultRequestHeaders.Authorization = $header.Authorization
44+ }
45+ process {
46+ try {
47+ $Content = [System.Net.Http.MultipartFormDataContent ]::new()
48+ $FileName = [System.IO.Path ]::GetFileName($NuGetComponent )
49+ $FileStream = [System.IO.File ]::OpenRead((Convert-Path $NuGetComponent ))
50+ $FileContent = [System.Net.Http.StreamContent ]::new($FileStream )
51+ $Content.Add ($FileContent , ' nuget.asset' , $FileName )
5352
54- $params = @ {
55- Uri = $Uri
56- Method = ' POST'
57- ContentType = " multipart/form-data; boundary=----WebKitFormBoundary$ ( $Boundary ) "
58- Body = $MultipartContent
59- Headers = $header
60- UseBasicParsing = $true
53+ $Result = $Client.PostAsync ($Uri , $Content ).Result
54+ if ($Result.EnsureSuccessStatusCode ()) {
55+ Write-Verbose " '$ ( $FileName ) ' was successfully uploaded to '$ ( $RepositoryName ) '"
56+ }
57+ } catch {
58+ Write-Error " '$ ( $FileName ) ' failed to upload to '$ ( $RepositoryName ) ': $ ( $Result.ReasonPhrase ) `n $_ "
59+ } finally {
60+ if ($Content ) {$Content.Dispose ()}
61+ if ($FileStream ) {$FileStream.Dispose ()}
62+ if ($FileContent ) {$FileContent.Dispose ()}
6163 }
62-
63- $null = Invoke-WebRequest @params
64-
6564 }
66-
67-
65+ end {
66+ if ($Client ) {$Client.Dispose ()}
67+ }
6868}
0 commit comments