diff --git a/Rubjerg.Graphviz/GraphvizCommand.cs b/Rubjerg.Graphviz/GraphvizCommand.cs index 417a49c..7e624e5 100644 --- a/Rubjerg.Graphviz/GraphvizCommand.cs +++ b/Rubjerg.Graphviz/GraphvizCommand.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; @@ -40,12 +41,18 @@ internal static string Rid internal static readonly Lazy _DotExePath = new Lazy(() => { // Depending on the method of deployment, there are several possible directories to look for dot - string[] possibleLocations = [ - Path.Combine(AppContext.BaseDirectory, "runtimes", Rid, "native"), - Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), - Path.GetDirectoryName(AppContext.BaseDirectory), - "" - ]; + // The DOT_BINARY_PATH environment variable can be set to point to a specific location, which will be checked first. + var dotBinaryPath = Environment.GetEnvironmentVariable("DOT_BINARY_PATH"); + var possibleLocations = new List(); + if (!string.IsNullOrEmpty(dotBinaryPath)) + { + possibleLocations.Add(dotBinaryPath); + } + possibleLocations.Add(Path.Combine(AppContext.BaseDirectory, "runtimes", Rid, "native")); + possibleLocations.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); + possibleLocations.Add(Path.GetDirectoryName(AppContext.BaseDirectory)); + possibleLocations.Add(""); + return possibleLocations.Where(d => d != null).Select(dir => Path.Combine(dir, "dot")).FirstOrDefault(File.Exists) ?? possibleLocations.Where(d => d != null).Select(dir => Path.Combine(dir, "dot.exe")).FirstOrDefault(File.Exists) ?? throw new InvalidOperationException("Could not find path to dot binary in any of: " + string.Join(", ", possibleLocations));