ps/Modules/Alkami.PowerShell.Choco/Public/Select-InstallPackages.ps1

54 lines
1.6 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Select-InstallPackages {
<#
.SYNOPSIS
Overwrites packages that share the same from the first list, with the second list.
Makes sure everything in the second list is in the result.
Used for overwriting the packages from the environment with packages intended to be installed.
Used during a full deploy
.PARAMETER Packages
The list of packages currently installed in an environment
.PARAMETER InstallPackages
The list of packages to be installed in an environment.
#>
[CmdletBinding()]
param (
[array]$Packages,
[array]$InstallPackages
)
# Strip null entries out of the packages.
$Packages = $Packages | Where-Object { $null -ne $_ }
$InstallPackages = $InstallPackages | Where-Object { $null -ne $_ }
# Just return the original list if the install packages are null/empty.
if (Test-IsCollectionNullOrEmpty $InstallPackages) {
return $Packages
}
# Load the install packages into a map for quick lookup.
$installMap = @{ }
foreach ($package in $InstallPackages) {
$installMap[$package.Name.ToLower()] = $package
}
# If the package name exists in $installPackages, overwrite the package from $packages.
$result = @()
foreach ($package in $Packages) {
$name = $package.Name.ToLower()
if ($installMap.ContainsKey($name)) {
$result += $installMap[$name]
$installMap.Remove($name)
} else {
$result += $package
}
}
# Anything left in $installMap is a new package. Add them in.
if ($installMap.Count -gt 0) {
$result += $installMap.Values
}
return $result
}