Skip to content

Commit

Permalink
Merge pull request #189 from cihandeniz/issue/auto-find-entry-assembly
Browse files Browse the repository at this point in the history
Issue / Auto Find Entry Assembly
  • Loading branch information
cihandeniz authored Oct 31, 2024
2 parents 474c8c2 + ea82afd commit 0d88ec4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/features/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ c => c.ObjectAsJson()

## Records are DTOs

Configures domain type records as valid input paramters. Methods containing
Configures domain type records as valid input parameters. Methods containing
record parameters render as api endpoints.

```csharp
Expand Down
8 changes: 8 additions & 0 deletions src/core/Baked.Architecture/Testing/Nfr.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using NUnit.Framework;
using System.Reflection;

namespace Baked.Testing;

public abstract class Nfr
{
public static Assembly? EntryAssembly { get; private set; }

protected static void Init<TEntryPoint>() where TEntryPoint : class
{
EntryAssembly = typeof(TEntryPoint).Assembly;
}

[OneTimeSetUp]
public virtual Task OneTimeSetUp() => Task.CompletedTask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static DotnetCoreFeature Dotnet(this CoreConfigurator _,
Func<Assembly, string?>? baseNamespace = default
)
{
entryAssembly ??= Assembly.GetEntryAssembly() ?? throw new("'EntryAssembly' should have existed");
baseNamespace ??= assembly => Regexes.AssemblyNameBeforeApplicationSuffix().Match(assembly.FullName ?? string.Empty).Value;

return new(entryAssembly, baseNamespace);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
using Baked.Architecture;
using Baked.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System.Reflection;

namespace Baked.Core.Dotnet;

public class DotnetCoreFeature(Assembly _entryAssembly, Func<Assembly, string?> _baseNamespace)
public class DotnetCoreFeature(Assembly? _entryAssembly, Func<Assembly, string?> _baseNamespace)
: IFeature<CoreConfigurator>
{
public void Configure(LayerConfigurator configurator)
{
configurator.ConfigureServiceCollection(services =>
{
var entryAssembly = _entryAssembly
?? (configurator.IsNfr() ? Nfr.EntryAssembly : Assembly.GetEntryAssembly())
?? throw new("'EntryAssembly' should have existed");
services.AddSingleton(TimeProvider.System);
services.AddSingleton<ITextTransformer, HumanizerTextTransformer>();
services.AddFileProvider(new EmbeddedFileProvider(_entryAssembly, _baseNamespace(_entryAssembly)));
services.AddFileProvider(new PhysicalFileProvider(Path.GetDirectoryName(_entryAssembly.Location) ??
throw new("'EntryAssembly' should have a not null location"))
);
services.AddFileProvider(new EmbeddedFileProvider(entryAssembly, _baseNamespace(entryAssembly)));
services.AddFileProvider(new PhysicalFileProvider(Path.GetDirectoryName(entryAssembly.Location) ?? string.Empty));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public abstract class WebApplicationNfr : Nfr

protected static IConfiguration Configuration => ServiceProvider.GetRequiredService<IConfiguration>();

protected static void Init<TEntryPoint>() where TEntryPoint : class
protected static new void Init<TEntryPoint>() where TEntryPoint : class
{
Nfr.Init<TEntryPoint>();

Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", nameof(Nfr));

var webApplicationFactory = new WebApplicationFactory<TEntryPoint>();
Expand Down
5 changes: 1 addition & 4 deletions test/recipe/Baked.Test.Recipe.Service.Application/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Baked.Test.Orm;
using System.Reflection;

Bake.New
.Service(
Expand Down Expand Up @@ -28,9 +27,7 @@
claims: ["User", "Admin", "BaseA", "BaseB", "GivenA", "GivenB", "GivenC"],
baseClaims: ["BaseA", "BaseB"]
),
core: c => c
.Dotnet(baseNamespace: _ => "Baked.Test")
.ForNfr(c.Dotnet(entryAssembly: Assembly.GetExecutingAssembly(), baseNamespace: _ => "Baked.Test")),
core: c => c.Dotnet(baseNamespace: _ => "Baked.Test"),
cors: c => c.AspNetCore(Settings.Required<string>("CorsOrigin")),
database: c => c
.Sqlite()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net.Http.Json;

namespace Baked.Test.Runtime;
namespace Baked.Test.Core;

public class ReadingResources : TestServiceNfr
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.FileProviders;

namespace Baked.Test.Core;

public class ReadingResourcesDuringTests : TestServiceSpec
{
[TestCase("Core/DomainEmbedded.txt")]
public void Specs_includes_business_resources(string subpath)
{
var provider = GiveMe.The<IFileProvider>();

var exists = provider.Exists(subpath);

exists.ShouldBeTrue();
}

[TestCase("Core/ApplicationEmbedded.txt")]
[TestCase("Core/ApplicationPhysical.txt")]
public void Specs_excludes_application_resources(string subpath)
{
var provider = GiveMe.The<IFileProvider>();

var exists = provider.Exists(subpath);

exists.ShouldBeFalse();
}
}

0 comments on commit 0d88ec4

Please sign in to comment.