API cleanup

- Removed unneeded code from LibraryManager and ProjectContextBuilder
This commit is contained in:
David Fowler 2015-10-17 07:34:04 -07:00
parent 58de7c4793
commit de116164dd
3 changed files with 40 additions and 201 deletions

View file

@ -84,15 +84,33 @@ namespace Microsoft.Extensions.ProjectModel
var frameworkReferenceResolver = new FrameworkReferenceResolver();
var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
var unresolvedDependencyProvider = new UnresolvedDependencyProvider();
// Resolve the dependencies
ResolveDependencies(libraries, referenceAssemblyDependencyResolver, unresolvedDependencyProvider);
ResolveDependencies(libraries, referenceAssemblyDependencyResolver);
var diagnostics = new List<DiagnosticMessage>();
// REVIEW: Should this be in NuGet (possibly stored in the lock file?)
if (LockFile == null)
{
diagnostics.Add(new DiagnosticMessage(
ErrorCodes.NU1009,
$"The expected lock file doesn't exist. Please run \"dnu restore\" to generate a new lock file.",
Path.Combine(Project.ProjectDirectory, LockFile.FileName),
DiagnosticMessageSeverity.Error));
}
if (!validLockFile)
{
diagnostics.Add(new DiagnosticMessage(
ErrorCodes.NU1006,
$"{lockFileValidationMessage}. Please run \"dnu restore\" to generate a new lock file.",
Path.Combine(Project.ProjectDirectory, LockFile.FileName),
DiagnosticMessageSeverity.Warning));
}
// Create a library manager
var libraryManager = new LibraryManager(Project.ProjectFilePath, TargetFramework, libraries.Values.ToList());
AddLockFileDiagnostics(libraryManager, LockFile != null, validLockFile, lockFileValidationMessage);
var libraryManager = new LibraryManager(Project.ProjectFilePath, TargetFramework, libraries.Values.ToList(), diagnostics);
return new ProjectContext(
GlobalSettings,
@ -103,11 +121,11 @@ namespace Microsoft.Extensions.ProjectModel
libraryManager);
}
private void AddLockFileDiagnostics(LibraryManager libraryManager, bool lockFileExists, bool validLockFile, string lockFileValidationMessage)
private void AddLockFileDiagnostics(List<DiagnosticMessage> diagnostics, bool lockFileExists, bool validLockFile, string lockFileValidationMessage)
{
if (!lockFileExists)
{
libraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
diagnostics.Add(new DiagnosticMessage(
ErrorCodes.NU1009,
$"The expected lock file doesn't exist. Please run \"dnu restore\" to generate a new lock file.",
Path.Combine(Project.ProjectDirectory, LockFile.FileName),
@ -116,7 +134,7 @@ namespace Microsoft.Extensions.ProjectModel
if (!validLockFile)
{
libraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
diagnostics.Add(new DiagnosticMessage(
ErrorCodes.NU1006,
$"{lockFileValidationMessage}. Please run \"dnu restore\" to generate a new lock file.",
Path.Combine(Project.ProjectDirectory, LockFile.FileName),
@ -124,7 +142,7 @@ namespace Microsoft.Extensions.ProjectModel
}
}
private void ResolveDependencies(Dictionary<string, LibraryDescription> libraries, ReferenceAssemblyDependencyResolver referenceAssemblyDependencyResolver, UnresolvedDependencyProvider unresolvedDependencyProvider)
private void ResolveDependencies(Dictionary<string, LibraryDescription> libraries, ReferenceAssemblyDependencyResolver referenceAssemblyDependencyResolver)
{
foreach (var library in libraries.Values.ToList())
{
@ -144,14 +162,14 @@ namespace Microsoft.Extensions.ProjectModel
if (Equals(LibraryType.ReferenceAssembly, dependency.Target))
{
dep = referenceAssemblyDependencyResolver.GetDescription(dependency, TargetFramework) ??
unresolvedDependencyProvider.GetDescription(dependency, TargetFramework);
UnresolvedDependencyProvider.GetDescription(dependency, TargetFramework);
dep.Framework = TargetFramework;
libraries[dependency.Name] = dep;
}
else
{
dep = unresolvedDependencyProvider.GetDescription(dependency, TargetFramework);
dep = UnresolvedDependencyProvider.GetDescription(dependency, TargetFramework);
libraries[dependency.Name] = dep;
}
}

View file

@ -1,11 +1,8 @@
// 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.IO;
using System.Linq;
using System.Runtime.Versioning;
using NuGet.Frameworks;
using NuGet.Versioning;
@ -13,76 +10,25 @@ namespace Microsoft.Extensions.ProjectModel.Resolution
{
public class LibraryManager
{
private IList<LibraryDescription> _libraries;
private IList<DiagnosticMessage> _diagnostics;
private readonly object _initializeLock = new object();
private Dictionary<string, IEnumerable<LibraryDescription>> _inverse;
private Dictionary<string, LibraryDescription> _graph;
private readonly IList<LibraryDescription> _libraries;
private readonly IList<DiagnosticMessage> _diagnostics;
private readonly string _projectPath;
private readonly NuGetFramework _targetFramework;
public LibraryManager(string projectPath, NuGetFramework targetFramework, IList<LibraryDescription> libraries)
public LibraryManager(string projectPath,
NuGetFramework targetFramework,
IList<LibraryDescription> libraries,
IList<DiagnosticMessage> diagnostics)
{
_projectPath = projectPath;
_targetFramework = targetFramework;
_libraries = libraries;
_diagnostics = diagnostics;
}
public void AddGlobalDiagnostics(DiagnosticMessage message)
public IList<LibraryDescription> GetLibraries()
{
if (_diagnostics == null)
{
_diagnostics = new List<DiagnosticMessage>();
}
_diagnostics.Add(message);
}
private Dictionary<string, LibraryDescription> Graph
{
get
{
EnsureGraph();
return _graph;
}
}
private Dictionary<string, IEnumerable<LibraryDescription>> InverseGraph
{
get
{
EnsureInverseGraph();
return _inverse;
}
}
public IEnumerable<LibraryDescription> GetReferencingLibraries(string name)
{
IEnumerable<LibraryDescription> libraries;
if (InverseGraph.TryGetValue(name, out libraries))
{
return libraries;
}
return Enumerable.Empty<LibraryDescription>();
}
public LibraryDescription GetLibrary(string name)
{
LibraryDescription library;
if (Graph.TryGetValue(name, out library))
{
return library;
}
return null;
}
public IEnumerable<LibraryDescription> GetLibraries()
{
EnsureGraph();
return _graph.Values;
return _libraries;
}
public IList<DiagnosticMessage> GetAllDiagnostics()
@ -163,130 +109,5 @@ namespace Microsoft.Extensions.ProjectModel.Resolution
return messages;
}
private void EnsureGraph()
{
lock (_initializeLock)
{
if (_graph == null)
{
_graph = _libraries.ToDictionary(l => l.Identity.Name, StringComparer.Ordinal);
_libraries = null;
}
}
}
private void EnsureInverseGraph()
{
EnsureGraph();
lock (_initializeLock)
{
if (_inverse == null)
{
BuildInverseGraph();
}
}
}
private void BuildInverseGraph()
{
var firstLevelLookups = new Dictionary<string, List<LibraryDescription>>(StringComparer.OrdinalIgnoreCase);
var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var item in _graph.Values)
{
Visit(item, firstLevelLookups, visited);
}
_inverse = new Dictionary<string, IEnumerable<LibraryDescription>>(StringComparer.OrdinalIgnoreCase);
// Flatten the graph
foreach (var item in _graph.Values)
{
Flatten(item, firstLevelLookups: firstLevelLookups);
}
}
private void Visit(LibraryDescription item,
Dictionary<string, List<LibraryDescription>> inverse,
HashSet<string> visited)
{
if (!visited.Add(item.Identity.Name))
{
return;
}
foreach (var dependency in item.Dependencies)
{
List<LibraryDescription> dependents;
if (!inverse.TryGetValue(dependency.Name, out dependents))
{
dependents = new List<LibraryDescription>();
inverse[dependency.Name] = dependents;
}
dependents.Add(item);
Visit(_graph[dependency.Name], inverse, visited);
}
}
private void Flatten(LibraryDescription info,
Dictionary<string, List<LibraryDescription>> firstLevelLookups,
HashSet<LibraryDescription> parentDependents = null)
{
IEnumerable<LibraryDescription> libraryDependents;
if (!_inverse.TryGetValue(info.Identity.Name, out libraryDependents))
{
List<LibraryDescription> firstLevelDependents;
if (firstLevelLookups.TryGetValue(info.Identity.Name, out firstLevelDependents))
{
var allDependents = new HashSet<LibraryDescription>(LibraryDescriptionComparer.Instance);
foreach (var dependent in firstLevelDependents)
{
allDependents.Add(dependent);
Flatten(dependent, firstLevelLookups, allDependents);
}
libraryDependents = allDependents;
}
else
{
libraryDependents = Enumerable.Empty<LibraryDescription>();
}
_inverse[info.Identity.Name] = libraryDependents;
}
AddRange(parentDependents, libraryDependents);
}
private static Func<IEnumerable<LibraryDescription>> GetLibraryInfoThunk(IEnumerable<LibraryDescription> libraries)
{
return () => libraries;
}
private static void AddRange(HashSet<LibraryDescription> source, IEnumerable<LibraryDescription> values)
{
if (source != null)
{
foreach (var value in values)
{
source.Add(value);
}
}
}
private class LibraryDescriptionComparer : IEqualityComparer<LibraryDescription>
{
public static readonly LibraryDescriptionComparer Instance = new LibraryDescriptionComparer();
private LibraryDescriptionComparer() { }
public bool Equals(LibraryDescription x, LibraryDescription y)
{
return string.Equals(x.Identity.Name, y.Identity.Name, StringComparison.OrdinalIgnoreCase);
}
public int GetHashCode(LibraryDescription obj)
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Identity.Name);
}
}
}
}

View file

@ -7,9 +7,9 @@ using NuGet.Frameworks;
namespace Microsoft.Extensions.ProjectModel.Resolution
{
public class UnresolvedDependencyProvider
public static class UnresolvedDependencyProvider
{
public LibraryDescription GetDescription(LibraryRange libraryRange, NuGetFramework targetFramework)
public static LibraryDescription GetDescription(LibraryRange libraryRange, NuGetFramework targetFramework)
{
return new LibraryDescription(
libraryRange,