Skip to content

Commit

Permalink
Merge pull request #12 from MariBotOfficial/redesign
Browse files Browse the repository at this point in the history
[ WIP ] Redesign
  • Loading branch information
renanrcp authored Aug 13, 2020
2 parents 5dea855 + f5d6b5a commit 00899a5
Show file tree
Hide file tree
Showing 134 changed files with 3,868 additions and 1,277 deletions.
4 changes: 4 additions & 0 deletions MariCommands.Tests/MariCommands.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MariCommands\MariCommands.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions MariCommands/Attributes/Commands/CommandAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using MariCommands.Utils;

namespace MariCommands
{
Expand Down
2 changes: 2 additions & 0 deletions MariCommands/Attributes/Core/ArgumentParserAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using MariCommands.Parsers;
using MariCommands.Utils;

namespace MariCommands
{
Expand Down
1 change: 1 addition & 0 deletions MariCommands/Attributes/Core/DescriptionAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MariCommands.Utils;

namespace MariCommands
{
Expand Down
1 change: 1 addition & 0 deletions MariCommands/Attributes/Core/NameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MariCommands.Utils;

namespace MariCommands
{
Expand Down
1 change: 1 addition & 0 deletions MariCommands/Attributes/Core/RemarksAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MariCommands.Utils;

namespace MariCommands
{
Expand Down
1 change: 1 addition & 0 deletions MariCommands/Attributes/Modules/GroupAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using MariCommands.Utils;

namespace MariCommands
{
Expand Down
25 changes: 0 additions & 25 deletions MariCommands/Attributes/Modules/LifetimeAttribute.cs

This file was deleted.

6 changes: 4 additions & 2 deletions MariCommands/Attributes/Params/TypeParserAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using MariCommands.TypeParsers;
using MariCommands.Utils;

namespace MariCommands
{
Expand All @@ -9,12 +11,12 @@ namespace MariCommands
public class TypeParserAttribute : Attribute
{
/// <summary>
/// The type of the <see cref="ITypeParser{T}" />.
/// The type of the <see cref="ITypeParser" />.
/// </summary>
public Type Value { get; }

/// <summary>
/// Defines the <see cref="ITypeParser{T}" /> for this param.
/// Defines the <see cref="ITypeParser" /> for this param.
/// </summary>
/// <param name="typeParserType"></param>
/// <exception cref="ArgumentNullException">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using MariCommands.Results;

namespace MariCommands
{
Expand All @@ -15,13 +16,11 @@ public abstract class ParamPreconditionAttribute : Attribute
/// <param name="value">The parsed value of this parameter.</param>
/// <param name="parameter">The parameter that is linked to that attribute.</param>
/// <param name="context">The current command execution context.</param>
/// <param name="provider">The current dependencies container.</param>
/// <returns>A <see cref="Task" /> representing an asynchronous operation
/// with an <see cref="IPreconditionResult" />.</returns>
public abstract Task<IPreconditionResult> ExecuteAsync(
object value,
IParameter parameter,
CommandContext context,
IServiceProvider provider);
CommandContext context);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using MariCommands.Results;

namespace MariCommands
{
Expand All @@ -12,10 +13,10 @@ public abstract class PreconditionAttribute : Attribute
/// <summary>
/// Asynchronously execute this precondition.
/// </summary>
/// <param name="command">The command that is linked to this attribute.</param>
/// <param name="context">The current command execution context.</param>
/// <param name="provider">The current dependencies container.</param>
/// <returns>A <see cref="Task" /> representing an asynchronous operation
/// with an <see cref="IPreconditionResult" />.</returns>
public abstract Task<IPreconditionResult> ExecuteAsync(CommandContext context, IServiceProvider provider);
public abstract Task<IPreconditionResult> ExecuteAsync(ICommand command, CommandContext context);
}
}
86 changes: 86 additions & 0 deletions MariCommands/Builder/CommandApplicationBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MariCommands.Extensions;
using MariCommands.Middlewares;
using MariCommands.Utils;
using MariGlobals.Extensions;

namespace MariCommands.Builder
{
/// <inheritdoc />
public class CommandApplicationBuilder : ICommandApplicationBuilder
{
private readonly List<Func<CommandDelegate, CommandDelegate>> _components;

/// <summary>
/// Creates a new instance of <see cref="CommandApplicationBuilder" />.
/// </summary>
/// <param name="properties">The properties of this builder.</param>
/// <param name="applicationServices">The service container for this application.</param>
public CommandApplicationBuilder(IDictionary<string, object> properties, IServiceProvider applicationServices)
{
_components = new List<Func<CommandDelegate, CommandDelegate>>();
Properties = properties;
ApplicationServices = applicationServices;
}

/// <summary>
/// Creates a new instance of <see cref="CommandApplicationBuilder" />.
/// </summary>
/// <param name="applicationServices">The service container for this application.</param>
public CommandApplicationBuilder(IServiceProvider applicationServices)
: this(new Dictionary<string, object>(), applicationServices)
{
}

/// <inheritdoc />
public IDictionary<string, object> Properties { get; }

/// <inheritdoc />
public IServiceProvider ApplicationServices { get; }

/// <inheritdoc />
public ICommandApplicationBuilder Use(Func<CommandDelegate, CommandDelegate> component)
{
_components.Add(component);
return this;
}

/// <inheritdoc />
public CommandDelegate Build()
{
CommandDelegate app = context =>
{
context.NotNull(nameof(context));

if (context.Command.HasNoContent())
{
var message =
$"The request reached the end of the pipeline without a command defined." +
$"Please register the {nameof(CommandParserMiddleware)} using '{nameof(ICommandApplicationBuilder)}'.{nameof(MariCommandsApplicationBuilderExtensions.UseParser)}().";

throw new InvalidOperationException(message);
}

if (context.Result.HasNoContent())
{
var message =
$"The request reached the end of the pipeline without executing the command: '{context.Command.Name}'." +
$"Please register the {nameof(CommandExecutorMiddleware)} using '{nameof(ICommandApplicationBuilder)}.{nameof(MariCommandsApplicationBuilderExtensions.UseCommandExecutor)}().'";

throw new InvalidOperationException(message);
}

return Task.CompletedTask;
};

_components.Reverse();

foreach (var component in _components)
app = component(app);

return app;
}
}
}
34 changes: 34 additions & 0 deletions MariCommands/Builder/ICommandApplicationBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;

namespace MariCommands.Builder
{
/// <summary>
/// Represents a builder that will build the command pipeline request.
/// </summary>
public interface ICommandApplicationBuilder
{
/// <summary>
/// Properties to be used across middlewares.
/// </summary>
IDictionary<string, object> Properties { get; }

/// <summary>
/// The general service container for this application.
/// </summary>
IServiceProvider ApplicationServices { get; }

/// <summary>
/// Add a middleware to the command execution pipeline.
/// </summary>
/// <param name="component">The middleware function to execute the current command request.</param>
/// <returns>The current command application builder.</returns>
ICommandApplicationBuilder Use(Func<CommandDelegate, CommandDelegate> component);

/// <summary>
/// Build the command execution pipeline.
/// </summary>
/// <returns></returns>
CommandDelegate Build();
}
}
59 changes: 59 additions & 0 deletions MariCommands/Builder/IModuleConfigurer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace MariCommands.Builder
{
/// <summary>
/// A configurer for Add Modules.
/// </summary>
public interface IModuleConfigurer
{
/// <summary>
/// General config for <see cref="ICommandService" />
/// </summary>
ICommandServiceOptions Options { get; }

/// <summary>
/// Search all modules in your project and add them to the module cache.
/// </summary>
/// <param name="assembly">The assembly.</param>
IReadOnlyCollection<IModule> AddModules(Assembly assembly);

/// <summary>
/// Add this module to the module cache.
/// </summary>
/// <param name="module">Any module.</param>
IModule AddModule(IModule module);

/// <summary>
/// Add this module to the module cache.
/// </summary>
/// <param name="builder">Any module builder.</param>
IModule AddModule(IModuleBuilder builder)
=> AddModule(builder.Build(null));

/// <summary>
/// Add this module type to the module cache.
/// </summary>
/// <typeparam ref="T">Any module type.</typeparam>
IModule AddModule<T>()
where T : class
{
return AddModule(typeof(T));
}


/// <summary>
/// Add this module type to the module cache.
/// </summary>
/// <param name="type">Any module type.</param>
IModule AddModule(Type type);

/// <summary>
/// Remove this module from the module cache.
/// </summary>
/// <param name="module">The module to be removed.</param>
void RemoveModule(IModule module);
}
}
Loading

0 comments on commit 00899a5

Please sign in to comment.