Skip to content

Commit

Permalink
Merge pull request #29 from 8T4/feature/c4chapr-tools
Browse files Browse the repository at this point in the history
Feature/c4chapr tools
  • Loading branch information
yanjustino authored Dec 20, 2021
2 parents 3899fee + 6005a95 commit 6073e36
Show file tree
Hide file tree
Showing 34 changed files with 391 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ main ]

env:
VERSION: 4.0.${{ github.run_number }}
VERSION: 4.1.${{ github.run_number }}
NUGET_INDEX: https://api.nuget.org/v3/index.json
BUILD_TYPE: Release

Expand Down
2 changes: 1 addition & 1 deletion samples/C4Sharp.Sample/Diagrams/ContainerDiagramBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static ContainerDiagram Build()
{
People.Customer,
Boundary(),
Systems.BankingSystem,
Systems.BankingSystem.AddTags("services"),
Systems.MailSystem,
},
Relationships = new[]
Expand Down
4 changes: 2 additions & 2 deletions samples/C4Sharp.Sample/Diagrams/ContextDiagramBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class ContextDiagramBuilder
{
public static ContextDiagram Build()
{
return new ()
return new ContextDiagram()
{
Title = "System Context diagram for Internet Banking System",
Structures = new Structure[]
Expand All @@ -25,7 +25,7 @@ public static ContextDiagram Build()
},
Relationships = new[]
{
Customer > BankingSystem,
(Customer > BankingSystem).AddTags("error"),
(Customer < MailSystem)["Sends e-mails to"],
(BankingSystem > MailSystem)["Sends e-mails", "SMTP"][Neighbor],
BankingSystem > Mainframe,
Expand Down
6 changes: 3 additions & 3 deletions samples/C4Sharp.Sample/Diagrams/DeploymentDiagramBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public static DeploymentDiagram Build()
(Containers.Spa > Containers.BackendApi)["Makes API calls to", "json/HTTPS"],
(Containers.WebApp > Containers.Spa)["Delivers to the customer's web browser"][Position.Up],
(Containers.BackendApi > Containers.OracleDatabase[1])["Writes to", "JDBC"],
(Containers.BackendApi < Containers.OracleDatabase[2])["Reads from", "JDBC"],
(Containers.OracleDatabase[1] > Containers.OracleDatabase[2])["Replicates data to", "JDBC"][Position.Right],
(Containers.BackendApi < Containers.OracleDatabase["Data Reader"])["Reads from", "JDBC"],
(Containers.OracleDatabase[1] > Containers.OracleDatabase["Data Reader"])["Replicates data to", "JDBC"][Position.Right],
}
};
}
Expand All @@ -39,7 +39,7 @@ private static DeploymentNode BigBankNode()
{
Nodes.Ubuntu("dn", "bigbank-api***\tx8", Nodes.ApacheTomCat("apache", Containers.BackendApi)),
Nodes.Ubuntu("bigbankdb01", "bigbank-db01", Nodes.OracleNode("oracle", Containers.OracleDatabase[1])),
Nodes.Ubuntu("bigbankdb02", "bigbank-db02", Nodes.OracleNode("oracle2", Containers.OracleDatabase[2])),
Nodes.Ubuntu("bigbankdb02", "bigbank-db02", Nodes.OracleNode("oracle2", Containers.OracleDatabase["Data Reader"])),
Nodes.Ubuntu("bb2", "bigbank-web***\tx4", Nodes.ApacheTomCat("apache2", Containers.WebApp)),
}
};
Expand Down
26 changes: 23 additions & 3 deletions samples/C4Sharp.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
using C4Sharp.Diagrams;
using C4Sharp.Models.Plantuml;
using C4Sharp.Models.Plantuml.Constants;
using C4Sharp.Models.Plantuml.IO;
using C4Sharp.Sample.Diagrams;

namespace C4Sharp.Sample
{
internal static class Program
{
private static readonly ElementStyle Style = new ElementStyle()
.UpdateElementStyle(ElementName.ExternalPerson, "#7f3b08", "#7f3b08")
.UpdateElementStyle(ElementName.Person, "#55ACEE", "#55ACEE")
.UpdateElementStyle(ElementName.ExternalSystem, "#3F6684", shape: Shape.RoundedBoxShape);

private static readonly RelationshipTag Reltags = new RelationshipTag()
.AddRelTag("error", "red", "red", LineStyle.DashedLine);

private static readonly ElementTag Tags = new ElementTag()
.AddElementTag("services", "#3F6684", shape: Shape.EightSidedShape);

private static void Main()
{

var diagrams = new Diagram[]
{
ContextDiagramBuilder.Build(),
ContainerDiagramBuilder.Build(),
ContextDiagramBuilder.Build()
.SetStyle(Style)
.SetRelTags(Reltags),

ContainerDiagramBuilder.Build()
.SetTags(Tags),

ComponentDiagramBuilder.Build(),
DeploymentDiagramBuilder.Build(),
EnterpriseDiagramBuilder.Build(),
EnterpriseDiagramBuilder.Build()
.SetStyle(Style),
};

new PlantumlSession()
Expand Down
2 changes: 1 addition & 1 deletion src/C4Sharp/C4Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<RepositoryUrl>https://github.com/8T4/c4sharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>c4, diagrams</PackageTags>
<PackageVersion>4.0.0</PackageVersion>
<PackageVersion>4.1.0</PackageVersion>
<PackageIconUrl>https://github.com/8T4/c4sharp/blob/main/LICENSE</PackageIconUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
26 changes: 16 additions & 10 deletions src/C4Sharp/Diagrams/Diagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ namespace C4Sharp.Diagrams
public abstract record Diagram
{
internal string Name { get; }
public bool LayoutWithLegend { get; set; }
public bool ShowLegend { get; set; }
public bool LayoutAsSketch { get; set; }
public string? Title { get; set; }
public DiagramLayout FlowVisualization { get; set; }
public Structure[] Structures { get; set; }
public Relationship[] Relationships { get; set; }
public bool LayoutWithLegend { get; init; }
public bool ShowLegend { get; init; }
public bool LayoutAsSketch { get; init; }
public string? Title { get; init; }
public DiagramLayout FlowVisualization { get; init; }
public Structure[] Structures { get; init; }
public Relationship[] Relationships { get; init; }
public IElementStyle? Style { get; private init; } = default;
public IElementTag? Tags { get; private init; } = default;
public IRelationshipTag? RelTags { get; private init; } = default;

/// <summary>
/// Constructor
Expand All @@ -34,12 +37,15 @@ protected Diagram(string name)
Structures = Array.Empty<Structure>();
Relationships = Array.Empty<Relationship>();
}


public Diagram SetStyle(IElementStyle style) => this with { Style = style };
public Diagram SetTags(IElementTag tag) => this with { Tags = tag };
public Diagram SetRelTags(IRelationshipTag tag) => this with { RelTags = tag };

/// <summary>
/// Slugfy "title-name"
/// </summary>
/// <returns></returns>
public string Slug() =>
$"{Title}-{Name}".GenerateSlug();
public string Slug() => $"{Title}-{Name}".GenerateSlug();
}
}
9 changes: 9 additions & 0 deletions src/C4Sharp/Diagrams/IDiagramStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace C4Sharp.Diagrams
{
public interface IDiagramStyle
{
public IDictionary<string, string> Items { get; }
}
}
6 changes: 6 additions & 0 deletions src/C4Sharp/Diagrams/IElementStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace C4Sharp.Diagrams
{
public interface IElementStyle: IDiagramStyle
{
}
}
8 changes: 8 additions & 0 deletions src/C4Sharp/Diagrams/IElementTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;

namespace C4Sharp.Diagrams
{
public interface IElementTag: IDiagramStyle
{
}
}
8 changes: 8 additions & 0 deletions src/C4Sharp/Diagrams/IRelationshipTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;

namespace C4Sharp.Diagrams
{
public interface IRelationshipTag: IDiagramStyle
{
}
}
5 changes: 3 additions & 2 deletions src/C4Sharp/Extensions/StringMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ internal static class StringMethods
/// </summary>
/// <see href="https://stackoverflow.com/questions/19335215/what-is-a-slug"/>
/// <param name="phrase">text to slugfy</param>
/// <param name="separator">default separator is hyphens('-')</param>
/// <returns>text with slug style</returns>
internal static string GenerateSlug(this string phrase)
internal static string GenerateSlug(this string phrase, string separator = "-")
{
var str = phrase.RemoveAccent().ToLower(CultureInfo.InvariantCulture);
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
str = Regex.Replace(str, @"\s+", " ").Trim();
str = Regex.Replace(str, @"\s", "-"); // hyphens
str = Regex.Replace(str, @"\s", separator);
return str;
}

Expand Down
2 changes: 0 additions & 2 deletions src/C4Sharp/FileSystem/C4SharpDirectory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using C4Sharp.Extensions;

namespace C4Sharp.FileSystem
{
Expand Down
5 changes: 1 addition & 4 deletions src/C4Sharp/Models/Component.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using C4Sharp.Models.Relationships;

namespace C4Sharp.Models
namespace C4Sharp.Models
{
/// <summary>
/// The word "component" is a hugely overloaded term in the software development industry, but in this context a
Expand Down
31 changes: 17 additions & 14 deletions src/C4Sharp/Models/Container.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using C4Sharp.Extensions;
using C4Sharp.Models.Relationships;

namespace C4Sharp.Models
{
Expand All @@ -17,32 +16,36 @@ namespace C4Sharp.Models
/// </summary>
public sealed record Container(string Alias, string Label) : Structure(Alias, Label)
{
private readonly Dictionary<int, Container> _instances = new();
private readonly Dictionary<string, Container> _instances = new();

public ContainerType ContainerType{ get; init; }
public string? Technology { get; init; }
public Container this[int index] => this.GetInstance(index);
public Container this[int index] => GetInstance($"instance {index}");
public Container this[string instanceName] => GetInstance(instanceName);

/// <summary>
/// Get or Create a instance of current container
/// </summary>
/// <param name="code">instance code</param>
/// <param name="name">instance name</param>
/// <returns>New Container</returns>
private Container GetInstance(int code)
private Container GetInstance(string name)
{
if (_instances.ContainsKey(code))
var key = name.GenerateSlug(".");

if (_instances.ContainsKey(key))
{
return _instances[code];
return _instances[key];
}

var container = new Container($"{Alias}{code}", Label) with
var container = new Container($"{Alias}.{key}", Label)
{
ContainerType = this.ContainerType,
Description = this.Description,
Technology = this.Technology,
Boundary = this.Boundary
ContainerType = ContainerType,
Description = Description,
Technology = Technology,
Boundary = Boundary
};

_instances[code] = container;
_instances[key] = container;
return container;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/C4Sharp/Models/Person.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using C4Sharp.Models.Relationships;

namespace C4Sharp.Models
namespace C4Sharp.Models
{
/// <summary>
/// A person represents one of the human users of your software system (e.g. actors, roles, personas, etc)
Expand Down
16 changes: 16 additions & 0 deletions src/C4Sharp/Models/Plantuml/Constants/ElementName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace C4Sharp.Models.Plantuml.Constants
{
public record ElementName
{
public static ElementName Person => new() { Name = "person" };
public static ElementName ExternalPerson => new() { Name = "external_person"};
public static ElementName System => new() { Name = "system"};
public static ElementName ExternalSystem => new() { Name = "external_system"};
public static ElementName Component => new() { Name = "component"};
public static ElementName ExternalComponent => new() { Name = "external_component"};
public static ElementName Container => new() { Name = "container"};
public static ElementName ExternalContainer => new() { Name = "external_container"};

public string Name { get; private init; } = string.Empty;
}
}
20 changes: 20 additions & 0 deletions src/C4Sharp/Models/Plantuml/Constants/LineStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace C4Sharp.Models.Plantuml.Constants
{
public record LineStyle
{
/// <summary>
/// This call returns the name of the dashed line and can be used as ?lineStyle argument.
/// </summary>
public static LineStyle DashedLine => new() { Value = "DashedLine()" };
/// <summary>
/// This call returns the name of the dotted line and can be used as ?lineStyle argument.
/// </summary>
public static LineStyle DottedLine => new() { Value = "DottedLine()"};
/// <summary>
/// This call returns the name of the bold line and can be used as ?lineStyle argument.
/// </summary>
public static LineStyle BoldLine => new() { Value = "BoldLine()"};

public string Value { get; private init; } = string.Empty;
}
}
16 changes: 16 additions & 0 deletions src/C4Sharp/Models/Plantuml/Constants/Shape.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace C4Sharp.Models.Plantuml.Constants
{
public record Shape
{
/// <summary>
/// This call returns the name of the rounded box shape and can be used as ?shape argument.
/// </summary>
public static Shape RoundedBoxShape => new() { Value = "RoundedBoxShape()" };
/// <summary>
/// This call returns the name of the eight sided shape and can be used as ?shape argument.
/// </summary>
public static Shape EightSidedShape => new() { Value = "EightSidedShape()"};

public string Value { get; private init; } = string.Empty;
}
}
30 changes: 30 additions & 0 deletions src/C4Sharp/Models/Plantuml/ElementStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using C4Sharp.Diagrams;
using C4Sharp.Models.Plantuml.Constants;

namespace C4Sharp.Models.Plantuml
{
public class ElementStyle: IElementStyle
{
public IDictionary<string, string> Items { get; }

public ElementStyle()
{
Items = new Dictionary<string, string>();
}

public ElementStyle UpdateElementStyle(ElementName elementName, string bgColor, string fontColor="#ffffff", string borderColor="#00000000", bool shadowing = false, Shape? shape = null)
{
if (elementName is null)
throw new ArgumentNullException(nameof(elementName), $"{nameof(elementName)} is required");

var value = shape is null
? $"UpdateElementStyle(\"{elementName.Name}\", $bgColor={bgColor}, $fontColor={fontColor}, $borderColor={borderColor}, $shadowing=\"{shadowing}\")"
: $"UpdateElementStyle(\"{elementName.Name}\", $bgColor={bgColor}, $fontColor={fontColor}, $borderColor={borderColor}, $shadowing=\"{shadowing}\", $shape={shape.Value})";

Items[elementName.Name] = value;
return this;
}
}
}
Loading

0 comments on commit 6073e36

Please sign in to comment.