Refactor the logic for reordering the association config

This commit is contained in:
Manuel Thalmann 2023-07-26 18:12:18 +02:00
parent 1855ab7e5f
commit a892256374

View file

@ -1,6 +1,7 @@
$null = New-Module {
class AppAssociations {
static $associationSelector = "/DefaultAssociations/Association";
static $defaultAssociationsSelector = "/DefaultAssociations";
static $associationSelector = "./Association";
static [string] GetSelector([string] $identifier) {
return "$([AppAssociations]::associationSelector)[@Identifier='$identifier']";
@ -13,25 +14,27 @@ $null = New-Module {
}
static SetAssociation([string] $identifier, [string] $progId, [string] $applicationName) {
$associations = [AppAssociations]::GetAppAssociations();
$document = [AppAssociations]::GetAppAssociations();
$associations = $document.SelectSingleNode([AppAssociations]::defaultAssociationsSelector);
[System.Xml.XmlNode] $association = $null;
$candidates = $associations.SelectNodes([AppAssociations]::GetSelector($identifier));
if ($candidates.Count -eq 1) {
$association = $candidates[0];
} else {
$defaultAssociations = $associations.SelectSingleNode("/DefaultAssociations");
$association = ($associations.SelectNodes([AppAssociations]::associationSelector) | Select-Object -Last 1).CloneNode($true);
$association.Identifier = $identifier;
$association = $defaultAssociations.AppendChild($association);
$association = $associations.AppendChild($association);
}
$association.ProgId = $progId;
$association.ApplicationName = $applicationName;
[AppAssociations]::SaveAssociations($associations);
[AppAssociations]::SaveAssociations($document);
}
static SaveAssociations([xml] $associations) {
static SaveAssociations([xml] $document) {
$associations = $document.SelectSingleNode([AppAssociations]::defaultAssociationsSelector);
$defaultAssociations = $associations.SelectNodes([AppAssociations]::associationSelector);
$defaultAssociations | ForEach-Object { $associations.RemoveChild($_); } | Sort-Object -Property "Identifier" | ForEach-Object { $associations.AppendChild($_); };