-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathSemVerLevelKey.cs
122 lines (108 loc) · 5.13 KB
/
SemVerLevelKey.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using NuGet.Versioning;
namespace NuGetGallery
{
// Hard-coded for now, but we can easily expand to use an additional Sql table to join with
// when supporting additional semVerLevel's is needed.
/// <summary>
/// Helper class to use to determine the SemVer level of a package version.
/// </summary>
public static class SemVerLevelKey
{
public static readonly string SemVerLevel2 = "2.0.0";
private static readonly NuGetVersion _semVer2Version = NuGetVersion.Parse(SemVerLevel2);
/// <summary>
/// This could either indicate being SemVer1-compliant, or non-SemVer-compliant at all (e.g. System.Versioning pattern).
/// </summary>
public static readonly int? Unknown = null;
/// <summary>
/// Indicates being SemVer2-compliant, but not SemVer1-compliant.
/// This key corresponds to semVerLevel=2.0.0
/// </summary>
public static readonly int SemVer2 = 2;
/// <summary>
/// Identifies the SemVer-level of a package based on original version string and dependency versions.
/// </summary>
/// <param name="originalVersion">The package's non-normalized, original version string.</param>
/// <param name="dependencies">The package's direct dependencies as defined in the package's manifest.</param>
/// <returns>Returns <c>null</c> when unknown; otherwise the identified SemVer-level key.</returns>
public static int? ForPackage(NuGetVersion originalVersion, IEnumerable<PackageDependency> dependencies)
{
if (originalVersion == null)
{
throw new ArgumentNullException(nameof(originalVersion));
}
if (originalVersion.IsSemVer2)
{
// No need to further check the dependencies:
// the original version already is identified to be SemVer2-compliant, but not SemVer1-compliant.
return SemVer2;
}
if (dependencies != null)
{
foreach (var dependency in dependencies)
{
// Check the package dependencies for SemVer-compliance.
// As soon as a SemVer2-compliant dependency version is found that is not SemVer1-compliant,
// this package in itself is to be identified as to have SemVerLevelKey.SemVer2.
VersionRange dependencyVersionRange;
if (dependency.VersionSpec != null && VersionRange.TryParse(dependency.VersionSpec, out dependencyVersionRange))
{
if ((dependencyVersionRange.MinVersion != null && dependencyVersionRange.MinVersion.IsSemVer2)
|| (dependencyVersionRange.MaxVersion != null && dependencyVersionRange.MaxVersion.IsSemVer2))
{
return SemVer2;
}
}
}
}
return Unknown;
}
/// <summary>
/// Identifies the SemVer-level for a given semVerLevel version string.
/// </summary>
/// <param name="semVerLevel">The version string indicating the supported SemVer-level.</param>
/// <returns>
/// Returns <c>null</c> when unknown; otherwise the identified SemVer-level key.
/// </returns>
/// <remarks>
/// Older clients don't send the semVerLevel query parameter at all,
/// so we default to Unknown for backwards-compatibility.
/// </remarks>
public static int? ForSemVerLevel(string semVerLevel)
{
if (semVerLevel == null)
{
return Unknown;
}
NuGetVersion parsedVersion;
if (NuGetVersion.TryParse(semVerLevel, out parsedVersion))
{
return _semVer2Version <= parsedVersion ? SemVer2 : Unknown;
}
else
{
return Unknown;
}
}
/// <summary>
/// Indicates whether the provided SemVer-level key is compliant with the provided SemVer-level version string.
/// </summary>
/// <param name="semVerLevel">The SemVer-level string indicating the SemVer-level to comply with.</param>
/// <returns><c>True</c> if compliant; otherwise <c>false</c>.</returns>
public static Expression<Func<Package, bool>> IsPackageCompliantWithSemVerLevel(string semVerLevel)
{
// Note: we must return an expression that Linq to Entities is able to translate to SQL
var parsedSemVerLevelKey = ForSemVerLevel(semVerLevel);
if (parsedSemVerLevelKey == SemVer2)
{
return p => p.SemVerLevelKey == Unknown || p.SemVerLevelKey == parsedSemVerLevelKey;
}
return p => p.SemVerLevelKey == Unknown;
}
}
}