Skip to content

Commit

Permalink
Add option to include (or not) versions in the dependency graph file
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Mar 12, 2024
1 parent 586127a commit e9fb2d1
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 138 deletions.
1 change: 1 addition & 0 deletions src/Chisel/Chisel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.9.5" PrivateAssets="all" ExcludeAssets="runtime" />
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="all" />
<PackageReference Include="NuGet.ProjectModel" Version="6.9.1" PrivateAssets="all" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
34 changes: 25 additions & 9 deletions src/Chisel/ChiselTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ public class ChiselTask : Task
public string GraphDirection { get; set; } = nameof(Chisel.GraphDirection.LeftToRight);

/// <summary>
/// Writes ignored packages (<c>ChiselIgnores</c>) to the dependency graph in gray. Used for debugging.
/// Include the version numbers in the generated dependency graph file.
/// </summary>
public string WriteIgnoredPackages { get; set; } = "";
public string GraphIncludeVersions { get; set; } = "";

/// <summary>
/// Writes ignored packages (<c>ChiselIgnores</c>) to the dependency graph file in gray. Used for debugging.
/// </summary>
public string GraphWriteIgnoredPackages { get; set; } = "";

/// <summary>
/// The <c>RuntimeCopyLocalItems</c> to remove from the build.
Expand Down Expand Up @@ -154,16 +159,10 @@ public override bool Execute()
var graphPath = Path.Combine(IntermediateOutputPath, Graph);
try
{
if (!Enum.TryParse<GraphDirection>(GraphDirection, ignoreCase: true, out var graphDirection))
{
Log.LogWarning($"The ChiselGraphDirection property ({GraphDirection}) must be either {nameof(Chisel.GraphDirection.LeftToRight)} or {nameof(Chisel.GraphDirection.TopToBottom)}");
}
bool.TryParse(WriteIgnoredPackages, out var writeIgnoredPackages);

using var graphStream = new FileStream(graphPath, FileMode.Create);
using var writer = new StreamWriter(graphStream);
var graphWriter = Path.GetExtension(Graph) is ".mmd" or ".mermaid" ? GraphWriter.Mermaid(writer) : GraphWriter.Graphviz(writer);
graphWriter.Write(graph, graphDirection, writeIgnoredPackages);
graphWriter.Write(graph, GetGraphOptions());
GraphPath = [ new TaskItem(graphPath) ];
}
catch (Exception exception)
Expand All @@ -188,6 +187,23 @@ public override bool Execute()
}
}

private GraphOptions GetGraphOptions()
{
if (!Enum.TryParse<GraphDirection>(GraphDirection, ignoreCase: true, out var direction))
{
Log.LogWarning($"The ChiselGraphDirection property ({GraphDirection}) must be either {nameof(Chisel.GraphDirection.LeftToRight)} or {nameof(Chisel.GraphDirection.TopToBottom)}");
}
bool.TryParse(GraphIncludeVersions, out var includeVersions);
bool.TryParse(GraphWriteIgnoredPackages, out var writeIgnoredPackages);

return new GraphOptions
{
Direction = direction,
IncludeVersions = includeVersions,
WriteIgnoredPackages = writeIgnoredPackages,
};
}

private string NuGetPackageId(ITaskItem item)
{
var packageId = item.GetMetadata("NuGetPackageId");
Expand Down
8 changes: 8 additions & 0 deletions src/Chisel/GraphOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Chisel;

internal struct GraphOptions
{
public required GraphDirection Direction { get; init; }
public required bool IncludeVersions { get; init; }
public required bool WriteIgnoredPackages { get; init; }
}
14 changes: 7 additions & 7 deletions src/Chisel/GraphWriter.Graphviz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ public GraphvizWriter(TextWriter writer) : base(writer)
{
}

protected override void WriteHeader(GraphDirection graphDirection)
protected override void WriteHeader(GraphOptions options)
{
Writer.WriteLine("# Generated by https://github.com/0xced/Chisel");
Writer.WriteLine();
Writer.WriteLine("digraph");
Writer.WriteLine("{");

if (graphDirection == GraphDirection.LeftToRight)
if (options.Direction == GraphDirection.LeftToRight)
Writer.WriteLine(" rankdir=LR");
else if (graphDirection == GraphDirection.TopToBottom)
else if (options.Direction == GraphDirection.TopToBottom)
Writer.WriteLine(" rankdir=TB");

Writer.WriteLine(" node [ fontname = \"Segoe UI, sans-serif\", shape = box, style = filled, color = aquamarine ]");
Expand All @@ -29,9 +29,9 @@ protected override void WriteFooter()
Writer.WriteLine("}");
}

protected override void WriteNode(Package package)
protected override void WriteNode(Package package, GraphOptions options)
{
Writer.Write($" \"{package.Id}\"");
Writer.Write($" \"{GetPackageId(package, options)}\"");
if (package.State == PackageState.Ignore)
{
Writer.Write(" [ color = lightgray ]");
Expand All @@ -52,8 +52,8 @@ protected override void WriteNode(Package package)
Writer.WriteLine();
}

protected override void WriteEdge(Package package, Package dependency)
protected override void WriteEdge(Package package, Package dependency, GraphOptions options)
{
Writer.WriteLine($" \"{package.Id}\" -> \"{dependency.Id}\"");
Writer.WriteLine($" \"{GetPackageId(package, options)}\" -> \"{GetPackageId(dependency, options)}\"");
}
}
14 changes: 7 additions & 7 deletions src/Chisel/GraphWriter.Mermaid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public MermaidWriter(TextWriter writer) : base(writer)
{
}

protected override void WriteHeader(GraphDirection graphDirection)
protected override void WriteHeader(GraphOptions options)
{
Writer.WriteLine("%% Generated by https://github.com/0xced/Chisel");
Writer.WriteLine();
Writer.Write("graph");
if (graphDirection == GraphDirection.LeftToRight)
if (options.Direction == GraphDirection.LeftToRight)
Writer.Write(" LR");
else if (graphDirection == GraphDirection.TopToBottom)
else if (options.Direction == GraphDirection.TopToBottom)
Writer.Write(" TB");
Writer.WriteLine();

Expand All @@ -32,9 +32,9 @@ protected override void WriteFooter()
{
}

protected override void WriteNode(Package package)
protected override void WriteNode(Package package, GraphOptions options)
{
Writer.Write($"class {package.Id} ");
Writer.Write($"class {GetPackageId(package, options)} ");
if (package.State == PackageState.Ignore)
{
Writer.WriteLine("ignored");
Expand All @@ -57,8 +57,8 @@ protected override void WriteNode(Package package)
}
}

protected override void WriteEdge(Package package, Package dependency)
protected override void WriteEdge(Package package, Package dependency, GraphOptions options)
{
Writer.WriteLine($"{package.Id} --> {dependency.Id}");
Writer.WriteLine($"{GetPackageId(package, options)} --> {GetPackageId(dependency, options)}");
}
}
32 changes: 17 additions & 15 deletions src/Chisel/GraphWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,39 @@ internal abstract class GraphWriter

protected GraphWriter(TextWriter writer) => Writer = writer;

public void Write(DependencyGraph graph, GraphDirection graphDirection, bool writeIgnoredPackages)
public void Write(DependencyGraph graph, GraphOptions options)
{
WriteHeader(graphDirection);
WriteEdges(graph, writeIgnoredPackages);
WriteHeader(options);
WriteEdges(graph, options);
Writer.WriteLine();
WriteNodes(graph, writeIgnoredPackages);
WriteNodes(graph, options);
WriteFooter();
}

protected abstract void WriteHeader(GraphDirection graphDirection);
protected abstract void WriteHeader(GraphOptions options);
protected abstract void WriteFooter();
protected abstract void WriteNode(Package package);
protected abstract void WriteEdge(Package package, Package dependency);
protected abstract void WriteNode(Package package, GraphOptions options);
protected abstract void WriteEdge(Package package, Package dependency, GraphOptions options);

private static bool FilterIgnored(Package package, bool writeIgnoredPackages) => writeIgnoredPackages || package.State != PackageState.Ignore;
protected static string GetPackageId(Package package, GraphOptions options) => options.IncludeVersions ? package.Id : package.Name;

private void WriteNodes(DependencyGraph graph, bool writeIgnoredPackages)
private static bool FilterIgnored(Package package, GraphOptions options) => options.WriteIgnoredPackages || package.State != PackageState.Ignore;

private void WriteNodes(DependencyGraph graph, GraphOptions options)
{
foreach (var package in graph.Packages.Where(e => FilterIgnored(e, writeIgnoredPackages)).OrderBy(e => e.Id))
foreach (var package in graph.Packages.Where(e => FilterIgnored(e, options)).OrderBy(e => e.Id))
{
WriteNode(package);
WriteNode(package, options);
}
}

private void WriteEdges(DependencyGraph graph, bool writeIgnoredPackages)
private void WriteEdges(DependencyGraph graph, GraphOptions options)
{
foreach (var (package, dependencies) in graph.Dependencies.Select(e => (e.Key, e.Value)).Where(e => FilterIgnored(e.Key, writeIgnoredPackages)).OrderBy(e => e.Key.Id))
foreach (var (package, dependencies) in graph.Dependencies.Select(e => (e.Key, e.Value)).Where(e => FilterIgnored(e.Key, options)).OrderBy(e => e.Key.Id))
{
foreach (var dependency in dependencies.Where(e => FilterIgnored(e, writeIgnoredPackages)).OrderBy(e => e.Id))
foreach (var dependency in dependencies.Where(e => FilterIgnored(e, options)).OrderBy(e => e.Id))
{
WriteEdge(package, dependency);
WriteEdge(package, dependency, options);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Chisel/build/Chisel.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<ChiselEnabled Condition="$(ChiselEnabled) == ''">true</ChiselEnabled>
<ChiselGraph Condition="$(ChiselGraph) == ''">$(MSBuildProjectName).Chisel.mermaid</ChiselGraph>
<ChiselGraphDirection Condition="$(ChiselGraphDirection) == ''">LeftToRight</ChiselGraphDirection>
<ChiselGraphIncludeVersions Condition="$(ChiselGraphIncludeVersions) == ''">true</ChiselGraphIncludeVersions>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Chisel/build/Chisel.targets
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
ProjectName="$(MSBuildProjectName)"
Graph="$(ChiselGraph)"
GraphDirection="$(ChiselGraphDirection)"
WriteIgnoredPackages="$(ChiselWriteIgnoredPackages)"
GraphIncludeVersions="$(ChiselGraphIncludeVersions)"
GraphWriteIgnoredPackages="$(ChiselGraphWriteIgnoredPackages)"
ChiselPackages="@(ChiselPackages)"
ChiselIgnores="@(ChiselIgnores)"
RuntimeAssemblies="@(RuntimeCopyLocalItems)"
Expand Down
6 changes: 6 additions & 0 deletions src/Chisel/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
"NuGet.DependencyResolver.Core": "6.9.1"
}
},
"PolySharp": {
"type": "Direct",
"requested": "[1.14.1, )",
"resolved": "1.14.1",
"contentHash": "mOOmFYwad3MIOL14VCjj02LljyF1GNw1wP0YVlxtcPvqdxjGGMNdNJJxHptlry3MOd8b40Flm8RPOM8JOlN2sQ=="
},
"Microsoft.Bcl.AsyncInterfaces": {
"type": "Transitive",
"resolved": "7.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ digraph
rankdir=LR
node [ fontname = "Segoe UI, sans-serif", shape = box, style = filled, color = aquamarine ]

"AWSSDK.SecurityToken/3.7.100.14" -> "AWSSDK.Core/3.7.100.14"
"DnsClient/1.6.1" -> "Microsoft.Win32.Registry/5.0.0"
"Microsoft.Extensions.Logging.Abstractions/8.0.0" -> "Microsoft.Extensions.DependencyInjection.Abstractions/8.0.0"
"Microsoft.Win32.Registry/5.0.0" -> "System.Security.AccessControl/5.0.0"
"Microsoft.Win32.Registry/5.0.0" -> "System.Security.Principal.Windows/5.0.0"
"MongoDB.Bson/2.24.0" -> "System.Runtime.CompilerServices.Unsafe/6.0.0"
"MongoDB.Driver.Core/2.24.0" -> "AWSSDK.SecurityToken/3.7.100.14"
"MongoDB.Driver.Core/2.24.0" -> "DnsClient/1.6.1"
"MongoDB.Driver.Core/2.24.0" -> "Microsoft.Extensions.Logging.Abstractions/8.0.0"
"MongoDB.Driver.Core/2.24.0" -> "MongoDB.Bson/2.24.0"
"MongoDB.Driver.Core/2.24.0" -> "MongoDB.Libmongocrypt/1.8.2"
"MongoDB.Driver.Core/2.24.0" -> "SharpCompress/0.30.1"
"MongoDB.Driver.Core/2.24.0" -> "Snappier/1.0.0"
"MongoDB.Driver.Core/2.24.0" -> "ZstdSharp.Port/0.7.3"
"MongoDB.Driver/2.24.0" -> "Microsoft.Extensions.Logging.Abstractions/8.0.0"
"MongoDB.Driver/2.24.0" -> "MongoDB.Bson/2.24.0"
"MongoDB.Driver/2.24.0" -> "MongoDB.Driver.Core/2.24.0"
"MongoDB.Driver/2.24.0" -> "MongoDB.Libmongocrypt/1.8.2"
"System.Security.AccessControl/5.0.0" -> "System.Security.Principal.Windows/5.0.0"
"AWSSDK.SecurityToken" -> "AWSSDK.Core"
"DnsClient" -> "Microsoft.Win32.Registry"
"Microsoft.Extensions.Logging.Abstractions" -> "Microsoft.Extensions.DependencyInjection.Abstractions"
"Microsoft.Win32.Registry" -> "System.Security.AccessControl"
"Microsoft.Win32.Registry" -> "System.Security.Principal.Windows"
"MongoDB.Bson" -> "System.Runtime.CompilerServices.Unsafe"
"MongoDB.Driver.Core" -> "AWSSDK.SecurityToken"
"MongoDB.Driver.Core" -> "DnsClient"
"MongoDB.Driver.Core" -> "Microsoft.Extensions.Logging.Abstractions"
"MongoDB.Driver.Core" -> "MongoDB.Bson"
"MongoDB.Driver.Core" -> "MongoDB.Libmongocrypt"
"MongoDB.Driver.Core" -> "SharpCompress"
"MongoDB.Driver.Core" -> "Snappier"
"MongoDB.Driver.Core" -> "ZstdSharp.Port"
"MongoDB.Driver" -> "Microsoft.Extensions.Logging.Abstractions"
"MongoDB.Driver" -> "MongoDB.Bson"
"MongoDB.Driver" -> "MongoDB.Driver.Core"
"MongoDB.Driver" -> "MongoDB.Libmongocrypt"
"System.Security.AccessControl" -> "System.Security.Principal.Windows"

"AWSSDK.Core/3.7.100.14" [ color = lightcoral ]
"AWSSDK.SecurityToken/3.7.100.14" [ color = lightcoral ]
"DnsClient/1.6.1"
"Microsoft.Extensions.DependencyInjection.Abstractions/8.0.0"
"Microsoft.Extensions.Logging.Abstractions/8.0.0"
"Microsoft.Win32.Registry/5.0.0"
"MongoDB.Bson/2.24.0"
"MongoDB.Driver.Core/2.24.0"
"MongoDB.Driver/2.24.0"
"MongoDB.Libmongocrypt/1.8.2"
"SharpCompress/0.30.1"
"Snappier/1.0.0"
"System.Runtime.CompilerServices.Unsafe/6.0.0"
"System.Security.AccessControl/5.0.0"
"System.Security.Principal.Windows/5.0.0"
"ZstdSharp.Port/0.7.3"
"AWSSDK.Core" [ color = lightcoral ]
"AWSSDK.SecurityToken" [ color = lightcoral ]
"DnsClient"
"Microsoft.Extensions.DependencyInjection.Abstractions"
"Microsoft.Extensions.Logging.Abstractions"
"Microsoft.Win32.Registry"
"MongoDB.Bson"
"MongoDB.Driver.Core"
"MongoDB.Driver"
"MongoDB.Libmongocrypt"
"SharpCompress"
"Snappier"
"System.Runtime.CompilerServices.Unsafe"
"System.Security.AccessControl"
"System.Security.Principal.Windows"
"ZstdSharp.Port"
}
Loading

0 comments on commit e9fb2d1

Please sign in to comment.