Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/NetEvolve.ForgingBlazor/Commands/CommandBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.CommandLine;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NetEvolve.ForgingBlazor.Configurations;
using NetEvolve.ForgingBlazor.Extensibility;
using NetEvolve.ForgingBlazor.Extensibility.Abstractions;
Expand All @@ -22,24 +23,33 @@
/// </remarks>
/// <seealso cref="CommandOptions"/>
/// <seealso cref="IStartUpMarker"/>
internal sealed class CommandBuild : Command, IStartUpMarker
internal sealed partial class CommandBuild : Command, IStartUpMarker
{
/// <summary>
/// Stores the service provider for transferring services during command execution.
/// </summary>
private readonly IServiceProvider _serviceProvider;

/// <summary>
/// Stores the logger instance for logging command execution details.
/// </summary>
private readonly ILogger _logger;

/// <summary>
/// Initializes a new instance of the <see cref="CommandBuild"/> class with the specified service provider.
/// </summary>
/// <param name="serviceProvider">
/// The <see cref="IServiceProvider"/> instance providing access to registered application services.
/// This is used to transfer services for command execution.
/// </param>
public CommandBuild(IServiceProvider serviceProvider)
/// <param name="logger">
/// The <see cref="ILogger{T}"/> instance for logging command execution details.
/// </param>
public CommandBuild(IServiceProvider serviceProvider, ILogger<CommandBuild> logger)
: base("build", "Builds and generates static content for a Forging Blazor application.")
{
_serviceProvider = serviceProvider;
_logger = logger;

Add(CommandOptions.ContentPath);
Add(CommandOptions.Environment);
Expand Down Expand Up @@ -111,9 +121,17 @@ private async Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken

return 0;
}
catch (Exception)
catch (Exception ex)
{
LogUnhandledException(ex);
return 1;
}
}

[LoggerMessage(
EventId = 1,
Level = LogLevel.Error,
Message = "An unhandled exception occurred during the build process."
)]
private partial void LogUnhandledException(Exception ex);
}
26 changes: 24 additions & 2 deletions src/NetEvolve.ForgingBlazor/Services/ContentRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ public async ValueTask CollectAsync(CancellationToken cancellationToken)
{
var collectors = contentCollectors.GetOrAdd(
registration.Segment,
(segment) => _serviceProvider.GetKeyedServices<IContentCollector>(segment)
_serviceProvider.GetKeyedServices<IContentCollector>
);

foreach (var collector in collectors)
{
var collectorTypeFullName = collector.GetType().Name;
var collectorTypeFullName = GetCollectorName(collector);
LogStartingContentCollection(registration.Segment, collectorTypeFullName);
await collector.CollectAsync(this, registration, cancellationToken).ConfigureAwait(false);
LogCompletedContentCollection(registration.Segment, collectorTypeFullName);
Expand Down Expand Up @@ -180,6 +180,28 @@ private static IContentRegistration[] UpdateRegistrations(IContentRegistration[]
return [.. registrations.OrderByDescending(r => r.Priority)];
}

private readonly ConcurrentDictionary<Type, string> _collectorNameCache = new();

private string GetCollectorName(IContentCollector collector)
{
var collectorType = collector.GetType();

return _collectorNameCache.GetOrAdd(collectorType, GetTypeName);

static string GetTypeName(Type type)
{
if (type.IsGenericType)
{
return type.Name.Split('`')[0]
+ "<"
+ string.Join(", ", type.GetGenericArguments().Select(x => GetTypeName(x)).ToArray())
+ ">";
}

return type.Name;
}
}

/// <summary>
/// Logs that content collection has started for a specific segment and collector.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public async ValueTask Build_DefaultArguments_GeneratesStaticContent(string[] ar
{
using var directory = new TempDirectory();

if (args is not null && args.Length != 0)
if (args is { Length: > 0 })
{
args = [.. args, directory.Path, "--content-path", "_setup/content"];
}
else
{
args = ["build", "--content-path", "_setup/Content"];
args = ["build", "--content-path", "_setup/content"];
}

await Helper.VerifyStaticContent(directory.Path, args).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class ConfigurationLoaderTests
public async ValueTask Load_ConfigurationFile_Expected(string projectPath, string? environment)
{
var services = new ServiceCollection()
.AddSingleton(sp => ConfigurationLoader.Load(environment, projectPath))
.AddSingleton(_ => ConfigurationLoader.Load(environment, projectPath))
.ConfigureOptions<SiteConfigurationConfigure>();
var serviceProvider = services.BuildServiceProvider();

Expand Down
7 changes: 6 additions & 1 deletion tests/NetEvolve.ForgingBlazor.Tests.Integration/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
namespace NetEvolve.ForgingBlazor.Tests.Integration;

using Microsoft.Extensions.Logging;
using NetEvolve.ForgingBlazor.Logging;

internal static class Helper
{
public static async ValueTask VerifyStaticContent(string directoryPath, string[] args)
{
var builder = ApplicationBuilder.CreateDefaultBuilder(args);
var builder = ApplicationBuilder
.CreateDefaultBuilder(args)
.WithLogging(loggingBuilder => loggingBuilder.AddConsole().SetMinimumLevel(LogLevel.Debug));

var app = builder.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NetEvolve.ForgingBlazor.Logging\NetEvolve.ForgingBlazor.Logging.csproj" />
<ProjectReference Include="..\..\src\NetEvolve.ForgingBlazor\NetEvolve.ForgingBlazor.csproj" />
</ItemGroup>

Expand Down
Loading