Refactor Install-WinGet and New-WinGetSettings for improved readability

This commit is contained in:
Zehadi Alam
2024-07-11 23:32:58 -04:00
parent 325413de13
commit a9afba9185
+51 -52
View File
@@ -1659,55 +1659,48 @@ function Get-Office {
} }
function Install-WinGet { function Install-WinGet {
param (
[bool]$InstallWithDependencies
)
$wingetPreviewLink = "https://aka.ms/getwingetpreview" $wingetPreviewLink = "https://aka.ms/getwingetpreview"
$wingetPackageDestination = "$env:TEMP\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" $wingetPackageDestination = "$env:TEMP\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
if ($InstallWithDependencies) { $dependencies = @(
$dependencies = @( @{
@{ Source = "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx"
Source = "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx" Destination = "$env:TEMP\Microsoft.VCLibs.x64.14.00.Desktop.appx"
Destination = "$env:TEMP\Microsoft.VCLibs.x64.14.00.Desktop.appx" },
}, @{
@{ Source = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx"
Source = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx" Destination = "$env:TEMP\Microsoft.UI.Xaml.2.8.x64.appx"
Destination = "$env:TEMP\Microsoft.UI.Xaml.2.8.x64.appx"
}
)
Start-BitsTransferWithRetry -Source $wingetPreviewLink -Destination $wingetPackageDestination
foreach ($dependency in $dependencies) {
Start-BitsTransferWithRetry -Source $dependency.Source -Destination $dependency.Destination
Add-AppxPackage -Path $dependency.Destination
Remove-Item -Path $dependency.Destination -Force -ErrorAction SilentlyContinue
} }
Add-AppxPackage -Path $wingetPackageDestination )
Remove-Item -Path $wingetPackageDestination -Force -ErrorAction SilentlyContinue foreach ($dependency in $dependencies) {
} $dependencyName = [System.IO.Path]::GetFileName($dependency.Source)
else { WriteLog "Downloading $dependencyName..."
# If WinGet was already installed, then installing the dependencies can cause an error if the system has a newer version of the dependencies than the ones downloaded. Start-BitsTransferWithRetry -Source $dependency.Source -Destination $dependency.Destination
WriteLog "Downloading WinGet..." WriteLog "Installing $dependencyName..."
Start-BitsTransferWithRetry -Source $wingetPreviewLink -Destination $wingetPackageDestination Add-AppxPackage -Path $dependency.Destination -ErrorAction SilentlyContinue
WriteLog "Installing WinGet..." WriteLog "Removing $dependencyName..."
Add-AppxPackage -Path $wingetPackageDestination Remove-Item -Path $dependency.Destination -Force -ErrorAction SilentlyContinue
WriteLog "Removing WinGet installer..."
Remove-Item -Path $wingetPackageDestination -Force -ErrorAction SilentlyContinue
} }
WriteLog "Downloading WinGet..."
Start-BitsTransferWithRetry -Source $wingetPreviewLink -Destination $wingetPackageDestination
WriteLog "Installing WinGet..."
Add-AppxPackage -Path $wingetPackageDestination -ErrorAction SilentlyContinue
WriteLog "Removing WinGet installer..."
Remove-Item -Path $wingetPackageDestination -Force -ErrorAction SilentlyContinue
} }
function Confirm-WinGetInstallation { function Confirm-WinGetInstallation {
$wingetPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" $wingetPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe"
if (-not (Test-Path $wingetPath)) { if (-not (Test-Path $wingetPath)) {
WriteLog "WinGet is not installed. Downloading preview version of WinGet and its dependencies..." WriteLog "WinGet is not installed. Downloading preview version of WinGet..."
Install-WinGet -InstallWithDependencies $true Install-WinGet
} }
elseif (-not (Get-Command winget -ErrorAction SilentlyContinue)) { elseif (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
WriteLog "WinGet is not on the path. Downloading preview version of WinGet without dependencies..." WriteLog "WinGet is not on the path. Downloading preview version of WinGet..."
Install-WinGet -InstallWithDependencies $false Install-WinGet
} }
elseif (-not ((& winget.exe --version) -like "*preview*")) { elseif (-not ((& winget.exe --version) -like "*preview*")) {
WriteLog "The preview version of WinGet is not installed. Downloading preview version of WinGet without dependencies..." WriteLog "The preview version of WinGet is not installed. Downloading preview version of WinGet..."
Install-WinGet -InstallWithDependencies $false Install-WinGet
} }
} }
@@ -1730,24 +1723,21 @@ function New-WinGetSettings {
if (Test-Path -Path $wingetSettingsFile -PathType Leaf) { if (Test-Path -Path $wingetSettingsFile -PathType Leaf) {
$jsonContent = Get-Content -Path $wingetSettingsFile -Raw $jsonContent = Get-Content -Path $wingetSettingsFile -Raw
# Check if storeDownload feature is already enabled # Check if storeDownload feature is already enabled
if ($jsonContent -notmatch '"storeDownload"\s*:\s*true') { WriteLog "Checking if storeDownload feature is enabled in WinGet configuration."
# Back up existing settings.json file if ($jsonContent -match '"storeDownload"\s*:\s*true') {
$backupWingetSettingsFile = $wingetSettingsFile + ".bak"
if (-not (Test-Path -Path $backupWingetSettingsFile -PathType Leaf)) {
WriteLog "Backing up existing WinGet settings.json file to $backupWingetSettingsFile"
Copy-Item -Path $wingetSettingsFile -Destination $backupWingetSettingsFile -Force | Out-Null
}
WriteLog "Creating WinGet settings.json file to allow the storeDownload feature. Writing file to $wingetSettingsFile"
$wingetSettingsContent | Out-File -FilePath $wingetSettingsFile -Encoding utf8 -Force
}
else {
WriteLog "WinGet's settings.json file is already configured to enable the storeDownload feature." WriteLog "WinGet's settings.json file is already configured to enable the storeDownload feature."
return
}
# Back up existing settings.json file
WriteLog "The storeDownload feature is not enabled in WinGet configuration."
$backupWingetSettingsFile = $wingetSettingsFile + ".bak"
if (-not (Test-Path -Path $backupWingetSettingsFile -PathType Leaf)) {
WriteLog "Backing up existing WinGet settings.json file to $backupWingetSettingsFile"
Copy-Item -Path $wingetSettingsFile -Destination $backupWingetSettingsFile -Force | Out-Null
} }
} }
else { WriteLog "Creating WinGet settings.json file to allow the storeDownload feature. Writing file to $wingetSettingsFile"
WriteLog "Creating WinGet settings.json file to allow the storeDownload feature. Writing file to $wingetSettingsFile" $wingetSettingsContent | Out-File -FilePath $wingetSettingsFile -Encoding utf8 -Force
$wingetSettingsContent | Out-File -FilePath $wingetSettingsFile -Encoding utf8 -Force
}
} }
function Add-Win32SilentInstallCommand { function Add-Win32SilentInstallCommand {
@@ -1812,10 +1802,19 @@ function Get-WinGetApp {
WriteLog "Downloading $WinGetAppName..." WriteLog "Downloading $WinGetAppName..."
$wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String
if ($wingetDownloadResult -match "No applicable installer found") { if ($wingetDownloadResult -match "No applicable installer found") {
WriteLog "No installer found for $WindowsArch architecture. Attempting to download without specifying architecture..."
$wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget | Out-String $wingetDownloadResult = & winget.exe download --id "$WinGetAppId" --exact --download-directory "$appFolderPath" --scope machine --source winget | Out-String
if ($wingetDownloadResult -match "Installer downloaded") {
WriteLog "Downloaded $WinGetAppName without specifying architecture."
}
else {
WriteLog "No installer found for $WinGetAppName. Skipping download."
Remove-Item -Path $appFolderPath -Recurse -Force
return $false
}
} }
if ($wingetDownloadResult -notmatch "Installer downloaded") { if ($wingetDownloadResult -notmatch "Installer downloaded") {
WriteLog "$WinGetAppName did not successfully download." WriteLog "No installer found for $WinGetAppName. Skipping download."
Remove-Item -Path $appFolderPath -Recurse -Force Remove-Item -Path $appFolderPath -Recurse -Force
return $false return $false
} }