public void Render(string filePath) { if (!IsDotInstalled) { Logger.LogError(new FileNotFoundException("dot tool (Graphviz) is not installed")); return; } string ext = Path.GetExtension(filePath); if (ext.Length > 0) { ext = ext.Substring(1); } GraphvizOutputFormat outputFormat = OutputFormat; string imagePath = filePath; bool appendExt = false; if (OutputFormat == GraphvizOutputFormat.None) { if (!Enum.TryParse(ext, true, out outputFormat)) { outputFormat = GraphvizOutputFormat.Png; imagePath = $"{imagePath}.{outputFormat.ToString().ToLowerInvariant()}"; appendExt = true; } } else { string outputFormatString = OutputFormat.ToString().ToLowerInvariant(); if (!filePath.EndsWith(outputFormatString)) { imagePath = $"{imagePath}.{outputFormatString}"; appendExt = true; } } string dotFilePath = appendExt ? filePath + ".dot" : Path.ChangeExtension(filePath, "dot"); File.WriteAllText(dotFilePath, DotGraph); var processor = new Processor("dot") { Arguments = $"\"{dotFilePath}\" -T{outputFormat.ToString().ToLowerInvariant()} -o \"{imagePath}\"" }; processor.ErrorDataReceived += (sender, e) => { Logger.LogError(new Exception($"Error while graph rendering: {e}")); }; ProcessExecutionResult executionResult = processor.Start(); if (!SaveDot) { File.Delete(dotFilePath); } if (executionResult.ExitCode != 0) { Logger.LogError(new Exception($"Graph rendering completed with error code {executionResult.ExitCode}")); } }
public void Dump(string filePath) { string ext = Path.GetExtension(filePath); if (ext.Length > 0) { ext = ext.Substring(1); } GraphvizOutputFormat outputFormat = OutputFormat; string imagePath = filePath; bool appendExt = false; if (OutputFormat == GraphvizOutputFormat.None) { if (!Enum.TryParse(ext, true, out outputFormat)) { outputFormat = GraphvizOutputFormat.Png; imagePath = imagePath + "." + outputFormat.ToString().ToLowerInvariant(); appendExt = true; } } else { string outputFormatString = OutputFormat.ToString().ToLowerInvariant(); if (!filePath.EndsWith(outputFormatString)) { imagePath = imagePath + "." + outputFormatString; appendExt = true; } } string dotFilePath; if (SaveDot) { dotFilePath = appendExt ? filePath + ".dot" : Path.ChangeExtension(filePath, "dot"); } else { dotFilePath = Path.GetTempFileName(); } File.WriteAllText(dotFilePath, DotGraph); var process = Process.Start(new ProcessStartInfo { FileName = GraphvizPath, Arguments = $"\"{dotFilePath}\" -T{outputFormat.ToString().ToLowerInvariant()} -o \"{imagePath}\"", UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true }); process.WaitForExit(); if (!SaveDot) { File.Delete(dotFilePath); } var errors = process.StandardError.ReadToEnd(); if (process.ExitCode != 0 || errors != "") { throw new Exception($"Error while graph rendering. Errors: {errors}"); } }