-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-package-reference.ps1
40 lines (30 loc) · 1.18 KB
/
sort-package-reference.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# [CmdletBinding()]
# param(
# [switch]$FromClipboard,
# [switch]$ToClipboard
# )
Set-StrictMode -Version 'Latest'
[xml]$XmlConfig = Get-ClipboardText
[System.XML.XMLDocument]$Doc = New-Object System.Xml.XmlDocument
[System.XML.XMLElement]$Root = $Doc.CreateElement('ItemGroup')
$Doc.appendChild($Root) | Out-Null
Select-Xml '/ItemGroup/PackageReference' $XmlConfig |
Select-Object -ExpandProperty Node |
Select-Object -Property Include,Version |
Sort-Object -Property Include |
ForEach-Object {
$Child = $Root.AppendChild($Doc.CreateElement('PackageReference'))
$IncludeAttr = $Doc.CreateAttribute("Include")
$IncludeAttr.Value = $_.Include
$Child.SetAttributeNode($IncludeAttr) | Out-Null
$VersionAttr = $Doc.CreateAttribute("Version")
$VersionAttr.Value = $_.Version
$Child.SetAttributeNode($VersionAttr) | Out-Null
}
$StringWriter = New-Object System.IO.StringWriter
$Settings = New-Object System.Xml.XmlWriterSettings
$Settings.OmitXmlDeclaration = $true
$Settings.Indent = $true
$Writer = [System.Xml.XmlWriter]::Create($StringWriter, $Settings);
$Doc.Save($Writer)
Set-ClipboardText $StringWriter.ToString()