-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNuGetMetadata.psm1
317 lines (296 loc) · 8.02 KB
/
NuGetMetadata.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#assemblies loaded in the manifest:
Add-Type -AssemblyName "System.IO.Compression"
function Get-NuGetMetadata {
[CmdletBinding(
DefaultParameterSetName = 'Path'
)]
Param(
[Parameter(ParameterSetName = 'Path')]
[string]$Path,
[Parameter(ParameterSetName = 'ConfigPath')]
[string]$ConfigPath
)
BEGIN {
$GCIparam = @{
Path = $Path
}
$sln = Get-ChildItem -Filter '*.sln' @GCIparam
if (!$sln) {
$csproj = Get-ChildItem -Filter '*.csproj' @GCIparam
}
}
PROCESS {
if (!$csproj -and $sln) {
$csproj = $sln | GetProjectFromSolution
}
$csproj |
GetPackageNameVersion |
GetNuGetPackageDirectory |
Get-NupkgMetadata
}
END {}
}
#::string -> XmlDocument
function Get-NupkgMetadata {
[CmdLetBinding()]
Param(
[Parameter(
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[ValidateNotNullOrEmpty()]
[string[]]$Path = ".",
[Parameter(Position = 1)]
[string[]]$FilePattern = '*.nupkg',
[Parameter(Position = 2)]
[string[]]$EntryPattern = '*.nuspec',
[switch]$NoRecurse
)
BEGIN {
$GCIParam = @{
'Recurse' = !$NoRecurse
'File' = $true
}
}
PROCESS {
foreach ($p in $Path) {
Write-Verbose "Get-NupkgMetadata path: $p"
if (Test-Path $p) {
Get-ChildItem @GCIParam -Path $p |
SelectMatchingFullName -Pattern $FilePattern |
Get-ZipFileEntry |
SelectMatchingFullName -Pattern $EntryPattern |
Get-ZipFileEntryContent |
GetNuGetPackageMetadata
}
else {
Write-Error -Message "Path '$p' not found"
}
}
}
END {}
}
function GetProjectFromSolution {
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('FullName')]
[string]$Path
)
BEGIN {
$projectPattern = '^Project\('
$filenameIndex = 2
}
PROCESS {
Write-Verbose "GetCsprojPath input: '$Path'"
$directory = Split-Path $Path
Get-Content $Path |
Select-String $projectPattern |
ForEach-Object {
$projectPath = ($_ -split '[=,]')[$filenameIndex].Trim(' "')
$output = Join-Path $directory $projectPath
Write-Verbose "GetCsprojPath project file: $output"
if ($output -match '\.csproj$') {
[PSCustomObject]@{
Path = $output
}
}
}
}
END {}
}
function GetPackageNameVersion {
<#
.SYNOPSIS
Receives a .csproj file.
Outputs name and version of the package references.
.PARAMETER Path
A .csproj file.
#>
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('FullName')]
[string]$Path
)
BEGIN {}
PROCESS {
Write-Verbose "GetPackageNameVersion input: $Path"
if (Test-Path $Path) {
$xml = [xml](Get-Content $Path)
Select-Xml -Xml $xml -XPath '//PackageReference' |
select -ExpandProperty Node |
ForEach-Object {
$output = [PSCustomObject]@{
Name = $_.Include
Version = $_.Version
}
Write-Verbose "GetPackageNameVersion output: $output"
$output
}
}
}
END {}
}
function GetNuGetPackageDirectory {
<#
.SYNOPSIS
Find NuGet packages when they aren't stored in the project directory.
Receives an object with Name and Version of a NuGet package.
Outputs the NuGet package directory.
.PARAMETER NameVersion
Parameter description
Name
#>
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[PSCustomObject]$NameVersion
)
BEGIN {
#todo: add logic here, for NuGet.config n stuff
$NuGetDefaultFolder = "$HOME\.NuGet\packages"
}
PROCESS {
Write-Verbose "GetNuGetPackageDirectory input: $Path"
$output = [PSCustomObject]@{
Path = "$NuGetDefaultFolder\$($NameVersion.Name)\$($NameVersion.Version)"
}
Write-Verbose "GetNuGetPackageDirectory output: $output"
$output
}
END {}
}
#::string -> System.IO.Compression.ZipArchiveEntry
function Get-ZipFileEntry {
[CmdletBinding()]
Param(
[Parameter(
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('FullName')]
[string[]]$FilePath
)
BEGIN {}
PROCESS {
foreach ($file in $FilePath) {
Write-Verbose "Get-ZipFileEntry file: $file"
try {
$fullName = (Resolve-Path -Path $file -ErrorAction Stop).Path
if (!(Test-Path -Path $fullName -PathType Leaf)) {
throw "'$fullName' is not a file"
}
$zipFile = [System.IO.Compression.ZipFile]::OpenRead($fullName)
$zipFile.Entries
}
catch [System.IO.InvalidDataException] {
Write-Error "'$fullName' is not a zip file"
}
catch {
WriteExceptionAsError $_
}
finally {
if ($zipFile) { $zipFile.Dispose() }
}
}
}
END {}
}
#::System.IO.Compression.ZipArchiveEntry -> string
function Get-ZipFileEntryContent {
[CmdletBinding()]
Param(
[Parameter(
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias('FullName')]
[System.IO.Compression.ZipArchiveEntry]$FileName
)
BEGIN {}
PROCESS {
foreach ($file in $FileName) {
Write-Verbose "Get-ZipFileEntryContent entry: $file"
try {
$deflateStream = $file.Open()
$streamReader = New-Object System.IO.StreamReader($deflateStream)
$fileContent = $streamReader.ReadToEnd()
$fileContent
}
catch {
WriteExceptionAsError $_
}
finally {
if ($deflateStream) { $deflateStream.Dispose() }
if ($streamReader) { $streamReader.Dispose() }
}
}
}
END {}
}
#::XmlDocument -> XmlDocument
function GetNuGetPackageMetadata {
Param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[string[]]$Xml,
[switch]$IncludeFullXml
)
BEGIN {}
PROCESS {
if ($IncludeFullXml) {
[xml]$Xml
}
else {
([xml]$Xml).package.metadata
}
}
END {}
}
function SelectMatchingFullName {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline = $true)]
$InputObject,
[string[]]$Pattern = '*'
)
BEGIN {}
PROCESS {
foreach ($obj in $InputObject) {
foreach ($pat in $Pattern) {
if ($obj.FullName -like $pat) {
$obj
break
}
}
}
}
END {}
}
function WriteExceptionAsError {
Param(
$Exception
)
if (-not ($ex = $Exception.Exception.InnerException)) {
$ex = $Exception.Exception
}
$exName = $ex.GetType().FullName
$exMsg = $ex.Message
Write-Error -Message "[$exName] : $exMsg"
}
#internal functions have no dashes
Export-ModuleMember *-*