Skip to content

Commit

Permalink
Use Repair-WinGetPackageManager as a fallback install method (#137066)
Browse files Browse the repository at this point in the history
* Use Repair-WinGetPackageManager instead of managing dependencies directly

* Remove download of the dependencies

* Add back some spaces

* Update experimental features

* * Fix error with variable parsing
* Fix ScriptAnalyzer warnings
* Add Try-Catch
* Update comment

* Missed a variable

* Use function for backup instead of as primary
  • Loading branch information
Trenly authored Feb 1, 2024
1 parent c388a64 commit 8cefcfa
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions Tools/SandboxTest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ $oldProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'

$latestRelease = Get-Release
$versionTag = $latestRelease.releaseTag
$desktopAppInstaller = @{
url = $latestRelease.msixFileUrl
hash = $latestRelease.shaFileContent
SaveTo = $(Join-Path $env:LOCALAPPDATA -ChildPath "Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\bin\$($latestRelease.releaseTag)\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")
SaveTo = $(Join-Path $env:LOCALAPPDATA -ChildPath "Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\bin\$versionTag\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle")
}

$ProgressPreference = $oldProgressPreference
Expand Down Expand Up @@ -165,11 +166,15 @@ foreach ($dependency in $dependencies) {
$WebClient.DownloadFile($dependency.url, $dependency.SaveTo)

} catch {
#Pass the exception as an inner exception
throw [System.Net.WebException]::new("Error downloading $($dependency.url).", $_.Exception)
# If the download failed, remove the item so the sandbox can fall-back to using the PowerShell module
Remove-Item $dependency.SaveTo -Force | Out-Null
}
if (-not ($dependency.hash -eq $(Get-FileHash $dependency.SaveTo).Hash)) {
throw [System.Activities.VersionMismatchException]::new('Dependency hash does not match the downloaded file')
# If the hash didn't match, remove the item so the sandbox can fall-back to using the PowerShell module
Write-Host -ForegroundColor Red ' Dependency hash does not match the downloaded file'
Write-Host -ForegroundColor Red ' Please open an issue referencing this error at https://bit.ly/WinGet-SandboxTest-Needs-Update'
Write-Host
Remove-Item $dependency.SaveTo -Force | Out-Null
}
}
}
Expand Down Expand Up @@ -226,7 +231,23 @@ Write-Host @'
--> Installing WinGet
'@
`$ProgressPreference = 'SilentlyContinue'
Add-AppxPackage -Path '$($desktopAppInstaller.pathInSandbox)' -DependencyPath '$($vcLibsUwp.pathInSandbox)','$($uiLibsUwp.pathInSandbox)'
try {
Add-AppxPackage -Path '$($desktopAppInstaller.pathInSandbox)' -DependencyPath '$($vcLibsUwp.pathInSandbox)','$($uiLibsUwp.pathInSandbox)'
} catch {
Write-Host -ForegroundColor Red 'Could not install from cached packages. Falling back to Repair-WinGetPackageManager cmdlet'
try {
Install-PackageProvider -Name NuGet -Force | Out-Null
Install-Module -Name Microsoft.WinGet.Client -Force -Repository PSGallery | Out-Null
} catch {
throw "Microsoft.Winget.Client was not installed successfully"
} finally {
# Check to be sure it acutally installed
if (-not(Get-Module -ListAvailable -Name Microsoft.Winget.Client)) {
throw "Microsoft.Winget.Client was not found. Check that the Windows Package Manager PowerShell module was installed correctly."
}
}
Repair-WinGetPackageManager -Version $versionTag
}
Write-Host @'
--> Disabling safety warning when running installer
Expand Down

0 comments on commit 8cefcfa

Please sign in to comment.