Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining the assemblies where the problems are located #181

Merged
merged 2 commits into from
Nov 29, 2023
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
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