54 lines
1.6 KiB
PowerShell
54 lines
1.6 KiB
PowerShell
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
|
|
} |