Skip to content
This repository was archived by the owner on Dec 18, 2017. It is now read-only.

Commit

Permalink
fix #1683 by preferring listed packages
Browse files Browse the repository at this point in the history
...but accepting unlisted packages if there are no matches
  • Loading branch information
analogrelay committed Jul 2, 2015
1 parent 1f092f7 commit 0f9779b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IWalkProvider
{
bool IsHttp { get; }

Task<WalkProviderMatch> FindLibrary(LibraryRange libraryRange, FrameworkName targetFramework);
Task<WalkProviderMatch> FindLibrary(LibraryRange libraryRange, FrameworkName targetFramework, bool includeUnlisted);
Task<IEnumerable<LibraryDependency>> GetDependencies(WalkProviderMatch match, FrameworkName targetFramework);
Task<RuntimeFile> GetRuntimes(WalkProviderMatch match, FrameworkName targetFramework);
Task CopyToAsync(WalkProviderMatch match, Stream stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public LocalWalkProvider(IDependencyProvider dependencyProvider)

public bool IsHttp { get; private set; }

public Task<WalkProviderMatch> FindLibrary(LibraryRange libraryRange, FrameworkName targetFramework)
public Task<WalkProviderMatch> FindLibrary(LibraryRange libraryRange, FrameworkName targetFramework, bool includeUnlisted)
{
var description = _dependencyProvider.GetDescription(libraryRange, targetFramework);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ namespace Microsoft.Framework.PackageManager.Restore.NuGet
{
public class NuGetv2Feed : IPackageFeed
{
private static readonly XNamespace _defaultNamespace = XNamespace.Get("http://www.w3.org/2005/Atom");
private static readonly XName _xnameEntry = XName.Get("entry", _defaultNamespace.NamespaceName);
private static readonly XName _xnameTitle = XName.Get("title", _defaultNamespace.NamespaceName);
private static readonly XName _xnameContent = XName.Get("content", _defaultNamespace.NamespaceName);
private static readonly XName _xnameLink = XName.Get("link", _defaultNamespace.NamespaceName);
private static readonly XName _xnameProperties = XName.Get("properties", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
private static readonly XName _xnameId = XName.Get("Id", "http://schemas.microsoft.com/ado/2007/08/dataservices");
private static readonly XName _xnameVersion = XName.Get("Version", "http://schemas.microsoft.com/ado/2007/08/dataservices");
private static readonly XNamespace _defaultNamespace = "http://www.w3.org/2005/Atom";
private static readonly XNamespace _odataMetadataNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
private static readonly XNamespace _odataNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices";
private static readonly XName _xnameEntry = _defaultNamespace + "entry";
private static readonly XName _xnameTitle = _defaultNamespace + "title";
private static readonly XName _xnameContent = _defaultNamespace + "content";
private static readonly XName _xnameLink = _defaultNamespace + "link";
private static readonly XName _xnameProperties = _odataMetadataNamespace + "properties";
private static readonly XName _xnameId = _odataNamespace + "Id";
private static readonly XName _xnameVersion = _odataNamespace + "Version";
private static readonly XName _xnamePublish = _odataNamespace + "Published";

// An unlisted package's publish time must be 1900-01-01T00:00:00.
private static readonly DateTime _unlistedPublishedTime = new DateTime(1900, 1, 1, 0, 0, 0);

private readonly string _baseUri;
private readonly Reports _reports;
Expand Down Expand Up @@ -186,6 +192,18 @@ public PackageInfo BuildModel(string id, XElement element)
var properties = element.Element(_xnameProperties);
var idElement = properties.Element(_xnameId);
var titleElement = element.Element(_xnameTitle);
var publishElement = properties.Element(_xnamePublish);

var listed = true;
if (publishElement != null)
{
DateTime publishDate;
if (DateTime.TryParse(publishElement.Value, out publishDate) && (publishDate == _unlistedPublishedTime))
{
listed = false;
}
}


return new PackageInfo
{
Expand All @@ -195,6 +213,7 @@ public PackageInfo BuildModel(string id, XElement element)
Id = idElement?.Value ?? titleElement?.Value ?? id,
Version = SemanticVersion.Parse(properties.Element(_xnameVersion).Value),
ContentUri = element.Element(_xnameContent).Attribute("src").Value,
Listed = listed
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ public PackageInfo BuildModel(string id, string version)
Id = id,
Version = SemanticVersion.Parse(version),
ContentUri = $"{_baseUri}{lowerInvariantId}/{lowerInvariantVersion}/{lowerInvariantId}.{lowerInvariantVersion}{Constants.PackageExtension}",

// v3 feed doesn't indicate if listed?
Listed = true
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class PackageInfo
public string Id { get; set; }
public SemanticVersion Version { get; set; }
public string ContentUri { get; set; }
public bool Listed { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ public RemoteWalkProvider(IPackageFeed source)

public bool IsHttp { get; private set; }

public async Task<WalkProviderMatch> FindLibrary(LibraryRange libraryRange, FrameworkName targetFramework)
public async Task<WalkProviderMatch> FindLibrary(LibraryRange libraryRange, FrameworkName targetFramework, bool includeUnlisted)
{
var results = await _source.FindPackagesByIdAsync(libraryRange.Name);
PackageInfo bestResult = null;
if(!includeUnlisted)
{
results = results.Where(p => p.Listed);
}

foreach (var result in results)
{
if (VersionUtility.ShouldUseConsidering(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private async Task<WalkProviderMatch> FindProjectMatch(RestoreContext context, s

foreach (var provider in context.ProjectLibraryProviders)
{
var match = await provider.FindLibrary(libraryRange, context.FrameworkName);
var match = await provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false);
if (match != null)
{
return match;
Expand All @@ -281,20 +281,26 @@ private async Task<WalkProviderMatch> FindLibraryByVersion(RestoreContext contex
if (libraryRange.VersionRange.VersionFloatBehavior != SemanticVersionFloatBehavior.None)
{
// Don't optimize the non http path for floating versions or we'll miss things
return await FindLibrary(libraryRange, providers, provider => provider.FindLibrary(libraryRange, context.FrameworkName));
return await FindLibrary(libraryRange, providers, provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false));
}

// Try the non http sources first
var nonHttpMatch = await FindLibrary(libraryRange, providers.Where(p => !p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName));
var nonHttpMatch = await FindLibrary(libraryRange, providers.Where(p => !p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false));

// If we found an exact match then use it
if (nonHttpMatch != null && nonHttpMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
{
return nonHttpMatch;
}

// Otherwise try the http sources
var httpMatch = await FindLibrary(libraryRange, providers.Where(p => p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName));
// Otherwise try listed packages on http sources
var httpMatch = await FindLibrary(libraryRange, providers.Where(p => p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: false));

// If the http sources failed to find a listed package that matched, try unlisted packages
if (httpMatch == null)
{
httpMatch = await FindLibrary(libraryRange, providers.Where(p => p.IsHttp), provider => provider.FindLibrary(libraryRange, context.FrameworkName, includeUnlisted: true));
}

// Pick the best match of the 2
if (VersionUtility.ShouldUseConsidering(
Expand Down

0 comments on commit 0f9779b

Please sign in to comment.