-
Notifications
You must be signed in to change notification settings - Fork 1
/
NuGetPackagePublishing.psm1
245 lines (215 loc) · 8.75 KB
/
NuGetPackagePublishing.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
function Submit-Package {
<#
.SYNOPSIS
Submits a NuGet package (or set of packages) to the gallery, but as hidden (unlisted).
.DESCRIPTION
Uploads the specified package (or all packages from a packages.config file) to the gallery and then immediately marks the package(s) as unlisted by running the nuget delete command.
.EXAMPLE
Submit-Package -packageId MyAwesomePackage -packageVersion 2.0.0.0 -packageFile MyAwesomePackage.2.0.0.nupkg -apiKey 00000000-0000-0000-0000-000000000000 -galleryUrl https://nuget.org
.EXAMPLE
Submit-Package -packagesConfig packages.config -apiKey 00000000-0000-0000-0000-000000000000 -galleryUrl https://nuget.org
.PARAMETER packageId
The Id of the package to hide/show
.PARAMETER packageVersion
The Version of the package to hide/show
.PARAMETER packageFile
The nupkg file to upload for the NuGet package
.PARAMETER packagesConfig
The XML config file that lists the packages to be hidden/shown
.PARAMETER galleryUrl
The NuGet gallery Url to connect to. By default, https://nuget.org
#>
param(
[Parameter(ParameterSetName="package")] $packageId,
[Parameter(ParameterSetName="package")] $packageVersion,
[Parameter(ParameterSetName="package")] $packageFile,
[Parameter(ParameterSetName="config")] $packagesConfig,
$apiKey,
$galleryUrl = "https://nuget.org"
)
If ($apiKey -eq $null) { throw "Parameter 'apiKey' was not specified" }
If ($galleryUrl -eq $null) { throw "Parameter 'galleryUrl' was not specified" }
If ($PSCmdlet.ParameterSetName -match "package") {
If ($packageId -eq $null) { throw "Parameter 'packageId' was not specified" }
If ($packageVersion -eq $null) { throw "Parameter 'packageVersion' was not specified" }
If ($packageFile -eq $null) { throw "Parameter 'packageFile' was not specified" }
$exists = Test-Path $packageFile
if ($exists -eq $false)
{
throw "File not found: $packageFile"
}
PushDelete -packageId $packageId -packageVersion $packageVersion -packageFile $packageFile -apiKey $apiKey -galleryUrl $galleryUrl
}
ElseIf ($PSCmdlet.ParameterSetName -match "config") {
If ($packagesConfig -eq $null) { throw "Parameter 'packagesConfig' was not specified" }
If (!(Test-Path $packagesConfig)) { throw "File '$packagesConfig' was not found" }
[xml]$packages = Get-Content $packagesConfig
foreach ($package in $packages.packages.package) {
$path = ".\" + $package.culture + "\" + $package.id + "." + $package.version + ".nupkg"
$path = $path.Replace("\\", "\")
$exists = Test-Path $path
if ($exists -eq $false)
{
throw "File not found: $path"
}
PushDelete -packageId $package.id -packageVersion $package.version -packageFile $path -apiKey $apiKey -galleryUrl $galleryUrl
}
}
}
function Set-NuGetPackageVisibility {
<#
.SYNOPSIS
Sets a package's visibility within the NuGet gallery
.DESCRIPTION
Hide (unlist) a package from the gallery or show (list) a package on the gallery.
.EXAMPLE
Set-PackageVisibility -action hide -packageId MyAwesomePackage -packageVersion 2.0.0.0 -apiKey 00000000-0000-0000-0000-000000000000 -galleryUrl https://nuget.org
.EXAMPLE
Set-PackageVisibility -action show -packageId MyAwesomePackage -packageVersion 2.0.0.0 -apiKey 00000000-0000-0000-0000-000000000000 -galleryUrl https://preview.nuget.org
.PARAMETER action
The action to take: hide or show
.PARAMETER packageId
The Id of the package to hide/show
.PARAMETER packageVersion
The Version of the package to hide/show
.PARAMETER packagesConfig
The XML config file that lists the packages to be hidden/shown
.PARAMETER galleryUrl
The NuGet gallery Url to connect to. By default, https://nuget.org
#>
[CmdletBinding(DefaultParameterSetName='package')]
param(
$action,
[Parameter(ParameterSetName="package")] $packageId,
[Parameter(ParameterSetName="package")] $packageVersion,
[Parameter(ParameterSetName="config")] $packagesConfig,
$apiKey,
$galleryUrl = "https://nuget.org"
)
If ($action -eq $null) { throw "Parameter 'action' was not specified" }
If ($apiKey -eq $null) { throw "Parameter 'apiKey' was not specified" }
If ($galleryUrl -eq $null) { throw "Parameter 'galleryUrl' was not specified" }
If ($PSCmdlet.ParameterSetName -match "package") {
If ($packageId -eq $null) { throw "Parameter 'packageId' was not specified" }
If ($packageVersion -eq $null) { throw "Parameter 'packageVersion' was not specified" }
SetVisibility -action $action -packageId $packageId -packageVersion $packageVersion -apiKey $apiKey -galleryUrl $galleryUrl
}
ElseIf ($PSCmdlet.ParameterSetName -match "config") {
If ($packagesConfig -eq $null) { throw "Parameter 'packagesConfig' was not specified" }
If (!(Test-Path $packagesConfig)) { throw "File '$packagesConfig' was not found" }
[xml]$packages = Get-Content $packagesConfig
foreach ($package in $packages.packages.package) {
SetVisibility -action $action -packageId $package.id -packageVersion $package.version -apiKey $apiKey -galleryUrl $galleryUrl
}
}
}
function Hide-NuGetPackage {
<#
.SYNOPSIS
Hides a package from the NuGet gallery
.DESCRIPTION
Marks the specified NuGet package as unlisted, hiding it from the gallery.
.EXAMPLE
Hide-NuGetPackage -packageId MyAwesomePackage -packageVersion 2.0.0.0 -apiKey 00000000-0000-0000-0000-000000000000 -galleryUrl https://preview.nuget.org
.PARAMETER packageId
The Id of the package to hide
.PARAMETER packageVersion
The Version of the package to hide
.PARAMETER packagesConfig
The XML config file that lists the packages to be hidden/shown
.PARAMETER galleryUrl
The NuGet gallery Url to connect to. By default, https://nuget.org
#>
param(
[Parameter(ParameterSetName="package")] $packageId,
[Parameter(ParameterSetName="package")] $packageVersion,
[Parameter(ParameterSetName="config")] $packagesConfig,
$apiKey,
$galleryUrl = "https://nuget.org"
)
If ($PSCmdlet.ParameterSetName -match "config") {
Set-NuGetPackageVisibility -action hide -packagesConfig $packagesConfig -apiKey $apiKey -galleryUrl $galleryUrl
}
Else {
Set-NuGetPackageVisibility -action hide -packageId $packageId -packageVersion $packageVersion -apiKey $apiKey -galleryUrl $galleryUrl
}
}
function Show-NuGetPackage {
<#
.SYNOPSIS
Shows a package on the NuGet gallery, listing an already-published but unlisted package.
.DESCRIPTION
Marks the specified NuGet package as listed, showing it on the gallery.
.EXAMPLE
Show-NuGetPackage -packageId MyAwesomePackage -packageVersion 2.0.0.0 -apiKey 00000000-0000-0000-0000-000000000000 -galleryUrl https://preview.nuget.org
.PARAMETER packageId
The Id of the package to show
.PARAMETER packageVersion
The Version of the package to show
.PARAMETER packagesConfig
The XML config file that lists the packages to be hidden/shown
.PARAMETER galleryUrl
The NuGet gallery Url to connect to. By default, https://nuget.org
#>
param(
[Parameter(ParameterSetName="package")] $packageId,
[Parameter(ParameterSetName="package")] $packageVersion,
[Parameter(ParameterSetName="config")] $packagesConfig,
$apiKey,
$galleryUrl = "https://nuget.org"
)
If ($PSCmdlet.ParameterSetName -match "config") {
Set-NuGetPackageVisibility -action show -packagesConfig $packagesConfig -apiKey $apiKey -galleryUrl $galleryUrl
}
Else {
Set-NuGetPackageVisibility -action show -packageId $packageId -packageVersion $packageVersion -apiKey $apiKey -galleryUrl $galleryUrl
}
}
function PushDelete {
param(
$packageId,
$packageVersion,
$packageFile,
$apiKey,
$galleryUrl
)
nuget push $packageFile -source $galleryUrl -apiKey $apiKey
nuget delete $packageId $packageVersion -source $galleryUrl -noninteractive -apiKey $apiKey
}
function SetVisibility {
param(
$action,
$packageId,
$packageVersion,
$apiKey,
$galleryUrl
)
If ($action -match "hide") {
$method = "DELETE"
$message = "hidden (unlisted)"
}
ElseIf ($action -match "show") {
$method = "POST"
$message = "shown (listed)"
}
Else {
throw "Invalid 'action' parameter value. Valid values are 'hide' and 'show'."
}
$url = "$galleryUrl/api/v2/Package/$packageId/$packageVersion"
$web = [System.Net.WebRequest]::Create($url)
$web.Method = $method
$web.Headers.Add("X-NuGet-ApiKey", "$apiKey")
$web.ContentLength = 0
Write-Host ""
Write-Host "Submitting the $method request to $url..." -foregroundColor Cyan
Write-Host ""
$response = $web.GetResponse()
If ($response.StatusCode -match "OK") {
Write-Host "Package '$packageId' Version '$packageVersion' has been $message." -foregroundColor Green -backgroundColor Black
Write-Host ""
}
Else {
Write-Host $response.StatusCode
}
}
Export-ModuleMember *-*