-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWinGetHelpers.psm1
More file actions
1071 lines (994 loc) · 39.5 KB
/
Copy pathWinGetHelpers.psm1
File metadata and controls
1071 lines (994 loc) · 39.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
#Requires -Module Carbon
function Start-WinGetSandbox {
# The reason why this function so good is because I didn't write it, Felipe Santos (@felipecrs) did.
# I added a couple helper parameters for the rest of this module, which is why this is here.
# Parse arguments
Param(
[Parameter(Position = 0, HelpMessage = "The Manifest to install in the Sandbox.")]
[String] $Manifest,
[Parameter(Position = 1, HelpMessage = "The script to run in the Sandbox.")]
[ScriptBlock] $Script,
[Parameter(HelpMessage = "The folder to map in the Sandbox.")]
[String] $MapFolder = $pwd,
[Parameter(HelpMessage = "Automatically run manifest and send output to file.")]
[Switch] $auto,
[Parameter(HelpMessage = "Check to make sure these values match the ARP table values")]
[Hashtable] $metadata = $null
)
if ($null -ne $metadata) {
# Ugly hack to unravel the metadata. Fix this easton.
$displayVersion = $metadata.PackageVersion
$displayName = $metadata.PackageName
$publisher = $metadata.Publisher
}
$ErrorActionPreference = "Stop"
$mapFolder = (Resolve-Path -Path $MapFolder).Path
if (-Not (Test-Path -Path $mapFolder -PathType Container)) {
Write-Error -Category InvalidArgument -Message 'The provided MapFolder is not a folder.'
}
# Validate manifest file
if (-Not [String]::IsNullOrWhiteSpace($Manifest)) {
Write-Host '--> Validating Manifest'
if (-Not (Test-Path -Path $Manifest)) {
throw 'The Manifest file does not exist.'
}
$out = winget.exe validate $Manifest
if (-Not $?) {
throw 'Manifest validation failed.'
}
Write-Host
}
# Check if Windows Sandbox is enabled
if (-Not (Get-Command 'WindowsSandbox' -ErrorAction SilentlyContinue)) {
Write-Error -Category NotInstalled -Message @'
Windows Sandbox does not seem to be available. Check the following URL for prerequisites and further details:
https://docs.microsoft.com/en-us/windows/security/threat-protection/windows-sandbox/windows-sandbox-overview
You can run the following command in an elevated PowerShell for enabling Windows Sandbox:
$ Enable-WindowsOptionalFeature -Online -FeatureName 'Containers-DisposableClientVM'
'@
}
# Close Windows Sandbox
$sandbox = Get-Process 'WindowsSandboxClient' -ErrorAction SilentlyContinue
if ($sandbox) {
Write-Host '--> Closing Windows Sandbox'
$sandbox | Stop-Process
Start-Sleep -Seconds 5
Write-Host
}
Remove-Variable sandbox
# Set dependencies
$apiJson = (Invoke-WebRequest 'https://api.github.com/repos/microsoft/winget-cli/releases' -UseBasicParsing | ConvertFrom-Json)[0]
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$WebClient = New-Object System.Net.WebClient
function Get-LatestUrl {
(($apiJson).assets | Where-Object { $_.name -match '^Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle$' }).browser_download_url
}
function Get-LatestHash {
$shaUrl = (($apiJson).assets | Where-Object { $_.name -match '^Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt$' }).browser_download_url
$shaFile = Join-Path -Path $env:TEMP -ChildPath 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt'
$WebClient.DownloadFile($shaUrl, $shaFile)
Get-Content $shaFile
}
# Hide the progress bar of Invoke-WebRequest
# $oldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$desktopAppInstaller = @{
fileName = 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle'
url = $(Get-LatestUrl)
hash = $(Get-LatestHash)
}
$uiLibsUwp = @{
fileName = 'Microsoft.UI.Xaml.2.7.zip'
url = 'https://www.nuget.org/api/v2/package/Microsoft.UI.Xaml/2.7.0'
hash = "422FD24B231E87A842C4DAEABC6A335112E0D35B86FAC91F5CE7CF327E36A591"
}
$vcLibsUwp = @{
fileName = 'Microsoft.VCLibs.x64.14.00.Desktop.appx'
url = 'https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx'
hash = 'A39CEC0E70BE9E3E48801B871C034872F1D7E5E8EEBE986198C019CF2C271040'
}
$settingsFile = @{
fileName = 'settings.json'
url = 'https://gist.githubusercontent.com/jedieaston/28db9c14a50f18bc9731a14b2b1fd265/raw/c0370686c60b6cca8566a93112f7689a55d34d67/settings.json'
hash = 'BBDC9B3CA350576FD292AB7D7DF224B8D2B8E1DF387F4C776B57A9D1D0D1BED5'
}
$dependencies = @($desktopAppInstaller, $vcLibsUwp, $settingsFile, $uiLibsUwp)
# Initialize Temp Folder
$tempFolderName = 'SandboxTest'
$tempFolder = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath $tempFolderName
New-Item $tempFolder -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
Get-ChildItem $tempFolder -Recurse -Exclude $dependencies.fileName | Remove-Item -Force -Recurse
if (-Not [String]::IsNullOrWhiteSpace($Manifest)) {
Copy-Item -Path $Manifest -Destination $tempFolder -Recurse
}
# Download dependencies
Write-Host '--> Downloading dependencies'
$desktopInSandbox = 'C:\Users\WDAGUtilityAccount\Desktop'
$WebClient = New-Object System.Net.WebClient
foreach ($dependency in $dependencies) {
$dependency.file = Join-Path -Path $tempFolder -ChildPath $dependency.fileName
$dependency.pathInSandbox = Join-Path -Path $desktopInSandbox -ChildPath (Join-Path -Path $tempFolderName -ChildPath $dependency.fileName)
# Only download if the file does not exist, or its hash does not match.
if (-Not ((Test-Path -Path $dependency.file -PathType Leaf) -And $dependency.hash -eq $(get-filehash $dependency.file).Hash)) {
Write-Host @"
- Downloading:
$($dependency.url)
"@
try {
$WebClient.DownloadFile($dependency.url, $dependency.file)
}
catch {
throw "Error downloading $($dependency.url) ."
}
if (-not ($dependency.hash -eq $(get-filehash $dependency.file).Hash)) {
throw 'Hashes do not match, try gain. (Expected ' + $dependency.hash + ', got ' + $(get-filehash $dependency.file).Hash
}
}
}
Write-Host
# Create Bootstrap script
$bootstrapPs1Content = @'
function Update-Environment {
$locations = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
'HKCU:\Environment'
$locations | ForEach-Object {
$k = Get-Item $_
$k.GetValueNames() | ForEach-Object {
$name = $_
$value = $k.GetValue($_)
if ($userLocation -and $name -ieq 'PATH') {
$Env:Path += ";$value"
} else {
Set-Item -Path Env:$name -Value $value
}
}
$userLocation = $true
}
}
'@
$bootstrapPs1Content += @"
Write-Host @'
--> Installing WinGet
'@
Expand-Archive '$($uiLibsUwp.pathInSandbox)' ~\Desktop\xaml\
Add-AppxPackage ~\Desktop\xaml\tools\AppX\x64\Release\Microsoft.UI.Xaml.2.7.appx
Add-AppxPackage -Path '$($desktopAppInstaller.pathInSandbox)' -DependencyPath '$($vcLibsUwp.pathInSandbox)'
Copy-Item '$($settingsFile.pathInSandbox)' 'C:\Users\WDAGUtilityAccount\AppData\Local\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\settings.json'
Write-Host @'
Tip: you can type 'Update-Environment' to update your environment variables, such as after installing a new software.
Write-Host @'
--> Changing winget settings for testing (disabling msstore and enabling local manifest installation).
'@
winget settings --Enable LocalManifestFiles
winget source remove msstore
"@
if (-Not [String]::IsNullOrWhiteSpace($Manifest)) {
$manifestFileName = Split-Path $Manifest -Leaf
if (-Not (Test-Path -Path $Manifest -PathType leaf)) {
# It's a multi file manifest!!!
$manifestFileName += "\"
}
$manifestPathInSandbox = Join-Path -Path $desktopInSandbox -ChildPath (Join-Path -Path $tempFolderName -ChildPath ($manifestFileName))
Write-Host $manifestPathInSandbox
if ($auto) {
$bootstrapPs1Content += @"
Write-Host @'
--> Installing the Manifest $manifestFileName
'@
Write-Host '$manifestPathInSandbox';
winget install -m '$manifestPathInSandbox' | Out-File .\tmp.log
Write-Host @'
--> Refreshing environment variables
'@
Update-Environment;
Write-Host @'
--> Getting list of installed applications...
'@
winget list | Add-Content .\tmp.log;
"@
}
else {
$bootstrapPs1Content += @"
Write-Host @'
--> Installing the Manifest $manifestFileName
'@
Write-Host '$manifestPathInSandbox';
winget install -m '$manifestPathInSandbox'
Write-Host @'
--> Refreshing environment variables
'@
Update-Environment;
Write-Host @'
--> Getting list of installed applications...
'@
winget list;
"@
}
}
if (-Not [String]::IsNullOrWhiteSpace($Script)) {
$bootstrapPs1Content += @"
Write-Host @'
--> Running the following script:
{
$Script
}
'@
$Script
"@
}
if ($null -ne $metadata ) {
$bootstrapPs1Content += @"
Write-Host --> Checking the ARP table.
if (Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Where-Object DisplayVersion -eq '$displayVersion' | Where-Object DisplayName -eq '$displayName' | Where-Object Publisher -eq '$publisher') {
if(Test-Path .\tmp.log) {
'ARP check went great!' | Add-Content .\tmp.log ;
Write-Host HEY!
}
else {
Write-Host 'ARP check went great!' -ForegroundColor Green;
}
}
elseif (Get-ItemProperty HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Where-Object DisplayVersion -eq '$displayVersion' | Where-Object DisplayName -eq '$displayName' | Where-Object Publisher -eq '$publisher') {
if(Test-Path .\tmp.log) {
'ARP check went great!' | Add-Content .\tmp.log ;
Write-Host HEY!
}
else {
Write-Host 'ARP check went great!' -ForegroundColor Green;
}
}
elseif (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Where-Object DisplayVersion -eq '$displayVersion' | Where-Object DisplayName -eq '$displayName' | Where-Object Publisher -eq '$publisher') {
if(Test-Path .\tmp.log) {
'ARP check went great!' | Add-Content .\tmp.log ;
Write-Host HEY!
}
else {
Write-Host 'ARP check went great!' -ForegroundColor Green;
}
}
else {
if(Test-Path .\tmp.log) {
'ARP mismatch detected.' | Add-Content .\tmp.log ;
}
else {
Write-Host 'ARP mismatch detected.' -ForegroundColor Red;
}
}
;
"@
}
if ($auto) {
$bootstrapPs1Content += @"
New-Item .\done;
"@
}
$bootstrapPs1Content += @"
Write-Host
"@
$bootstrapPs1FileName = 'Bootstrap.ps1'
$bootstrapPs1Content | Out-File (Join-Path -Path $tempFolder -ChildPath $bootstrapPs1FileName)
# Create Wsb file
$bootstrapPs1InSandbox = Join-Path -Path $desktopInSandbox -ChildPath (Join-Path -Path $tempFolderName -ChildPath $bootstrapPs1FileName)
$mapFolderInSandbox = Join-Path -Path $desktopInSandbox -ChildPath (Split-Path -Path $mapFolder -Leaf)
$sandboxTestWsbContent = @"
<Configuration>
<vGPU>Enable</vGPU>
<MappedFolders>
<MappedFolder>
<HostFolder>$tempFolder</HostFolder>
<ReadOnly>true</ReadOnly>
</MappedFolder>
<MappedFolder>
<HostFolder>$mapFolder</HostFolder>
</MappedFolder>
</MappedFolders>
<LogonCommand>
<Command>PowerShell Start-Process PowerShell -WindowStyle Maximized -WorkingDirectory '$mapFolderInSandbox' -ArgumentList '-ExecutionPolicy Bypass -NoExit -NoLogo -File $bootstrapPs1InSandbox'</Command>
</LogonCommand>
</Configuration>
"@
$sandboxTestWsbFileName = 'SandboxTest.wsb'
$sandboxTestWsbFile = Join-Path -Path $tempFolder -ChildPath $sandboxTestWsbFileName
$sandboxTestWsbContent | Out-File $sandboxTestWsbFile
Write-Host @"
--> Starting Windows Sandbox, and:
- Mounting the following directories:
- $tempFolder as read-only
- $mapFolder as read-and-write
- Installing WinGet
"@
if (-Not [String]::IsNullOrWhiteSpace($Manifest)) {
Write-Host @"
- Installing the Manifest $manifestFileName
- Refreshing environment variables
- Getting a list of installed applications and their product codes
"@
}
if (-Not [String]::IsNullOrWhiteSpace($Script)) {
Write-Host @"
- Running the following script:
{
$Script
}
"@
}
Write-Host
WindowsSandbox $SandboxTestWsbFile
}
function Get-WinGetManifestType {
# Helper function. Given a folder, we see if it contains a multi-file or singleton manifest.
param (
[Parameter(Position = 0, Mandatory = $true, HelpMessage = "Manifest to check on.")]
[string]$manifestFolder
)
$ErrorActionPreference = "Stop"
if (Test-Path -Path $manifestFolder -PathType Leaf) {
throw "This isn't a folder, this is a file!"
}
foreach ($i in (Get-ChildItem -Path $manifestFolder -File)) {
$manifest = Get-Content ($manifestFolder + "\" + $i) | ConvertFrom-Yaml -Ordered
if (($manifest.ManifestType.ToLower() -eq "version") -or $manifest.ManifestType.ToLower() -eq "singleton") {
break
}
}
if ($manifest.ManifestType.ToLower() -eq "version") {
return "multifile"
}
elseif ($manifest.ManifestType.ToLower() -eq "singleton") {
return "singleton"
}
else {
throw "Unknown manifest type: " + $manifest.ManifestType
}
}
function Get-URLFileHash {
<#
.SYNOPSIS
Given a URL, this function returns the SHA256 hash of the file located at that address.
.DESCRIPTION
Given a URL, this function returns the SHA256 hash of the file located at that address.
Optionally, with the -Clipboard parameter, the hash will be written to the clipboard so that it can be pasted
into another application.
.INPUTS
Nothing can be piped (yet).
.OUTPUTS
A SHA256 hash of the file at the URL.
.EXAMPLE
Get-URLFileHash https://dl.google.com/tag/s/dl/chrome/install/googlechromestandaloneenterprise64.msi
Returns the SHA256 hash of the googlechromesandaloneenterprise64.msi.
#>
param (
# The URL to get the hash for.
[Parameter(mandatory = $true)]
[string]$url,
# Write the SHA256 hash to the clipboard.
[Parameter(HelpMessage = "Write to clipboard?")]
[switch]$clipboard
)
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
Invoke-WebRequest -UseBasicParsing -OutFile $env:TEMP\installer $url
$hash = Get-FileHash $env:TEMP\installer
Remove-Item $env:TEMP\installer
if ($clipboard) {
Set-Clipboard $hash.hash
Write-Host "The hash has been written to the clipboard."
}
return $hash.hash
}
function Get-GitHubReleases {
# Requires the GitHub CLI!
Param(
[Parameter(mandatory = $true, Position = 0, HelpMessage = "The repository to get the releases for, in the form user/repo.")]
[String] $repo
)
$env:GH_REPO = $repo; gh release list -L 5; Remove-Item env:\GH_REPO
}
function Test-WinGetManifest {
Param(
[Parameter(mandatory = $true, Position = 0, HelpMessage = "The Manifest to test.")]
[String] $manifest,
[Parameter(HelpMessage = "Keep the log file no matter what.")]
[Switch]$keepLog,
[Parameter(HelpMessage = "Don't stop the function after 30 minutes.")]
[Switch]$noStop,
[Parameter(HelpMessage = "Check these values against the ARP table.")]
[hashtable] $metadata = $null
)
$ErrorActionPreference = 'Stop'
Remove-Item ".\tmp.log" -ErrorAction "SilentlyContinue"
Remove-Item ".\done" -ErrorAction "SilentlyContinue"
$howManySeconds = 0
Start-WinGetSandbox $manifest -auto -metadata $metadata
Write-Host "Waiting for installation of" $manifest "to complete."
while ((Test-Path -PathType Leaf ".\done") -ne $true) {
# Write-Host "Waiting for file..."
Start-Sleep -s 1
if ($noStop -ne $true) {
$howManySeconds += 1
if ($howManySeconds -ge 1800) {
Write-Host "Script timed out after 30 minutes. The sandbox will continue, but I'll stop looking for the log file."
Write-Host "Next time, try the -noStop parameter or fix your manifest :D"
return $false
}
}
}
Remove-Item ".\done" -ErrorAction "SilentlyContinue"
$str = Select-String -Path ".\tmp.log" -Pattern "Successfully installed"
if (-Not [string]::IsNullOrEmpty($str)) {
if ($null -ne $metadata) {
$str = Select-String -Path ".\tmp.log" -Pattern "ARP mismatch detected."
if (-Not [string]::IsNullOrEmpty($str)) {
Write-Host "Uh-oh! You wanted me to look for ARP errors, and I found one." -ForegroundColor Red
Write-Host "The sandbox will stay open so you can investigate."
return $false
}
}
$sandbox = Get-Process 'WindowsSandboxClient' -ErrorAction SilentlyContinue
if ($sandbox) {
Write-Host '--> Closing Windows Sandbox'
$sandbox | Stop-Process
Start-Sleep -Seconds 3
Write-Host
Remove-Variable sandbox
}
if ($keepLog -ne $true) {
Remove-Item ".\tmp.log"
}
return $true
}
else {
$err = (Select-String -Path ".\tmp.log" -Pattern "Installer failed" | Select-Object Line).Line
Write-Host "Uh-oh! $err. The sandbox will stay open so you can investigate."
# Get-Content ".\tmp.log"
return $false
}
}
function Update-WinGetManifest {
# Now with support for 1.0 manifests! I hope.
param (
[Parameter(Mandatory = $true, Position = 0, HelpMessage = "Path to manifest to be updated.")]
[string] $oldManifestFolder,
[Parameter(Mandatory = $true, Position = 1, HelpMessage = "The new version number.")]
[string] $newVersion,
[Parameter(HelpMessage = "If this manifest only supports a single architecture, the link to the new installer.")]
[string] $newURL,
[Parameter(HelpMessage = "If this manifest supports multiple architectures, a hashtable of all of the URLs.")]
[hashtable] $urlMap = @{},
[Parameter(HelpMessage = "Get the Product Code for each installer.")]
[switch] $productCode,
[Parameter(HelpMessage = "Run the new manifest in a Windows Sandbox after it is created.")]
[switch] $test,
[Parameter(HelpMessage = "Run the new manifest in a Windows Sandbox and shut it down when installation completes successfully.")]
[switch] $silentTest,
[Parameter(HelpMessage = "Attempt to auto replace the version number in the Installer URLs.")]
[switch] $autoReplaceURL,
[Parameter(HelpMessage = "Run New-WinGetCommit if this function completes successfully.")]
[switch] $commit,
[Parameter(HelpMessage = "Check the applicable metadata values against the ARP table.")]
[switch] $metadataCheck,
[Parameter(HelpMessage = "The release date for this manifest (default: today")]
[string] $releaseDate = (Get-Date -f "yyyy-MM-dd"),
[Parameter(HelpMessage = "Remove the old manifest and replace it with this one.")]
[switch] $update
)
# Just checking to ensure Carbon is available.
Import-Module 'Carbon'
$ProgressPreference = "SilentlyContinue"
$ErrorActionPreference = "Stop"
# Get the manifest's content.
$type = Get-WinGetManifestType $oldManifestFolder
if (($urlMap.Count -gt 1) -and ($type -eq "singleton")) {
# If we were passed new architectures and were using a singleton manifest, we need a multifile one now.
$converted = $true
Write-Host "You're adding multiple architectures to a singleton manifest!" -Foreground Yellow
Write-Host "Creating a multifile version of $oldManifestFolder..." -ForegroundColor Yellow
$oldManifestFolder = Convert-WinGetSingletonToMultiFile $oldManifestFolder
$type = "multifile"
Write-Host "Conversion successful. Let's proceed." -ForegroundColor Yellow
}
else {
$converted = $false
}
$newManifest = @{}
$arpValues = $null
foreach ($i in (Get-ChildItem -Path $oldManifestFolder)) {
$content = (Get-Content ($oldManifestFolder + "\" + $i) -Encoding UTF8 | ConvertFrom-Yaml -Ordered)
if ($content.ManifestType -eq 'locale') {
$newManifest.add(($content.ManifestType + "-" + $content.PackageLocale), $content)
}
else {
$newManifest.add($content.ManifestType, $content)
}
}
# Now for the updating!
# Get the installers array and the PackageIdentifier
if ($type -eq "multifile") {
$installers = $newManifest.Installer.Installers
$packageIdentifier = $newManifest.Installer.PackageIdentifier
$oldVersion = $newManifest.Version.PackageVersion
if ($checkArp) {
$arpValues = @{}
$arpValues.PackageVersion = $newVersion
$arpValues.PackageName = $newManifest.defaultLocale.PackageName
$arpValues.Publisher = $newManifest.defaultLocale.Publisher
}
}
else {
$installers = $newManifest.singleton.Installers
$packageIdentifier = $newManifest.singleton.PackageIdentifier
$oldVersion = $newManifest.singleton.PackageVersion
if ($checkArp) {
$arpValues = @{}
$arpValues.PackageVersion = $newVersion
$arpValues.PackageName = $newManifest.singleton.PackageName
$arpValues.Publisher = $newManifest.singleton.Publisher
}
}
# Make sure all architectures are lowercase in new manifest
foreach ($i in $installers) {
$i.Architecture = $i.Architecture.ToLower()
}
# Make sure ProductCode is denormalized.
if ($type -eq "multifile") {
if ($newManifest.Installer.Contains("ProductCode")) {
foreach ($i in $installers) {
$i.ProductCode = $newManifest.Installer.ProductCode
}
$newManifest.Installer.Remove("ProductCode")
}
}
if ($type -eq "singleton") {
if ($newManifest.singleton.Contains("ProductCode")) {
foreach ($i in $installers) {
$i.ProductCode = $newManifest.singleton.ProductCode
}
$newManifest.singleton.Remove("ProductCode")
}
}
foreach ($i in $urlMap.Keys) {
# Add non existing architectures.
$inManifest = $false
foreach ($j in $installers) {
if ($i.ToLower() -eq $j.Architecture) {
$inManifest = $true
break
}
}
if (-Not $inManifest) {
# Copy everything but the arch from the existing architecture.
# Found at: https://www.powershellgallery.com/packages/PSTK/1.2.1/Content/Public%5CCopy-OrderedHashtable.ps1
$temp = New-Object -TypeName System.Collections.Specialized.OrderedDictionary
$MemoryStream = New-Object -TypeName System.IO.MemoryStream
$BinaryFormatter = New-Object -TypeName System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$BinaryFormatter.Serialize($MemoryStream, $installers[0])
$MemoryStream.Position = 0
$temp = $BinaryFormatter.Deserialize($MemoryStream)
$MemoryStream.Close()
$temp.Architecture = $i.ToLower()
$installers.add($temp)
}
}
if (($installers.Length -eq 1) -And (-Not [String]::IsNullOrEmpty($newURL))) {
$urlMap = @{$installers[0].Architecture = $newURL }
}
# elseif ($autoReplaceURL) {
# $urlMap = @{}
# foreach ($i in $installers) {
# $urlMap[$i.Architecture] = $i.InstallerUrl -Replace $oldVersion, $newVersion
# Write-Host "Auto-replace for arch" $i.Architecture "resulted in URL "$urlMap[$i.Architecture] -ForegroundColor Yellow
# }
# }
if ($urlMap.Count -ne $installers.Length -And (-Not $autoReplaceURL)) {
foreach ($i in $installers) {
if (-Not ($urlMap.ContainsKey($i.Architecture))) {
# The user didn't specify this URL.
Write-Host "What is the Installer URL for architecture "$i.Architecture"?"
$urlMap[$i.Architecture] = Read-Host $i.InstallerUrl
}
}
# If the user provided new architectures, we don't need to worry about it.
}
# Let's download the installers and get the needed info.
foreach ($i in $installers) {
if ($autoReplaceURL) {
$url = $i.InstallerUrl -Replace $oldVersion, $newVersion
Write-Host "Auto-replace for arch" $i.Architecture "resulted in URL " $url -ForegroundColor Yellow
}
else {
$url = $urlMap[$i.Architecture]
}
$i.InstallerUrl = $url
Write-Host 'Downloading installer for architecture'$i.Architecture'...' -ForegroundColor Yellow
Invoke-WebRequest -UseBasicParsing -UserAgent "winget/1.0" -OutFile $env:TEMP\installer $url
$i.InstallerSha256 = (Get-FileHash $env:TEMP\installer).Hash
# if ($type -eq "multifile") {
# $isMsi = ((($newManifest.Installer.InstallerType) -And ($newManifest.Installer.InstallerType -eq "msi")) -Or ($i.InstallerType.ToLower() -eq "msi") -Or ($i.InstallerType.ToLower -eq "burn"))
# }
# else {
# $isMsi = ((($newManifest.singleton.InstallerType) -And ($newManifest.singleton.InstallerType.ToLower() -eq "msi") -Or ($i.InstallerType.ToLower() -eq "msi") -Or ($i.InstallerType.ToLower -eq "burn")))
# }
# Get Product Code if necessary
if (($i.Contains("ProductCode") -Or ($productCode))) {
try {
$i.ProductCode = '{' + (((Get-CMsi $env:TEMP\installer).ProductCode).ToString()).ToUpper() + '}'
}
catch {
Write-Host -ForegroundColor Red "The file doesn't look to be an MSI but has a ProductCode. Please manually verify the ProductCode to make sure it is correct."
$i.ProductCode = $i.ProductCode -replace $oldVersion, $newVersion
}
}
if ($i.InstallerType -eq "msix")
{
$i.SignatureSha256 = (winget hash -m $env:TEMP\installer | ConvertFrom-Yaml).SignatureSha256
}
}
# put the new installers array in the right place.
if ($type -eq "multifile") {
$newManifest.Installer.Installers = $installers
}
else {
$newManifest.singleton.Installers = $installers
}
Write-Host "New installer downloads complete!" -ForegroundColor Green
# Begin writing the files. This'll be ugly.
# First, set the version numbers in every place they exist in a multifile manifest.
if ($type -eq "multifile") {
foreach ($i in $newManifest.Keys) {
$newManifest[$i]["PackageVersion"] = $newVersion
}
}
else {
$newManifest.singleton.PackageVersion = $newVersion
}
# Check if ReleaseNotes/ReleaseDate/ReleaseNotesURL is set.
if ($type -eq "multifile")
{
if ($newManifest.defaultLocale.Contains("ReleaseNotes") -or $newManifest.defaultLocale.Contains("ReleaseNotesUrl") -or $newManifest.installer.Contains("ReleaseDate"))
{
Write-Host -ForegroundColor Yellow "Warning! Something about the ReleaseNotes was set in the previous manifest. Please manually correct it."
}
if ($newManifest.installer.Contains("ReleaseDate"))
{
$newManifest.installer["ReleaseDate"] = (Get-Date ([datetime]::Parse($releaseDate)) -f "yyyy-MM-dd")
}
}
else
{
if ($newManifest.singleton.Contains("ReleaseNotes") -or $newManifest.singleton.Contains("ReleaseNotesUrl") -or $newManifest.singleton.Contains("ReleaseDate"))
{
Write-Host -ForegroundColor Yellow "Warning! Something about the ReleaseNotes was set in the previous manifest. Please manually correct it."
}
if ($newManifest.singleton.Contains("ReleaseDate"))
{
$newManifest.singleton["ReleaseDate"] = (Get-Date ([datetime]::Parse($releaseDate)) -f "yyyy-MM-dd")
}
}
# Now let's get these back to YAML.
$path = ".\" + $newVersion + "\"
New-Item -Type Directory $path -Force | Out-Null
foreach ($i in $newManifest.Keys) {
if (($newManifest[$i].ManifestType.ToLower() -eq "singleton") -or ($newManifest[$i].ManifestType.ToLower() -eq "version")) {
$fileName = $path + $packageIdentifier + ".yaml"
}
elseif ($newManifest[$i].ManifestType.ToLower() -eq "installer") {
$fileName = $path + $packageIdentifier + ".installer.yaml"
}
elseif (($newManifest[$i].ManifestType.ToLower() -eq "locale") -Or ($newManifest[$i].ManifestType.ToLower() -eq "defaultlocale")) {
$fileName = $path + $packageIdentifier + ".locale." + $newManifest[$i].PackageLocale + ".yaml"
}
else {
throw $newManifest[$i].ManifestType.ToLower() + " is an unknown type."
}
# Add schema info for IntelliSense
$fileContent = '# yaml-language-server: $schema=https://aka.ms/winget-manifest.' + $newManifest[$i].ManifestType.ToLower() + '.' + $newManifest[$i].ManifestVersion.ToLower() + '.schema.json' + "`r`n"
# And the manifest...
if (($newManifest[$i].ManifestType.ToLower() -ne "locale") -And ($newManifest[$i].ManifestType.ToLower() -ne "defaultlocale")) {
$fileContent += ($newManifest[$i] | ConvertTo-Yaml).replace("'", '"')
}
else {
$fileContent += $newManifest[$i] | ConvertTo-Yaml
}
[System.Environment]::CurrentDirectory = (Get-Location).Path
[System.IO.File]::WriteAllLines($fileName, $fileContent)
# $fileContent | Out-File -Encoding "utf8" -FilePath $fileName
Write-Host $fileName "written." -ForegroundColor Green
if (Get-Content $fileName | Select-String "Â") {
Write-Host ("UTF-8 Corruption detected in {0}" -f $fileName) -ForegroundColor Red
Write-Host ("Please resolve this manually before I continue.")
pause
}
}
# Get rid of the temporary manifest we created if we needed a conversion.
if ($converted) {
Remove-Item -Recurse $oldManifestFolder
}
# Validate this thing.
winget validate $path | Out-Null
if ($LASTEXITCODE -ne 0) {
throw "Manifest validation failed."
}
if ($test) {
if ($metadataCheck) {
Start-WinGetSandbox $path -metadata $arpValues
}
else {
Start-WinGetSandbox $path
}
return $true
}
elseif ($silentTest) {
if ($metadataCheck) {
$testSuccess = Test-WinGetManifest $path -metadata $arpValues
}
else {
$testSuccess = Test-WinGetManifest $path
}
if ($testSuccess) {
Write-Host "Manifest successfully installed in Windows Sandbox!"
if ($commit) {
if ($update)
{
New-WinGetCommit $path -update $oldManifestFolder
}
else {
New-WinGetCommit $path
}
}
return $true
}
else {
Write-Host "Manifest install failed/timed out. For more info check .\tmp.log."
return $false
}
}
else {
return $true
}
}
function Convert-WinGetSingletonToMultiFile {
# Given a folder that contains a WinGet singleton manifest, this function creates a
# bare minimum multifile manifest in a different folder.
param (
[Parameter(Position = 0, Mandatory = $true, HelpMessage = "The manifest you wish to convert.")]
[string]$oldManifestFolder,
[Parameter(HelpMessage = "Replace the old manifest with this new one.")]
[switch]$overwrite
)
$ErrorActionPreference = "Stop"
$versionSchema = Invoke-WebRequest "https://aka.ms/winget-manifest.version.1.0.0.schema.json" | ConvertFrom-Json
$localeSchema = Invoke-WebRequest "https://aka.ms/winget-manifest.defaultlocale.1.0.0.schema.json" | ConvertFrom-Json
$installerSchema = Invoke-WebRequest "https://aka.ms/winget-manifest.installer.1.0.0.schema.json" | ConvertFrom-Json
$requiredKeys = $versionSchema.required + $localeSchema.required + $installerSchema.required
$type = Get-WinGetManifestType $oldManifestFolder
if ($type -ne "singleton") {
throw "This folder does not contain a singleton manifest."
}
$currentManifest = (Get-ChildItem -Path $oldManifestFolder)[0] | Get-Content | ConvertFrom-Yaml -Ordered
$newManifest = @{}
# Add required metadata.
$currentManifest["PackageVersion"] = [string]$currentManifest["PackageVersion"]
$newManifest["version"] = [Ordered]@{
PackageIdentifier = $currentManifest["PackageIdentifier"];
PackageVersion = $currentManifest["PackageVersion"];
DefaultLocale = $currentManifest["PackageLocale"];
ManifestType = "version";
ManifestVersion = "1.0.0";
}
$newManifest["defaultLocale"] = [Ordered]@{
PackageIdentifier = $currentManifest["PackageIdentifier"];
PackageVersion = $currentManifest["PackageVersion"];
PackageLocale = $currentManifest["PackageLocale"];
Publisher = $currentManifest["Publisher"];
PackageName = $currentManifest["PackageName"];
License = $currentManifest["License"];
ShortDescription = $currentManifest["ShortDescription"];
ManifestType = "defaultLocale";
ManifestVersion = "1.0.0";
}
$newManifest["installer"] = [Ordered]@{
PackageIdentifier = $currentManifest["PackageIdentifier"];
PackageVersion = $currentManifest["PackageVersion"];
Installers = $currentManifest["Installers"];
ManifestType = "installer";
ManifestVersion = "1.0.0";
}
# Now, put any keys we missed in the right spots.
$extraKeys = [Ordered]@{}
$extraKeys["version"] = [Ordered]@{}
$extraKeys["defaultLocale"] = [Ordered]@{}
$extraKeys["installer"] = [Ordered]@{}
foreach ($i in $currentManifest.Keys) {
if (-Not ($requiredKeys -contains $i) ) {
if ($i -in $versionSchema.properties.PSObject.Properties.Name) {
$extraKeys["version"].add($i, $currentManifest[$i])
}
elseif ($i -in $localeSchema.properties.PSObject.Properties.Name) {
$extraKeys["defaultLocale"].add($i, $currentManifest[$i])
}
elseif ($i -in $installerSchema.properties.PSObject.Properties.Name) {
$extraKeys["installer"].add($i, $currentManifest[$i])
}
}
}
# Now, add the extra keys back to the new Manifest.
# Since you can't add multiple keys at a certain place in a ordered dict in PowerShell, I had to do this.
$count = 3
foreach ($i in $extraKeys["version"].Keys) {
$newManifest["version"].Insert($count, $i, $extraKeys["version"][$i])
$count++;
}
$count = 3
foreach ($i in $extraKeys["defaultLocale"].Keys) {
$newManifest["defaultLocale"].Insert($count, $i, $extraKeys["defaultLocale"][$i])
$count++;
}
$count = 2
foreach ($i in $extraKeys["installer"].Keys) {
$newManifest["installer"].Insert($count, $i, $extraKeys["installer"][$i])
$count++;
}
# Now we can write the files.
$path = ".\" + $currentManifest["PackageVersion"] + "-multiFile\"
New-Item -Type Directory $path -Force | Out-Null
# Copied (with modifications) from Update-WinGetManifest. I should break this out into a function...
foreach ($i in $newManifest.Keys) {
if ($newManifest[$i].ManifestType.ToLower() -eq "version") {
$fileName = $path + $currentManifest["PackageIdentifier"] + ".yaml"
}
elseif ($newManifest[$i].ManifestType.ToLower() -eq "installer") {
$fileName = $path + $currentManifest["PackageIdentifier"] + ".installer.yaml"
}
elseif ($newManifest[$i].ManifestType.ToLower() -eq "defaultlocale") {
$fileName = $path + $currentManifest["PackageIdentifier"] + ".locale." + $newManifest[$i].PackageLocale + ".yaml"
}
else {
throw $newManifest[$i].ManifestType.ToLower() + " is an unknown type."
}
# Add schema info for IntelliSense
$fileContent = '# yaml-language-server: $schema=https://aka.ms/winget-manifest.' + $newManifest[$i].ManifestType.ToLower() + '.1.0.0.schema.json' + "`r`n"
# And the manifest...
$fileContent += ($newManifest[$i] | ConvertTo-Yaml).replace("'", '"')
$fileContent | Out-File -Encoding "utf8" -FilePath $fileName
Write-Host $fileName "written." -ForegroundColor Green
}
winget validate $path | Out-Null
if ($LASTEXITCODE -ne 0) {
throw "Manifest validation failed. Check the source manifest to ensure it's good and try again."
}
if ($overwrite) {
Remove-Item -Recurse $oldManifestFolder
Move-Item $path $oldManifestFolder
$path = $oldManifestFolder
}
Write-Host "All done with the conversion. The new manifest can be found in "$path"." -ForegroundColor Green
Write-Host "Please check it before committing." -ForegroundColor Green
return $path
}
function New-WinGetCommit {
# Autocreates a new commit. Don't use this unless you're really lazy.
# Make sure that you have an upstream branch set too (to microsoft/winget-pkgs), or creating a new branch may fail.
param(
[Parameter(mandatory = $true, Position = 0, HelpMessage = "The manifest to commit.")]
[string] $manifest,
[Parameter(HelpMessage = "Use the currently checked out branch, instead of making a new one.")]
[switch] $currentBranch,
[Parameter(HelpMessage = "The commit message, if you don't want the automatically generated one.")]
[string] $customMessage,
[Parameter(HelpMessage = "Update this manifest with the new one (delete the old one).")]
[string] $update,
[Parameter(HelpMessage = "Remove this manifest.")]
[switch] $remove
)
$ErrorActionPreference = "Stop"
if (Test-Path -Path $manifest -PathType Leaf) {
throw "This isn't a folder, this is a file!"
}
# $content = Get-Content $manifest | ConvertFrom-Yaml
foreach ($i in (Get-ChildItem -Path $manifest)) {
$theSplitName = $i.Name.Split(".")
if ($theSplitName.length -ge 3) {
$content = Get-Content ($manifest + "\" + $i) | ConvertFrom-Yaml -Ordered
if ($content.ManifestType.ToLower() -eq "version") {
$content = Get-Content ($manifest + "\" + $content.PackageIdentifier + ".locale." + $content.DefaultLocale + ".yaml") | ConvertFrom-Yaml -Ordered
break
}
elseif ($content.ManifestType.ToLower() -eq "singleton") {
break
}
}
}
if ([string]::IsNullOrEmpty($customMessage) -or [string]::IsNullOrWhiteSpace($customMessage)) {
if($remove)
{
$commitMessage = "Removed non-working manifest for " + $content.PackageName + " version " + $content.PackageVersion + "."
}
elseif ([string]::IsNullOrEmpty($update)) {
$commitMessage = "Added " + $content.PackageName + " version " + $content.PackageVersion + "."
}
else {
$commitMessage = "Updated " + $content.PackageName + " to version " + $content.PackageVersion + "."
}
}
else {
$commitMessage = $customMessage
}
if (-Not $currentBranch) {
$branchName = $content.PackageIdentifier + "-" + $content.PackageVersion
if ($remove)
{
$branchName += "-remove"
}
git fetch --all | Out-Null
git checkout -b "$branchName" upstream/master | Out-Null
if ($LASTEXITCODE -ne 0) {
# The branch already exists.
git checkout "$branchName"
}