Skip to content

Commit

Permalink
Remove algorithmic complexity of exclusion check
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach committed Aug 25, 2023
1 parent b5eadcb commit cab0c74
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace NServiceBus.Core.Tests.AssemblyScanner
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -15,13 +16,13 @@ public void No_files_explicitly_excluded_are_returned()
{
var results = new AssemblyScanner(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestDlls"))
{
AssembliesToSkip = new List<string>
AssembliesToSkip = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"dotNet.dll"
},
ScanAppDomainAssemblies = false
}
.GetScannableAssemblies();
.GetScannableAssemblies();

var skippedFiles = results.SkippedFiles;
var explicitlySkippedDll = skippedFiles.FirstOrDefault(s => s.FilePath.Contains("dotNet.dll"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace NServiceBus.Core.Tests.Config
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -49,7 +50,7 @@ IEnumerable<Assembly> GetAssembliesInDirectory(string path, params string[] asse

if (assembliesToSkip != null)
{
assemblyScanner.AssembliesToSkip = assembliesToSkip.ToList();
assemblyScanner.AssembliesToSkip = new HashSet<string>(assembliesToSkip.ToList(), StringComparer.OrdinalIgnoreCase);
}
return assemblyScanner
.GetScannableAssemblies()
Expand Down
46 changes: 6 additions & 40 deletions src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,30 +302,9 @@ static List<FileInfo> ScanDirectoryForAssemblyFiles(string directoryToScan, bool
return fileInfo;
}

bool IsExcluded(string assemblyNameOrFileName)
{
foreach (var explicitlyExcludedAssembly in AssembliesToSkip)
{
if (IsMatch(explicitlyExcludedAssembly, assemblyNameOrFileName))
{
return true;
}
}

foreach (var excludedByDefaultAssembly in DefaultAssemblyExclusions)
{
if (IsMatch(excludedByDefaultAssembly, assemblyNameOrFileName))
{
return true;
}
}

return false;
}

static bool IsMatch(string expression1, string expression2)
=> string.Equals(RemoveFileExtensionIfNecessary(expression1), RemoveFileExtensionIfNecessary(expression2), StringComparison.OrdinalIgnoreCase);
bool IsExcluded(string assemblyNameOrFileName) => AssembliesToSkip.Contains(assemblyNameOrFileName) || DefaultAssemblyExclusions.Contains(assemblyNameOrFileName);

// The input and output signature of this method is deliberate
List<Type> AllowedTypes(Type[] types)
{
// assume the majority of types will be allowed to preallocate the list
Expand All @@ -345,15 +324,6 @@ bool IsAllowedType(Type type) =>
Attribute.GetCustomAttribute(type, typeof(CompilerGeneratedAttribute), false) == null &&
!TypesToSkip.Contains(type);

static string RemoveFileExtensionIfNecessary(string assemblyOrFileName)
{
if (assemblyOrFileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || assemblyOrFileName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
{
return assemblyOrFileName[..^4];
}
return assemblyOrFileName;
}

void AddTypesToResult(Assembly assembly, AssemblyScannerResults results)
{
try
Expand Down Expand Up @@ -398,15 +368,11 @@ bool ShouldScanDependencies(Assembly assembly)
return false;
}

if (IsExcluded(assemblyName.Name))
{
return false;
}

return true;
// AssemblyName.Name is without the file extension
return !IsExcluded(assemblyName.Name);
}

internal List<string> AssembliesToSkip = new();
internal HashSet<string> AssembliesToSkip = new(StringComparer.OrdinalIgnoreCase);
internal bool ScanNestedDirectories;
internal HashSet<Type> TypesToSkip = new();
readonly Assembly assemblyToScan;
Expand All @@ -420,7 +386,7 @@ bool ShouldScanDependencies(Assembly assembly)
};

//TODO: delete when we make message scanning lazy #1617
static readonly string[] DefaultAssemblyExclusions =
static readonly HashSet<string> DefaultAssemblyExclusions = new(StringComparer.OrdinalIgnoreCase)
{
// NSB Build-Dependencies
"nunit",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Settings;
using Hosting.Helpers;
Expand Down Expand Up @@ -31,7 +32,7 @@ public static AssemblyScanningComponent Initialize(Configuration configuration,

var assemblyScannerSettings = configuration.AssemblyScannerConfiguration;

assemblyScanner.AssembliesToSkip = assemblyScannerSettings.ExcludedAssemblies;
assemblyScanner.AssembliesToSkip = new HashSet<string>(assemblyScannerSettings.ExcludedAssemblies.Select(Path.GetFileNameWithoutExtension).ToArray(), StringComparer.OrdinalIgnoreCase);
assemblyScanner.TypesToSkip = new HashSet<Type>(assemblyScannerSettings.ExcludedTypes);
assemblyScanner.ScanNestedDirectories = assemblyScannerSettings.ScanAssembliesInNestedDirectories;
assemblyScanner.ThrowExceptions = assemblyScannerSettings.ThrowExceptions;
Expand Down

0 comments on commit cab0c74

Please sign in to comment.