11using NuGet . Versioning ;
2- using System . Text . RegularExpressions ;
32
43namespace Sdcb . OpenVINO . NuGetBuilders . ArtifactSources ;
54
@@ -16,37 +15,85 @@ public static VersionFolder FromFolder(StorageNode Folder)
1615 return new VersionFolder ( path , Folder , ver ) ;
1716 }
1817
19- static SemanticVersion ParseOpenVINOVersion ( string versionString )
18+ /// <summary>
19+ /// Supported formats:
20+ /// <list type="bullet">
21+ /// <item>2024.3.0-15549-ae01c973d53</item>
22+ /// <item>2022.1</item>
23+ /// <item>2022.3.1</item>
24+ /// <item>2024.2.0rc1</item>
25+ /// </list>
26+ /// </summary>
27+ /// <exception cref="ArgumentException"></exception>
28+ public static SemanticVersion ParseOpenVINOVersion ( string versionString )
2029 {
21- string [ ] versionParts = versionString . Split ( '.' ) ;
30+ // Split into major, minor, patch and optional additional label parts
31+ string [ ] parts = versionString . Split ( '.' ) ;
2232
23- // If the version string is in the format 'major.minor', e.g., '2014.7'
24- if ( versionParts . Length == 2 )
33+ if ( parts . Length < 2 )
2534 {
26- // Append '.0' to convert it into a valid Semantic Version format (major.minor.patch)
27- versionString += ".0" ;
35+ throw new ArgumentException ( "Invalid version string" ) ;
2836 }
2937
30- // If the version string is in the format 'major.minor.0.0.dev+time', e.g., '2022.7.0.0.dev20220714'
31- else if ( versionParts . Length == 4 && versionParts [ 3 ] . StartsWith ( "dev" ) )
38+ // Parse the major and minor versions
39+ int major = int . Parse ( parts [ 0 ] ) ;
40+ int minor = int . Parse ( parts [ 1 ] ) ;
41+
42+ // Initialize patch and label
43+ int patch = 0 ;
44+ string ? releaseLabel = null ;
45+
46+ if ( parts . Length > 2 )
3247 {
33- // Remove 'dev' part and convert the last part (time) into a valid build metadata part
34- versionString = OpenVINODevVersionRegex ( ) . Replace ( versionString , "$1-$2.$3" ) ;
48+ // Possible formats can be "patch", "patch-...", "patchrc..."
49+ // Split third part further by '-' to separate patch and prerelease label if exists
50+ string [ ] subParts = parts [ 2 ] . Split ( [ '-' ] , 2 ) ;
51+
52+ // Parse patch version which is always the first part
53+ // Handle cases where the patch part might have a release candidate or similar suffix directly attached
54+ string patchPart = subParts [ 0 ] ;
55+ int releaseLabelIndex = FindFirstNonNumericIndex ( patchPart ) ;
56+
57+ if ( releaseLabelIndex != - 1 )
58+ {
59+ // Separate the numeric patch from the non-numeric release label
60+ patch = int . Parse ( patchPart [ ..releaseLabelIndex ] ) ;
61+ releaseLabel = patchPart [ releaseLabelIndex ..] ;
62+ }
63+ else
64+ {
65+ patch = int . Parse ( patchPart ) ;
66+ }
67+
68+ // If there is any further information after a '-', it is part of the release label
69+ if ( subParts . Length > 1 )
70+ {
71+ releaseLabel = ( releaseLabel != null ? releaseLabel + "-" : "" ) + subParts [ 1 ] ;
72+ }
3573 }
3674
37- // Try to parse the version string into a SemanticVersion object
38- if ( SemanticVersion . TryParse ( versionString , out SemanticVersion ? semVersion ) )
75+ // Create the SemanticVersion based on extracted information
76+ if ( releaseLabel != null )
3977 {
40- return semVersion ;
78+ return new SemanticVersion ( major , minor , patch , releaseLabel ) ;
4179 }
4280 else
4381 {
44- throw new FormatException ( $ " { versionString } is not a known OpenVINO Version" ) ;
82+ return new SemanticVersion ( major , minor , patch ) ;
4583 }
46- }
4784
48- [ GeneratedRegex ( "(.+)\\ .(dev)(\\ d{8})" ) ]
49- private static partial Regex OpenVINODevVersionRegex ( ) ;
85+ static int FindFirstNonNumericIndex ( string str )
86+ {
87+ for ( int i = 0 ; i < str . Length ; i ++ )
88+ {
89+ if ( ! char . IsDigit ( str [ i ] ) )
90+ {
91+ return i ;
92+ }
93+ }
94+ return - 1 ;
95+ }
96+ }
5097
5198 public IEnumerable < ArtifactInfo > Artifacts => ArtifactInfo . FromFolder ( this ) ;
5299}
0 commit comments