Refactor WinGet app existence check for multi-arch support

Improves the pre-download check to correctly identify existing multi-architecture applications by looking for both x86 and x64 subfolders.

This prevents unnecessary re-downloads of apps that have separate builds for each architecture. The logic is also consolidated to remove a redundant code block.
This commit is contained in:
rbalsleyMSFT
2025-07-21 17:48:59 -07:00
parent c9499d839c
commit bb76d2edcf
@@ -428,23 +428,52 @@ function Start-WingetAppDownloadTask {
try { try {
$wingetAppsJson = Get-Content -Path $wingetWin32jsonFile -Raw | ConvertFrom-Json $wingetAppsJson = Get-Content -Path $wingetWin32jsonFile -Raw | ConvertFrom-Json
# Check if app already exists in WinGetWin32Apps.json # Check if app already exists in WinGetWin32Apps.json
$existingWin32Entry = $wingetAppsJson | Where-Object { $_.Name -eq $appName } # For multi-arch apps, there might be entries like "AppName (x86)" and "AppName (x64)"
if ($existingWin32Entry) { $existingWin32Entries = @($wingetAppsJson | Where-Object {
$_.Name -eq $appName -or
$_.Name -eq "$appName (x86)" -or
$_.Name -eq "$appName (x64)"
})
if ($existingWin32Entries.Count -gt 0) {
$appFolder = Join-Path -Path "$AppsPath\Win32" -ChildPath $appName $appFolder = Join-Path -Path "$AppsPath\Win32" -ChildPath $appName
$appContentFound = $false
# Check if it's a multi-arch app with subfolders
if ($ApplicationItemData.Architecture -eq 'x86 x64') {
$x86Folder = Join-Path -Path $appFolder -ChildPath "x86"
$x64Folder = Join-Path -Path $appFolder -ChildPath "x64"
if ((Test-Path -Path $x86Folder -PathType Container) -and (Test-Path -Path $x64Folder -PathType Container)) {
$x86Size = (Get-ChildItem -Path $x86Folder -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
$x64Size = (Get-ChildItem -Path $x64Folder -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if ($x86Size -gt 1MB -and $x64Size -gt 1MB) {
$appContentFound = $true
}
}
}
else {
# Single architecture app
if (Test-Path -Path $appFolder -PathType Container) { if (Test-Path -Path $appFolder -PathType Container) {
$folderSize = (Get-ChildItem -Path $appFolder -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum $folderSize = (Get-ChildItem -Path $appFolder -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if ($folderSize -gt 1MB) { if ($folderSize -gt 1MB) {
$appContentFound = $true
}
}
}
if ($appContentFound) {
$appFound = $true $appFound = $true
$status = "Not Downloaded: App already in $wingetWin32jsonFile and found in $appFolder" $status = "Not Downloaded: App already in $wingetWin32jsonFile and found in $appFolder"
Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $appId -Status $status Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $appId -Status $status
WriteLog "Found '$appName' in WinGetWin32Apps.json and content exists in '$appFolder'. Skipping download to prevent duplicate entry." WriteLog "Found '$appName' in WinGetWin32Apps.json and content exists in '$appFolder'. Skipping download to prevent duplicate entry."
return [PSCustomObject]@{ Id = $appId; Status = $status; ResultCode = 0 } return [PSCustomObject]@{ Id = $appId; Status = $status; ResultCode = 0 }
} }
}
else { else {
# App entry exists in WinGetWin32Apps.json but folder is missing # App entry exists in WinGetWin32Apps.json but folder is missing or incomplete
$appFound = $true $appFound = $true
$status = "Error: App in '$wingetWin32jsonFile' but content folder '$appFolder' not found. Remove entry from WinGetWin32Apps.json or restore content." $status = "Error: App in '$wingetWin32jsonFile' but content folder '$appFolder' not found or incomplete. Remove entry from WinGetWin32Apps.json or restore content."
Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $appId -Status $status Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $appId -Status $status
WriteLog $status WriteLog $status
return [PSCustomObject]@{ Id = $appId; Status = $status; ResultCode = 1 } return [PSCustomObject]@{ Id = $appId; Status = $status; ResultCode = 1 }
@@ -456,34 +485,6 @@ function Start-WingetAppDownloadTask {
} }
} }
} }
# For now, assuming Get-Application uses $global variables set in the main script or $using: scope.
# $global:AppsPath = $AppsPath # Potentially redundant if set globally before parallel call
# $global:WindowsArch = $ApplicationItemData.Architecture # Potentially redundant
# $global:orchestrationPath = $OrchestrationPath # Potentially redundant
$wingetWin32jsonFile = Join-Path -Path $OrchestrationPath -ChildPath "WinGetWin32Apps.json"
if (Test-Path -Path $wingetWin32jsonFile) {
try {
$wingetAppsJson = Get-Content -Path $wingetWin32jsonFile -Raw | ConvertFrom-Json
$wingetApp = $wingetAppsJson | Where-Object { $_.Name -eq $appName }
if ($wingetApp) {
$appFolder = Join-Path -Path "$AppsPath\Win32" -ChildPath $appName
if (Test-Path -Path $appFolder -PathType Container) {
$folderSize = (Get-ChildItem -Path $appFolder -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if ($folderSize -gt 1MB) {
$appFound = $true
$status = "Not Downloaded: App in $wingetWin32jsonFile and found in $appFolder"
Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $appId -Status $status
WriteLog "Found '$appName' via WinGetWin32Apps.json and content exists in '$appFolder'."
return [PSCustomObject]@{ Id = $appId; Status = $status; ResultCode = 0 }
}
}
}
}
catch {
WriteLog "Warning: Could not read or parse '$wingetWin32jsonFile'. Error: $($_.Exception.Message)"
}
}
} }
# Check MSStore folder # Check MSStore folder