44#nullable disable
55
66using System ;
7- using System . Collections ;
87using System . Linq ;
9- using System . Reflection ;
108
119namespace NuGet . Packaging
1210{
@@ -21,102 +19,39 @@ public static class ManifestVersionUtility
2119
2220 public static int GetManifestVersion ( ManifestMetadata metadata )
2321 {
24- return Math . Max ( GetVersionFromObject ( metadata ) , GetMaxVersionFromMetadata ( metadata ) ) ;
25- }
26-
27- private static int GetMaxVersionFromMetadata ( ManifestMetadata metadata )
28- {
29- // Important: always add newer version checks at the top
30-
31- bool referencesHasTargetFramework =
32- metadata . PackageAssemblyReferences != null &&
33- metadata . PackageAssemblyReferences . Any ( r => r . TargetFramework != null && r . TargetFramework . IsSpecificFramework ) ;
34-
35- if ( referencesHasTargetFramework )
36- {
37- return TargetFrameworkSupportForReferencesVersion ;
38- }
39-
40- bool dependencyHasTargetFramework =
41- metadata . DependencyGroups != null &&
42- metadata . DependencyGroups . Any ( d => d . TargetFramework != null && d . TargetFramework . IsSpecificFramework ) ;
43- if ( dependencyHasTargetFramework )
44- {
45- return TargetFrameworkSupportForDependencyContentsAndToolsVersion ;
46- }
47-
48- if ( metadata . Version != null && metadata . Version . IsPrerelease )
22+ if ( metadata == null )
4923 {
50- return SemverVersion ;
24+ throw new ArgumentNullException ( nameof ( metadata ) ) ;
5125 }
5226
53- return DefaultVersion ;
54- }
55-
56- private static int GetVersionFromObject ( object obj )
57- {
58- // all public, gettable, non-static properties
59- return obj ? . GetType ( )
60- . GetRuntimeProperties ( )
61- . Where ( prop => prop . GetMethod != null && prop . GetMethod . IsPublic && ! prop . GetMethod . IsStatic )
62- . Max ( prop => GetVersionFromPropertyInfo ( obj , prop ) )
63- ?? DefaultVersion ;
64- }
65-
66- private static int GetVersionFromPropertyInfo ( object obj , PropertyInfo property )
67- {
68- var value = property . GetValue ( obj , index : null ) ;
69- if ( value == null )
70- {
71- return DefaultVersion ;
72- }
27+ // Important: always add newer version checks at the top
7328
74- int ? version = GetPropertyVersion ( property ) ;
75- if ( ! version . HasValue )
29+ if ( ( metadata . PackageAssemblyReferences != null &&
30+ metadata . PackageAssemblyReferences . Any ( r => r . TargetFramework != null && r . TargetFramework . IsSpecificFramework ) ) ||
31+ ! string . IsNullOrEmpty ( metadata . MinClientVersionString ) )
7632 {
77- return DefaultVersion ;
33+ return 5 ;
7834 }
7935
80- var list = value as IList ;
81- if ( list != null )
36+ if ( metadata . DependencyGroups != null &&
37+ metadata . DependencyGroups . Any ( d => d . TargetFramework != null && d . TargetFramework . IsSpecificFramework ) )
8238 {
83- if ( list . Count > 0 )
84- {
85- return Math . Max ( version . Value , VisitList ( list ) ) ;
86- }
87- return DefaultVersion ;
39+ return 4 ;
8840 }
8941
90- var stringValue = value as string ;
91- if ( stringValue != null )
42+ if ( metadata . Version != null && metadata . Version . IsPrerelease )
9243 {
93- if ( ! string . IsNullOrEmpty ( stringValue ) )
94- {
95- return version . Value ;
96- }
97- return DefaultVersion ;
44+ return 3 ;
9845 }
9946
100- // For all other object types a null check would suffice.
101- return version . Value ;
102- }
103-
104- private static int VisitList ( IEnumerable list )
105- {
106- int version = DefaultVersion ;
107-
108- foreach ( var item in list )
47+ if ( ! string . IsNullOrEmpty ( metadata . ReleaseNotes ) ||
48+ ! string . IsNullOrEmpty ( metadata . Copyright ) ||
49+ ( metadata . PackageAssemblyReferences ? . Any ( ) ?? false ) )
10950 {
110- version = Math . Max ( version , GetVersionFromObject ( item ) ) ;
51+ return 2 ;
11152 }
11253
113- return version ;
114- }
115-
116- private static int ? GetPropertyVersion ( PropertyInfo property )
117- {
118- var attribute = property . GetCustomAttribute < ManifestVersionAttribute > ( ) ;
119- return attribute ? . Version ;
54+ return DefaultVersion ;
12055 }
12156 }
12257}
0 commit comments