Skip to content

Commit a527f9e

Browse files
AOT Compatible: ManifestVersionUtility (#7128)
1 parent 9404d87 commit a527f9e

3 files changed

Lines changed: 17 additions & 104 deletions

File tree

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestMetadata.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public ManifestMetadata(IPackageMetadata copy)
6868
Readme = copy.Readme;
6969
}
7070

71-
[ManifestVersion(5)]
7271
public string MinClientVersionString
7372
{
7473
get { return _minClientVersionString; }
@@ -173,10 +172,8 @@ public Uri ProjectUrl
173172

174173
public string Summary { get; set; }
175174

176-
[ManifestVersion(2)]
177175
public string ReleaseNotes { get; set; }
178176

179-
[ManifestVersion(2)]
180177
public string Copyright { get; set; }
181178

182179
public string Language { get; set; }
@@ -208,7 +205,6 @@ public IEnumerable<PackageDependencyGroup> DependencyGroups
208205

209206
private IEnumerable<PackageReferenceSet> _packageAssemblyReferences = [];
210207

211-
[ManifestVersion(2)]
212208
public IEnumerable<PackageReferenceSet> PackageAssemblyReferences
213209
{
214210
get

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestVersionAttribute.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/NuGet.Core/NuGet.Packaging/PackageCreation/Authoring/ManifestVersionUtility.cs

Lines changed: 17 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
#nullable disable
55

66
using System;
7-
using System.Collections;
87
using System.Linq;
9-
using System.Reflection;
108

119
namespace 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

Comments
 (0)