Skip to content

Commit

Permalink
Merge pull request #176 from andredered/Changing_slice_displays_and_f…
Browse files Browse the repository at this point in the history
…ocus_on_mod

added the ability for slices to display nested Namespaces, their structure and attachments.
  • Loading branch information
fgather authored Sep 27, 2022
2 parents a554fc0 + 629cb0b commit d8915f5
Show file tree
Hide file tree
Showing 9 changed files with 635 additions and 73 deletions.
1 change: 1 addition & 0 deletions ArchUnitNET/Domain/PlantUml/Export/GenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class GenerationOptions
public Func<ITypeDependency, bool> DependencyFilter { get; set; }
public bool IncludeDependenciesToOther { get; set; }
public bool IncludeNodesWithoutDependencies { get; set; } = true;
public bool CompactVersion { get; set; } = false;
}
}
108 changes: 104 additions & 4 deletions ArchUnitNET/Domain/PlantUml/Export/PlantUmlDependency.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace ArchUnitNET.Domain.PlantUml.Export
{
Expand All @@ -17,20 +18,111 @@ public PlantUmlDependency(string origin, string target, DependencyType dependenc
DependencyType = dependencyType;
}

public string GetPlantUmlString(RenderOptions renderOptions)
private static int CountOfDots(string str)
{
return str.Count(c => c == '.');
}

public int OriginCountOfDots()
{
return CountOfDots(Origin);
}

public int TargetCountOfDots()
{
return CountOfDots(Target);
}

public string GetPlantUmlString(RenderOptions renderOptions = null)
{
switch (DependencyType)
{
case DependencyType.OneToOne:
return "\"" + Origin + "\"" + " --|> " + "\"" + Target + "\"" + Environment.NewLine;
return "[" + Origin + "]" + " --|> " + "[" + Target + "]" + Environment.NewLine;

case DependencyType.OneToMany:
return "\"" + Origin + "\"" + " \"1\" --|> \"many\" " + "\"" + Target + "\"" + " " +
return "[" + Origin + "]" + " \"1\" --|> \"many\" " + "[" + Target + "]" +
Environment.NewLine;

case DependencyType.OneToPackage:
return "["+ Origin + "] -[#red]> " + GetChildNamespace(Target) + Environment.NewLine;

case DependencyType.PackageToOne:
return GetChildNamespace(Origin) + " -[#blue]> [" + Target + "]" +Environment.NewLine;

case DependencyType.PackageToPackage:
return GetChildNamespace(Origin) + " -[#green]> " + GetChildNamespace(Target) + Environment.NewLine;

case DependencyType.OneToOneCompact:
if (OriginCountOfDots() == TargetCountOfDots())
{
return "[" + Origin + "] --> [" + Target + "]" + Environment.NewLine;
}
return "";

case DependencyType.Circle:
return "[" + Origin + "]" + " <-[#red]> " + "[" + Target + "]" + Environment.NewLine;

case DependencyType.PackageToPackageIfSameParentNamespace:
if (OriginCountOfDots() == TargetCountOfDots() &&
(OriginCountOfDots() == 0 || HaveSameParentNamespace(Origin, Target)))
{
return GetChildNamespace(Origin) + " ..> " + GetChildNamespace(Target) + Environment.NewLine;
}
return "";

case DependencyType.OneToOneIfSameParentNamespace:
if (OriginCountOfDots() == TargetCountOfDots() &&
(OriginCountOfDots() == 0 || HaveSameParentNamespace(Origin, Target))
)
{
return Origin + " --|> " + Target + Environment.NewLine;
}

if (OriginCountOfDots() < TargetCountOfDots())
{
var tmp = GetParentNamespace(Target);
while (OriginCountOfDots() < CountOfDots(tmp))
{
tmp = GetParentNamespace(tmp);
}

if (tmp != Origin && HaveSameParentNamespace(tmp, Origin))
{
return Origin + " --> " + GetChildNamespace(tmp) + Environment.NewLine;
}
}
else
{
var tmp = GetParentNamespace(Origin);
while (CountOfDots(tmp) > TargetCountOfDots())
{
tmp = GetParentNamespace(tmp);
}

if (tmp != Target && HaveSameParentNamespace(tmp, Target))
{
return GetChildNamespace(tmp) + " -> " + Target + Environment.NewLine;
}
}
return "";

case DependencyType.NoDependency:
return "";
}

return "";
}

private static string GetParentNamespace(string ns) =>
ns.Remove(ns.LastIndexOf(".", StringComparison.Ordinal));

private static string GetChildNamespace(string ns) =>
ns.Remove(0,ns.LastIndexOf(".", StringComparison.Ordinal) + 1);

private static bool HaveSameParentNamespace(string origin, string target) =>
(GetParentNamespace(origin) == GetParentNamespace(target));

private bool Equals(PlantUmlDependency other)
{
return Equals(Target, other.Target) && Equals(Origin, other.Origin) &&
Expand Down Expand Up @@ -67,6 +159,14 @@ public override int GetHashCode()
public enum DependencyType
{
OneToOne,
OneToMany
OneToMany,
OneToPackage,
PackageToOne,
PackageToPackage,
OneToOneIfSameParentNamespace,
PackageToPackageIfSameParentNamespace,
OneToOneCompact,
Circle,
NoDependency
}
}
Loading

0 comments on commit d8915f5

Please sign in to comment.