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

Enable interface-wide command registration #165

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ app.Run();
You can add (classic) Class-based style commands with the `AddCommands<T>` method.

```csharp
// Adds a MyCommand class
app.AddCommands<MyCommand>();

// Adds all classes inheriting from the ICommands interface
app.AddCommands<ICommands>();
```

#### Public method as a command (Class-based style)
Expand Down
15 changes: 14 additions & 1 deletion src/Cocona.Core/Builder/CoconaCommandsBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,21 @@ public static CommandTypeConventionBuilder AddCommands(this ICoconaCommandsBuild
ThrowHelper.ThrowIfNull(commandType);

var conventions = new List<Action<ICommandBuilder>>();
builder.Add(new TypeCommandDataSource(commandType, conventions, builder.GetFilters().ToArray()));
if (!commandType.IsInterface)
{
builder.Add(new TypeCommandDataSource(commandType, conventions, builder.GetFilters().ToArray()));
return new CommandTypeConventionBuilder(conventions);
}

var commandTypes = commandType.Assembly.DefinedTypes
.Where(x => x is { IsAbstract: false, IsInterface: false } && commandType.IsAssignableFrom(x))
.Select(x => x.AsType());

foreach (var type in commandTypes)
{
builder.Add(new TypeCommandDataSource(type, conventions, builder.GetFilters().ToArray()));
}

return new CommandTypeConventionBuilder(conventions);
}

Expand Down
23 changes: 22 additions & 1 deletion test/Cocona.Test/Builder/CoconaCommandBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ class MyCommands
{
public void Hello() => Console.WriteLine("Hello");
}

[Fact]
public void AddCommands_Interface()
{
var builder = new CoconaCommandsBuilder();
builder.AddCommands<ICommands>();

var built = builder.Build();
built.Should().HaveCount(2);
built[0].Should().BeOfType<TypeCommandData>().Subject.Type.Should().Be<InterfaceCommands>();
built[1].Should().BeOfType<TypeCommandData>().Subject.Type.Should().Be<MoreInterfaceCommands>();
}

interface ICommands
{ }

class InterfaceCommands : ICommands
{ }

class MoreInterfaceCommands : ICommands
{ }

[Fact]
public void Filter_Builder_UseFilter()
Expand All @@ -175,4 +196,4 @@ class UseFilter_Filter2 : IFilterFactory
}
class UseFilter_Filter3 : IFilterMetadata
{ }
}
}
Loading