Skip to content

Commit

Permalink
Allow defining the assemblies where the problems are located (#181)
Browse files Browse the repository at this point in the history
* Add `SolverConfiguration.ProblemAssemblies` property, defaulting to current behavior (Assembly.GetEntryAssembly())

---------

Co-authored-by: Eduardo Cáceres <[email protected]>
  • Loading branch information
codemonkey85 and eduherminio authored Nov 29, 2023
1 parent de7ff78 commit f91dd1b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/AoCHelper/Solver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Spectre.Console;
using System.Diagnostics;
using System.Diagnostics;
using System.Reflection;
using Spectre.Console;

namespace AoCHelper
{
Expand Down Expand Up @@ -32,7 +32,7 @@ await AnsiConsole.Live(table)
.Cropping(configuration.VerticalOverflowCropping)
.StartAsync(async ctx =>
{
var lastProblem = LoadAllProblems(Assembly.GetEntryAssembly()!).LastOrDefault();
var lastProblem = LoadAllProblems(configuration.ProblemAssemblies).LastOrDefault();
if (lastProblem is not null)
{
var sw = new Stopwatch();
Expand Down Expand Up @@ -137,7 +137,7 @@ await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
var sw = new Stopwatch();
foreach (Type problemType in LoadAllProblems(Assembly.GetEntryAssembly()!))
foreach (Type problemType in LoadAllProblems(configuration.ProblemAssemblies))
{
if (problems.Contains(problemType))
{
Expand Down Expand Up @@ -187,7 +187,7 @@ await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
var sw = new Stopwatch();
foreach (Type problemType in LoadAllProblems(Assembly.GetEntryAssembly()!))
foreach (Type problemType in LoadAllProblems(configuration.ProblemAssemblies))
{
sw.Restart();
// Since we're trying to instantiate them all, we don't want to show unrelated errors or render unrelated problem rows
Expand Down Expand Up @@ -231,7 +231,7 @@ await AnsiConsole.Live(table)
.StartAsync(async ctx =>
{
var sw = new Stopwatch();
foreach (Type problemType in LoadAllProblems(Assembly.GetEntryAssembly()!))
foreach (Type problemType in LoadAllProblems(configuration.ProblemAssemblies))
{
sw.Restart();
var potentialProblem = InstantiateProblem(problemType);
Expand All @@ -253,13 +253,13 @@ await AnsiConsole.Live(table)
}

/// <summary>
/// Loads all <see cref="BaseProblem"/> in the given assembly
/// Loads all <see cref="BaseProblem"/> in the given assemblies
/// </summary>
/// <param name="assembly"></param>
/// <param name="assemblies"></param>
/// <returns></returns>
internal static IEnumerable<Type> LoadAllProblems(Assembly assembly)
internal static IEnumerable<Type> LoadAllProblems(List<Assembly> assemblies)
{
return assembly.GetTypes()
return assemblies.SelectMany(a => a.GetTypes())
.Where(type => typeof(BaseProblem).IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract)
.OrderBy(t => t.FullName);
}
Expand Down
11 changes: 10 additions & 1 deletion src/AoCHelper/SolverConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Spectre.Console;
using System.Reflection;
using Spectre.Console;

namespace AoCHelper
{
Expand Down Expand Up @@ -33,6 +34,12 @@ public class SolverConfiguration
/// </summary>
public string? ElapsedTimeFormatSpecifier { get; set; }

/// <summary>
/// Assemblies where the problems are located.
/// Defaults to the entry assembly: [Assembly.GetEntryAssembly()!]
/// </summary>
public List<Assembly> ProblemAssemblies { get; set; }

/// <summary>
/// Represents vertical overflow.
/// <see href="https://spectreconsole.net/live/live-display"/>
Expand All @@ -47,6 +54,8 @@ public class SolverConfiguration

public SolverConfiguration()
{
ProblemAssemblies = [Assembly.GetEntryAssembly()!];

ClearConsole = true;
ShowOverallResults = true;
ShowConstructorElapsedTime = false;
Expand Down
6 changes: 3 additions & 3 deletions tests/AoCHelper.Test/SolverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ public void LoadAllProblems()
{
Assert.Equal(
Assembly.GetExecutingAssembly()!.GetTypes().Count(type => typeof(BaseProblem).IsAssignableFrom(type) && !type.IsAbstract),
Solver.LoadAllProblems(Assembly.GetExecutingAssembly()).Count());
Solver.LoadAllProblems([Assembly.GetExecutingAssembly()]).Count());
}

[Fact]
public void LoadAllProblems_OrderedByFullName()
{
var orderedTypes = Solver.LoadAllProblems(Assembly.GetExecutingAssembly()).OrderBy(t => t.FullName);
var types = Solver.LoadAllProblems(Assembly.GetExecutingAssembly());
var orderedTypes = Solver.LoadAllProblems([Assembly.GetExecutingAssembly()]).OrderBy(t => t.FullName);
var types = Solver.LoadAllProblems([Assembly.GetExecutingAssembly()]);

foreach (var (First, Second) in orderedTypes.Zip(types))
{
Expand Down

0 comments on commit f91dd1b

Please sign in to comment.