44using System . Reflection ;
55using System . Text ;
66using System . Runtime . InteropServices ;
7+ using System . Linq ;
78
89namespace Rubjerg . Graphviz ;
910
@@ -33,6 +34,16 @@ internal static string Rid
3334 }
3435 }
3536
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 ) ) ;
45+ internal static string DotExePath => _DotExePath . Value ;
46+
3647 public static RootGraph CreateLayout ( Graph input , string engine = LayoutEngines . Dot , CoordinateSystem coordinateSystem = CoordinateSystem . BottomLeft )
3748 {
3849 var ( stdout , stderr ) = Exec ( input , engine : engine ) ;
@@ -56,11 +67,6 @@ public static string ConvertBytesOutputToString(byte[] data)
5667 /// <returns>stderr may contain warnings, stdout is in utf8 encoding</returns>
5768 public static ( byte [ ] stdout , string stderr ) Exec ( Graph input , string format = "xdot" , string ? outputPath = null , string engine = LayoutEngines . Dot )
5869 {
59- var exeName = Path . Combine ( AppContext . BaseDirectory , "runtimes" , Rid , "native" , "dot" ) ;
60-
61- // If graphviz is not found in the runtimes folder, look in the current directory for compatibility with nonportable windows builds.
62- exeName = File . Exists ( exeName ) ? exeName : "dot" ;
63-
6470 string arguments = $ "-T{ format } -K{ engine } ";
6571 if ( outputPath != null )
6672 {
@@ -75,7 +81,7 @@ public static (byte[] stdout, string stderr) Exec(Graph input, string format = "
7581 ?? Path . GetDirectoryName ( System . AppContext . BaseDirectory ) ;
7682
7783 // Construct the path to the executable
78- string exePath = Path . Combine ( exeDirectory , exeName ) ;
84+ string exePath = Path . Combine ( exeDirectory , DotExePath ) ;
7985
8086 Process process = new Process ( ) ;
8187
0 commit comments