Skip to content
Merged
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
32 changes: 14 additions & 18 deletions Rubjerg.Graphviz/GraphvizCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ internal static string Rid
}
}

internal static Lazy<string> _DotExePath = new Lazy<string>(() =>
// If graphviz is not found in the runtimes folder, look in the current directory for compatibility with nonportable windows builds.
new string[] {
Path.Combine(AppContext.BaseDirectory, "runtimes", Rid, "native", "dot"),
Path.Combine(AppContext.BaseDirectory, "runtimes", Rid, "native", "dot.exe"),
"dot",
"dot.exe"
}.FirstOrDefault(File.Exists));
internal static readonly Lazy<string> _DotExePath = new Lazy<string>(() =>
{
// 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),
""
];
return possibleLocations.Select(dir => Path.Combine(dir, "dot")).FirstOrDefault(File.Exists)
?? possibleLocations.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));
});
internal static string DotExePath => _DotExePath.Value;

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

// Get the location of the currently executing DLL
// https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.codebase?view=net-5.0
string exeDirectory = AppDomain.CurrentDomain.RelativeSearchPath
?? Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
?? Path.GetDirectoryName(System.AppContext.BaseDirectory);

// Construct the path to the executable
string exePath = Path.Combine(exeDirectory, DotExePath);

Process process = new Process();

process.StartInfo.FileName = exePath;
process.StartInfo.FileName = DotExePath;
process.StartInfo.Arguments = arguments;

// Redirect the input/output streams
Expand Down
Loading