-
Notifications
You must be signed in to change notification settings - Fork 19
Update feed2catalog and catalog2registration to properly handle SemVer 2.0.0 #145
Changes from 15 commits
1f1a3bf
8e38ffa
a6691f7
6e49f1d
fa00cbf
9b6acf0
d9e3b5d
43f283c
39a869f
ee0f9cc
37c7804
dbbb703
868e51b
aa89b84
7d19314
0f5e4ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// 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 NuGet.Versioning; | ||
using VDS.RDF; | ||
using VDS.RDF.Query; | ||
|
||
namespace NuGet.Services.Metadata.Catalog.Helpers | ||
{ | ||
public static class NuGetVersionUtility | ||
{ | ||
public static string NormalizeVersion(string version) | ||
{ | ||
NuGetVersion parsedVersion; | ||
if (!NuGetVersion.TryParse(version, out parsedVersion)) | ||
{ | ||
return version; | ||
} | ||
|
||
return parsedVersion.ToNormalizedString(); | ||
} | ||
|
||
public static string NormalizeVersionRange(string versionRange) | ||
{ | ||
VersionRange parsedVersionRange; | ||
if (!VersionRange.TryParse(versionRange, out parsedVersionRange)) | ||
{ | ||
return versionRange; | ||
} | ||
|
||
return parsedVersionRange.ToNormalizedString(); | ||
} | ||
|
||
public static string GetFullVersionString(string version) | ||
{ | ||
NuGetVersion parsedVersion; | ||
if (!NuGetVersion.TryParse(version, out parsedVersion)) | ||
{ | ||
return version; | ||
} | ||
|
||
return parsedVersion.ToFullString(); | ||
} | ||
|
||
public static bool IsVersionSemVer2(string version) | ||
{ | ||
NuGetVersion parsedVersion; | ||
if (!NuGetVersion.TryParse(version, out parsedVersion)) | ||
{ | ||
return false; | ||
} | ||
|
||
return parsedVersion.IsSemVer2; | ||
} | ||
|
||
public static bool IsVersionRangeSemVer2(string versionRange) | ||
{ | ||
VersionRange parsedVersionRange; | ||
if (!VersionRange.TryParse(versionRange, out parsedVersionRange)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (parsedVersionRange.HasLowerBound && parsedVersionRange.MinVersion.IsSemVer2) | ||
{ | ||
return true; | ||
} | ||
|
||
if (parsedVersionRange.HasUpperBound && parsedVersionRange.MaxVersion.IsSemVer2) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static bool IsGraphSemVer2(string version, string resourceUri, IGraph graph) | ||
{ | ||
// Is the package version itself SemVer 2.0.0? | ||
if (IsVersionSemVer2(version)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (resourceUri == null) | ||
{ | ||
throw new ArgumentNullException(nameof(resourceUri)); | ||
} | ||
|
||
if (graph == null) | ||
{ | ||
throw new ArgumentNullException(nameof(graph)); | ||
} | ||
|
||
// Are any of the dependency version ranges SemVer 2.0.0? | ||
var sparql = new SparqlParameterizedString | ||
{ | ||
CommandText = Utils.GetResource("sparql.SelectDistinctDependencyVersionRanges.rq") | ||
}; | ||
sparql.SetUri("resourceUri", new Uri(resourceUri)); | ||
var query = sparql.ToString(); | ||
|
||
TripleStore store = new TripleStore(); | ||
store.Add(graph, true); | ||
foreach (SparqlResult row in SparqlHelpers.Select(store, query)) | ||
{ | ||
var unparsedVersionRange = row["versionRange"].ToString(); | ||
if (IsVersionRangeSemVer2(unparsedVersionRange)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
// 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 NuGet.Versioning; | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Xml; | ||
using System.Xml.XPath; | ||
using NuGet.Services.Metadata.Catalog.Helpers; | ||
using NuGet.Versioning; | ||
|
||
namespace NuGet.Services.Metadata.Catalog | ||
{ | ||
|
@@ -40,22 +42,17 @@ public string LowerCase(string original) | |
|
||
public string NormalizeVersion(string original) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need those thin wrappers? Can use NuGetVersionUtility directly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose to wrap thinly here because |
||
{ | ||
NuGetVersion nugetVersion; | ||
if (NuGetVersion.TryParse(original, out nugetVersion)) | ||
{ | ||
return nugetVersion.ToNormalizedString(); | ||
} | ||
return original; | ||
return NuGetVersionUtility.NormalizeVersion(original); | ||
} | ||
|
||
public string GetFullVersionString(string original) | ||
{ | ||
return NuGetVersionUtility.GetFullVersionString(original); | ||
} | ||
|
||
public string NormalizeVersionRange(string original) | ||
{ | ||
VersionRange versionRange; | ||
if (VersionRange.TryParse(original, out versionRange)) | ||
{ | ||
return versionRange.ToString(); | ||
} | ||
return original; | ||
return NuGetVersionUtility.NormalizeVersionRange(original); | ||
} | ||
|
||
public string IsPrerelease(string original) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
// 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; | ||
using VDS.RDF; | ||
|
||
namespace NuGet.Services.Metadata.Catalog.Registration | ||
{ | ||
/// <summary> | ||
/// A delegate used to determine whether a package should be included in a registration hive. The delegate is | ||
/// important because some registration hives exclude specific packages (typically packages that break older | ||
/// clients). The first example of this difference is SemVer 2.0.0 packages, which should be excluded from the legacy | ||
/// registration hives. | ||
/// </summary> | ||
/// <param name="key">The package key. This contains the ID and version of the package.</param> | ||
/// <param name="resourceUri">The URI (identifier) of the package in the RDF graph.</param> | ||
/// <param name="graph">The RDF graph containing metadata about the package.</param> | ||
/// <returns>True if the package should be included in the registration hive. False otherwise.</returns> | ||
public delegate bool ShouldIncludeRegistrationPackage(RegistrationEntryKey key, string resourceUri, IGraph graph); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move the delegate inside ShouldIncludeRegistrationPackage. Will make the code more readable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what you mean by:
The delegate itself is called In general, delegates are just types so I see no reason why they should be nested. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant to move it inside RegistrationCatalogEntry, but I agree with your logic. However, the name is still confusing and needs a comment/better name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Latest revision has XML docs now. |
||
|
||
public class RegistrationCatalogEntry | ||
{ | ||
public RegistrationCatalogEntry(string resourceUri, IGraph graph, bool isExistingItem) | ||
|
@@ -20,16 +33,22 @@ public RegistrationCatalogEntry(string resourceUri, IGraph graph, bool isExistin | |
public IGraph Graph { get; set; } | ||
public bool IsExistingItem { get; set; } | ||
|
||
public static KeyValuePair<RegistrationEntryKey, RegistrationCatalogEntry> Promote(string resourceUri, IGraph graph, bool isExistingItem) | ||
public static KeyValuePair<RegistrationEntryKey, RegistrationCatalogEntry> Promote( | ||
string resourceUri, | ||
IGraph graph, | ||
ShouldIncludeRegistrationPackage shouldInclude, | ||
bool isExistingItem) | ||
{ | ||
INode subject = graph.CreateUriNode(new Uri(resourceUri)); | ||
string version = graph.GetTriplesWithSubjectPredicate(subject, graph.CreateUriNode(Schema.Predicates.Version)).First().Object.ToString(); | ||
|
||
RegistrationEntryKey registrationEntryKey = new RegistrationEntryKey(RegistrationKey.Promote(resourceUri, graph), version); | ||
|
||
RegistrationCatalogEntry registrationCatalogEntry = IsDelete(subject, graph) | ||
? null | ||
: new RegistrationCatalogEntry(resourceUri, graph, isExistingItem); | ||
RegistrationCatalogEntry registrationCatalogEntry = null; | ||
if (!IsDelete(subject, graph) && shouldInclude(registrationEntryKey, resourceUri, graph)) | ||
{ | ||
registrationCatalogEntry = new RegistrationCatalogEntry(resourceUri, graph, isExistingItem); | ||
} | ||
|
||
return new KeyValuePair<RegistrationEntryKey, RegistrationCatalogEntry>(registrationEntryKey, registrationCatalogEntry); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May need some argument checks on this utility class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.