2
2
using System . Text ;
3
3
using Newtonsoft . Json ;
4
4
5
- namespace Microsoft . PowerShell . CrossCompatibility . Utility
5
+ namespace Microsoft . PowerShell . CrossCompatibility
6
6
{
7
7
public class PowerShellVersion
8
8
{
@@ -11,12 +11,17 @@ public static PowerShellVersion Create(dynamic versionInput)
11
11
switch ( versionInput )
12
12
{
13
13
case Version systemVersion :
14
- return new PowerShellVersion ( systemVersion ) ;
14
+ return ( PowerShellVersion ) systemVersion ;
15
15
16
16
case string versionString :
17
17
return Parse ( versionString ) ;
18
18
}
19
19
20
+ if ( versionInput . BuildLabel != null )
21
+ {
22
+ return new PowerShellVersion ( versionInput . Major , versionInput . Minor , versionInput . Patch , $ "{ versionInput . PreReleaseLabel } +{ versionInput . BuildLabel } ") ;
23
+ }
24
+
20
25
return new PowerShellVersion ( versionInput . Major , versionInput . Minor , versionInput . Patch , versionInput . PreReleaseLabel ) ;
21
26
}
22
27
@@ -27,7 +32,7 @@ public static PowerShellVersion Parse(string versionStr)
27
32
throw new ArgumentNullException ( nameof ( versionStr ) ) ;
28
33
}
29
34
30
- int [ ] versionParts = new int [ 3 ] ;
35
+ int [ ] versionParts = new int [ 3 ] { - 1 , - 1 , - 1 } ;
31
36
32
37
int sectionStartOffset = 0 ;
33
38
int dotCount = 0 ;
@@ -69,7 +74,7 @@ public static PowerShellVersion Parse(string versionStr)
69
74
70
75
versionParts [ dotCount ] = int . Parse ( versionStr . Substring ( sectionStartOffset , i - sectionStartOffset ) ) ;
71
76
72
- return new PowerShellVersion ( versionParts [ 0 ] , versionParts [ 1 ] , versionParts [ 2 ] , preReleaseLabel : null ) ;
77
+ return new PowerShellVersion ( versionParts [ 0 ] , versionParts [ 1 ] , versionParts [ 2 ] , label : null ) ;
73
78
}
74
79
75
80
public static bool TryParse ( string versionStr , out PowerShellVersion version )
@@ -86,45 +91,75 @@ public static bool TryParse(string versionStr, out PowerShellVersion version)
86
91
}
87
92
}
88
93
89
- public static explicit operator Version ( PowerShellVersion psVersion )
94
+ public static void ValidateVersionArguments ( int major , int minor , int build , int revision , string preReleaseLabel )
90
95
{
91
- if ( psVersion . PreReleaseLabel != null )
96
+ if ( major < 0 )
92
97
{
93
- throw new InvalidCastException ( $ "Cannot convert version '{ psVersion } ' to System.Version, since there is a pre-release label") ;
98
+ throw new ArgumentException ( ) ;
99
+ }
100
+
101
+ if ( minor < 0 && ( build >= 0 || revision >= 0 ) )
102
+ {
103
+ throw new ArgumentException ( ) ;
94
104
}
95
105
96
- if ( psVersion . Revision != null )
106
+ if ( build < 0 && revision >= 0 )
97
107
{
98
- return new Version ( psVersion . Major , psVersion . Minor , psVersion . Patch , psVersion . Revision . Value ) ;
108
+ throw new ArgumentException ( ) ;
99
109
}
100
110
101
- return new Version ( psVersion . Major , psVersion . Minor , psVersion . Patch ) ;
111
+ if ( revision >= 0 && preReleaseLabel != null )
112
+ {
113
+ throw new ArgumentException ( ) ;
114
+ }
115
+ }
116
+
117
+ public static explicit operator Version ( PowerShellVersion psVersion )
118
+ {
119
+ if ( psVersion . PreReleaseLabel != null )
120
+ {
121
+ throw new InvalidCastException ( $ "Cannot convert version '{ psVersion } ' to System.Version, since there is a pre-release label") ;
122
+ }
123
+
124
+ return new Version ( psVersion . Major , psVersion . Minor , psVersion . Patch , psVersion . Revision ) ;
102
125
}
103
126
104
127
public static explicit operator PowerShellVersion ( string versionString )
105
128
{
106
129
return PowerShellVersion . Parse ( versionString ) ;
107
130
}
108
131
109
- public PowerShellVersion ( Version version )
110
- : this ( version . Major , version . Minor , version . Build , version . Revision )
132
+ public static explicit operator PowerShellVersion ( Version version )
111
133
{
134
+ return new PowerShellVersion ( version . Major , version . Minor , version . Build , version . Revision ) ;
112
135
}
113
136
114
137
public PowerShellVersion ( int major , int minor , int build , int revision )
115
138
{
139
+ ValidateVersionArguments ( major , minor , build , revision , preReleaseLabel : null ) ;
116
140
Major = major ;
117
141
Minor = minor ;
118
142
Build = build ;
119
143
Revision = revision ;
120
144
}
121
145
122
- public PowerShellVersion ( int major , int minor , int patch , string preReleaseLabel )
146
+ public PowerShellVersion ( int major , int minor , int patch , string label )
123
147
{
148
+ ValidateVersionArguments ( major , minor , patch , - 1 , label ) ;
124
149
Major = major ;
125
150
Minor = minor ;
126
151
Build = patch ;
127
- PreReleaseLabel = preReleaseLabel ;
152
+
153
+ int plusIdx = label ? . IndexOf ( '+' ) ?? - 1 ;
154
+ if ( plusIdx < 0 )
155
+ {
156
+ PreReleaseLabel = label ;
157
+ }
158
+ else
159
+ {
160
+ PreReleaseLabel = label . Substring ( 0 , plusIdx ) ;
161
+ BuildLabel = label . Substring ( plusIdx + 1 , label . Length - plusIdx - 1 ) ;
162
+ }
128
163
}
129
164
130
165
public int Major { get ; }
@@ -135,25 +170,37 @@ public PowerShellVersion(int major, int minor, int patch, string preReleaseLabel
135
170
136
171
public int Patch => Build ;
137
172
138
- public int ? Revision { get ; }
173
+ public int Revision { get ; }
139
174
140
175
public string PreReleaseLabel { get ; }
141
176
142
- public bool IsSemVer => Revision == null ;
177
+ public string BuildLabel { get ; }
178
+
179
+ public bool IsSemVer => Revision < 0 ;
143
180
144
181
public override string ToString ( )
145
182
{
146
- if ( ! IsSemVer )
183
+ var sb = new StringBuilder ( Major ) ;
184
+
185
+ if ( Minor < 0 )
186
+ {
187
+ return sb . ToString ( ) ;
188
+ }
189
+
190
+ sb . Append ( '.' ) . Append ( Minor ) ;
191
+
192
+ if ( Build < 0 )
147
193
{
148
- return $ " { Major } . { Minor } . { Build } . { Revision } " ;
194
+ return sb . ToString ( ) ;
149
195
}
150
196
151
- var sb = new StringBuilder ( )
152
- . Append ( Major ) . Append ( '.' )
153
- . Append ( Minor ) . Append ( '.' )
154
- . Append ( Patch ) ;
197
+ sb . Append ( '.' ) . Append ( Build ) ;
155
198
156
- if ( ! string . IsNullOrEmpty ( PreReleaseLabel ) )
199
+ if ( Revision >= 0 )
200
+ {
201
+ sb . Append ( '.' ) . Append ( Revision ) ;
202
+ }
203
+ else if ( PreReleaseLabel != null )
157
204
{
158
205
sb . Append ( '-' ) . Append ( PreReleaseLabel ) ;
159
206
}
@@ -167,7 +214,6 @@ public class PowerShellVersionJsonConverter : JsonConverter
167
214
public override bool CanConvert ( Type objectType )
168
215
{
169
216
return objectType == typeof ( PowerShellVersion )
170
- || objectType == typeof ( Version )
171
217
|| objectType . FullName == "System.Management.Automation.SemanticVersion" ;
172
218
}
173
219
0 commit comments