-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from tom-englert/dev/SupportCentralPackageTran…
…sitivePinning Add Features
- Loading branch information
Showing
31 changed files
with
440 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
using NuGetMonitor.Model.Services; | ||
|
||
namespace NuGetMonitor.Model.Abstractions | ||
namespace NuGetMonitor.Model.Abstractions; | ||
|
||
public interface ILoggerSink | ||
{ | ||
public interface ILoggerSink | ||
{ | ||
void Log(LogLevel severity, string message); | ||
} | ||
} | ||
void Log(LogLevel severity, string message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace NuGetMonitor; | ||
namespace NuGetMonitor.Model; | ||
|
||
public static class GlobalConstants | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
global using static NuGetMonitor.GlobalConstants; | ||
global using static NuGetMonitor.Model.GlobalConstants; | ||
global using static NuGetMonitor.Model.Services.LoggerService; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace NuGetMonitor.Models; | ||
namespace NuGetMonitor.Model.Models; | ||
|
||
public sealed record PackageReferenceInfo(PackageInfo PackageInfo, HashSet<PackageReferenceEntry> PackageReferenceEntries); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,73 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Diagnostics; | ||
using Microsoft.Build.Evaluation; | ||
using NuGet.Frameworks; | ||
using NuGetMonitor.Services; | ||
using NuGetMonitor.Model.Services; | ||
using TomsToolbox.Essentials; | ||
|
||
namespace NuGetMonitor.Model.Models | ||
namespace NuGetMonitor.Model.Models; | ||
|
||
[DebuggerDisplay("Project: {Name}, Framework: {TargetFramework}")] | ||
public sealed class ProjectInTargetFramework | ||
{ | ||
public sealed class ProjectInTargetFramework | ||
private static readonly ReadOnlyDictionary<string, ProjectItem> _emptyVersionMap = new(new Dictionary<string, ProjectItem>()); | ||
private static readonly DelegateEqualityComparer<ProjectItem> _itemIncludeComparer = new(item => item?.EvaluatedInclude.ToUpperInvariant()); | ||
|
||
public ProjectInTargetFramework(Project project, NuGetFramework targetFramework) | ||
{ | ||
private static readonly ReadOnlyDictionary<string, ProjectItem> _emptyVersionMap = new(new Dictionary<string, ProjectItem>()); | ||
private static readonly DelegateEqualityComparer<ProjectItem> _itemIncludeComparer = new(item => item?.EvaluatedInclude.ToUpperInvariant()); | ||
Project = project; | ||
TargetFramework = targetFramework; | ||
CentralVersionMap = GetCentralVersionMap(project); | ||
IsTransitivePinningEnabled = IsCentralVersionManagementEnabled && project.GetProperty("CentralPackageTransitivePinningEnabled").IsTrue(); | ||
} | ||
|
||
public ProjectInTargetFramework(Project project, NuGetFramework targetFramework) | ||
{ | ||
Project = project; | ||
TargetFramework = targetFramework; | ||
CentralVersionMap = GetCentralVersionMap(project); | ||
} | ||
public Project Project { get; init; } | ||
|
||
public Project Project { get; init; } | ||
public NuGetFramework TargetFramework { get; init; } | ||
|
||
public NuGetFramework TargetFramework { get; init; } | ||
public ReadOnlyDictionary<string, ProjectItem> CentralVersionMap { get; } | ||
|
||
public ReadOnlyDictionary<string, ProjectItem> CentralVersionMap { get; } | ||
public bool IsCentralVersionManagementEnabled => CentralVersionMap.Count > 0; | ||
|
||
private static ReadOnlyDictionary<string, ProjectItem> GetCentralVersionMap(Project project) | ||
{ | ||
var useCentralPackageManagement = project.GetProperty("ManagePackageVersionsCentrally").IsTrue(); | ||
public bool IsTransitivePinningEnabled { get; } | ||
|
||
public string Name => Path.GetFileName(Project.FullPath); | ||
|
||
public IEnumerable<ProjectInTargetFramework> GetReferencedProjects(IEnumerable<ProjectInTargetFramework> allProjects) | ||
{ | ||
return Project.GetItems("ProjectReference") | ||
.Select(item => item.EvaluatedInclude) | ||
.Select(path => Path.GetFullPath(Path.Combine(Project.DirectoryPath, path))) | ||
.Select(path => GetBestMatch(allProjects, path)) | ||
.ExceptNullItems(); | ||
} | ||
|
||
private static ReadOnlyDictionary<string, ProjectItem> GetCentralVersionMap(Project project) | ||
{ | ||
var useCentralPackageManagement = project.GetProperty("ManagePackageVersionsCentrally").IsTrue(); | ||
|
||
if (!useCentralPackageManagement) | ||
return _emptyVersionMap; | ||
|
||
if (!useCentralPackageManagement) | ||
return _emptyVersionMap; | ||
var versionMap = project | ||
.GetItems("PackageVersion") | ||
.Distinct(_itemIncludeComparer) | ||
.ToDictionary(item => item.EvaluatedInclude, item => item); | ||
|
||
var versionMap = project | ||
.GetItems("PackageVersion") | ||
.Distinct(_itemIncludeComparer) | ||
.ToDictionary(item => item.EvaluatedInclude, item => item); | ||
return new(versionMap); | ||
} | ||
|
||
private ProjectInTargetFramework? GetBestMatch(IEnumerable<ProjectInTargetFramework> projects, string projectPath) | ||
{ | ||
var candidates = projects | ||
.Where(project => string.Equals(project.Project.FullPath, projectPath, StringComparison.OrdinalIgnoreCase)) | ||
.ToArray(); | ||
|
||
return new ReadOnlyDictionary<string, ProjectItem>(versionMap); | ||
} | ||
return candidates.Length switch | ||
{ | ||
0 => null, | ||
1 => candidates[0], | ||
_ => NuGetFrameworkUtility.GetNearest(candidates, TargetFramework, item => item.TargetFramework) ?? candidates[0] | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,5 @@ | ||
using Microsoft.Build.Evaluation; | ||
|
||
namespace NuGetMonitor.Model.Models | ||
{ | ||
public sealed class ProjectItemInTargetFramework | ||
{ | ||
public ProjectItemInTargetFramework(ProjectItem projectItem, ProjectInTargetFramework project) | ||
{ | ||
ProjectItem = projectItem; | ||
Project = project; | ||
} | ||
namespace NuGetMonitor.Model.Models; | ||
|
||
public ProjectItem ProjectItem { get; init; } | ||
|
||
public ProjectInTargetFramework Project { get; } | ||
} | ||
} | ||
public sealed record ProjectItemInTargetFramework(ProjectItem ProjectItem, ProjectInTargetFramework Project); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
using NuGet.Frameworks; | ||
|
||
namespace NuGetMonitor.Models; | ||
namespace NuGetMonitor.Model.Models; | ||
|
||
public sealed record TransitiveDependencies(string ProjectName, string ProjectFullPath, NuGetFramework TargetFramework, IReadOnlyDictionary<PackageInfo, HashSet<PackageInfo>> ParentsByChild); | ||
public sealed record TransitiveDependencies(ProjectInTargetFramework Project, IReadOnlyDictionary<PackageInfo, HashSet<PackageInfo>> ParentsByChild) | ||
{ | ||
public string ProjectName => Path.GetFileName(ProjectFullPath); | ||
|
||
public string ProjectFullPath => Project.Project.FullPath; | ||
|
||
public NuGetFramework TargetFramework => Project.TargetFramework; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,34 @@ | ||
using NuGetMonitor.Model.Abstractions; | ||
|
||
namespace NuGetMonitor.Model.Services | ||
namespace NuGetMonitor.Model.Services; | ||
|
||
public enum LogLevel | ||
{ | ||
public enum LogLevel | ||
{ | ||
Error, | ||
Warning, | ||
Info | ||
} | ||
Error, | ||
Warning, | ||
Info | ||
} | ||
|
||
public static class LoggerService | ||
{ | ||
private static readonly List<ILoggerSink> _sinks = new(); | ||
public static class LoggerService | ||
{ | ||
private static readonly List<ILoggerSink> _sinks = new(); | ||
|
||
public static void Log(string message) | ||
{ | ||
Log(LogLevel.Info, message); | ||
public static void Log(string message) | ||
{ | ||
Log(LogLevel.Info, message); | ||
|
||
} | ||
} | ||
|
||
public static void Log(LogLevel severity, string message) | ||
public static void Log(LogLevel severity, string message) | ||
{ | ||
foreach (var sink in _sinks) | ||
{ | ||
foreach (var sink in _sinks) | ||
{ | ||
sink.Log(severity, message); | ||
} | ||
sink.Log(severity, message); | ||
} | ||
} | ||
|
||
public static void AddSink(ILoggerSink sink) | ||
{ | ||
_sinks.Add(sink); | ||
} | ||
public static void AddSink(ILoggerSink sink) | ||
{ | ||
_sinks.Add(sink); | ||
} | ||
} | ||
} |
Oops, something went wrong.