Optimize installed app detection (#771)

This commit is contained in:
EvilGorilla13
2026-10-03 00:48:15 +02:00
committed by GitHub
parent 8b736ddd50
commit 872d6b0bd5
2 changed files with 13 additions and 3 deletions
+11 -2
View File
@@ -37,6 +37,16 @@ function Import-AppDetailsFromJson {
return $apps
}
$installedAppNames = @{}
if ($OnlyInstalled) {
# Get-AppxPackage is expensive; query it once and use its package names for the catalog lookup.
foreach ($installedApp in @(Get-AppxPackage -ErrorAction SilentlyContinue)) {
if ($installedApp.Name -is [string]) {
$installedAppNames[$installedApp.Name] = $true
}
}
}
foreach ($appData in $jsonContent.Apps) {
# Handle AppId as array (could be single or multiple IDs)
$appIdArray = @(
@@ -51,8 +61,7 @@ function Import-AppDetailsFromJson {
if ($OnlyInstalled) {
$isInstalled = $false
foreach ($appId in $appIdArray) {
# Check Get-AppxPackage first (fast, no process launch)
if (Get-AppxPackage -Name $appId) {
if ($installedAppNames.ContainsKey($appId)) {
$isInstalled = $true
break
}
+2 -1
View File
@@ -187,13 +187,14 @@ Describe 'Import-AppDetailsFromJson' {
}
It 'filters to installed apps using Appx and winget detection' {
Mock Get-AppxPackage { param($Name) if ($Name -eq 'Two.App') { [PSCustomObject]@{ Name = $Name } } }
Mock Get-AppxPackage { [PSCustomObject]@{ Name = 'Two.App' } }
Mock Test-AppInWingetList { $false }
$apps = @(Import-AppDetailsFromJson -OnlyInstalled -InstalledList @())
$apps | Should -HaveCount 1
$apps[0].AppId | Should -Be 'Two.App'
Should -Invoke Get-AppxPackage -Times 1 -Exactly
}
It 'loads presets and preserves their ID arrays' {