Skip to content

Commit

Permalink
Merge pull request #35 from tom-englert/dev/SupportCentralPackageTran…
Browse files Browse the repository at this point in the history
…sitivePinning

Add Features
  • Loading branch information
sboulema authored Aug 15, 2024
2 parents 996a8ab + 21f87cb commit b6801b8
Show file tree
Hide file tree
Showing 31 changed files with 440 additions and 351 deletions.
11 changes: 5 additions & 6 deletions NuGetMonitor.Model/Abstractions/ILoggerSink.cs
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);
}
2 changes: 1 addition & 1 deletion NuGetMonitor.Model/GlobalConstants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace NuGetMonitor;
namespace NuGetMonitor.Model;

public static class GlobalConstants
{
Expand Down
2 changes: 1 addition & 1 deletion NuGetMonitor.Model/GlobalUsings.cs
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;

4 changes: 2 additions & 2 deletions NuGetMonitor.Model/Models/NugetSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using NuGet.Protocol.Core.Types;
using Settings = NuGet.Configuration.Settings;

namespace NuGetMonitor.Models;
namespace NuGetMonitor.Model.Models;

public sealed class NuGetSession : IDisposable
{
Expand All @@ -22,7 +22,7 @@ public NuGetSession(string? solutionPath)
var sourceRepositories = sourceRepositoryProvider.GetRepositories();

SourceRepositories = sourceRepositories.Select(item => new RepositoryContext(item)).ToArray();
PackageDownloadContext = new PackageDownloadContext(SourceCacheContext);
PackageDownloadContext = new(SourceCacheContext);
}

public MemoryCache Cache { get; } = new(new MemoryCacheOptions());
Expand Down
2 changes: 1 addition & 1 deletion NuGetMonitor.Model/Models/Package.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NuGet.Versioning;

namespace NuGetMonitor.Models;
namespace NuGetMonitor.Model.Models;

public sealed record Package(string Id, ICollection<NuGetVersion> Versions, RepositoryContext RepositoryContext)
{
Expand Down
5 changes: 3 additions & 2 deletions NuGetMonitor.Model/Models/PackageInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Text;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGetMonitor.Model;
using TomsToolbox.Essentials;

namespace NuGetMonitor.Models;
namespace NuGetMonitor.Model.Models;

public sealed class PackageInfo
{
Expand Down Expand Up @@ -40,6 +39,8 @@ public PackageInfo(PackageIdentity packageIdentity, Package package, NuGetSessio

public Uri ProjectUrl { get; }

public bool IsPinned { get; set; }

private IEnumerable<string?> GetIssues()
{
if (IsDeprecated)
Expand Down
4 changes: 2 additions & 2 deletions NuGetMonitor.Model/Models/PackageReference.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using NuGet.Packaging.Core;
using NuGet.Versioning;

namespace NuGetMonitor.Models;
namespace NuGetMonitor.Model.Models;

public sealed record PackageReference(string Id, VersionRange VersionRange)
{
public PackageIdentity? FindBestMatch(IEnumerable<NuGetVersion>? versions)
{
if (NuGetVersion.TryParse(VersionRange.OriginalString, out var simpleVersion))
return new PackageIdentity(Id, simpleVersion);
return new(Id, simpleVersion);

var version = VersionRange.FindBestMatch(versions);

Expand Down
10 changes: 6 additions & 4 deletions NuGetMonitor.Model/Models/PackageReferenceEntry.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using Microsoft.Build.Evaluation;
using NuGet.Versioning;
using NuGetMonitor.Model.Models;

namespace NuGetMonitor.Models;
namespace NuGetMonitor.Model.Models;

public sealed record PackageReferenceEntry
{
public PackageReferenceEntry(string id, VersionRange versionRange, ProjectItem versionSource, ProjectItemInTargetFramework projectItemInTargetFramework, string justification)
public PackageReferenceEntry(string id, VersionRange versionRange, ProjectItem versionSource, ProjectItemInTargetFramework projectItemInTargetFramework, string justification, bool isPrivateAsset)
{
VersionSource = versionSource;
ProjectItemInTargetFramework = projectItemInTargetFramework;
Justification = justification;
Identity = new PackageReference(id, versionRange);
IsPrivateAsset = isPrivateAsset;
Identity = new(id, versionRange);
}

public PackageReference Identity { get; }
Expand All @@ -21,4 +21,6 @@ public PackageReferenceEntry(string id, VersionRange versionRange, ProjectItem v
public ProjectItemInTargetFramework ProjectItemInTargetFramework { get; }

public string Justification { get; }

public bool IsPrivateAsset { get; }
}
2 changes: 1 addition & 1 deletion NuGetMonitor.Model/Models/PackageReferenceInfo.cs
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);
81 changes: 56 additions & 25 deletions NuGetMonitor.Model/Models/ProjectInTargetFramework.cs
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]
};
}
}
16 changes: 2 additions & 14 deletions NuGetMonitor.Model/Models/ProjectItemInTargetFramework.cs
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);
2 changes: 1 addition & 1 deletion NuGetMonitor.Model/Models/RepositoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using NuGetMonitor.Model.Services;
using TomsToolbox.Essentials;

namespace NuGetMonitor.Models;
namespace NuGetMonitor.Model.Models;

public sealed class RepositoryContext
{
Expand Down
11 changes: 9 additions & 2 deletions NuGetMonitor.Model/Models/TransitiveDependencies.cs
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;
}
6 changes: 3 additions & 3 deletions NuGetMonitor.Model/NuGetMonitor.Model.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<PackageReference Include="Microsoft.Build" Version="[17.4.0]" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="[17.4.0]" ExcludeAssets="runtime" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="NuGet.Protocol" Version="6.9.1" />
<PackageReference Include="TomsToolbox.Essentials" Version="2.14.0" />
<PackageReference Include="NuGet.Protocol" Version="6.11.0" />
<PackageReference Include="TomsToolbox.Essentials" Version="2.18.1" />
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.8.0" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.8.1" PrivateAssets="all" />
</ItemGroup>

</Project>
47 changes: 23 additions & 24 deletions NuGetMonitor.Model/Services/LoggerService.cs
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);
}
}
}
Loading

0 comments on commit b6801b8

Please sign in to comment.