Skip to content

Commit

Permalink
refactor(CommandFactory.cs): simplify ApplyExtensions method by remov…
Browse files Browse the repository at this point in the history
…ing nested try-catch and redundant checks
  • Loading branch information
tgardner authored and jeremydmiller committed Sep 4, 2024
1 parent 093625d commit 0134fb9
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/Oakton/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,35 +144,35 @@ public IEnumerable<IOaktonCommand> BuildAllCommands()

public void ApplyExtensions(IServiceCollection services)
{
if (_extensionTypes.Any())
try
{
try
foreach (var extensionType in _extensionTypes)
{
foreach (var extensionType in _extensionTypes)
{
var extension = Activator.CreateInstance(extensionType) as IServiceRegistrations;
extension?.Configure(services);
}

_hasAppliedExtensions = true;
var extension = Activator.CreateInstance(extensionType) as IServiceRegistrations;
extension?.Configure(services);
}
catch (Exception)

_hasAppliedExtensions = true;
}
catch (Exception)
{
// Swallow the error
if (_hasAppliedExtensions)
{
// Swallow the error
if (_hasAppliedExtensions)
{
return;
}

AnsiConsole.MarkupLine(
$"[red]Unable to apply Oakton extensions. Try adding IHostBuilder.{nameof(CommandLineHostingExtensions.ApplyOaktonExtensions)}(); to your bootstrapping code to apply Oakton extension loading[/]");
return;
}

AnsiConsole.MarkupLine(
$"[red]Unable to apply Oakton extensions. Try adding IHostBuilder.{nameof(CommandLineHostingExtensions.ApplyOaktonExtensions)}(); to your bootstrapping code to apply Oakton extension loading[/]");
}
}

public void ApplyExtensions(IHostBuilder builder)
{
builder.ConfigureServices(ApplyExtensions);
if (_extensionTypes.Any())
{
builder.ConfigureServices(ApplyExtensions);
}
}

public IEnumerable<Type> AllCommandTypes()
Expand Down

0 comments on commit 0134fb9

Please sign in to comment.