Skip to content

Commit

Permalink
Update PlantumlSession.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
yanjustino committed Apr 3, 2021
1 parent bdf04b0 commit 01ea458
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
18 changes: 3 additions & 15 deletions src/C4Sharp/Models/Plantuml/PlantumlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,11 @@ public static void Export(Diagram diagram, string path, PlantumlSession session)
/// <summary>
/// Export Diagram to PNG File
/// </summary>
/// <param name="path">File path</param>
/// <param name="pumlPath">File path</param>
/// <param name="session">Plantuml Session</param>
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);
}

/// <summary>
Expand Down
40 changes: 39 additions & 1 deletion src/C4Sharp/Models/Plantuml/PlantumlSession.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
14 changes: 14 additions & 0 deletions src/C4Sharp/Models/Plantuml/PlantumlSessionResult.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 01ea458

Please sign in to comment.