From 01ea45857b160294c6c0bcec47566ab3908aa0d0 Mon Sep 17 00:00:00 2001 From: yanjustino Date: Sat, 3 Apr 2021 14:17:10 -0300 Subject: [PATCH] Update PlantumlSession.cs --- src/C4Sharp/Models/Plantuml/PlantumlFile.cs | 18 ++------- .../Models/Plantuml/PlantumlSession.cs | 40 ++++++++++++++++++- .../Models/Plantuml/PlantumlSessionResult.cs | 14 +++++++ 3 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 src/C4Sharp/Models/Plantuml/PlantumlSessionResult.cs diff --git a/src/C4Sharp/Models/Plantuml/PlantumlFile.cs b/src/C4Sharp/Models/Plantuml/PlantumlFile.cs index 8a6eb9d..586c829 100644 --- a/src/C4Sharp/Models/Plantuml/PlantumlFile.cs +++ b/src/C4Sharp/Models/Plantuml/PlantumlFile.cs @@ -122,23 +122,11 @@ public static void Export(Diagram diagram, string path, PlantumlSession session) /// /// Export Diagram to PNG File /// - /// File path + /// File path /// Plantuml Session - private static void Export(string path, PlantumlSession session) + private static void Export(string pumlPath, PlantumlSession session) { - var jarPath = session.FilePath; - var directory = new FileInfo(path); - - var jar = $"-jar {jarPath} -verbose -o \"{directory.Directory.FullName}\" -charset UTF-8"; - - var info = new ProcessStartInfo - { - UseShellExecute = false, - FileName = "java", - Arguments = $"{jar} {path}" - }; - - Process.Start(info)?.WaitForExit(); + session.Execute(pumlPath); } /// diff --git a/src/C4Sharp/Models/Plantuml/PlantumlSession.cs b/src/C4Sharp/Models/Plantuml/PlantumlSession.cs index 346dd14..6179daf 100644 --- a/src/C4Sharp/Models/Plantuml/PlantumlSession.cs +++ b/src/C4Sharp/Models/Plantuml/PlantumlSession.cs @@ -1,15 +1,53 @@ using System; +using System.Diagnostics; +using System.IO; +using System.Text; using C4Sharp.Models.Diagrams; namespace C4Sharp.Models.Plantuml { public class PlantumlSession: IDisposable { - public string FilePath { get; } + private string FilePath { get; } + private ProcessStartInfo ProcessInfo { get; } public PlantumlSession() { FilePath = PlantumlStream.LoadPlantUmlEngine(); + ProcessInfo = new ProcessStartInfo + { + UseShellExecute = false, + FileName = "java", + }; + } + + public PlantumlSessionResult Execute(string puml) + { + var directory = new FileInfo(puml); + + if (directory.Directory != null) + { + var results = new StringBuilder(); + + var jar = $"-jar {FilePath} -verbose -o \"{directory.Directory.FullName}\" -charset UTF-8"; + ProcessInfo.Arguments = $"{jar} {puml}"; + ProcessInfo.RedirectStandardOutput = true; + ProcessInfo.StandardOutputEncoding = Encoding.UTF8; + + var process = new Process {StartInfo = ProcessInfo}; + + process.OutputDataReceived += (sender, args) => + { + results.AppendLine(args.Data); + }; + + process.Start(); + process.WaitForExit(); + + return new PlantumlSessionResult(process.ExitCode == 0, results.ToString()); + } + + return new PlantumlSessionResult(false, "Plantuml file not found"); } public void Dispose() diff --git a/src/C4Sharp/Models/Plantuml/PlantumlSessionResult.cs b/src/C4Sharp/Models/Plantuml/PlantumlSessionResult.cs new file mode 100644 index 0000000..9ca68bb --- /dev/null +++ b/src/C4Sharp/Models/Plantuml/PlantumlSessionResult.cs @@ -0,0 +1,14 @@ +namespace C4Sharp.Models.Plantuml +{ + public class PlantumlSessionResult + { + public bool Success { get; private set; } + public string Messages { get; private set; } + + public PlantumlSessionResult(bool success, string messages) + { + Success = success; + Messages = messages; + } + } +} \ No newline at end of file