Skip to content

Commit

Permalink
add cli tool
Browse files Browse the repository at this point in the history
  • Loading branch information
LiorBanai committed Jul 13, 2024
1 parent e50783c commit 0a1c5d4
Show file tree
Hide file tree
Showing 50 changed files with 978 additions and 1 deletion.
8 changes: 7 additions & 1 deletion NugetsDependency.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetsDependency", "src\NugetsDependency.csproj", "{E0CD7CAC-2BFD-47A0-BE94-1E04FED408B4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NugetsDependency", "src\NugetsDependency.Winforms\NugetsDependency.csproj", "{E0CD7CAC-2BFD-47A0-BE94-1E04FED408B4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NugetsDependencyGraph.CLI", "src\NugetsDependency.CLI\NugetsDependencyGraph.CLI.csproj", "{C876FF0C-9763-4E95-9EBC-A5D30E8B6310}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,6 +25,10 @@ Global
{E0CD7CAC-2BFD-47A0-BE94-1E04FED408B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0CD7CAC-2BFD-47A0-BE94-1E04FED408B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0CD7CAC-2BFD-47A0-BE94-1E04FED408B4}.Release|Any CPU.Build.0 = Release|Any CPU
{C876FF0C-9763-4E95-9EBC-A5D30E8B6310}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C876FF0C-9763-4E95-9EBC-A5D30E8B6310}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C876FF0C-9763-4E95-9EBC-A5D30E8B6310}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C876FF0C-9763-4E95-9EBC-A5D30E8B6310}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
98 changes: 98 additions & 0 deletions src/NugetsDependency.CLI/GraphGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Microsoft.Msagl.Drawing;
using Microsoft.Msagl.Layout.MDS;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NugetsDependencyGraph.CLI
{
internal class GraphGenerator
{
public string FileName { get; set; }

public GraphGenerator(string fileName)
{
FileName = fileName;
}

public void Generate(string targetFrameworks, string showOnlyDependenciesWithName, string limitToSingleProject, string colorSpecificDependency, string outputFolder)
{
if (!File.Exists(FileName))
{
Console.WriteLine("File does not exist");
return;
}
Graph graph = new Graph("graph");
var json = File.ReadAllText(FileName);


var jsonObject = JObject.Parse(json);
var targets = jsonObject["targets"][targetFrameworks] as IDictionary<string, JToken>;
if (targets == null)
{
Console.WriteLine("Target does not exists");
return;
}
foreach (var target in targets)
{
var nameVersion = target.Key.ToLower().Replace("/", ": ");
if (!string.IsNullOrEmpty(showOnlyDependenciesWithName) && !nameVersion.Contains(showOnlyDependenciesWithName,
StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
Node node = null;
if (string.IsNullOrEmpty(limitToSingleProject))
{
node = graph.AddNode(nameVersion);
}
else if (nameVersion.StartsWith(limitToSingleProject.ToLower()))
{
node = graph.AddNode(nameVersion);
node.Label.FontColor = Microsoft.Msagl.Drawing.Color.Green;
node.Label.FontSize += 10;
}

if (!string.IsNullOrEmpty(colorSpecificDependency) && nameVersion.StartsWith(colorSpecificDependency.ToLower()) && node is not null)
{
node.Label.FontColor = Microsoft.Msagl.Drawing.Color.DarkRed;
node.Label.FontSize += 5;
}
var deps = target.Value["dependencies"] as IDictionary<string, JToken>;
if (deps is null || node is null)
{
continue;
}
foreach (var dep in deps)
{
var dependent = dep.Key.ToLower() + ": " + (dep.Value as Newtonsoft.Json.Linq.JValue).Value;
if (!string.IsNullOrEmpty(showOnlyDependenciesWithName) && !dependent.Contains(showOnlyDependenciesWithName, StringComparison.InvariantCultureIgnoreCase))
{
continue;
}
graph.AddNode(dependent);
var edge = graph.AddEdge(nameVersion, dependent);
edge.Attr.ArrowheadLength = 10;
}
}
//bind the graph to the viewer
graph.Attr.LayerDirection = LayerDirection.None;
graph.Attr.OptimizeLabelPositions = true;
graph.CreateLayoutSettings();
graph.LayoutAlgorithmSettings = new MdsLayoutSettings();

var renderer = new Microsoft.Msagl.GraphViewerGdi.GraphRenderer(graph);
renderer.CalculateLayout();

var image = new Bitmap((int)(graph.Width * 2), (int)(graph.Height * 2));
renderer.Render(image);
string outputFileName = Path.Join(outputFolder, "NugetDependencyGraphOutput.png");
image.Save(outputFileName, System.Drawing.Imaging.ImageFormat.Png);
image.Dispose();
}
}
}
35 changes: 35 additions & 0 deletions src/NugetsDependency.CLI/NugetsDependencyGraph.CLI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<PackAsTool>true</PackAsTool>
<ToolCommandName>NugetsDependencyGraph</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<TargetFrameworks>net8.0-windows</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>NugetsDependencyGraph.CLI</RootNamespace>
<AssemblyName>NugetsDependencyGraph.CLI</AssemblyName>
<PackageId>NugetsDependencyGraph.CLI</PackageId>
<Product>NugetsDependencyGraph</Product>
<VersionPrefix>0.0.1.0</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Description>cli tool fror creating nuget dependency graph</Description>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageProjectUrl>https://github.com/LiorBanai/Nugets-Dependency-Graph</PackageProjectUrl>
<RepositoryUrl>https://github.com/LiorBanai/Nugets-Dependency-Graph</RepositoryUrl>
<Authors>Lior Banai</Authors>
<Copyright>Lior Banai @ 2024</Copyright>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Msagl" Version="1.1.6" />
<PackageReference Include="Microsoft.Msagl.Drawing" Version="1.1.6" />
<PackageReference Include="Microsoft.Msagl.GraphViewerGDI" Version="1.1.7" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/NugetsDependency.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace NugetsDependencyGraph.CLI
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("generating graph");
var filename =args[0];
var targetFramework = args[1];//"net8.0-windows7.0";
GraphGenerator graph = new GraphGenerator(filename);
graph.Generate(targetFramework,"","","",Environment.CurrentDirectory);
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"NugetDependency/1.0.0": {
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"Microsoft.Msagl": "1.1.6",
"Microsoft.Msagl.Drawing": "1.1.6",
"Microsoft.Msagl.GraphViewerGDI": "1.1.7",
"Newtonsoft.Json": "13.0.3",
"System.Data.DataSetExtensions": "4.5.0"
},
"runtime": {
"NugetDependency.dll": {}
}
},
"Microsoft.CSharp/4.7.0": {},
"Microsoft.Msagl/1.1.6": {
"runtime": {
"lib/netstandard2.0/Microsoft.Msagl.dll": {
"assemblyVersion": "0.0.0.0",
"fileVersion": "0.0.0.0"
}
}
},
"Microsoft.Msagl.Drawing/1.1.6": {
"dependencies": {
"Microsoft.Msagl": "1.1.6"
},
"runtime": {
"lib/netstandard2.0/Microsoft.Msagl.Drawing.dll": {
"assemblyVersion": "0.0.0.0",
"fileVersion": "0.0.0.0"
}
}
},
"Microsoft.Msagl.GraphViewerGDI/1.1.7": {
"dependencies": {
"Microsoft.Msagl.Drawing": "1.1.6"
},
"runtime": {
"lib/net7.0-windows7.0/Microsoft.Msagl.GraphViewerGdi.dll": {
"assemblyVersion": "0.0.0.0",
"fileVersion": "0.0.0.0"
}
}
},
"Newtonsoft.Json/13.0.3": {
"runtime": {
"lib/net6.0/Newtonsoft.Json.dll": {
"assemblyVersion": "13.0.0.0",
"fileVersion": "13.0.3.27908"
}
}
},
"System.Data.DataSetExtensions/4.5.0": {}
}
},
"libraries": {
"NugetDependency/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"Microsoft.CSharp/4.7.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-pTj+D3uJWyN3My70i2Hqo+OXixq3Os2D1nJ2x92FFo6sk8fYS1m1WLNTs0Dc1uPaViH0YvEEwvzddQ7y4rhXmA==",
"path": "microsoft.csharp/4.7.0",
"hashPath": "microsoft.csharp.4.7.0.nupkg.sha512"
},
"Microsoft.Msagl/1.1.6": {
"type": "package",
"serviceable": true,
"sha512": "sha512-Fqxj+pmZEo/H2z/LEMFSmtt6GrSj2FKZfz15HhTQRhYRpdewFaC5mPIUxkzELJTac/hbUDb28QBjG/w0JbG2wg==",
"path": "microsoft.msagl/1.1.6",
"hashPath": "microsoft.msagl.1.1.6.nupkg.sha512"
},
"Microsoft.Msagl.Drawing/1.1.6": {
"type": "package",
"serviceable": true,
"sha512": "sha512-xKEOHGDEzf1ulz7GISBxnjsqSl5+YhMhZ1I/UU7GMUkFtrwuShVg+irAZiZeK9VQRePXWeFWVhBlxrc3XulXNQ==",
"path": "microsoft.msagl.drawing/1.1.6",
"hashPath": "microsoft.msagl.drawing.1.1.6.nupkg.sha512"
},
"Microsoft.Msagl.GraphViewerGDI/1.1.7": {
"type": "package",
"serviceable": true,
"sha512": "sha512-av6SIZxaNILJ2r0ALvRBppOPYNCkGr2OQTx6XhwpB4rwrHtvKpcP5Olj9Qj4Yq6ijthKkmmkhHkDk6MHfpqbtA==",
"path": "microsoft.msagl.graphviewergdi/1.1.7",
"hashPath": "microsoft.msagl.graphviewergdi.1.1.7.nupkg.sha512"
},
"Newtonsoft.Json/13.0.3": {
"type": "package",
"serviceable": true,
"sha512": "sha512-HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ==",
"path": "newtonsoft.json/13.0.3",
"hashPath": "newtonsoft.json.13.0.3.nupkg.sha512"
},
"System.Data.DataSetExtensions/4.5.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-221clPs1445HkTBZPL+K9sDBdJRB8UN8rgjO3ztB0CQ26z//fmJXtlsr6whGatscsKGBrhJl5bwJuKSA8mwFOw==",
"path": "system.data.datasetextensions/4.5.0",
"hashPath": "system.data.datasetextensions.4.5.0.nupkg.sha512"
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
</startup>
</configuration>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"frameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
{
"name": "Microsoft.WindowsDesktop.App",
"version": "8.0.0"
}
],
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
Binary file not shown.
Loading

0 comments on commit 0a1c5d4

Please sign in to comment.