Skip to content

Commit fa324e9

Browse files
authored
Improve strategy for finding dot.exe (#112)
* Improve strategy for finding dot.exe * fixup * improve error msg * add readonly modifier
1 parent 5d2b501 commit fa324e9

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

Rubjerg.Graphviz/GraphvizCommand.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ internal static string Rid
3434
}
3535
}
3636

37-
internal static Lazy<string> _DotExePath = new Lazy<string>(() =>
38-
// If graphviz is not found in the runtimes folder, look in the current directory for compatibility with nonportable windows builds.
39-
new string[] {
40-
Path.Combine(AppContext.BaseDirectory, "runtimes", Rid, "native", "dot"),
41-
Path.Combine(AppContext.BaseDirectory, "runtimes", Rid, "native", "dot.exe"),
42-
"dot",
43-
"dot.exe"
44-
}.FirstOrDefault(File.Exists));
37+
internal static readonly Lazy<string> _DotExePath = new Lazy<string>(() =>
38+
{
39+
// Depending on the method of deployment, there are several possible directories to look for dot
40+
string[] possibleLocations = [
41+
Path.Combine(AppContext.BaseDirectory, "runtimes", Rid, "native"),
42+
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
43+
Path.GetDirectoryName(AppContext.BaseDirectory),
44+
""
45+
];
46+
return possibleLocations.Select(dir => Path.Combine(dir, "dot")).FirstOrDefault(File.Exists)
47+
?? possibleLocations.Select(dir => Path.Combine(dir, "dot.exe")).FirstOrDefault(File.Exists)
48+
?? throw new InvalidOperationException("Could not find path to dot binary in any of: " + string.Join(", ", possibleLocations));
49+
});
4550
internal static string DotExePath => _DotExePath.Value;
4651

4752
public static RootGraph CreateLayout(Graph input, string engine = LayoutEngines.Dot, CoordinateSystem coordinateSystem = CoordinateSystem.BottomLeft)
@@ -74,18 +79,9 @@ public static (byte[] stdout, string stderr) Exec(Graph input, string format = "
7479
}
7580
string? inputToStdin = input.ToDotString();
7681

77-
// Get the location of the currently executing DLL
78-
// https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.codebase?view=net-5.0
79-
string exeDirectory = AppDomain.CurrentDomain.RelativeSearchPath
80-
?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
81-
?? Path.GetDirectoryName(System.AppContext.BaseDirectory);
82-
83-
// Construct the path to the executable
84-
string exePath = Path.Combine(exeDirectory, DotExePath);
85-
8682
Process process = new Process();
8783

88-
process.StartInfo.FileName = exePath;
84+
process.StartInfo.FileName = DotExePath;
8985
process.StartInfo.Arguments = arguments;
9086

9187
// Redirect the input/output streams

0 commit comments

Comments
 (0)