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