Datei: NDO.ProviderFactory/NDO.ProviderFactory/NDOProviderPathFinder.cs
Last Commit (b99a7e5)
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.IO; |
| 4 | using System.Linq; |
| 5 | |
| 6 | namespace NDO.ProviderFactory |
| 7 | { |
| 8 | class NDOProviderPathFinder : IProviderPathFinder |
| 9 | ····{ |
| 10 | ········public NDOProviderPathFinder() |
| 11 | ········{ |
| 12 | ········} |
| 13 | |
| 14 | ········readonly string[] netVersions = { "net8.0", "net6.0", "netstandard2.0", "netstandard2.1" }; |
| 15 | |
| 16 | ········public IEnumerable<string> GetPaths() |
| 17 | ········{ |
| 18 | ············List<string>paths = new List<string>(); |
| 19 | ············var path = AppDomain.CurrentDomain.BaseDirectory; |
| 20 | ············paths.Add( path ); |
| 21 | ············ |
| 22 | ············string binPath = Path.Combine( path, "bin" ); |
| 23 | ············if (Directory.Exists( binPath )) |
| 24 | ················paths.Add(binPath); |
| 25 | |
| 26 | ············// This is a hack to determine any loaded provider packages. |
| 27 | ············var runtimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(); |
| 28 | |
| 29 | ············if (runtimeDir != null && runtimeDir.IndexOf( "Microsoft.NETCore.App" ) > -1) |
| 30 | ············{ |
| 31 | ················path = typeof( NDOProviderFactory ).Assembly.Location; |
| 32 | ················var pattern = $".nuget{Path.DirectorySeparatorChar}packages"; |
| 33 | ················int p; |
| 34 | ················if (( p = path.IndexOf( pattern ) ) > -1) |
| 35 | ················{ |
| 36 | ····················p += pattern.Length; |
| 37 | ····················path = path.Substring( 0, p ); |
| 38 | ····················var majorVersion = GetType().Assembly.GetName().Version.Major.ToString(); |
| 39 | |
| 40 | ····················foreach (var subPath in Directory.GetDirectories( path, "ndo.*" )) |
| 41 | ····················{ |
| 42 | ························if (Path.GetFileName( subPath ).Equals( "ndo.dll", StringComparison.OrdinalIgnoreCase )) |
| 43 | ····························continue; |
| 44 | ························if (Path.GetFileName( subPath ).Equals( "ndo.jsonformatter", StringComparison.OrdinalIgnoreCase ) ) |
| 45 | ····························continue; |
| 46 | ························//TODO We should sort the versions here and take the last one of the same major version |
| 47 | ························var versionDir = Directory.GetDirectories( subPath ).FirstOrDefault( s => Path.GetFileName( s ).StartsWith( majorVersion) ); |
| 48 | ························if (versionDir == null) |
| 49 | ····························continue; |
| 50 | |
| 51 | ························foreach (var v in netVersions) |
| 52 | ························{ |
| 53 | ····························var libDir = Path.Combine( versionDir, "lib", v ); |
| 54 | ····························if (!Directory.Exists( libDir )) |
| 55 | ································continue; |
| 56 | ····························paths.Add( libDir ); |
| 57 | ························} |
| 58 | ····················} |
| 59 | ················} |
| 60 | ············} |
| 61 | |
| 62 | ············return paths; |
| 63 | ········} |
| 64 | ····} |
| 65 | } |
| 66 |
New Commit (281510e)
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.IO; |
| 4 | using System.Linq; |
| 5 | |
| 6 | namespace NDO.ProviderFactory |
| 7 | { |
| 8 | /// <summary> |
| 9 | ····/// Provides Paths to search for NDO Provider dlls. |
| 10 | ····/// </summary> |
| 11 | ····public class NDOProviderPathFinder : IProviderPathFinder |
| 12 | ····{ |
| 13 | ········/// <summary> |
| 14 | ········/// Constructs an NDOProviderPathFinder object |
| 15 | ········/// </summary> |
| 16 | ········public NDOProviderPathFinder() |
| 17 | ········{ |
| 18 | ········} |
| 19 | |
| 20 | ········readonly string[] netVersions = { "net8.0", "net6.0", "netstandard2.0", "netstandard2.1" }; |
| 21 | |
| 22 | ········/// <summary> |
| 23 | ········/// Get all paths to search for NDO Provider dlls. |
| 24 | ········/// </summary> |
| 25 | ········/// <returns></returns> |
| 26 | ········public IEnumerable<string> GetPaths() |
| 27 | ········{ |
| 28 | ············List<string>paths = new List<string>(); |
| 29 | ············var path = AppDomain.CurrentDomain.BaseDirectory; |
| 30 | ············paths.Add( path ); |
| 31 | ············ |
| 32 | ············string binPath = Path.Combine( path, "bin" ); |
| 33 | ············if (Directory.Exists( binPath )) |
| 34 | ················paths.Add(binPath); |
| 35 | |
| 36 | ············// This is a hack to determine any loaded provider packages. |
| 37 | ············var runtimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(); |
| 38 | |
| 39 | ············if (runtimeDir != null && runtimeDir.IndexOf( "Microsoft.NETCore.App" ) > -1) |
| 40 | ············{ |
| 41 | ················path = typeof( NDOProviderFactory ).Assembly.Location; |
| 42 | ················var pattern = $".nuget{Path.DirectorySeparatorChar}packages"; |
| 43 | ················int p; |
| 44 | ················if (( p = path.IndexOf( pattern ) ) > -1) |
| 45 | ················{ |
| 46 | ····················p += pattern.Length; |
| 47 | ····················path = path.Substring( 0, p ); |
| 48 | ····················var majorVersion = GetType().Assembly.GetName().Version.Major.ToString(); |
| 49 | |
| 50 | ····················foreach (var subPath in Directory.GetDirectories( path, "ndo.*" )) |
| 51 | ····················{ |
| 52 | ························if (Path.GetFileName( subPath ).Equals( "ndo.dll", StringComparison.OrdinalIgnoreCase )) |
| 53 | ····························continue; |
| 54 | ························if (Path.GetFileName( subPath ).Equals( "ndo.jsonformatter", StringComparison.OrdinalIgnoreCase ) ) |
| 55 | ····························continue; |
| 56 | ························//TODO We should sort the versions here and take the last one of the same major version |
| 57 | ························var versionDir = Directory.GetDirectories( subPath ).FirstOrDefault( s => Path.GetFileName( s ).StartsWith( majorVersion) ); |
| 58 | ························if (versionDir == null) |
| 59 | ····························continue; |
| 60 | |
| 61 | ························foreach (var v in netVersions) |
| 62 | ························{ |
| 63 | ····························var libDir = Path.Combine( versionDir, "lib", v ); |
| 64 | ····························if (!Directory.Exists( libDir )) |
| 65 | ································continue; |
| 66 | ····························paths.Add( libDir ); |
| 67 | ························} |
| 68 | ····················} |
| 69 | ················} |
| 70 | ············} |
| 71 | |
| 72 | ············return paths; |
| 73 | ········} |
| 74 | ····} |
| 75 | } |
| 76 |