Skip to content

Commit

Permalink
Plumb SourceCacheContext and ILogger parameter down to FindPackageByI…
Browse files Browse the repository at this point in the history
…dResource (#852)

Fix NuGet/Home#1357
Fix NuGet/Home#2216
joelverhagen authored Aug 30, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 056274f commit edfc150
Showing 41 changed files with 720 additions and 250 deletions.
7 changes: 7 additions & 0 deletions NuGet.Core.sln
Original file line number Diff line number Diff line change
@@ -134,6 +134,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NuGet.Repositories.Test", "
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NuGet.Commands.Test.Utility", "test\NuGet.Core.Tests\NuGet.Commands.Test.Utility\NuGet.Commands.Test.Utility.xproj", "{975924FA-3B4F-46AD-AA3B-F7C066D3955C}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NuGet.DependencyResolver.Core.Tests.Utility", "test\NuGet.Core.Tests\NuGet.DependencyResolver.Core.Tests.Utility\NuGet.DependencyResolver.Core.Tests.Utility.xproj", "{23C35C81-55E5-4E21-9E67-CCDEE901072B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -360,6 +362,10 @@ Global
{975924FA-3B4F-46AD-AA3B-F7C066D3955C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{975924FA-3B4F-46AD-AA3B-F7C066D3955C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{975924FA-3B4F-46AD-AA3B-F7C066D3955C}.Release|Any CPU.Build.0 = Release|Any CPU
{23C35C81-55E5-4E21-9E67-CCDEE901072B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23C35C81-55E5-4E21-9E67-CCDEE901072B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23C35C81-55E5-4E21-9E67-CCDEE901072B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23C35C81-55E5-4E21-9E67-CCDEE901072B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -420,5 +426,6 @@ Global
{2034C981-C938-4F0F-8F3B-528B83CB8F7D} = {BB63CACA-866F-454F-BA4C-78B788D032BB}
{93D1D30C-45F8-4DCE-8EAD-7C7C504713B6} = {7323F93B-D141-4001-BB9E-7AF14031143E}
{975924FA-3B4F-46AD-AA3B-F7C066D3955C} = {7323F93B-D141-4001-BB9E-7AF14031143E}
{23C35C81-55E5-4E21-9E67-CCDEE901072B} = {7323F93B-D141-4001-BB9E-7AF14031143E}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -160,7 +160,12 @@ private async Task CopyToAsync(RemoteMatch remoteMatch, Stream destination, Canc
else
{
// Otherwise, get it from the provider.
await remoteMatch.Provider.CopyToAsync(remoteMatch.Library, destination, token);
await remoteMatch.Provider.CopyToAsync(
remoteMatch.Library,
destination,
_request.CacheContext,
_request.Log,
token);
}
}
}
Original file line number Diff line number Diff line change
@@ -28,9 +28,9 @@ internal class ProjectRestoreCommand

private readonly ProjectRestoreRequest _request;

public ProjectRestoreCommand(ILogger logger, ProjectRestoreRequest request)
public ProjectRestoreCommand(ProjectRestoreRequest request)
{
_logger = logger;
_logger = request.Log;
_request = request;
}

@@ -272,7 +272,12 @@ private async Task InstallPackageAsync(RemoteMatch installItem, CancellationToke
_request.XmlDocFileSaveMode);

await PackageExtractor.InstallFromSourceAsync(
stream => installItem.Provider.CopyToAsync(installItem.Library, stream, token),
stream => installItem.Provider.CopyToAsync(
installItem.Library,
stream,
_request.CacheContext,
_logger,
token),
versionFolderPathContext,
token);
}
Original file line number Diff line number Diff line change
@@ -3,10 +3,12 @@

using System.Collections.Concurrent;
using System.Collections.Generic;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.Protocol.Core.Types;
using NuGet.RuntimeModel;

namespace NuGet.Commands
@@ -20,6 +22,8 @@ public ProjectRestoreRequest(
Dictionary<NuGetFramework, RuntimeGraph> runtimeGraphCache,
ConcurrentDictionary<PackageIdentity, RuntimeGraph> runtimeGraphCacheByPackage)
{
CacheContext = request.CacheContext;
Log = request.Log;
PackagesDirectory = request.PackagesDirectory;
ExistingLockFile = existingLockFile;
RuntimeGraphCache = runtimeGraphCache;
@@ -30,6 +34,8 @@ public ProjectRestoreRequest(
XmlDocFileSaveMode = request.XmlDocFileSaveMode;
}

public SourceCacheContext CacheContext { get; }
public ILogger Log { get; }
public string PackagesDirectory { get; }
public int MaxDegreeOfConcurrency { get; }
public LockFile ExistingLockFile { get; }
Original file line number Diff line number Diff line change
@@ -91,6 +91,7 @@ protected virtual RestoreSummaryRequest Create(
var request = new RestoreRequest(
project.PackageSpec,
sharedCache,
restoreContext.CacheContext,
restoreContext.Log);

restoreContext.ApplyStandardProperties(request);
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@
using System.Linq;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.ProjectModel;

namespace NuGet.Commands
@@ -84,6 +83,7 @@ private RestoreSummaryRequest Create(
var request = new RestoreRequest(
project,
sharedCache,
restoreContext.CacheContext,
restoreContext.Log);

restoreContext.ApplyStandardProperties(request);
Original file line number Diff line number Diff line change
@@ -388,7 +388,7 @@ private async Task<IEnumerable<ToolRestoreResult>> ExecuteToolRestoresAsync(
existingToolLockFile,
new Dictionary<NuGetFramework, RuntimeGraph>(),
_runtimeGraphCacheByPackage);
var projectRestoreCommand = new ProjectRestoreCommand(_logger, projectRestoreRequest);
var projectRestoreCommand = new ProjectRestoreCommand(projectRestoreRequest);
var result = await projectRestoreCommand.TryRestore(
tool.LibraryRange,
projectFrameworkRuntimePairs,
@@ -556,7 +556,7 @@ private async Task<IEnumerable<RestoreTargetGraph>> ExecuteRestoreAsync(
_request.ExistingLockFile,
_runtimeGraphCache,
_runtimeGraphCacheByPackage);
var projectRestoreCommand = new ProjectRestoreCommand(_logger, projectRestoreRequest);
var projectRestoreCommand = new ProjectRestoreCommand(projectRestoreRequest);
var result = await projectRestoreCommand.TryRestore(
projectRange,
projectFrameworkRuntimePairs,
@@ -660,7 +660,9 @@ private static IEnumerable<FrameworkRuntimePair> CreateFrameworkRuntimePairs(

private static RemoteWalkContext CreateRemoteWalkContext(RestoreRequest request)
{
var context = new RemoteWalkContext();
var context = new RemoteWalkContext(
request.CacheContext,
request.Log);

foreach (var provider in request.DependencyProviders.LocalProviders)
{
10 changes: 10 additions & 0 deletions src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreRequest.cs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
using NuGet.Packaging;
using NuGet.Packaging.PackageExtraction;
using NuGet.ProjectModel;
using NuGet.Protocol.Core.Types;

namespace NuGet.Commands
{
@@ -18,6 +19,7 @@ public class RestoreRequest
public RestoreRequest(
PackageSpec project,
RestoreCommandProviders dependencyProviders,
SourceCacheContext cacheContext,
ILogger log)
{
if (project == null)
@@ -30,6 +32,11 @@ public RestoreRequest(
throw new ArgumentNullException(nameof(dependencyProviders));
}

if (cacheContext == null)
{
throw new ArgumentNullException(nameof(cacheContext));
}

if (log == null)
{
throw new ArgumentNullException(nameof(log));
@@ -43,11 +50,14 @@ public RestoreRequest(
PackagesDirectory = dependencyProviders.GlobalPackages.RepositoryRoot;
IsLowercasePackagesDirectory = true;

CacheContext = cacheContext;
Log = log;

DependencyProviders = dependencyProviders;
}

public SourceCacheContext CacheContext { get; set; }

public ILogger Log { get; set; }

/// <summary>
Original file line number Diff line number Diff line change
@@ -5,19 +5,36 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Protocol.Core.Types;

namespace NuGet.DependencyResolver
{
public interface IRemoteDependencyProvider
{
bool IsHttp { get; }

Task<LibraryIdentity> FindLibraryAsync(LibraryRange libraryRange, NuGetFramework targetFramework, CancellationToken cancellationToken);
Task<LibraryIdentity> FindLibraryAsync(
LibraryRange libraryRange,
NuGetFramework targetFramework,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken);

Task<IEnumerable<LibraryDependency>> GetDependenciesAsync(LibraryIdentity match, NuGetFramework targetFramework, CancellationToken cancellationToken);
Task<IEnumerable<LibraryDependency>> GetDependenciesAsync(
LibraryIdentity match,
NuGetFramework targetFramework,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken);

Task CopyToAsync(LibraryIdentity match, Stream stream, CancellationToken cancellationToken);
Task CopyToAsync(
LibraryIdentity match,
Stream stream,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -6,8 +6,10 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Frameworks;
using NuGet.LibraryModel;
using NuGet.Protocol.Core.Types;

namespace NuGet.DependencyResolver
{
@@ -22,7 +24,12 @@ public LocalDependencyProvider(IDependencyProvider dependencyProvider)

public bool IsHttp { get; private set; }

public Task<LibraryIdentity> FindLibraryAsync(LibraryRange libraryRange, NuGetFramework targetFramework, CancellationToken cancellationToken)
public Task<LibraryIdentity> FindLibraryAsync(
LibraryRange libraryRange,
NuGetFramework targetFramework,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken)
{
var library = _dependencyProvider.GetLibrary(libraryRange, targetFramework);

@@ -34,14 +41,24 @@ public Task<LibraryIdentity> FindLibraryAsync(LibraryRange libraryRange, NuGetFr
return Task.FromResult(library.Identity);
}

public Task<IEnumerable<LibraryDependency>> GetDependenciesAsync(LibraryIdentity library, NuGetFramework targetFramework, CancellationToken cancellationToken)
public Task<IEnumerable<LibraryDependency>> GetDependenciesAsync(
LibraryIdentity library,
NuGetFramework targetFramework,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken)
{
var description = _dependencyProvider.GetLibrary(library, targetFramework);

return Task.FromResult(description.Dependencies);
}

public Task CopyToAsync(LibraryIdentity match, Stream stream, CancellationToken cancellationToken)
public Task CopyToAsync(
LibraryIdentity match,
Stream stream,
SourceCacheContext cacheContext,
ILogger logger,
CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
Original file line number Diff line number Diff line change
@@ -354,7 +354,12 @@ private async Task<GraphItem<RemoteResolveResult>> FindLibraryEntry(
else
{
// Look up the dependencies from the source
dependencies = await match.Provider.GetDependenciesAsync(match.Library, framework, cancellationToken);
dependencies = await match.Provider.GetDependenciesAsync(
match.Library,
framework,
_context.CacheContext,
_context.Logger,
cancellationToken);
}

return new GraphItem<RemoteResolveResult>(match.Library)
@@ -553,21 +558,44 @@ private async Task<RemoteMatch> FindLibraryByVersion(LibraryRange libraryRange,
if (libraryRange.VersionRange.IsFloating)
{
// Don't optimize the non http path for floating versions or we'll miss things
return await FindLibrary(libraryRange, providers, provider => provider.FindLibraryAsync(libraryRange, framework, token));
return await FindLibrary(
libraryRange,
providers,
provider => provider.FindLibraryAsync(
libraryRange,
framework,
_context.CacheContext,
_context.Logger,
token));
}

// Try the non http sources first
var nonHttpMatch = await FindLibrary(libraryRange, providers.Where(p => !p.IsHttp), provider => provider.FindLibraryAsync(libraryRange, framework, token));
var nonHttpMatch = await FindLibrary(
libraryRange,
providers.Where(p => !p.IsHttp),
provider => provider.FindLibraryAsync(
libraryRange,
framework,
_context.CacheContext,
_context.Logger,
token));

// If we found an exact match then use it
if (nonHttpMatch != null
&& nonHttpMatch.Library.Version.Equals(libraryRange.VersionRange.MinVersion))
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.FindLibraryAsync(libraryRange, framework, token));
var httpMatch = await FindLibrary(
libraryRange,
providers.Where(p => p.IsHttp),
provider => provider.FindLibraryAsync(
libraryRange,
framework,
_context.CacheContext,
_context.Logger,
token));

// Pick the best match of the 2
if (libraryRange.VersionRange.IsBetter(
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
// 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.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using NuGet.LibraryModel;
using NuGet.Common;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;

namespace NuGet.DependencyResolver
{
public class RemoteWalkContext
{
public RemoteWalkContext()
public RemoteWalkContext(SourceCacheContext cacheContext, ILogger logger)
{
if (cacheContext == null)
{
throw new ArgumentNullException(nameof(cacheContext));
}

if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

CacheContext = cacheContext;
Logger = logger;

ProjectLibraryProviders = new List<IProjectDependencyProvider>();
LocalLibraryProviders = new List<IRemoteDependencyProvider>();
RemoteLibraryProviders = new List<IRemoteDependencyProvider>();
@@ -21,6 +36,9 @@ public RemoteWalkContext()
PackageFileCache = new ConcurrentDictionary<PackageIdentity, IList<string>>(PackageIdentity.Comparer);
}

public SourceCacheContext CacheContext { get; }
public ILogger Logger { get; }

public IList<IProjectDependencyProvider> ProjectLibraryProviders { get; }
public IList<IRemoteDependencyProvider> LocalLibraryProviders { get; }
public IList<IRemoteDependencyProvider> RemoteLibraryProviders { get; }
Loading

0 comments on commit edfc150

Please sign in to comment.