Refactor the logic for reordering the association config
This commit is contained in:
parent
1855ab7e5f
commit
a892256374
1 changed files with 9 additions and 6 deletions
|
@ -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($_); };
|
||||
|
||||
|
|
Loading…
Reference in a new issue