Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #890 from thecodejunkie/resourcescanningconvention

Resource scanning convention
  • Loading branch information...
commit cf59df101df30c1203352e9292b50a0eadfe1b2d 2 parents 7cf2d88 + 0c3e893
@grumpydev grumpydev authored
View
14 src/Nancy.Testing/ConfigurableBootstrapper.cs
@@ -1137,24 +1137,24 @@ public ConfigurableBoostrapperConfigurator RequestDispatcher(IRequestDispatcher
}
/// <summary>
- /// Configures the bootstrapper to use the provided instance of <see cref="IAssemblyProvider"/>.
+ /// Configures the bootstrapper to use the provided instance of <see cref="IResourceAssemblyProvider"/>.
/// </summary>
- /// <param name="assemblyProvider">The <see cref="IAssemblyProvider"/> instance that should be used by the bootstrapper.</param>
+ /// <param name="resourceAssemblyProvider">The <see cref="IResourceAssemblyProvider"/> instance that should be used by the bootstrapper.</param>
/// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
- public ConfigurableBoostrapperConfigurator ResourceAssemblyProvider(IAssemblyProvider assemblyProvider)
+ public ConfigurableBoostrapperConfigurator ResourceAssemblyProvider(IResourceAssemblyProvider resourceAssemblyProvider)
{
this.bootstrapper.registeredInstances.Add(
- new InstanceRegistration(typeof(IAssemblyProvider), assemblyProvider));
+ new InstanceRegistration(typeof(IResourceAssemblyProvider), resourceAssemblyProvider));
return this;
}
/// <summary>
- /// Configures the bootstrapper to create an <see cref="IAssemblyProvider"/> instance of the specified type.
+ /// Configures the bootstrapper to create an <see cref="IResourceAssemblyProvider"/> instance of the specified type.
/// </summary>
- /// <typeparam name="T">The type of the <see cref="IAssemblyProvider"/> that the bootstrapper should use.</typeparam>
+ /// <typeparam name="T">The type of the <see cref="IResourceAssemblyProvider"/> that the bootstrapper should use.</typeparam>
/// <returns>A reference to the current <see cref="ConfigurableBoostrapperConfigurator"/>.</returns>
- public ConfigurableBoostrapperConfigurator ResourceAssemblyProvider<T>() where T : IAssemblyProvider
+ public ConfigurableBoostrapperConfigurator ResourceAssemblyProvider<T>() where T : IResourceAssemblyProvider
{
this.bootstrapper.configuration.ResourceAssemblyProvider = typeof(T);
return this;
View
3  src/Nancy.Tests/Unit/Localization/ResourceBasedTextResourceFixture.cs
@@ -1,7 +1,6 @@
namespace Nancy.Tests.Unit.Localization
{
using FakeItEasy;
- using Nancy.ViewEngines;
using Xunit;
using Nancy.Localization;
@@ -11,7 +10,7 @@ public class ResourceBasedTextResourceFixture
public void Should_Return_Null_If_No_Assembly_Found()
{
//Given
- var resourceAssemblyProvider = A.Fake<IAssemblyProvider>();
+ var resourceAssemblyProvider = A.Fake<IResourceAssemblyProvider>();
A.CallTo(() => resourceAssemblyProvider.GetAssembliesToScan()).Returns(new[] { typeof(NancyEngine).Assembly });
var defaultTextResource = new ResourceBasedTextResource(resourceAssemblyProvider);
View
12 src/Nancy.Tests/Unit/ViewEngines/ResourceViewLocationProviderFixture.cs
@@ -12,22 +12,22 @@ namespace Nancy.Tests.Unit.ViewEngines
public class ResourceViewLocationProviderFixture
{
private readonly IResourceReader reader;
- private readonly IAssemblyProvider assemblyProvider;
+ private readonly IResourceAssemblyProvider resourceAssemblyProvider;
private readonly ResourceViewLocationProvider viewProvider;
public ResourceViewLocationProviderFixture()
{
ResourceViewLocationProvider.Ignore.Clear();
this.reader = A.Fake<IResourceReader>();
- this.assemblyProvider = A.Fake<IAssemblyProvider>();
- this.viewProvider = new ResourceViewLocationProvider(this.reader, this.assemblyProvider);
+ this.resourceAssemblyProvider = A.Fake<IResourceAssemblyProvider>();
+ this.viewProvider = new ResourceViewLocationProvider(this.reader, this.resourceAssemblyProvider);
if (!ResourceViewLocationProvider.RootNamespaces.ContainsKey(this.GetType().Assembly))
{
ResourceViewLocationProvider.RootNamespaces.Add(this.GetType().Assembly, "Some.Resource");
}
- A.CallTo(() => this.assemblyProvider.GetAssembliesToScan()).Returns(new[] { this.GetType().Assembly });
+ A.CallTo(() => this.resourceAssemblyProvider.GetAssembliesToScan()).Returns(new[] { this.GetType().Assembly });
}
[Fact]
@@ -176,7 +176,7 @@ public void Should_return_view_location_result_where_location_is_set_in_platform
public void Should_scan_assemblies_returned_by_assembly_provider()
{
// Given
- A.CallTo(() => this.assemblyProvider.GetAssembliesToScan()).Returns(new[]
+ A.CallTo(() => this.resourceAssemblyProvider.GetAssembliesToScan()).Returns(new[]
{
typeof(NancyEngine).Assembly,
this.GetType().Assembly
@@ -196,7 +196,7 @@ public void Should_scan_assemblies_returned_by_assembly_provider()
public void Should_not_scan_ignored_assemblies()
{
// Given
- A.CallTo(() => this.assemblyProvider.GetAssembliesToScan()).Returns(new[]
+ A.CallTo(() => this.resourceAssemblyProvider.GetAssembliesToScan()).Returns(new[]
{
typeof(NancyEngine).Assembly,
this.GetType().Assembly
View
6 src/Nancy/Bootstrapper/NancyInternalConfiguration.cs
@@ -47,6 +47,8 @@ public sealed class NancyInternalConfiguration
asm => asm.FullName.StartsWith("Nancy.Testing", StringComparison.InvariantCulture),
asm => asm.FullName.StartsWith("MonoDevelop.NUnit", StringComparison.InvariantCulture),
asm => asm.FullName.StartsWith("SMDiagnostics", StringComparison.InvariantCulture),
+ asm => asm.FullName.StartsWith("CppCodeProvider", StringComparison.InvariantCulture),
+ asm => asm.FullName.StartsWith("WebDeb.DebHost40", StringComparison.InvariantCulture),
};
/// <summary>
@@ -93,7 +95,7 @@ public static NancyInternalConfiguration Default
RouteDescriptionProvider = typeof(DefaultRouteDescriptionProvider),
CultureService = typeof(DefaultCultureService),
TextResource = typeof(ResourceBasedTextResource),
- ResourceAssemblyProvider = typeof(DefaultAssemblyProvider),
+ ResourceAssemblyProvider = typeof(ResourceAssemblyProvider),
};
}
}
@@ -265,7 +267,7 @@ public IEnumerable<TypeRegistration> GetTypeRegistations()
new TypeRegistration(typeof(IRouteDescriptionProvider), this.RouteDescriptionProvider),
new TypeRegistration(typeof(ICultureService), this.CultureService),
new TypeRegistration(typeof(ITextResource), this.TextResource),
- new TypeRegistration(typeof(IAssemblyProvider), this.ResourceAssemblyProvider),
+ new TypeRegistration(typeof(IResourceAssemblyProvider), this.ResourceAssemblyProvider),
};
}
View
50 src/Nancy/DefaultAssemblyProvider.cs
@@ -1,50 +0,0 @@
-namespace Nancy
-{
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using Nancy.Bootstrapper;
-
- /// <summary>
- /// Default set of assemblies that should be scanned for views embedded as resources.
- /// </summary>
- public class DefaultAssemblyProvider : IAssemblyProvider
- {
- private IEnumerable<Assembly> filteredAssemblies;
-
- private readonly List<Func<Assembly, bool>> excludedAssemblies = new List<Func<Assembly, bool>>()
- {
- asm => asm.FullName.StartsWith("Microsoft.", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("System.", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("System,", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("CR_ExtUnitTest", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("mscorlib,", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("CR_VSTest", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("DevExpress.CodeRush", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("IronPython", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("IronRuby", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("xunit", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("Nancy.Testing", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("MonoDevelop.NUnit", StringComparison.InvariantCulture),
- asm => asm.FullName.StartsWith("SMDiagnostics", StringComparison.InvariantCulture),
- };
-
- /// <summary>
- /// Gets a list of assemblies that should be scanned for views.
- /// </summary>
- /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="Assembly"/> instances.</returns>
- public IEnumerable<Assembly> GetAssembliesToScan()
- {
- return (this.filteredAssemblies ?? (this.filteredAssemblies = GetFilteredAssemblies()));
- }
-
- private IEnumerable<Assembly> GetFilteredAssemblies()
- {
- return AppDomainAssemblyTypeScanner
- .Assemblies
- .Where(x => !x.IsDynamic)
- .Where(x => !this.excludedAssemblies.Any(asm => asm.Invoke(x)));
- }
- }
-}
View
2  src/Nancy/IAssemblyProvider.cs → src/Nancy/IResourceAssemblyProvider.cs
@@ -6,7 +6,7 @@
/// <summary>
/// Defines the functionality for retrieving which assemblies that should be used by Nancy.
/// </summary>
- public interface IAssemblyProvider
+ public interface IResourceAssemblyProvider
{
/// <summary>
/// Gets a list of assemblies that should be scanned.
View
23 src/Nancy/Localization/ResourceBasedTextResource.cs
@@ -3,6 +3,7 @@ namespace Nancy.Localization
using System;
using System.Collections.Generic;
using System.Linq;
+ using System.Reflection;
using System.Resources;
/// <summary>
@@ -10,18 +11,32 @@ namespace Nancy.Localization
/// </summary>
public class ResourceBasedTextResource : ITextResource
{
- private readonly IAssemblyProvider assemblyProvider;
+ private readonly IResourceAssemblyProvider resourceAssemblyProvider;
private readonly IDictionary<string, ResourceManager> resourceManagers;
+
+ /// <summary>
+ /// Convention that determins if an Assembly should be scanned for resources.
+ /// </summary>
+ /// <remarks>The default convention will scan all assemblies that references another assemblies that has a name that starts with Nancy*</remarks>
+ public static Func<Assembly, bool> ScanningPredicate = assembly =>
+ {
+ return assembly.GetReferencedAssemblies().Any(reference =>
+ {
+ return reference.Name.StartsWith("Nancy", StringComparison.OrdinalIgnoreCase);
+ });
+ };
/// <summary>
/// Initializes a new instance of <see cref="ResourceBasedTextResource"/> to read strings from *.resx files
/// </summary>
- public ResourceBasedTextResource(IAssemblyProvider assemblyProvider)
+ /// <param name="resourceAssemblyProvider">The <see cref="IResourceAssemblyProvider"/> that should be used when scanning.</param>
+ public ResourceBasedTextResource(IResourceAssemblyProvider resourceAssemblyProvider)
{
- this.assemblyProvider = assemblyProvider;
+ this.resourceAssemblyProvider = resourceAssemblyProvider;
var resources =
- from assembly in this.assemblyProvider.GetAssembliesToScan()
+ from assembly in this.resourceAssemblyProvider.GetAssembliesToScan()
+ where ScanningPredicate.Invoke(assembly)
from resourceName in assembly.GetManifestResourceNames()
where resourceName.EndsWith(".resources")
let parts = resourceName.Split(new[] { '.' })
View
4 src/Nancy/Nancy.csproj
@@ -271,7 +271,7 @@
<Compile Include="ViewEngines\DefaultFileSystemReader.cs" />
<Compile Include="ViewEngines\DefaultRenderContext.cs" />
<Compile Include="ViewEngines\DefaultRenderContextFactory.cs" />
- <Compile Include="DefaultAssemblyProvider.cs" />
+ <Compile Include="ResourceAssemblyProvider.cs" />
<Compile Include="ViewEngines\DefaultResourceReader.cs" />
<Compile Include="Localization\ResourceBasedTextResource.cs" />
<Compile Include="ViewEngines\DefaultViewCache.cs" />
@@ -374,7 +374,7 @@
<Compile Include="ViewEngines\IFileSystemReader.cs" />
<Compile Include="ViewEngines\IRenderContext.cs" />
<Compile Include="ViewEngines\IRenderContextFactory.cs" />
- <Compile Include="IAssemblyProvider.cs" />
+ <Compile Include="IResourceAssemblyProvider.cs" />
<Compile Include="ViewEngines\IResourceReader.cs" />
<Compile Include="Localization\ITextResource.cs" />
<Compile Include="ViewEngines\IViewCache.cs" />
View
29 src/Nancy/ResourceAssemblyProvider.cs
@@ -0,0 +1,29 @@
+namespace Nancy
+{
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Reflection;
+ using Nancy.Bootstrapper;
+
+ /// <summary>
+ /// Default set of assemblies that should be scanned for views embedded as resources.
+ /// </summary>
+ public class ResourceAssemblyProvider : IResourceAssemblyProvider
+ {
+ private IEnumerable<Assembly> filteredAssemblies;
+
+ /// <summary>
+ /// Gets a list of assemblies that should be scanned for views.
+ /// </summary>
+ /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="Assembly"/> instances.</returns>
+ public IEnumerable<Assembly> GetAssembliesToScan()
+ {
+ return (this.filteredAssemblies ?? (this.filteredAssemblies = GetFilteredAssemblies()));
+ }
+
+ private static IEnumerable<Assembly> GetFilteredAssemblies()
+ {
+ return AppDomainAssemblyTypeScanner.Assemblies.Where(x => !x.IsDynamic);
+ }
+ }
+}
View
12 src/Nancy/ViewEngines/ResourceViewLocationProvider.cs
@@ -12,7 +12,7 @@
public class ResourceViewLocationProvider : IViewLocationProvider
{
private readonly IResourceReader resourceReader;
- private readonly IAssemblyProvider assemblyProvider;
+ private readonly IResourceAssemblyProvider resourceAssemblyProvider;
/// <summary>
/// User-configured root namespaces for assemblies.
@@ -28,7 +28,7 @@ public class ResourceViewLocationProvider : IViewLocationProvider
/// Initializes a new instance of the <see cref="ResourceViewLocationProvider"/> class.
/// </summary>
public ResourceViewLocationProvider()
- : this(new DefaultResourceReader(), new DefaultAssemblyProvider())
+ : this(new DefaultResourceReader(), new ResourceAssemblyProvider())
{
}
@@ -36,11 +36,11 @@ public ResourceViewLocationProvider()
/// Initializes a new instance of the <see cref="ResourceViewLocationProvider"/> class.
/// </summary>
/// <param name="resourceReader">An <see cref="IResourceReader"/> instance that should be used when extracting embedded views.</param>
- /// <param name="assemblyProvider">An <see cref="IAssemblyProvider"/> instance that should be used to determine which assemblies to scan for embedded views.</param>
- public ResourceViewLocationProvider(IResourceReader resourceReader, IAssemblyProvider assemblyProvider)
+ /// <param name="resourceAssemblyProvider">An <see cref="IResourceAssemblyProvider"/> instance that should be used to determine which assemblies to scan for embedded views.</param>
+ public ResourceViewLocationProvider(IResourceReader resourceReader, IResourceAssemblyProvider resourceAssemblyProvider)
{
this.resourceReader = resourceReader;
- this.assemblyProvider = assemblyProvider;
+ this.resourceAssemblyProvider = resourceAssemblyProvider;
}
/// <summary>
@@ -56,7 +56,7 @@ public IEnumerable<ViewLocationResult> GetLocatedViews(IEnumerable<string> suppo
return Enumerable.Empty<ViewLocationResult>();
}
- return this.assemblyProvider
+ return this.resourceAssemblyProvider
.GetAssembliesToScan()
.Where(x => !Ignore.Contains(x))
.SelectMany(x => GetViewLocations(x, supportedViewExtensions));
Please sign in to comment.
Something went wrong with that request. Please try again.