-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #772 from dolittle/deps
Maintenance release, upgrade dependencies
- Loading branch information
Showing
14 changed files
with
242 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
namespace Dolittle.Runtime.DependencyInversion; | ||
|
||
/// <summary> | ||
/// Utility to discover and load assemblies installed in your application | ||
/// </summary> | ||
static class AssemblyFinder | ||
{ | ||
static readonly AssemblyLoadContextWrapper _loader = new(AssemblyLoadContext.Default); | ||
|
||
/// <summary> | ||
/// Find assemblies in the application's binary path | ||
/// </summary> | ||
/// <param name="logFailure">Take an action when an assembly file could not be loaded</param> | ||
/// <param name="includeExeFiles">Optionally include *.exe files</param> | ||
/// <returns></returns> | ||
public static IEnumerable<Assembly> FindAssemblies(Action<string> logFailure, Func<AssemblyName, bool> filter, | ||
bool includeExeFiles) | ||
{ | ||
string path; | ||
try | ||
{ | ||
path = AppContext.BaseDirectory; | ||
} | ||
catch (Exception) | ||
{ | ||
path = Directory.GetCurrentDirectory(); | ||
} | ||
|
||
return FindAssemblies(filter, path, logFailure, includeExeFiles); | ||
} | ||
|
||
/// <summary> | ||
/// Find assemblies in the given path | ||
/// </summary> | ||
/// <param name="assemblyPath">The path to probe for assembly files</param> | ||
/// <param name="logFailure">Take an action when an assembly file could not be loaded</param> | ||
/// <param name="includeExeFiles">Optionally include *.exe files</param> | ||
/// <returns></returns> | ||
public static IEnumerable<Assembly> FindAssemblies(Func<AssemblyName, bool> filter, string assemblyPath, | ||
Action<string> logFailure, bool includeExeFiles) | ||
{ | ||
var assemblies = FindAssemblies(assemblyPath, filter, logFailure, includeExeFiles) | ||
.OrderBy(x => x.GetName().Name) | ||
.ToArray(); | ||
|
||
return assemblies.TopologicalSort((Func<Assembly, Assembly[]>)FindDependencies, throwOnCycle: false); | ||
|
||
Assembly[] FindDependencies(Assembly a) => assemblies | ||
.Where(x => a.GetReferencedAssemblies().Any(_ => _.Name == x.GetName().Name)).ToArray(); | ||
} | ||
|
||
static IEnumerable<Assembly> FindAssemblies(string assemblyPath, Func<AssemblyName, bool> filter, | ||
Action<string> logFailure, | ||
bool includeExeFiles) | ||
{ | ||
var dllFiles = Directory.EnumerateFiles(assemblyPath, "*.dll", SearchOption.AllDirectories); | ||
var files = dllFiles; | ||
|
||
if (includeExeFiles) | ||
{ | ||
var exeFiles = Directory.EnumerateFiles(assemblyPath, "*.exe", SearchOption.AllDirectories); | ||
files = dllFiles.Concat(exeFiles); | ||
} | ||
|
||
foreach (var file in files) | ||
{ | ||
var name = Path.GetFileNameWithoutExtension(file); | ||
Assembly? assembly = null; | ||
var assemblyName = new AssemblyName(name); | ||
if (!filter(assemblyName)) | ||
{ | ||
continue; | ||
} | ||
|
||
try | ||
{ | ||
assembly = _loader.LoadFromAssemblyName(assemblyName); | ||
} | ||
catch | ||
{ | ||
try | ||
{ | ||
assembly = _loader.LoadFromAssemblyPath(file); | ||
} | ||
catch | ||
{ | ||
logFailure(file); | ||
} | ||
} | ||
|
||
if (assembly != null) | ||
{ | ||
yield return assembly; | ||
} | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Find assembly files matching a given filter | ||
/// </summary> | ||
/// <param name="filter"></param> | ||
/// <param name="includeExeFiles"></param> | ||
/// <returns></returns> | ||
public static IEnumerable<Assembly> FindAssemblies(Func<AssemblyName, bool> filter, bool includeExeFiles = false) | ||
{ | ||
filter ??= a => true; | ||
|
||
return FindAssemblies(file => { }, filter, includeExeFiles: includeExeFiles); | ||
} | ||
} | ||
|
||
|
||
sealed class AssemblyLoadContextWrapper | ||
{ | ||
readonly AssemblyLoadContext _ctx; | ||
|
||
public AssemblyLoadContextWrapper(AssemblyLoadContext ctx) | ||
{ | ||
_ctx = ctx; | ||
} | ||
|
||
public Assembly LoadFromStream(Stream assembly) | ||
{ | ||
return _ctx.LoadFromStream(assembly); | ||
} | ||
|
||
public Assembly LoadFromAssemblyName(AssemblyName assemblyName) | ||
{ | ||
return _ctx.LoadFromAssemblyName(assemblyName); | ||
} | ||
|
||
public Assembly LoadFromAssemblyPath(string assemblyName) | ||
{ | ||
return _ctx.LoadFromAssemblyPath(assemblyName); | ||
} | ||
} | ||
|
||
static class TopologicalSortExtensions | ||
{ | ||
/// <summary> | ||
/// Performs a topological sort on the enumeration based on dependencies | ||
/// </summary> | ||
/// <param name="source"></param> | ||
/// <param name="dependencies"></param> | ||
/// <param name="throwOnCycle"></param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
public static IEnumerable<T> TopologicalSort<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> dependencies, | ||
bool throwOnCycle = true) | ||
{ | ||
var sorted = new List<T>(); | ||
var visited = new HashSet<T>(); | ||
|
||
foreach (var item in source) | ||
{ | ||
Visit(item, visited, sorted, dependencies, throwOnCycle); | ||
} | ||
|
||
return sorted; | ||
} | ||
|
||
static void Visit<T>(T item, ISet<T> visited, ICollection<T> sorted, Func<T, IEnumerable<T>> dependencies, | ||
bool throwOnCycle) | ||
{ | ||
if (!visited.Add(item)) | ||
{ | ||
if (throwOnCycle && !sorted.Contains(item)) | ||
{ | ||
throw new Exception("Cyclic dependency found"); | ||
} | ||
} | ||
else | ||
{ | ||
foreach (var dep in dependencies(item)) | ||
{ | ||
Visit(dep, visited, sorted, dependencies, throwOnCycle); | ||
} | ||
|
||
sorted.Add(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<AutofacVersion>6.3.0</AutofacVersion> | ||
<AutofacExtensionsVersion>7.2.0</AutofacExtensionsVersion> | ||
<BaselineTypeDiscoveryVersion>1.1.2</BaselineTypeDiscoveryVersion> | ||
<ConsoleTablesVersion>2.4.2</ConsoleTablesVersion> | ||
<AutofacVersion>8.0.0</AutofacVersion> | ||
<AutofacExtensionsVersion>9.0.0</AutofacExtensionsVersion> | ||
<ConsoleTablesVersion>2.6.1</ConsoleTablesVersion> | ||
<ContractsVersion>7.8.0</ContractsVersion> | ||
<CoverletVersion>3.1.0</CoverletVersion> | ||
<CoverletVersion>6.0.2</CoverletVersion> | ||
<DolittleCommonSpecsVersion>2.*</DolittleCommonSpecsVersion> | ||
<GrpcVersion>2.63.0</GrpcVersion> | ||
<GrpcVersion>2.65.0</GrpcVersion> | ||
<GrpcTestingVersion>2.46.6</GrpcTestingVersion> | ||
<GrpcToolsVersion>2.64.0</GrpcToolsVersion> | ||
<GrpcToolsVersion>2.65.0</GrpcToolsVersion> | ||
<MachineSpecificationsRunnerVersion>2.10.2</MachineSpecificationsRunnerVersion> | ||
<MachineSpecificationsVersion>1.0.0</MachineSpecificationsVersion> | ||
<MicrosoftDotNetTestVersion>17.9.0</MicrosoftDotNetTestVersion> | ||
<MachineSpecificationsVersion>1.1.2</MachineSpecificationsVersion> | ||
<MicrosoftDotNetTestVersion>17.10.0</MicrosoftDotNetTestVersion> | ||
<MicrosoftExtensionsVersion>8.0.0</MicrosoftExtensionsVersion> | ||
<MongoDBDriverVersion>2.25.0</MongoDBDriverVersion> | ||
<MongoDBOpenTelemetryVersion>1.0.0</MongoDBOpenTelemetryVersion> | ||
<MongoDBDriverVersion>2.28.0</MongoDBDriverVersion> | ||
<MongoDBDiagnosticSourcesVersion>1.5.0</MongoDBDiagnosticSourcesVersion> | ||
<MoqVersion>4.18.*</MoqVersion> | ||
<FluentAssertionsVersion>6.12.0</FluentAssertionsVersion> | ||
<NetEscapadesYaml>2.2.0</NetEscapadesYaml> | ||
<NewtonsoftVersion>13.0.3</NewtonsoftVersion> | ||
<NitoAsyncVersion>5.1.0</NitoAsyncVersion> | ||
<PollyVersion>7.2.3</PollyVersion> | ||
<PrometheusVersion>6.0.0</PrometheusVersion> | ||
<PrometheusNetDotNetRuntimeVersion>4.2.4</PrometheusNetDotNetRuntimeVersion> | ||
<NitoAsyncVersion>5.1.2</NitoAsyncVersion> | ||
<PollyVersion>8.4.1</PollyVersion> | ||
<PrometheusVersion>8.2.1</PrometheusVersion> | ||
<PrometheusNetDotNetRuntimeVersion>4.4.0</PrometheusNetDotNetRuntimeVersion> | ||
<ProtoActorVersion>1.6.0</ProtoActorVersion> | ||
<ProtobufVersion>3.26.1</ProtobufVersion> | ||
<SwashbuckleVersion>6.5.0</SwashbuckleVersion> | ||
<SystemLinqAsyncVersion>5.1.0</SystemLinqAsyncVersion> | ||
<ProtobufVersion>3.27.3</ProtobufVersion> | ||
<SwashbuckleVersion>6.7.1</SwashbuckleVersion> | ||
<SystemLinqAsyncVersion>6.0.1</SystemLinqAsyncVersion> | ||
<SystemSecurityVersion>4.3.0</SystemSecurityVersion> | ||
<XUnitVersion>2.5.3</XUnitVersion> | ||
<XUnitVersion>2.9.0</XUnitVersion> | ||
<XUnitRunnerVersion>2.8.2</XUnitRunnerVersion> | ||
</PropertyGroup> | ||
</Project> |