diff --git a/src/components/C4Sharp/C4Sharp.csproj b/src/components/C4Sharp/C4Sharp.csproj index 1351aed..4b9735b 100644 --- a/src/components/C4Sharp/C4Sharp.csproj +++ b/src/components/C4Sharp/C4Sharp.csproj @@ -11,7 +11,7 @@ https://github.com/8T4/c4sharp git c4, diagrams - 2.2.1 + 3.1.0 https://github.com/8T4/c4sharp/blob/main/LICENSE true true diff --git a/src/components/C4Sharp/Models/Plantuml/PlantumlFile.cs b/src/components/C4Sharp/Models/Plantuml/PlantumlFile.cs index b4622b0..cbffd6f 100644 --- a/src/components/C4Sharp/Models/Plantuml/PlantumlFile.cs +++ b/src/components/C4Sharp/Models/Plantuml/PlantumlFile.cs @@ -1,10 +1,8 @@ -using System; +using C4Sharp.Diagrams; +using C4Sharp.FileSystem; +using System; using System.Collections.Generic; using System.IO; -using System.Threading; -using System.Threading.Tasks; -using C4Sharp.Diagrams; -using C4Sharp.FileSystem; namespace C4Sharp.Models.Plantuml { @@ -50,8 +48,14 @@ public static void Export(this PlantumlSession session, string path, IEnumerable if (session.GenerateDiagramImages) { - session.Execute(path, true); + session.Execute(path, true, "png"); + } + + if (session.GenerateDiagramSvgImages) + { + session.Execute(path, true, "svg"); } + } /// diff --git a/src/components/C4Sharp/Models/Plantuml/PlantumlSession.cs b/src/components/C4Sharp/Models/Plantuml/PlantumlSession.cs index 71357e5..86da69c 100644 --- a/src/components/C4Sharp/Models/Plantuml/PlantumlSession.cs +++ b/src/components/C4Sharp/Models/Plantuml/PlantumlSession.cs @@ -13,6 +13,7 @@ public class PlantumlSession : IDisposable { public bool StandardLibraryBaseUrl { get; private set; } public bool GenerateDiagramImages { get; private set; } + public bool GenerateDiagramSvgImages { get; private set; } private string FilePath { get; } private ProcessStartInfo ProcessInfo { get; } @@ -71,13 +72,25 @@ public PlantumlSession UseDiagramImageBuilder() return this; } + /// + /// The C4Sharp will generate *.puml files of your diagram. + /// Also, you could save the *.svg files using this method + /// + /// + public PlantumlSession UseDiagramSvgImageBuilder() + { + GenerateDiagramSvgImages = true; + return this; + } + /// /// Execute plantuml.jar /// /// puml files path /// process all *.puml files + /// specifies the format of the generated images /// - internal void Execute(string path, bool processWholeDirectory) + internal void Execute(string path, bool processWholeDirectory, string generatedImageFormat) { try { @@ -91,10 +104,8 @@ internal void Execute(string path, bool processWholeDirectory) } var results = new StringBuilder(); - - var jar = StandardLibraryBaseUrl - ? $"-jar {FilePath} -verbose -o \"{directory}\" -charset UTF-8" - : $"-jar {FilePath} -DRELATIVE_INCLUDE=\".\" -verbose -o \"{directory}\" -charset UTF-8"; + + var jar = CalculateJarCommand(StandardLibraryBaseUrl, generatedImageFormat, directory); ProcessInfo.Arguments = $"{jar} {path}"; ProcessInfo.RedirectStandardOutput = true; @@ -113,6 +124,16 @@ internal void Execute(string path, bool processWholeDirectory) } } + private string CalculateJarCommand(bool useStandardLibrary, string generatedImageFormat, string directory) + { + const string includeLocalFilesArg = "-DRELATIVE_INCLUDE=\".\""; + + var resourcesOriginArg = useStandardLibrary ? string.Empty : includeLocalFilesArg; + var imageFormatOutputArg = string.IsNullOrWhiteSpace(generatedImageFormat) ? string.Empty: $"-t{generatedImageFormat}"; + + return $"-jar {FilePath} {resourcesOriginArg} {imageFormatOutputArg} -verbose -o \"{directory}\" -charset UTF-8"; + } + /// /// 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. diff --git a/src/tests/C4Sharp.IntegratedTests/ExportingDiagramFixture.cs b/src/tests/C4Sharp.IntegratedTests/ExportingDiagramFixture.cs index b92ef44..a0cad0a 100644 --- a/src/tests/C4Sharp.IntegratedTests/ExportingDiagramFixture.cs +++ b/src/tests/C4Sharp.IntegratedTests/ExportingDiagramFixture.cs @@ -56,7 +56,19 @@ protected static void VerifyIfPngFilesExists(string diagramName, string path = " VerifyIfFilesExists(files); } - + + protected static void VerifyIfSvgFilesExists(string diagramName, string path = "c4") + { + var files = new[] + { + Path.Join(path, $"{diagramName}-c4component.svg"), + Path.Join(path, $"{diagramName}-c4context.svg"), + Path.Join(path, $"{diagramName}-c4container.svg"), + Path.Join(path, $"{diagramName}-c4deployment.svg") + }; + + VerifyIfFilesExists(files); + } protected static void VerifyIfPngFilesNonExists(string diagramName, string path = "c4") { var files = new[] @@ -70,6 +82,19 @@ protected static void VerifyIfPngFilesNonExists(string diagramName, string path VerifyIfFilesNonExists(files); } + protected static void VerifyIfSvgFilesNonExists(string diagramName, string path = "c4") + { + var files = new[] + { + Path.Join(path, $"{diagramName}-c4component.svg"), + Path.Join(path, $"{diagramName}-c4context.svg"), + Path.Join(path, $"{diagramName}-c4container.svg"), + Path.Join(path, $"{diagramName}-c4deployment.svg") + }; + + VerifyIfFilesNonExists(files); + } + private static void VerifyIfFilesExists(params string[] path) { foreach (var file in path) diff --git a/src/tests/C4Sharp.IntegratedTests/ExportingDiagramTests.cs b/src/tests/C4Sharp.IntegratedTests/ExportingDiagramTests.cs index e70295d..8abc6f3 100644 --- a/src/tests/C4Sharp.IntegratedTests/ExportingDiagramTests.cs +++ b/src/tests/C4Sharp.IntegratedTests/ExportingDiagramTests.cs @@ -1,3 +1,4 @@ +using System; using System.IO; using C4Sharp.Diagrams; using C4Sharp.IntegratedTests.Stubs.Diagrams; @@ -7,13 +8,16 @@ namespace C4Sharp.IntegratedTests { - public class ExportingDiagramTests: ExportingDiagramFixture + public class ExportingDiagramTests: ExportingDiagramFixture, IDisposable { - [Fact] - public void TestExporteWithoutImages() + public ExportingDiagramTests() { Setup(); - + } + + [Fact] + public void TestExportWithoutImages() + { var diagrams = new Diagram[] { ContextDiagramBuilder.Build() with { Title = "Diagram" }, @@ -28,12 +32,10 @@ public void TestExporteWithoutImages() VerifyIfResourceFilesExists(); VerifyIfPumlFilesExists("diagram"); VerifyIfPngFilesNonExists("diagram"); - - CleanUp(); } [Fact] - public void TestExportToEspecifiedPath() + public void TestExportToSpecifiedPath() { const string path = "c4temp"; Setup(path); @@ -60,10 +62,8 @@ public void TestExportToEspecifiedPath() } [Fact] - public void TestExportToDefaultPath() + public void TestExportOnlyPngToDefaultPath() { - Setup(); - var diagrams = new Diagram[] { ContextDiagramBuilder.Build() with { Title = "Diagram" }, @@ -72,7 +72,6 @@ public void TestExportToDefaultPath() DeploymentDiagramBuilder.Build() with { Title = "Diagram" } }; - new PlantumlSession() .UseDiagramImageBuilder() .Export(diagrams); @@ -80,7 +79,54 @@ public void TestExportToDefaultPath() VerifyIfResourceFilesExists(); VerifyIfPumlFilesExists("diagram"); VerifyIfPngFilesExists("diagram"); - + VerifyIfSvgFilesNonExists("diagram"); + } + + [Fact] + public void TestExportOnlySvgToDefaultPath() + { + var diagrams = new Diagram[] + { + ContextDiagramBuilder.Build() with { Title = "Diagram" }, + ContainerDiagramBuilder.Build() with { Title = "Diagram" }, + ComponentDiagramBuilder.Build() with { Title = "Diagram" }, + DeploymentDiagramBuilder.Build() with { Title = "Diagram" } + }; + + new PlantumlSession() + .UseDiagramSvgImageBuilder() + .Export(diagrams); + + VerifyIfResourceFilesExists(); + VerifyIfPumlFilesExists("diagram"); + VerifyIfPngFilesNonExists("diagram"); + VerifyIfSvgFilesExists("diagram"); + } + + [Fact] + public void TestExportPngAndSvgToDefaultPath() + { + var diagrams = new Diagram[] + { + ContextDiagramBuilder.Build() with { Title = "Diagram" }, + ContainerDiagramBuilder.Build() with { Title = "Diagram" }, + ComponentDiagramBuilder.Build() with { Title = "Diagram" }, + DeploymentDiagramBuilder.Build() with { Title = "Diagram" } + }; + + new PlantumlSession() + .UseDiagramImageBuilder() + .UseDiagramSvgImageBuilder() + .Export(diagrams); + + VerifyIfResourceFilesExists(); + VerifyIfPumlFilesExists("diagram"); + VerifyIfPngFilesExists("diagram"); + VerifyIfSvgFilesExists("diagram"); + } + + public void Dispose() + { CleanUp(); } }