Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Java/JarRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public JarRunner(string javaPath, string jarPath)
this.jarPath = jarPath;
}

public async Task<IProcessResult> RunJarWithInputAsync(string input, CancellationToken cancellationToken,
params string[] arguments)
public async Task<IProcessResult> RunJarWithInputAsync(string input, string workingDirectory,
CancellationToken cancellationToken, params string[] arguments)
{
var argumentString = $"-Dfile.encoding=UTF-8 -jar \"{jarPath}\" {string.Join(" ", arguments)}";
return await new ProcessHelper().RunProcessWithInputAsync(javaPath, argumentString, input, cancellationToken)
.ConfigureAwait(false);
return await new ProcessHelper().RunProcessWithInputAsync(javaPath, argumentString,
input, workingDirectory, cancellationToken).ConfigureAwait(false);
}
}
}
14 changes: 13 additions & 1 deletion src/Local/LocalCommandProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ internal class LocalCommandProvider
{
private readonly ErrorReportMode errorReportMode;
private readonly string localGraphvizDotPath;
private readonly string include;
private readonly string delimitor;
private readonly int imageIndex;

private string ErrorReportModeCommand
{
Expand All @@ -26,27 +28,37 @@ private string ErrorReportModeCommand
}
}

private string GraphvizDotCommand => string.IsNullOrEmpty(localGraphvizDotPath)
private string GraphvizDotCommand => string.IsNullOrEmpty(localGraphvizDotPath)
? string.Empty
: $" -graphvizdot \"{localGraphvizDotPath}\"";

private string IncludeCommand => string.IsNullOrEmpty(include)
? string.Empty
: $" \"-I{include}\"";

private string DelimitorCommand => string.IsNullOrEmpty(delimitor)
? string.Empty
: $" -pipedelimitor \"{delimitor}\"";

private string ImageIndexCommand => $" -pipeimageindex {imageIndex}";

public LocalCommandProvider(PlantUmlSettings settings)
{
errorReportMode = settings.ErrorReportMode;
localGraphvizDotPath = settings.LocalGraphvizDotPath;
include = settings.Include;
delimitor = settings.Delimitor;
imageIndex = settings.ImageIndex;
}

public string GetCommand(OutputFormat outputFormat)
{
string outputFormatCommand = GetOuputFormatCommand(outputFormat);
outputFormatCommand += ErrorReportModeCommand;
outputFormatCommand += GraphvizDotCommand;
outputFormatCommand += IncludeCommand;
outputFormatCommand += DelimitorCommand;
outputFormatCommand += ImageIndexCommand;
return outputFormatCommand;
}

Expand Down
13 changes: 9 additions & 4 deletions src/Local/LocalPlantUmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
using System.Threading.Tasks;
using PlantUml.Net.Java;
using PlantUml.Net.Remote;
using PlantUml.Net.Tools;

namespace PlantUml.Net.Local
{
internal class LocalPlantUmlRenderer : IPlantUmlRenderer
{
private readonly JarRunner jarRunner;
private readonly string workingDirectory;
private readonly LocalCommandProvider commandProvider;
private readonly RenderUrlCalculator renderUrlCalculator;

public LocalPlantUmlRenderer(JarRunner jarRunner, LocalCommandProvider commandProvider, RenderUrlCalculator renderUrlCalculator)
public LocalPlantUmlRenderer(JarRunner jarRunner, string workingDirectory,
LocalCommandProvider commandProvider, RenderUrlCalculator renderUrlCalculator)
{
this.jarRunner = jarRunner;
this.workingDirectory = workingDirectory;
this.commandProvider = commandProvider;
this.renderUrlCalculator = renderUrlCalculator;
}

public async Task<byte[]> RenderAsync(string code, OutputFormat outputFormat, CancellationToken cancellationToken = default)
public async Task<byte[]> RenderAsync(string code, OutputFormat outputFormat,
CancellationToken cancellationToken = default(CancellationToken))
{
string command = commandProvider.GetCommand(outputFormat);
var processResult = await jarRunner.RunJarWithInputAsync(code, cancellationToken, command, "-pipe", "-charset UTF-8")
.ConfigureAwait(false);
IProcessResult processResult = await jarRunner.RunJarWithInputAsync(workingDirectory, code,
cancellationToken, command, "-pipe", "-charset UTF-8").ConfigureAwait(false);
if (processResult.ExitCode != 0)
{
string message = Encoding.UTF8.GetString(processResult.Error);
Expand Down
16 changes: 16 additions & 0 deletions src/PlantUmlSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,28 @@ public class PlantUmlSettings
/// </summary>
public RenderingMode RenderingMode { get; set; }

/// <summary>
/// Include a file as if '!include file' were used, also allowing pattern like '*.puml'
/// </summary>
public string Include { get; set; }

/// <summary>
/// The working directory to use which may is needed for relative includes.
/// Defaults to null to use the applications executable directory.
/// </summary>
public string WorkingDirectory { get; set; }

/// <summary>
/// Separators between diagrams if multiple diagrams are generated.
/// This way it can be determined can determine where one image ends and another starts.
/// </summary>
public string Delimitor { get; set; }

/// <summary>
/// To generate the Nth image
/// </summary>
public int ImageIndex { get; set; }

public PlantUmlSettings()
{
RenderingMode = RenderingMode.Remote;
Expand Down
2 changes: 1 addition & 1 deletion src/RendererFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IPlantUmlRenderer CreateRenderer(PlantUmlSettings settings)

JarRunner jarRunner = CreateJarRunner(settings);
LocalCommandProvider commandProvider = new LocalCommandProvider(settings);
return new LocalPlantUmlRenderer(jarRunner, commandProvider, renderUrlCalculator);
return new LocalPlantUmlRenderer(jarRunner, settings.WorkingDirectory, commandProvider, renderUrlCalculator);

default:
throw new ArgumentException("Invalid rendering mode", nameof(settings.RenderingMode));
Expand Down
24 changes: 17 additions & 7 deletions src/Tools/ProcessHelper.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace PlantUml.Net.Tools
{
internal class ProcessHelper
{
public async Task<IProcessResult> RunProcessWithInputAsync(string fileName, string arguments, string input, CancellationToken cancellationToken)
public async Task<IProcessResult> RunProcessWithInputAsync(string fileName, string arguments, string workingDirectory,
string input, CancellationToken cancellationToken)
{
using (Process process = new Process()
{
StartInfo = GetProcessStartInfo(fileName, arguments),
StartInfo = GetProcessStartInfo(fileName, arguments, workingDirectory),
EnableRaisingEvents = true
})
{
Expand All @@ -21,15 +23,22 @@ public async Task<IProcessResult> RunProcessWithInputAsync(string fileName, stri
{
if (tcs.TrySetCanceled())
{
process.Kill();
try
{
process.Kill();
}
catch (ArgumentNullException)
{
//Catch potential ArgumentNullException: SafeHandle cannot be null
}
}
});
}

process.Start();
process.WriteInput(input);

Task.Run(() =>
_ = Task.Run(() =>
{
ProcessResult result = new ProcessResult
{
Expand All @@ -44,7 +53,7 @@ public async Task<IProcessResult> RunProcessWithInputAsync(string fileName, stri
}
}

private static ProcessStartInfo GetProcessStartInfo(string command, string arguments)
private static ProcessStartInfo GetProcessStartInfo(string command, string arguments, string workingDirectory)
{
return new ProcessStartInfo(command)
{
Expand All @@ -56,7 +65,8 @@ private static ProcessStartInfo GetProcessStartInfo(string command, string argum
CreateNoWindow = true,
Arguments = arguments,
StandardErrorEncoding = System.Text.Encoding.UTF8,
StandardOutputEncoding = System.Text.Encoding.UTF8
StandardOutputEncoding = System.Text.Encoding.UTF8,
WorkingDirectory = workingDirectory
};
}
}
Expand Down