Skip to content

Commit

Permalink
Merge pull request #26 from 8T4/freature/plantumlstream
Browse files Browse the repository at this point in the history
Freature/plantumlstream
  • Loading branch information
yanjustino authored Nov 17, 2021
2 parents 64cd0d8 + 5e0b059 commit 5f0008a
Show file tree
Hide file tree
Showing 23 changed files with 241 additions and 67 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: 3.1.${{ github.run_number }}
VERSION: 3.2.${{ github.run_number }}
NUGET_INDEX: https://api.nuget.org/v3/index.json
BUILD_TYPE: Release

Expand Down
2 changes: 1 addition & 1 deletion src/components/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>3.1.0</PackageVersion>
<PackageVersion>3.2.0</PackageVersion>
<PackageIconUrl>https://github.com/8T4/c4sharp/blob/main/LICENSE</PackageIconUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
10 changes: 1 addition & 9 deletions src/components/C4Sharp/Diagrams/Core/ComponentDiagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,5 @@
/// components are, their responsibilities and the technology/implementation details.
/// <see href="https://c4model.com/#ComponentDiagram"/>
/// </summary>
public record ComponentDiagram: Diagram
{
/// <summary>
/// Constructor
/// </summary>
public ComponentDiagram() : base("C4_Component")
{
}
}
public record ComponentDiagram() : Diagram("C4_Component");
}
10 changes: 1 addition & 9 deletions src/components/C4Sharp/Diagrams/Core/ContainerDiagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,5 @@
/// support/operations staff alike.
/// <see href="https://c4model.com/#ContainerDiagram"/>
/// </summary>
public record ContainerDiagram: Diagram
{
/// <summary>
/// Constructor
/// </summary>
public ContainerDiagram() : base("C4_Container")
{
}
}
public record ContainerDiagram() : Diagram("C4_Container");
}
11 changes: 1 addition & 10 deletions src/components/C4Sharp/Diagrams/Core/ContextDiagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,5 @@
/// by its users and the other systems that it interacts with.
/// <see href="https://c4model.com/#SystemContextDiagram"/>
/// </summary>
public record ContextDiagram: Diagram
{
/// <summary>
/// Constructor
/// </summary>
public ContextDiagram() : base("C4_Context")
{

}
}
public record ContextDiagram() : Diagram("C4_Context");
}
7 changes: 4 additions & 3 deletions src/components/C4Sharp/Diagrams/Diagram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using C4Sharp.Extensions;
using System;
using C4Sharp.Extensions;
using C4Sharp.Models;
using C4Sharp.Models.Relationships;

Expand Down Expand Up @@ -30,8 +31,8 @@ protected Diagram(string name)
ShowLegend = false;
FlowVisualization = DiagramLayout.TopDown;
Name = name;
Structures = default;
Relationships = default;
Structures = Array.Empty<Structure>();
Relationships = Array.Empty<Relationship>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,5 @@ namespace C4Sharp.Diagrams.Supplementary
/// (e.g. a database server, Java EE web/application server, Microsoft IIS), etc. Deployment nodes can be nested.
/// <see href="https://c4model.com/#DeploymentDiagram"/>
/// </summary>
public record DeploymentDiagram : Diagram
{
/// <summary>
/// Constructor
/// </summary>
public DeploymentDiagram() : base("C4_Deployment")
{
}
}
public record DeploymentDiagram() : Diagram("C4_Deployment");
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace C4Sharp.FileSystem
/// <summary>
/// Manipulate the C4 folder and their resoucers
/// </summary>
internal static class C4Directory
internal static class C4SharpDirectory
{
/// <summary>
/// Default Directory Name
Expand All @@ -16,7 +16,7 @@ internal static class C4Directory
/// <summary>
/// Default Resource Folder Name
/// </summary>
public static string ResourcesFolderName => "resources";
public static string ResourcesFolderName => Path.Join("..", ".c4s");

/// <summary>
/// Load all C4_Plantuml files
Expand Down
10 changes: 10 additions & 0 deletions src/components/C4Sharp/Models/EnterpriseBoundary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;

namespace C4Sharp.Models
{
public record EnterpriseBoundary(string Alias, string Label) : Structure(Alias, Label)
{
public IEnumerable<Structure> Structures { get; init; } = Array.Empty<Structure>();
}
}
2 changes: 1 addition & 1 deletion src/components/C4Sharp/Models/Plantuml/PlantumlDiagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static string GetPumlFilePath(this Diagram diagram, bool useUrlInclude)

return useUrlInclude
? $"{standardLibraryBaseUrl}/{pumlFileName}"
: Path.Join(C4Directory.ResourcesFolderName, pumlFileName);
: Path.Join(C4SharpDirectory.ResourcesFolderName, pumlFileName);
}
}
}
14 changes: 10 additions & 4 deletions src/components/C4Sharp/Models/Plantuml/PlantumlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace C4Sharp.Models.Plantuml
/// </summary>
public static class PlantumlFile
{
private static readonly object Lock = new object();

/// <summary>
/// It creates a Puml file into the default directory "./c4"
/// If the attribute of Session GenerateDiagramImages is true
Expand All @@ -21,7 +23,7 @@ public static class PlantumlFile
public static void Export(this PlantumlSession session, IEnumerable<Diagram> diagrams)
{
var dirPath = Directory.GetCurrentDirectory();
var path = Path.Join(dirPath, C4Directory.DirectoryName);
var path = Path.Join(dirPath, C4SharpDirectory.DirectoryName);
Export(session, path, diagrams);
}

Expand Down Expand Up @@ -66,9 +68,13 @@ private static void Save(Diagram diagram, string path, PlantumlSession session)
{
try
{
C4Directory.LoadResources(path);
var filePath = Path.Combine(path, $"{diagram.Slug()}.puml");
File.WriteAllText(filePath, diagram.ToPumlString(session.StandardLibraryBaseUrl));
lock (Lock)
{
C4SharpDirectory.LoadResources(path);
var filePath = Path.Combine(path, $"{diagram.Slug()}.puml");
Directory.CreateDirectory(path);
File.WriteAllText(filePath, diagram.ToPumlString(session.StandardLibraryBaseUrl));
}
}
catch (Exception e)
{
Expand Down
55 changes: 51 additions & 4 deletions src/components/C4Sharp/Models/Plantuml/PlantumlSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ public PlantumlSession UseDiagramSvgImageBuilder()
/// <exception cref="PlantumlException"></exception>
internal void Execute(string path, bool processWholeDirectory, string generatedImageFormat)
{
var directory = processWholeDirectory
? path
: new FileInfo(path)?.Directory?.FullName;

try
{
var directory = processWholeDirectory
? path
: new FileInfo(path)?.Directory?.FullName;

if (string.IsNullOrEmpty(directory))
{
throw new PlantumlException($"{nameof(PlantumlException)}: puml file not found.");
Expand Down Expand Up @@ -134,6 +134,53 @@ private string CalculateJarCommand(bool useStandardLibrary, string generatedImag
return $"-jar {FilePath} {resourcesOriginArg} {imageFormatOutputArg} -verbose -o \"{directory}\" -charset UTF-8";
}

/// <summary>
/// Using the -pipe option, you can easily use PlantUML in your scripts.
/// With this option, a diagram description is received through standard input and the PNG file is generated to standard output.
/// No file is written on the local file system.
/// </summary>
/// <param name="input">puml content</param>
/// <exception cref="PlantumlException"></exception>
internal (string, Stream) GetStream(string input)
{
try
{
var results = new StringBuilder();

var jar = StandardLibraryBaseUrl
? $"-jar {FilePath} -verbose -charset UTF-8"
: $"-jar {FilePath} -DRELATIVE_INCLUDE=\".\" -verbose -charset UTF-8";

var fileName = Guid.NewGuid().ToString("N");

ProcessInfo.Arguments = $"{jar} -pipe > {fileName}.png";
ProcessInfo.RedirectStandardOutput = true;
ProcessInfo.RedirectStandardInput = true;
ProcessInfo.StandardOutputEncoding = Encoding.UTF8;

var process = new Process { StartInfo = ProcessInfo };

process.OutputDataReceived += (p, args) =>
{
results.AppendLine(args.Data);
};

process.Start();
process.StandardInput.Write(input);
process.StandardInput.Flush();
process.StandardInput.Close();
process.BeginOutputReadLine();
process.WaitForExit();

var buffer = Encoding.UTF8.GetBytes(results.ToString());
return (fileName, new MemoryStream(buffer));
}
catch (Exception e)
{
throw new PlantumlException($"{nameof(PlantumlException)}: puml file not found.", e);
}
}

/// <summary>
/// Clear Plantuml Resource
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions src/components/C4Sharp/Models/Plantuml/PlantumlStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.IO;
using C4Sharp.Diagrams;

namespace C4Sharp.Models.Plantuml
{
public static class PlantumlStream
{
private static readonly object Lock = new object();

public static (string, Stream) GetStream(this PlantumlSession session, Diagram diagram)
{
lock (Lock)
{
var puml = diagram.ToPumlString(session.StandardLibraryBaseUrl);
return session.GetStream(puml);
}
}
}
}
22 changes: 21 additions & 1 deletion src/components/C4Sharp/Models/Plantuml/PlantumlStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static string ToPumlString(this Structure structure)
Component component => component.ToPumlString(),
Container container => container.ToPumlString(),
ContainerBoundary containerBoundary => containerBoundary.ToPumlString(),
EnterpriseBoundary enterpriseBoundary => enterpriseBoundary.ToPumlString(),
_ => string.Empty
};
}
Expand Down Expand Up @@ -56,7 +57,26 @@ private static string ToPumlString(this SoftwareSystemBoundary boundary)
stream.AppendLine("}");

return stream.ToString();
}
}

private static string ToPumlString(this EnterpriseBoundary boundary)
{
var stream = new StringBuilder();
stream.AppendLine();
stream.AppendLine($"Enterprise_Boundary({boundary.Alias}, \"{boundary.Label}\") {{");

foreach (var structure in boundary.Structures)
{
if (structure is (Person or SoftwareSystem or EnterpriseBoundary))
{
stream.AppendLine($"{SpaceMethods.Indent()}{structure.ToPumlString()}");
}
}

stream.AppendLine("}");

return stream.ToString();
}

private static string ToPumlString(this Component component)
{
Expand Down
58 changes: 58 additions & 0 deletions src/samples/C4Sharp.Sample/Diagrams/EnterpriseDiagramBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using C4Sharp.Diagrams.Core;
using C4Sharp.Models;
using C4Sharp.Models.Relationships;
using C4Sharp.Sample.Structures;

namespace C4Sharp.Sample.Diagrams
{
using static Position;
using static People;
using static Systems;

public class EnterpriseDiagramBuilder
{
public static ContextDiagram Build()
{
return new ()
{
Title = "System Enterprise diagram for Internet Banking System",
Structures = new Structure[]
{
Customer,
new EnterpriseBoundary("eboundary", "Domain A")
{
Structures = new Structure []
{
BankingSystem,
new EnterpriseBoundary("eboundary1", "Domain Internal Users")
{
Structures = new Structure []
{
InternalCustomer,
}
},
new EnterpriseBoundary("eboundary2", "Domain Managers")
{
Structures = new Structure []
{
Manager,
}
},
}
},
Mainframe,
MailSystem
},
Relationships = new[]
{
Customer > BankingSystem,
InternalCustomer > BankingSystem,
Manager > BankingSystem,
(Customer < MailSystem)["Sends e-mails to"],
(BankingSystem > MailSystem)["Sends e-mails", "SMTP"][Neighbor],
BankingSystem > Mainframe,
}
};
}
}
}
8 changes: 5 additions & 3 deletions src/samples/C4Sharp.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using C4Sharp.Diagrams;
using C4Sharp.Diagrams;
using C4Sharp.Models;
using C4Sharp.Models.Plantuml;
using C4Sharp.Sample.Diagrams;

Expand All @@ -14,11 +14,13 @@ private static void Main(string[] args)
ContextDiagramBuilder.Build(),
ContainerDiagramBuilder.Build(),
ComponentDiagramBuilder.Build(),
DeploymentDiagramBuilder.Build()
DeploymentDiagramBuilder.Build(),
EnterpriseDiagramBuilder.Build(),
};

new PlantumlSession()
.UseDiagramImageBuilder()
.UseDiagramSvgImageBuilder()
.UseStandardLibraryBaseUrl()
.Export(diagrams);
}
Expand Down
2 changes: 1 addition & 1 deletion src/samples/C4Sharp.Sample/Structures/Containers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class Containers
private static Container _webApp;

public static Container WebApp => _webApp ??= new Container(
"WebApp", "WebApp")
"Corporate.Finance.Limits.Service.ServiceBus", "WebApp")
{
ContainerType = ContainerType.WebApplication,
Description = "Delivers the static content and the Internet banking SPA",
Expand Down
Loading

0 comments on commit 5f0008a

Please sign in to comment.