2
2
// Licensed under the MIT License.
3
3
4
4
using Microsoft . PowerShell . PSResourceGet . UtilClasses ;
5
+ using NuGet . Versioning ;
5
6
using System ;
6
7
using System . Collections . Generic ;
8
+ using System . Linq ;
7
9
using System . Xml ;
8
10
9
11
namespace Microsoft . PowerShell . PSResourceGet . Cmdlets
@@ -70,6 +72,9 @@ public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResu
70
72
#region V2 Specific Methods
71
73
72
74
public XmlNode [ ] ConvertResponseToXML ( string httpResponse ) {
75
+ NuGetVersion emptyVersion = new NuGetVersion ( "0.0.0.0" ) ;
76
+ NuGetVersion firstVersion = emptyVersion ;
77
+ NuGetVersion lastVersion = emptyVersion ;
73
78
74
79
//Create the XmlDocument.
75
80
XmlDocument doc = new XmlDocument ( ) ;
@@ -80,7 +85,48 @@ public XmlNode[] ConvertResponseToXML(string httpResponse) {
80
85
XmlNode [ ] nodes = new XmlNode [ entryNode . Count ] ;
81
86
for ( int i = 0 ; i < entryNode . Count ; i ++ )
82
87
{
83
- nodes [ i ] = entryNode [ i ] ;
88
+ XmlNode node = entryNode [ i ] ;
89
+ nodes [ i ] = node ;
90
+ var entryChildNodes = node . ChildNodes ;
91
+ foreach ( XmlElement childNode in entryChildNodes )
92
+ {
93
+ var entryKey = childNode . LocalName ;
94
+ if ( entryKey . Equals ( "properties" ) )
95
+ {
96
+ var propertyChildNodes = childNode . ChildNodes ;
97
+ foreach ( XmlElement propertyChild in propertyChildNodes )
98
+ {
99
+ var propertyKey = propertyChild . LocalName ;
100
+ var propertyValue = propertyChild . InnerText ;
101
+ if ( propertyKey . Equals ( "NormalizedVersion" ) )
102
+ {
103
+ if ( ! NuGetVersion . TryParse ( propertyValue , out NuGetVersion parsedNormalizedVersion ) )
104
+ {
105
+ parsedNormalizedVersion = emptyVersion ;
106
+ }
107
+
108
+ if ( i == 0 )
109
+ {
110
+ firstVersion = parsedNormalizedVersion ;
111
+ }
112
+ else
113
+ {
114
+ // later version element
115
+ lastVersion = parsedNormalizedVersion ;
116
+ }
117
+
118
+ break ; // don't care about rest of the childNode's properties
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+
125
+ // only order the array in desc order if array has more than 1 element and is currently in ascending order
126
+ // check for emptyVersion is in case a version that couldn't be parsed was found, just keep ordering as is.
127
+ if ( nodes . Length > 1 && firstVersion != emptyVersion && lastVersion != emptyVersion && firstVersion < lastVersion )
128
+ {
129
+ nodes = nodes . Reverse ( ) . ToArray ( ) ;
84
130
}
85
131
86
132
return nodes ;
0 commit comments