-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow-PlatformPackageManager.ps1
More file actions
1812 lines (1548 loc) · 65.5 KB
/
Copy pathShow-PlatformPackageManager.ps1
File metadata and controls
1812 lines (1548 loc) · 65.5 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function Show-PlatformPackageManager
{
<#
.SYNOPSIS
Opens a unified console UI for native platform package management.
.DESCRIPTION
Provides one interactive entry point for the platform package management commands
backed by winget on Windows, Homebrew on macOS, and apt or apk on Linux.
The manager delegates to the existing package functions so their object output and
automation behavior remain available:
- Show-InstalledPlatformPackage for installed package browsing and export.
- Find-PlatformPackage for remote registry search.
- Install-PlatformPackage for search-driven installs.
- Upgrade-PlatformPackage for package upgrades.
- Remove-PlatformPackage for package removal.
- Get-PlatformPackageDependency for dependency inspection.
.PARAMETER PackageManager
Package manager to use. Auto detects the current platform package manager.
.PARAMETER Top
Maximum number of search results to retrieve for search-driven package actions.
.PARAMETER SkipRefresh
Skips registry refresh when launching the upgrade workflow.
.PARAMETER UninstallPrevious
Passes winget --uninstall-previous when launching the upgrade workflow.
.PARAMETER Purge
Requests package-manager-specific purge or zap behavior when launching removal.
.PARAMETER NoSudo
On Linux package managers that normally require elevated privileges, do not
automatically prefix install, upgrade, or removal commands with sudo.
.PARAMETER FilterSource
Sets the initial source filter for delegated interactive pickers that support
source selection. Press S inside those pickers to cycle available sources.
.PARAMETER WhatIf
Shows what install, upgrade, or removal commands would run without invoking the
platform package manager.
.PARAMETER Confirm
Prompts before delegated install, upgrade, or removal commands are invoked.
.EXAMPLE
PS > Show-PlatformPackageManager
Opens the unified package management menu.
.EXAMPLE
PS > Show-PlatformPackageManager -PackageManager brew
Opens the unified package management menu using Homebrew.
.EXAMPLE
PS > Show-PlatformPackageManager -SkipRefresh -NoSudo
Opens the menu and forwards SkipRefresh and NoSudo to workflows that support them.
.OUTPUTS
None. Results emitted by underlying package workflows are rendered inside the
manager UI as formatted tables.
.NOTES
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/SystemAdministration/Show-PlatformPackageManager.ps1
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/SystemAdministration/Show-PlatformPackageManager.ps1
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
param(
[Parameter()]
[ValidateSet('Auto', 'winget', 'brew', 'apt', 'apk')]
[String]$PackageManager = 'Auto',
[Parameter()]
[ValidateRange(0, 500)]
[Int32]$Top = 50,
[Parameter()]
[Switch]$SkipRefresh,
[Parameter()]
[Switch]$UninstallPrevious,
[Parameter()]
[Switch]$Purge,
[Parameter()]
[Switch]$NoSudo,
[Parameter()]
[String]$FilterSource = '',
[Parameter(DontShow = $true)]
[ScriptBlock]$CommandRunner,
[Parameter(DontShow = $true)]
[ScriptBlock]$KeyReader,
[Parameter(DontShow = $true)]
[ScriptBlock]$PromptReader,
[Parameter(DontShow = $true)]
[ValidateRange(0, 500)]
[Int32]$PickerPageSize = 0
)
begin
{
function Get-PlatformPackageManagerDependencyPath
{
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$FunctionName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$FileName
)
if (Get-Command -Name $FunctionName -ErrorAction SilentlyContinue)
{
Write-Verbose "$FunctionName is already loaded"
return $null
}
$dependencyPath = Join-Path -Path $PSScriptRoot -ChildPath $FileName
$dependencyPath = [System.IO.Path]::GetFullPath($dependencyPath)
if (-not (Test-Path -Path $dependencyPath -PathType Leaf))
{
throw "Required function '$FunctionName' could not be found. Expected location: $dependencyPath"
}
return $dependencyPath
}
function Invoke-PlatformPackageManagerFunction
{
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$FunctionName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$FileName,
[Parameter(Mandatory)]
[ScriptBlock]$Invocation,
[Parameter()]
[Hashtable]$Parameters = @{}
)
$dependencyPath = Get-PlatformPackageManagerDependencyPath -FunctionName $FunctionName -FileName $FileName
if (-not [String]::IsNullOrWhiteSpace($dependencyPath))
{
try
{
. $dependencyPath
Write-Verbose "Loaded $FunctionName from: $dependencyPath"
}
catch
{
throw "Failed to load required dependency '$FunctionName' from '$dependencyPath': $($_.Exception.Message)"
}
}
& $Invocation $Parameters
}
function Read-PlatformPackageManagerInput
{
param(
[Parameter(Mandatory)]
[String]$Prompt
)
if ($PromptReader)
{
$value = & $PromptReader -Prompt $Prompt
if ($null -eq $value)
{
return $null
}
return "$value"
}
try
{
if ([Console]::IsInputRedirected)
{
throw 'Console input is redirected.'
}
}
catch
{
throw 'Interactive package management requires an attached console.'
}
return Read-PlatformPackageManagerLineInput -Prompt $Prompt
}
function Read-PlatformPackageManagerLineInput
{
param(
[Parameter(Mandatory)]
[String]$Prompt
)
function Write-PlatformPackageManagerPromptText
{
param(
[Parameter(Mandatory)]
[String]$Text
)
$pattern = '\([^\)]*\? for help[^\)]*\)|\[[^\]]*\? for help[^\]]*\]'
$promptMatches = [regex]::Matches($Text, $pattern)
if ($promptMatches.Count -eq 0)
{
[Console]::Write($Text)
return
}
$cursor = 0
foreach ($match in $promptMatches)
{
if ($match.Index -gt $cursor)
{
[Console]::Write($Text.Substring($cursor, $match.Index - $cursor))
}
Write-Host $match.Value -NoNewline -ForegroundColor DarkGray
$cursor = $match.Index + $match.Length
}
if ($cursor -lt $Text.Length)
{
[Console]::Write($Text.Substring($cursor))
}
}
Write-PlatformPackageManagerPromptText -Text $Prompt
[Console]::Write(': ')
$buffer = [System.Text.StringBuilder]::new()
while ($true)
{
$key = [Console]::ReadKey($true)
if ($key.Key -eq [ConsoleKey]::Enter)
{
[Console]::WriteLine()
return $buffer.ToString()
}
if ($key.Key -eq [ConsoleKey]::Escape)
{
[Console]::WriteLine()
return $null
}
if ($key.Key -eq [ConsoleKey]::Backspace)
{
if ($buffer.Length -gt 0)
{
$buffer.Length = $buffer.Length - 1
[Console]::Write("`b `b")
}
continue
}
if ($key.KeyChar -ge [char]32)
{
$buffer.Append($key.KeyChar) | Out-Null
[Console]::Write($key.KeyChar)
}
}
}
function Read-PlatformPackageManagerKey
{
if ($KeyReader)
{
return (& $KeyReader)
}
try
{
if ([Console]::IsInputRedirected)
{
throw 'Console input is redirected.'
}
}
catch
{
throw 'Interactive package management requires an attached console.'
}
$previousTreatControlCAsInput = [Console]::TreatControlCAsInput
[Console]::TreatControlCAsInput = $true
try
{
return [Console]::ReadKey($true)
}
finally
{
[Console]::TreatControlCAsInput = $previousTreatControlCAsInput
}
}
function Test-PlatformPackageManagerCancelKey
{
param(
[Parameter(Mandatory)]
[ConsoleKeyInfo]$KeyInfo
)
$isControlC = $KeyInfo.Key -eq [ConsoleKey]::C -and (($KeyInfo.Modifiers -band [ConsoleModifiers]::Control) -eq [ConsoleModifiers]::Control)
return $KeyInfo.Key -in @([ConsoleKey]::Escape, [ConsoleKey]::Q) -or $isControlC
}
function Test-PlatformPackageManagerExportCancelRequested
{
try
{
if ([Console]::IsInputRedirected)
{
return $false
}
while ([Console]::KeyAvailable)
{
$cancelKey = [Console]::ReadKey($true)
if (Test-PlatformPackageManagerCancelKey -KeyInfo $cancelKey)
{
return $true
}
}
}
catch
{
Write-Verbose "Unable to inspect pending export cancel keys: $($_.Exception.Message)"
}
return $false
}
function Test-PlatformPackageManagerHelpKey
{
param(
[Parameter(Mandatory)]
[ConsoleKeyInfo]$KeyInfo
)
return $KeyInfo.KeyChar -eq '?'
}
function Show-PlatformPackageManagerHelp
{
param(
[Parameter()]
[ValidateSet('Menu', 'Result', 'SearchQuery', 'ExportPath', 'ExportFormat', 'ExportDependencyMode', 'DependencyPackage', 'DependencyDirection', 'YesNo')]
[String]$Topic = 'Menu'
)
Clear-Host
$subtitle = switch ($Topic)
{
'Result' { 'Result screen shortcuts' }
'SearchQuery' { 'Search prompt help' }
'ExportPath' { 'Export path prompt help' }
'ExportFormat' { 'Export format help' }
'ExportDependencyMode' { 'Export dependency help' }
'DependencyPackage' { 'Dependency package prompt help' }
'DependencyDirection' { 'Dependency direction help' }
'YesNo' { 'Confirmation prompt help' }
default { 'Keyboard shortcuts' }
}
Write-PlatformPackageManagerHeader -Title 'Platform Package Manager Help' -Subtitle $subtitle
function Get-PlatformPackageManagerHelpItem
{
param(
[Parameter(Mandatory)]
[String]$Shortcut,
[Parameter(Mandatory)]
[String]$Description
)
[PSCustomObject]@{
Shortcut = $Shortcut
Description = $Description
}
}
function Write-PlatformPackageManagerHelpItem
{
param(
[Parameter(Mandatory)]
[PSCustomObject]$Item
)
Write-Host ' - ' -NoNewline -ForegroundColor White
Write-Host "$($Item.Shortcut): " -NoNewline -ForegroundColor White
Write-Host $Item.Description -ForegroundColor DarkGray
}
$helpItems = switch ($Topic)
{
'Result'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut 'Any key or Enter' -Description 'return to the manager menu'
Get-PlatformPackageManagerHelpItem -Shortcut 'Q, Esc, or Ctrl+C' -Description 'quit the manager'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
'SearchQuery'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut 'Text' -Description 'enter a package name, package id, or registry search term'
Get-PlatformPackageManagerHelpItem -Shortcut 'Blank' -Description 'cancel the search workflow'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
'ExportPath'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut 'Text' -Description 'enter a .json or .csv export path'
Get-PlatformPackageManagerHelpItem -Shortcut 'Blank' -Description 'cancel the export workflow'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
'ExportFormat'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut '1 or JSON' -Description 'write JSON records'
Get-PlatformPackageManagerHelpItem -Shortcut '2 or CSV' -Description 'write CSV records'
Get-PlatformPackageManagerHelpItem -Shortcut 'Blank' -Description 'cancel the export workflow'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
'ExportDependencyMode'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut '1 or None' -Description 'export package records only'
Get-PlatformPackageManagerHelpItem -Shortcut '2 or DependsOn' -Description 'include direct dependencies'
Get-PlatformPackageManagerHelpItem -Shortcut '3 or Both' -Description 'include direct and required-by relationships'
Get-PlatformPackageManagerHelpItem -Shortcut 'Blank' -Description 'export package records only'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
'DependencyPackage'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut 'Text' -Description 'enter one or more package names or ids'
Get-PlatformPackageManagerHelpItem -Shortcut 'Comma' -Description 'separate multiple packages in one lookup'
Get-PlatformPackageManagerHelpItem -Shortcut 'Blank' -Description 'cancel the dependency workflow'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
'DependencyDirection'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut '1 or DependsOn' -Description 'show packages required by the requested package'
Get-PlatformPackageManagerHelpItem -Shortcut '2 or RequiredBy' -Description 'show packages that depend on the requested package'
Get-PlatformPackageManagerHelpItem -Shortcut '3 or Both' -Description 'show both relationship directions'
Get-PlatformPackageManagerHelpItem -Shortcut 'Blank' -Description 'use DependsOn'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
'YesNo'
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut 'Y or Yes' -Description 'accept the prompt'
Get-PlatformPackageManagerHelpItem -Shortcut 'N or No' -Description 'decline the prompt'
Get-PlatformPackageManagerHelpItem -Shortcut 'Blank' -Description 'use the displayed default'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
default
{
@(
Get-PlatformPackageManagerHelpItem -Shortcut 'Up/Down' -Description 'choose an action'
Get-PlatformPackageManagerHelpItem -Shortcut 'Home/End' -Description 'move to the first or last action'
Get-PlatformPackageManagerHelpItem -Shortcut 'Enter' -Description 'run the selected action'
Get-PlatformPackageManagerHelpItem -Shortcut '1-6' -Description 'jump to a numbered workflow'
Get-PlatformPackageManagerHelpItem -Shortcut 'B' -Description 'browse installed packages'
Get-PlatformPackageManagerHelpItem -Shortcut 'E' -Description 'export installed packages'
Get-PlatformPackageManagerHelpItem -Shortcut 'S or I' -Description 'search and install packages'
Get-PlatformPackageManagerHelpItem -Shortcut 'U' -Description 'upgrade packages'
Get-PlatformPackageManagerHelpItem -Shortcut 'R' -Description 'remove packages'
Get-PlatformPackageManagerHelpItem -Shortcut 'D' -Description 'inspect dependencies'
Get-PlatformPackageManagerHelpItem -Shortcut 'Q, Esc, or Ctrl+C' -Description 'quit'
Get-PlatformPackageManagerHelpItem -Shortcut '?' -Description 'show this help'
)
}
}
foreach ($item in $helpItems)
{
Write-PlatformPackageManagerHelpItem -Item $item
}
Write-Host ''
if ($KeyReader -or -not $PromptReader)
{
Write-Host 'Press any key to return to the menu. Q/Esc/Ctrl+C quits.' -ForegroundColor DarkGray
$null = Read-PlatformPackageManagerKey
}
else
{
$null = Read-PlatformPackageManagerInput -Prompt 'Press Enter to return'
}
}
function ConvertFrom-PlatformPackageManagerListInput
{
param(
[Parameter()]
[String]$Value
)
if ([String]::IsNullOrWhiteSpace($Value))
{
return @()
}
return @(
$Value -split ',' |
ForEach-Object { "$_".Trim() } |
Where-Object { -not [String]::IsNullOrWhiteSpace($_) }
)
}
function Read-PlatformPackageManagerList
{
param(
[Parameter(Mandatory)]
[String]$Prompt,
[Parameter()]
[ValidateSet('DependencyPackage')]
[String]$HelpTopic = 'DependencyPackage'
)
while ($true)
{
$value = Read-PlatformPackageManagerInput -Prompt "$Prompt (? for help)"
if ($null -eq $value)
{
return @()
}
if ($value.Trim() -eq '?')
{
Show-PlatformPackageManagerHelp -Topic $HelpTopic
continue
}
return @(ConvertFrom-PlatformPackageManagerListInput -Value $value)
}
}
function Get-PlatformPackageExportFormatFromPath
{
param(
[Parameter(Mandatory)]
[String]$Path
)
$extension = [System.IO.Path]::GetExtension($Path)
if ([String]::IsNullOrWhiteSpace($extension))
{
return 'Auto'
}
switch ($extension.ToLowerInvariant())
{
'.json' { return 'Json' }
'.csv' { return 'Csv' }
default { return 'Auto' }
}
}
function Read-PlatformPackageExportPath
{
while ($true)
{
$value = Read-PlatformPackageManagerInput -Prompt 'Export path (.json or .csv, ? for help)'
if ($null -eq $value -or [String]::IsNullOrWhiteSpace($value))
{
return $null
}
$value = $value.Trim()
if ($value -eq '?')
{
Show-PlatformPackageManagerHelp -Topic ExportPath
continue
}
return $value
}
}
function Read-PlatformPackageExportFormat
{
param(
[Parameter(Mandatory)]
[String]$Path
)
while ($true)
{
Write-Host 'Export format:' -ForegroundColor White
Write-Host ' 1. JSON' -ForegroundColor White
Write-Host ' 2. CSV' -ForegroundColor White
Write-Host ' ?. Help' -ForegroundColor DarkGray
$value = Read-PlatformPackageManagerInput -Prompt "Select format for $Path [1, ? for help]"
if ($null -eq $value)
{
return $null
}
$value = $value.Trim()
if ([String]::IsNullOrWhiteSpace($value))
{
return $null
}
switch ($value.ToLowerInvariant())
{
{ $_ -in @('1', 'j', 'json') } { return 'Json' }
{ $_ -in @('2', 'c', 'csv') } { return 'Csv' }
'?' { Show-PlatformPackageManagerHelp -Topic ExportFormat }
default { Write-Host 'Choose 1 or 2.' -ForegroundColor DarkGray }
}
}
}
function Read-PlatformPackageExportDependencyMode
{
while ($true)
{
Write-Host 'Dependency export:' -ForegroundColor White
Write-Host ' 1. Packages only' -ForegroundColor White
Write-Host ' 2. Direct dependencies' -ForegroundColor White
Write-Host ' 3. Direct + required-by relationships' -ForegroundColor White
Write-Host ' ?. Help' -ForegroundColor DarkGray
$value = Read-PlatformPackageManagerInput -Prompt 'Select dependency mode [1, ? for help]'
if ($null -eq $value)
{
return $null
}
$value = $value.Trim()
if ([String]::IsNullOrWhiteSpace($value))
{
return 'None'
}
switch ($value.ToLowerInvariant())
{
{ $_ -in @('1', 'n', 'no', 'none') } { return 'None' }
{ $_ -in @('2', 'd', 'dependson', 'depends on', 'dependencies') } { return 'DependsOn' }
{ $_ -in @('3', 'b', 'both', 'all') } { return 'Both' }
'?' { Show-PlatformPackageManagerHelp -Topic ExportDependencyMode }
default { Write-Host 'Choose 1, 2, or 3.' -ForegroundColor DarkGray }
}
}
}
function Read-PlatformPackageManagerYesNo
{
param(
[Parameter(Mandatory)]
[String]$Prompt,
[Parameter()]
[Switch]$DefaultYes
)
$suffix = if ($DefaultYes) { 'Y/n' } else { 'y/N' }
while ($true)
{
$value = Read-PlatformPackageManagerInput -Prompt "$Prompt [$suffix, ? for help]"
if ($null -eq $value)
{
return $null
}
$value = $value.Trim()
if ([String]::IsNullOrWhiteSpace($value))
{
return $DefaultYes.IsPresent
}
switch ($value.ToLowerInvariant())
{
{ $_ -in @('y', 'yes') } { return $true }
{ $_ -in @('n', 'no') } { return $false }
'?' { Show-PlatformPackageManagerHelp -Topic YesNo }
default { Write-Host 'Enter y or n.' -ForegroundColor DarkGray }
}
}
}
function Read-PlatformPackageDependencyDirection
{
while ($true)
{
Write-Host 'Dependency direction:' -ForegroundColor White
Write-Host ' 1. Depends on' -ForegroundColor White
Write-Host ' 2. Required by' -ForegroundColor White
Write-Host ' 3. Both' -ForegroundColor White
Write-Host ' ?. Help' -ForegroundColor DarkGray
$value = Read-PlatformPackageManagerInput -Prompt 'Select direction [1, ? for help]'
if ($null -eq $value)
{
return $null
}
$value = $value.Trim()
if ([String]::IsNullOrWhiteSpace($value))
{
return 'DependsOn'
}
switch ($value.ToLowerInvariant())
{
{ $_ -in @('1', 'depends', 'dependson', 'depends on') } { return 'DependsOn' }
{ $_ -in @('2', 'requiredby', 'required by', 'uses') } { return 'RequiredBy' }
{ $_ -in @('3', 'both', 'all') } { return 'Both' }
'?' { Show-PlatformPackageManagerHelp -Topic DependencyDirection }
default { Write-Host 'Choose 1, 2, or 3.' -ForegroundColor DarkGray }
}
}
}
function Get-PlatformPackageManagerCommonParameters
{
$parameters = @{
PackageManager = $PackageManager
}
if ($CommandRunner)
{
$parameters.CommandRunner = $CommandRunner
}
return $parameters
}
function Add-PlatformPackageManagerPickerParameters
{
param(
[Parameter(Mandatory)]
[Hashtable]$Parameters
)
if ($KeyReader)
{
$Parameters.KeyReader = $KeyReader
}
$Parameters.ReturnToPlatformPackageManagerOnBackKey = $true
if ($PickerPageSize -gt 0)
{
$Parameters.PickerPageSize = $PickerPageSize
}
if (-not [String]::IsNullOrWhiteSpace($FilterSource))
{
$Parameters.FilterSource = $FilterSource
}
}
function Get-PlatformPackageManagerStatusText
{
$flags = @()
if ($NoSudo)
{
$flags += 'NoSudo'
}
if ($SkipRefresh)
{
$flags += 'SkipRefresh'
}
if ($UninstallPrevious)
{
$flags += 'UninstallPrevious'
}
if ($Purge)
{
$flags += 'Purge'
}
if (-not [String]::IsNullOrWhiteSpace($FilterSource))
{
$flags += "FilterSource=$FilterSource"
}
$managerText = if ($PackageManager -eq 'Auto')
{
"Auto -> $(Get-PlatformPackageManagerDetectedName)"
}
else
{
$PackageManager
}
$flagText = if ($flags.Count -gt 0) { $flags -join ', ' } else { 'none' }
$dot = [char]0x00B7
return "Manager: $managerText $dot Top: $Top $dot Flags: $flagText"
}
function Test-PlatformPackageManagerCommandAvailable
{
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$Name
)
if ($CommandRunner)
{
return $PackageManager -ne 'Auto' -and $PackageManager -eq $Name
}
return $null -ne (Get-Command -Name $Name -CommandType Application -ErrorAction SilentlyContinue |
Select-Object -First 1)
}
function Get-PlatformPackageManagerDetectedName
{
if ($PackageManager -ne 'Auto')
{
return $PackageManager
}
$isWindowsPlatform = if ($PSVersionTable.PSVersion.Major -lt 6) { $true } else { [Bool]$IsWindows }
$isMacOSPlatform = if ($PSVersionTable.PSVersion.Major -lt 6) { $false } else { [Bool]$IsMacOS }
$isLinuxPlatform = if ($PSVersionTable.PSVersion.Major -lt 6) { $false } else { [Bool]$IsLinux }
if ($isWindowsPlatform -and (Test-PlatformPackageManagerCommandAvailable -Name 'winget'))
{
return 'winget'
}
if ($isMacOSPlatform -and (Test-PlatformPackageManagerCommandAvailable -Name 'brew'))
{
return 'brew'
}
if ($isLinuxPlatform)
{
if (Test-PlatformPackageManagerCommandAvailable -Name 'apt')
{
return 'apt'
}
if (Test-PlatformPackageManagerCommandAvailable -Name 'apk')
{
return 'apk'
}
}
foreach ($fallbackManager in @('brew', 'winget', 'apt', 'apk'))
{
if (Test-PlatformPackageManagerCommandAvailable -Name $fallbackManager)
{
return $fallbackManager
}
}
return 'unresolved'
}
function Write-PlatformPackageManagerHeader
{
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String]$Title,
[Parameter()]
[String]$Subtitle
)
$ruleWidth = 78
try
{
$w = [Console]::BufferWidth
if ($w -gt 0)
{
$ruleWidth = [Math]::Max(40, $w - 1)
}
}
catch
{
Write-Verbose "Unable to determine the console buffer width; using $ruleWidth characters. $($_.Exception.Message)"
}
$rule = '=' * $ruleWidth
Write-Host $rule -ForegroundColor DarkGray
Write-Host $Title -ForegroundColor Cyan
if (-not [String]::IsNullOrWhiteSpace($Subtitle))
{
Write-Host $Subtitle -ForegroundColor White
}
Write-Host (Get-PlatformPackageManagerStatusText) -ForegroundColor DarkGray
Write-Host $rule -ForegroundColor DarkGray
Write-Host ''
}
function Format-PlatformPackageManagerResultTable
{
param(
[Parameter()]
[Object[]]$InputObject = @()
)
$records = @($InputObject | Where-Object { $null -ne $_ })
if ($records.Count -eq 0)
{
return ''
}
$displayRecords = @(
foreach ($record in $records)
{
$excludeProperties = @('Results', 'CapturedOutput', 'InformationalOutput', 'InformationalResults')
$record | Select-Object -Property * -ExcludeProperty $excludeProperties
}
)
return ($displayRecords | Format-Table -AutoSize | Out-String -Width 4096).TrimEnd()
}
function Get-PlatformPackageManagerNestedResults
{
param(
[Parameter()]
[Object[]]$InputObject = @()
)
@(
foreach ($record in @($InputObject | Where-Object { $null -ne $_ }))
{
if ($record.PSObject.Properties['Results'] -and $null -ne $record.Results)
{
@($record.Results | Where-Object { $null -ne $_ })
}
}
)
}
function Get-PlatformPackageManagerInformationalResults
{
param(
[Parameter()]
[Object[]]$Records = @()
)
@(
foreach ($record in @($Records | Where-Object { $null -ne $_ }))
{
if ($record.PSObject.Properties['InformationalResults'] -and $null -ne $record.InformationalResults)
{
@($record.InformationalResults | Where-Object { $null -ne $_ })
}
}
)
}