Skip to content

Commit

Permalink
Allow custom namespace grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
maisiesadler committed Aug 4, 2021
1 parent dd6e9a2 commit a560151
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
30 changes: 30 additions & 0 deletions src/Lively.Diagrams.Tests/FullNamePlantUmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,31 @@ class Lively.Diagrams.Tests.FullNamePlantUmlTests.ExampleImplementationWithMetho
Lively.Diagrams.Tests.FullNamePlantUmlTests.ExampleInterfaceWithMethods <--- Lively.Diagrams.Tests.FullNamePlantUmlTests.ExampleImplementationWithMethods
@enduml";

Assert.Equal(Normalise(expected), Normalise(diagram));
}

[Fact]
public void CanApplyCustomNamespaceGrouping()
{
var assembly = this.GetType().Assembly;
var config = new DependencyTreeConfig(assembly);
var fullTypeName = "Lively.Diagrams.Tests.FullNamePlantUmlTests+ExampleTypeReferencingOtherAssemblies";

var tree = new DependencyTree(config);
var depTree = tree.GetDependencies(fullTypeName);
var diagram = FullNamePlantUml.Create(new[] { depTree }, new[] { "Lively.Diagrams.Tests", "Lively.Diagrams" });

var expected = @"@startuml
class Lively.Diagrams.Tests.FullNamePlantUmlTests_ExampleTypeReferencingOtherAssemblies {
}
class Lively.Diagrams.FullNamePlantUml {
}
Lively.Diagrams.Tests.FullNamePlantUmlTests_ExampleTypeReferencingOtherAssemblies ---> Lively.Diagrams.FullNamePlantUml
@enduml";

Assert.Equal(Normalise(expected), Normalise(diagram));
Expand Down Expand Up @@ -207,6 +232,11 @@ public void One() { }
public string Beans() => "hello";
}

public class ExampleTypeReferencingOtherAssemblies
{
public ExampleTypeReferencingOtherAssemblies(FullNamePlantUml fullNamePlantUml) { }
}

public class Startup
{
public void ConfigureServices(IServiceCollection services)
Expand Down
25 changes: 21 additions & 4 deletions src/Lively.Diagrams/FullNamePlantUml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
using System.Collections.Generic;
using Lively.TypeDescriptions;

namespace Lively.Diagrams
{
public class FullNamePlantUml
{
public static string Create(IList<DependencyTreeNode> nodes)
public static string Create(IList<DependencyTreeNode> nodes, string[] namespaceMatches = null)
{
var renderer = new PlantUmlRenderer(type => type.FullName);
return renderer.Create(nodes);
var renderer = new PlantUmlRenderer(type =>
{
if (namespaceMatches != null)
{
foreach (var ns in namespaceMatches)
{
if (type.FullName.StartsWith(ns + "."))
return OverrideNamespaceGrouping(ns, type.FullName);
}
}

return type.FullName;
});
return renderer.Create(nodes);
}

private static string OverrideNamespaceGrouping(string @namespace, string fullName)
{
var end = fullName.Remove(0, @namespace.Length + 1);
return $"{@namespace}.{end.Replace('.', '_').Replace('+', '_')}";
}
}
}
1 change: 0 additions & 1 deletion src/Lively.Diagrams/PlantUml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Lively.TypeDescriptions;

namespace Lively.Diagrams
{
Expand Down

0 comments on commit a560151

Please sign in to comment.