Skip to content

Commit

Permalink
removed IConventionFactory and replaced with a simple delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Dec 29, 2024
1 parent 4a3c7d7 commit 8fe10d0
Show file tree
Hide file tree
Showing 968 changed files with 9,763 additions and 17,366 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" />
<PackageVersion Include="System.Collections.Immutable" Version="9.0.0" />
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.12.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Features" Version="4.12.0" />
Expand Down
16 changes: 8 additions & 8 deletions benchmarks/Hosting.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Benchmarks
public async Task Default_Hosting()
{
using var host = Host
.CreateDefaultBuilder(Array.Empty<string>())
.CreateDefaultBuilder()
.ConfigureLogging(x => x.ClearProviders())
.Build();
await host.StartAsync().ConfigureAwait(false);
Expand All @@ -50,7 +50,7 @@ public async Task Default_Hosting()
[Benchmark(Baseline = true)]
public async Task Default_Hosting_Application()
{
var builder = Host.CreateApplicationBuilder(Array.Empty<string>());
var builder = Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();
var host = builder.Build();
await host.StartAsync().ConfigureAwait(false);
Expand All @@ -60,9 +60,9 @@ public async Task Default_Hosting_Application()
[Benchmark]
public async Task Rocket_Surgery_Hosting_Application()
{
var builder = Host.CreateApplicationBuilder(Array.Empty<string>());
var builder = Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();
var host = await builder.ConfigureRocketSurgery(Imports.Instance);
var host = await builder.ConfigureRocketSurgery();
await host.StartAsync().ConfigureAwait(false);
await host.StopAsync().ConfigureAwait(false);
}
Expand All @@ -71,7 +71,7 @@ public async Task Rocket_Surgery_Hosting_Application()
public async Task Default_Hosting_With_Service()
{
using var host = Host
.CreateDefaultBuilder(Array.Empty<string>())
.CreateDefaultBuilder()
.ConfigureLogging(x => x.ClearProviders())
.ConfigureServices(x => x.AddHostedService<HostedService>())
.Build();
Expand All @@ -82,10 +82,10 @@ public async Task Default_Hosting_With_Service()
[Benchmark]
public async Task Rocket_Surgery_Hosting_Application_With_Service()
{
var builder = Host.CreateApplicationBuilder(Array.Empty<string>());
var builder = Host.CreateApplicationBuilder();
builder.Services.AddHostedService<HostedService>();
builder.Logging.ClearProviders();
var host = await builder.ConfigureRocketSurgery(Imports.Instance);
var host = await builder.ConfigureRocketSurgery();
await host.StartAsync().ConfigureAwait(false);
await host.StopAsync().ConfigureAwait(false);
}
Expand All @@ -102,4 +102,4 @@ public Task StopAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
}
}
}
}
4 changes: 2 additions & 2 deletions sample/Diagnostics/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static async Task<int> Main(string[] args)

public static async Task<IHost> CreateHostBuilder(string[] args)
{
return await Host.CreateApplicationBuilder(args).LaunchWith(RocketBooster.For(Imports.Instance));
return await Host.CreateApplicationBuilder(args).ConfigureRocketSurgery();
}
}

Expand Down Expand Up @@ -59,4 +59,4 @@ public override async Task<int> ExecuteAsync(CommandContext context, AppSettings

return 0;
}
}
}
4 changes: 2 additions & 2 deletions sample/Sample.BlazorWasm/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new(builder.HostEnvironment.BaseAddress), });

var host = await builder.ConfigureRocketSurgery(Imports.Instance);
await host.RunAsync();
var host = await builder.ConfigureRocketSurgery();
await host.RunAsync();
2 changes: 1 addition & 1 deletion sample/Sample.Core.Tests/SampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task Should_Register_Services()

public SampleTests()
{
_builder = new ConventionContextBuilder(new Dictionary<object, object>(), []).Set(HostType.UnitTest);
_builder = ConventionContextBuilder.Create(_ => [], new Dictionary<object, object>(), []).Set(HostType.UnitTest);
}

private readonly ConventionContextBuilder _builder;
Expand Down
2 changes: 1 addition & 1 deletion sample/Sample.Tests/SampleTestHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Should_Register_Services()

public async Task InitializeAsync()
{
var builder = ConventionContextBuilder.Create().ForTesting(Imports.Instance);
var builder = ConventionContextBuilder.Create(_ => []).ForTesting();
_host = await Host.CreateApplicationBuilder().ConfigureRocketSurgery(builder);
await _host.StartAsync();
}
Expand Down
14 changes: 11 additions & 3 deletions src/Conventions.Abstractions/ConventionContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public sealed class ConventionContextBuilder
{
internal readonly ConventionContextState state;

/// <summary>
/// Create a default context builder
/// </summary>
/// <param name="conventionFactory"></param>
/// <returns></returns>
public static ConventionContextBuilder Create(LoadConventions conventionFactory) =>
new(conventionFactory, new PropertiesDictionary(), []);

/// <summary>
/// Create a default context builder
/// </summary>
Expand All @@ -25,7 +33,7 @@ public sealed class ConventionContextBuilder
/// <param name="categories"></param>
/// <returns></returns>
[OverloadResolutionPriority(-1)]
public static ConventionContextBuilder Create(IConventionFactory conventionFactory, PropertiesType? properties = null, params ConventionCategory[] categories) =>
public static ConventionContextBuilder Create(LoadConventions conventionFactory, PropertiesType? properties, params ConventionCategory[] categories) =>
new(conventionFactory, properties ?? new PropertiesDictionary(), categories);

/// <summary>
Expand All @@ -35,7 +43,7 @@ public static ConventionContextBuilder Create(IConventionFactory conventionFacto
/// <param name="properties"></param>
/// <param name="categories"></param>
/// <returns></returns>
public static ConventionContextBuilder Create(IConventionFactory conventionFactory, PropertiesType? properties = null, params IEnumerable<ConventionCategory> categories) =>
public static ConventionContextBuilder Create(LoadConventions conventionFactory, PropertiesType? properties, params IEnumerable<ConventionCategory> categories) =>
new(conventionFactory, properties ?? new PropertiesDictionary(), categories);

private static readonly string[] categoryEnvironmentVariables =
Expand All @@ -49,7 +57,7 @@ public static ConventionContextBuilder Create(IConventionFactory conventionFacto
/// <param name="conventionFactory"></param>
/// <param name="properties"></param>
/// <param name="categories"></param>
private ConventionContextBuilder(IConventionFactory conventionFactory, PropertiesType? properties, IEnumerable<ConventionCategory> categories)
private ConventionContextBuilder(LoadConventions conventionFactory, PropertiesType? properties, IEnumerable<ConventionCategory> categories)
{
Properties = new ServiceProviderDictionary(properties ?? new PropertiesDictionary());
Properties.Set(conventionFactory);
Expand Down
4 changes: 2 additions & 2 deletions src/Conventions.Abstractions/ConventionContextState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public void ExceptConventions(params IEnumerable<Assembly> assemblies)

internal IEnumerable<IConventionMetadata> CalculateConventions(
ConventionContextBuilder builder,
IConventionFactory factory,
LoadConventions factory,
ILogger? logger
)
{
logger ??= NullLogger.Instance;
var conventions = factory.LoadConventions(builder);
var conventions = factory(builder);

if (_exceptAssemblyConventions.Count > 0)
SkippingConventionsInAssemblies(logger, _exceptAssemblyConventions.Select(x => x.GetName().Name));
Expand Down
14 changes: 0 additions & 14 deletions src/Conventions.Abstractions/IConventionFactory.cs

This file was deleted.

21 changes: 9 additions & 12 deletions src/Conventions.Abstractions/ImportHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@ public class ImportsTypeAttribute(Type type) : Attribute
public static class ImportHelpers
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static IConventionFactory? ExternalConventions
public static LoadConventions? ExternalConventions
{
get => externalConventions;
set => externalConventions = value;
get => externalLoader;
set => externalLoader = value;
}

[EditorBrowsable(EditorBrowsableState.Never)]
public static IConventionFactory OrCallerConventions(this IConventionFactory conventionFactory)
public static LoadConventions OrCallerConventions(this LoadConventions loader)
{
ArgumentNullException.ThrowIfNull(conventionFactory);
return Assembly.GetEntryAssembly() is not { } entryAssembly || conventionFactory.GetType().Assembly == entryAssembly || externalConventions == null
? conventionFactory
: entryAssembly.GetCustomAttribute<ImportsTypeAttribute>()?.Type is { } executingImportsType
&& Activator.CreateInstance(executingImportsType) is IConventionFactory imports
? imports
: externalConventions;
ArgumentNullException.ThrowIfNull(loader);
return Assembly.GetEntryAssembly() is not { } entryAssembly || loader.GetType().Assembly == entryAssembly || externalLoader == null
? loader
: externalLoader;
}

private static IConventionFactory? externalConventions;
private static LoadConventions? externalLoader;
}
6 changes: 6 additions & 0 deletions src/Conventions.Abstractions/LoadConventions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Rocket.Surgery.Conventions;

/// <summary>
/// A factory that provides a list of conventions
/// </summary>
public delegate IEnumerable<IConventionMetadata> LoadConventions(ConventionContextBuilder builder);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" VersionOverride="4.8.0" PrivateAssets="all">
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" VersionOverride="4.8.0" PrivateAssets="all">
<Version>4.12.0</Version>
</PackageReference>
<PackageReference Include="System.Text.Json" PrivateAssets="all" />
Expand Down
2 changes: 1 addition & 1 deletion src/Conventions.Analyzers/ConventionConfigurationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ public static InnerConventionConfigurationData FromDefaults(ConventionConfigurat
public bool DefinedNamespace { get; init; }
public bool WasConfigured { get; init; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
<PackageReference Include="System.Text.Json" PrivateAssets="all" />
<PackageReference Include="Polyfill" PrivateAssets="All" />
<ProjectReference
Expand Down
Loading

0 comments on commit 8fe10d0

Please sign in to comment.