* Improved "MessageChatTypeFilter", now accepts flagged version of "ChatType" enum. "ChatTypeFlags"

* Added classes to reactive update filter annotations implementation "FilterAnnotation"
* "CollectHandlersDowmainWide" method moved as extension to IHandlersCollection and added method "CollectHandlerAssemblyWide" named respectfully
* Added "DefaultRouterExceptionHandler" to reactive implement "IRouterExceptionHandler" from function delegate
* Small bug fixes in API overview generator logic
This commit is contained in:
2025-07-26 21:03:42 +04:00
parent cec7c88b6a
commit b86699a65e
17 changed files with 307 additions and 93 deletions
+30 -15
View File
@@ -1,10 +1,7 @@
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Reflection.Metadata.Ecma335;
using System.Text; using System.Text;
using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
namespace Telegrator.Generators namespace Telegrator.Generators
@@ -71,7 +68,7 @@ namespace Telegrator.Generators
if (!string.IsNullOrWhiteSpace(typeSummary)) if (!string.IsNullOrWhiteSpace(typeSummary))
sourceBuilder.AppendFormat("> {0}\n\n", typeSummary); sourceBuilder.AppendFormat("> {0}\n\n", typeSummary);
// Writing members // Writing fields
if (type.TypeKind == TypeKind.Enum) if (type.TypeKind == TypeKind.Enum)
{ {
WriteEnumValues(sourceBuilder, type); WriteEnumValues(sourceBuilder, type);
@@ -105,7 +102,7 @@ namespace Telegrator.Generators
sourceBuilder.AppendLine("**Constructors:**"); sourceBuilder.AppendLine("**Constructors:**");
foreach (IMethodSymbol ctor in ctors) foreach (IMethodSymbol ctor in ctors)
{ {
// Формируем строку вида ClassName<Type1, T>(Param1, Param2) с generic-аргументами // Formatting constructor signature
string genericArgs = type.FormatGenericTypes(); string genericArgs = type.FormatGenericTypes();
string parameters = string.Join(", ", ctor.Parameters.Select(p => p.Type.GetShortName())); string parameters = string.Join(", ", ctor.Parameters.Select(p => p.Type.GetShortName()));
string signature = string.Format("{0}{1}({2})", type.Name, genericArgs, parameters); string signature = string.Format("{0}{1}({2})", type.Name, genericArgs, parameters);
@@ -114,7 +111,9 @@ namespace Telegrator.Generators
// Writing summary // Writing summary
string? propSummary = ctor.ExtractSummary(); string? propSummary = ctor.ExtractSummary();
if (!string.IsNullOrWhiteSpace(propSummary)) if (!string.IsNullOrWhiteSpace(propSummary))
sourceBuilder.Append(" > ").Append(propSummary).AppendLine(); sourceBuilder.Append(" > ").Append(propSummary);
sourceBuilder.AppendLine();
} }
sourceBuilder.AppendLine(); sourceBuilder.AppendLine();
@@ -122,19 +121,32 @@ namespace Telegrator.Generators
private static void WriteEnumValues(StringBuilder sourceBuilder, INamedTypeSymbol type) private static void WriteEnumValues(StringBuilder sourceBuilder, INamedTypeSymbol type)
{ {
var members = type.GetMembers().OfType<IFieldSymbol>().Where(f => f.HasConstantValue && f.DeclaredAccessibility == Accessibility.Public).ToList(); // Getting enum values
if (members.Count == 0) List<IFieldSymbol> fields = type
.GetMembers()
.OfType<IFieldSymbol>()
.Where(f => f.HasConstantValue && f.DeclaredAccessibility == Accessibility.Public)
.ToList();
// Checking for any
if (fields.Count == 0)
return; return;
// Writing
sourceBuilder.AppendLine("**Values:**"); sourceBuilder.AppendLine("**Values:**");
foreach (IFieldSymbol field in members) foreach (IFieldSymbol field in fields)
{ {
// Writing value
sourceBuilder.Append("- `").Append(field.Name).Append("`"); sourceBuilder.Append("- `").Append(field.Name).Append("`");
// Writing summary
string? summary = field.ExtractSummary(); string? summary = field.ExtractSummary();
if (!string.IsNullOrWhiteSpace(summary)) if (!string.IsNullOrWhiteSpace(summary))
sourceBuilder.Append(" — ").Append(summary); sourceBuilder.Append(" — ").Append(summary);
sourceBuilder.AppendLine(); sourceBuilder.AppendLine();
} }
sourceBuilder.AppendLine(); sourceBuilder.AppendLine();
} }
@@ -187,9 +199,9 @@ namespace Telegrator.Generators
sourceBuilder.AppendLine("**Methods:**"); sourceBuilder.AppendLine("**Methods:**");
foreach (IMethodSymbol method in methods) foreach (IMethodSymbol method in methods)
{ {
// Формируем generic-параметры для метода // Formating method signature
string genericArgs = method.FormatGenericTypes(); string genericArgs = method.FormatGenericTypes();
string parameters = string.Join(", ", method.Parameters.Select(p => p.Type.GetShortName())); string parameters = string.Join(", ", method.Parameters.Select(p => p.Type.GetShortName()).Where(p => !string.IsNullOrEmpty(p)));
sourceBuilder.AppendFormat(" - `{0}{1}({2})`\n", method.Name, genericArgs, parameters); sourceBuilder.AppendFormat(" - `{0}{1}({2})`\n", method.Name, genericArgs, parameters);
// Writing summary // Writing summary
@@ -215,17 +227,20 @@ namespace Telegrator.Generators
try try
{ {
XDocument doc = XDocument.Parse(xmlDoc); XDocument doc = XDocument.Parse(xmlDoc);
XElement? summary = doc.Root?.Element("summary"); XElement? xSummary = doc.Root?.Element("summary");
if (summary == null) if (xSummary == null)
return null; return null;
// Убираем лишние пробелы и переносы строк // Убираем лишние пробелы и переносы строк
return summary.Value.Trim().Replace("\n", " ").Replace(" ", " "); string summary = xSummary.Value.Trim().Replace("\n", " ");
while (summary.Contains(" "))
summary = summary.Replace(" ", " ");
return summary;
} }
catch catch
{ {
// Игнорируем ошибки парсинга XML
return null; return null;
} }
} }
@@ -18,7 +18,6 @@ namespace Telegrator.Hosting.Polling
/// </summary> /// </summary>
protected readonly ILogger<HostUpdateRouter> Logger; protected readonly ILogger<HostUpdateRouter> Logger;
// Ehat a mess :/
/// <inheritdoc/> /// <inheritdoc/>
public HostUpdateRouter( public HostUpdateRouter(
IHandlersProvider handlersProvider, IHandlersProvider handlersProvider,
@@ -28,7 +27,7 @@ namespace Telegrator.Hosting.Polling
ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, options.Value, handlersPool) ILogger<HostUpdateRouter> logger) : base(handlersProvider, awaitingProvider, options.Value, handlersPool)
{ {
Logger = logger; Logger = logger;
ExceptionHandler = new HostExceptionHandler(logger); ExceptionHandler = new DefaultRouterExceptionHandler(HandleException);
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -41,21 +40,21 @@ namespace Telegrator.Hosting.Polling
/// <summary> /// <summary>
/// Default exception handler of this router /// Default exception handler of this router
/// </summary> /// </summary>
/// <param name="logger"></param> /// <param name="botClient"></param>
private class HostExceptionHandler(ILogger<HostUpdateRouter> logger) : IRouterExceptionHandler /// <param name="exception"></param>
{ /// <param name="source"></param>
/// <param name="cancellationToken"></param>
public void HandleException(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken) public void HandleException(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken)
{ {
if (exception is HandlerFaultedException handlerFaultedException) if (exception is HandlerFaultedException handlerFaultedException)
{ {
logger.LogError("\"{handler}\" handler's execution was faulted :\n{exception}", Logger.LogError("\"{handler}\" handler's execution was faulted :\n{exception}",
handlerFaultedException.HandlerInfo.ToString(), handlerFaultedException.HandlerInfo.ToString(),
handlerFaultedException.InnerException?.ToString() ?? "No inner exception"); handlerFaultedException.InnerException?.ToString() ?? "No inner exception");
return; return;
} }
logger.LogError("Exception was thrown during update routing faulted :\n{exception}", exception.ToString()); Logger.LogError("Exception was thrown during update routing faulted :\n{exception}", exception.ToString());
}
} }
} }
} }
+1 -1
View File
@@ -15,7 +15,7 @@
<EnableNETAnalyzers>True</EnableNETAnalyzers> <EnableNETAnalyzers>True</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.1</Version> <Version>1.0.2</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+1 -1
View File
@@ -43,7 +43,7 @@ namespace Telegrator.Hosting
/// <returns></returns> /// <returns></returns>
public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services) public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services)
{ {
services.AddLogging(builder => builder.AddConsole()); services.AddLogging(builder => builder.AddConsole().AddDebug());
services.AddSingleton<IUpdateHandlersPool, HostUpdateHandlersPool>(); services.AddSingleton<IUpdateHandlersPool, HostUpdateHandlersPool>();
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>(); services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
services.AddSingleton<IHandlersProvider, HostHandlersProvider>(); services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
@@ -21,10 +21,22 @@ namespace Telegrator.Annotations
/// <summary> /// <summary>
/// Attribute for filtering messages sent in chats of a specific type. /// Attribute for filtering messages sent in chats of a specific type.
/// </summary> /// </summary>
/// <param name="type">The chat type to match</param> public class ChatTypeAttribute : MessageFilterAttribute
public class ChatTypeAttribute(ChatType type) {
: MessageFilterAttribute(new MessageChatTypeFilter(type)) /// <summary>
{ } /// Initialize new instance of <see cref="ChatTypeAttribute"/> to filter messages from chat from specific chats
/// </summary>
/// <param name="type"></param>
public ChatTypeAttribute(ChatType type)
: base(new MessageChatTypeFilter(type)) { }
/// <summary>
/// Initialize new instance of <see cref="ChatTypeAttribute"/> to filter messages from chat from specific chats (with flags)
/// </summary>
/// <param name="flags"></param>
public ChatTypeAttribute(ChatTypeFlags flags)
: base(new MessageChatTypeFilter(flags)) { }
}
/// <summary> /// <summary>
/// Attribute for filtering messages based on the chat title. /// Attribute for filtering messages based on the chat title.
+49
View File
@@ -0,0 +1,49 @@
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegrator.Filters;
using Telegrator.Filters.Components;
namespace Telegrator.Attributes
{
/// <summary>
/// Reactive way to implement a new <see cref="UpdateFilterAttribute{T}"/> of type <typeparamref name="T"/>
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class FilterAnnotation<T> : UpdateFilterAttribute<T>, IFilter<T> where T : class
{
/// <inheritdoc/>
public bool IsCollectible => false;
/// <summary>
/// Initializes new instance of <see cref="FilterAnnotation{T}"/>
/// </summary>
public FilterAnnotation() : base()
{
UpdateFilter = Filter<T>.If(CanPass);
AnonymousFilter = AnonymousTypeFilter.Compile(UpdateFilter, GetFilterringTarget);
}
/// <inheritdoc/>
public abstract bool CanPass(FilterExecutionContext<T> context);
}
/// <inheritdoc/>
public abstract class MessageFilterAnnotation() : FilterAnnotation<Message>()
{
/// <inheritdoc/>
public override UpdateType[] AllowedTypes => [UpdateType.Message, UpdateType.EditedMessage, UpdateType.ChannelPost, UpdateType.EditedChannelPost, UpdateType.BusinessMessage, UpdateType.EditedBusinessMessage];
/// <inheritdoc/>
public override Message? GetFilterringTarget(Update update) => update.Message;
}
/// <inheritdoc/>
public abstract class CallbackQueryFilterAnnotation() : FilterAnnotation<CallbackQuery>()
{
/// <inheritdoc/>
public override UpdateType[] AllowedTypes => [UpdateType.CallbackQuery];
/// <inheritdoc/>
public override CallbackQuery? GetFilterringTarget(Update update) => update.CallbackQuery;
}
}
+11 -1
View File
@@ -20,7 +20,17 @@ namespace Telegrator.Attributes
/// <summary> /// <summary>
/// Gets the compiled filter logic for the update target. /// Gets the compiled filter logic for the update target.
/// </summary> /// </summary>
public Filter<T> UpdateFilter { get; private set; } public Filter<T> UpdateFilter { get; protected set; }
/// <summary>
/// Empty constructor for internal using
/// </summary>
internal UpdateFilterAttribute()
{
AnonymousFilter = null!;
UpdateFilter = null!;
_ = 0xBAD + 0xC0DE;
}
/// <summary> /// <summary>
/// Initializes the attribute with one or more filters for the update target. /// Initializes the attribute with one or more filters for the update target.
+60
View File
@@ -36,4 +36,64 @@
/// </summary> /// </summary>
Casino Casino
} }
/// <summary>
/// Flags version of <see cref="Telegram.Bot.Types.Enums.ChatType"/>
/// Type of the <see cref="Telegram.Bot.Types.Chat"/>, from which the message or inline query was sent
/// </summary>
[Flags]
public enum ChatTypeFlags
{
/// <summary>
/// Normal one-to-one chat with a user or bot
/// </summary>
Private = 0x1,
/// <summary>
/// Normal group chat
/// </summary>
Group = 0x2,
/// <summary>
/// A channel
/// </summary>
Channel = 0x4,
/// <summary>
/// A supergroup
/// </summary>
Supergroup = 0x8,
/// <summary>
/// Value possible only in <see cref="Telegram.Bot.Types.InlineQuery.ChatType"/>: private chat with the inline query sender
/// </summary>
Sender
}
/// <summary>
/// Levels of debug writing
/// </summary>
[Flags]
public enum DebugLevel
{
/// <summary>
/// Write debug messages from filters execution
/// </summary>
Filters = 0x1,
/// <summary>
/// Write debug messages from handlers providers execution
/// </summary>
Providers = 0x2,
/// <summary>
/// Write debug messages from update router's execution
/// </summary>
Router = 0x4,
/// <summary>
/// Write debug messages from handlers pool execution
/// </summary>
HandlersPool = 0x8
}
} }
+6
View File
@@ -57,6 +57,12 @@ namespace Telegrator.Filters
/// <param name="context">The filter execution context.</param> /// <param name="context">The filter execution context.</param>
/// <returns>True if the filter passes; otherwise, false.</returns> /// <returns>True if the filter passes; otherwise, false.</returns>
public abstract bool CanPass(FilterExecutionContext<T> context); public abstract bool CanPass(FilterExecutionContext<T> context);
/// <summary>
/// Implicitly creates <see cref="IFilter{T}"/> from function
/// </summary>
/// <param name="filter"></param>
public static implicit operator Filter<T>(Func<FilterExecutionContext<T>, bool> filter) => Filter<T>.If(filter);
} }
/// <summary> /// <summary>
+40 -3
View File
@@ -1,5 +1,6 @@
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
using Telegrator.Filters.Components; using Telegrator.Filters.Components;
namespace Telegrator.Filters namespace Telegrator.Filters
@@ -54,13 +55,49 @@ namespace Telegrator.Filters
/// <summary> /// <summary>
/// Filters messages whose chat type matches the specified value. /// Filters messages whose chat type matches the specified value.
/// </summary> /// </summary>
public class MessageChatTypeFilter(ChatType type) : MessageChatFilter public class MessageChatTypeFilter : MessageChatFilter
{ {
private readonly ChatType Type = type; private readonly ChatType? Type;
private readonly ChatTypeFlags? Flags;
/// <summary>
/// Initialize new instance of <see cref="MessageChatTypeFilter"/>
/// </summary>
/// <param name="type"></param>
public MessageChatTypeFilter(ChatType type)
=> Type = type;
/// <summary>
/// Initialize new instance of <see cref="MessageChatTypeFilter"/> with <see cref="ChatTypeFlags"/>
/// </summary>
/// <param name="type"></param>
public MessageChatTypeFilter(ChatTypeFlags type)
=> Flags = type;
/// <inheritdoc/> /// <inheritdoc/>
protected override bool CanPassNext(FilterExecutionContext<Chat> _) protected override bool CanPassNext(FilterExecutionContext<Chat> _)
=> Chat.Type == Type; {
if (Type.HasValue)
return Chat.Type == Type.Value;
if (Flags != null)
{
ChatTypeFlags? asFlag = ToFlag(Chat.Type);
return asFlag.HasValue && Flags.Value.HasFlag(asFlag.Value);
}
return false;
}
private static ChatTypeFlags? ToFlag(ChatType type) => type switch
{
ChatType.Channel => ChatTypeFlags.Channel,
ChatType.Group => ChatTypeFlags.Group,
ChatType.Supergroup => ChatTypeFlags.Supergroup,
ChatType.Sender => ChatTypeFlags.Sender,
ChatType.Private => ChatTypeFlags.Private,
_ => null
};
} }
/// <summary> /// <summary>
+1 -27
View File
@@ -1,4 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using Telegram.Bot.Types.Enums;
namespace Telegrator namespace Telegrator
{ {
@@ -91,31 +92,4 @@ namespace Telegrator
Debug.WriteLine(message, args); Debug.WriteLine(message, args);
} }
} }
/// <summary>
/// Levels of debug writing
/// </summary>
[Flags]
public enum DebugLevel
{
/// <summary>
/// Write debug messages from filters execution
/// </summary>
Filters = 0x1,
/// <summary>
/// Write debug messages from handlers providers execution
/// </summary>
Providers = 0x2,
/// <summary>
/// Write debug messages from update router's execution
/// </summary>
Router = 0x4,
/// <summary>
/// Write debug messages from handlers pool execution
/// </summary>
HandlersPool = 0x8
}
} }
@@ -32,11 +32,13 @@ namespace Telegrator.MadiatorCore
/// <returns>The handler descriptor list for the given update type.</returns> /// <returns>The handler descriptor list for the given update type.</returns>
public HandlerDescriptorList this[UpdateType updateType] { get; } public HandlerDescriptorList this[UpdateType updateType] { get; }
/*
/// <summary> /// <summary>
/// Collects all handlers domain-wide and returns a new <see cref="IHandlersCollection"/>. /// Collects all handlers domain-wide and returns a new <see cref="IHandlersCollection"/>.
/// </summary> /// </summary>
/// <returns>A new <see cref="IHandlersCollection"/> with all handlers collected.</returns> /// <returns>A new <see cref="IHandlersCollection"/> with all handlers collected.</returns>
public IHandlersCollection CollectHandlersDomainWide(); public IHandlersCollection CollectHandlersDomainWide();
*/
/// <summary> /// <summary>
/// Adds a <see cref="HandlerDescriptor"/> to the collection and returns the updated collection. /// Adds a <see cref="HandlerDescriptor"/> to the collection and returns the updated collection.
@@ -1,5 +1,6 @@
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Polling; using Telegram.Bot.Polling;
using Telegrator.Polling;
namespace Telegrator.MadiatorCore namespace Telegrator.MadiatorCore
{ {
@@ -0,0 +1,36 @@
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegrator.MadiatorCore;
namespace Telegrator.Polling
{
/// <summary>
/// Delegate used to handle <see cref="IUpdateRouter"/> exception
/// </summary>
/// <param name="botClient"></param>
/// <param name="exception"></param>
/// <param name="source"></param>
/// <param name="cancellationToken"></param>
public delegate void RouterExceptionHandler(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken);
/// <summary>
/// Realizes <see cref="IRouterExceptionHandler"/> using function delegate
/// </summary>
/// <param name="handler"></param>
public sealed class DefaultRouterExceptionHandler(RouterExceptionHandler handler) : IRouterExceptionHandler
{
private readonly RouterExceptionHandler _handler = handler;
/// <inheritdoc/>
public void HandleException(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken)
{
try
{
_handler.Invoke(botClient, exception, source, cancellationToken);
}
finally
{
_ = 0xBAD + 0xC0DE;
}
}}
}
@@ -1,6 +1,5 @@
using System.Reflection; using System.Reflection;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using Telegrator;
using Telegrator.Annotations; using Telegrator.Annotations;
using Telegrator.Attributes; using Telegrator.Attributes;
using Telegrator.Configuration; using Telegrator.Configuration;
@@ -60,25 +59,6 @@ namespace Telegrator.Providers
get => InnerDictionary[updateType]; get => InnerDictionary[updateType];
} }
/// <inheritdoc/>
/// <summary>
/// Collects all handlers from the entry assembly domain-wide.
/// Scans for types that implement handlers and adds them to the collection.
/// </summary>
/// <returns>This collection instance for method chaining.</returns>
/// <exception cref="Exception">Thrown when the entry assembly cannot be found.</exception>
public virtual IHandlersCollection CollectHandlersDomainWide()
{
Assembly? entryAssembly = Assembly.GetEntryAssembly() ?? throw new Exception();
entryAssembly.GetExportedTypes()
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
.Where(type => type.IsHandlerRealization())
.ForEach(type => AddHandler(type));
return this;
}
/// <inheritdoc/>
/// <summary> /// <summary>
/// Adds a handler descriptor to the collection. /// Adds a handler descriptor to the collection.
/// </summary> /// </summary>
@@ -100,7 +80,6 @@ namespace Telegrator.Providers
return this; return this;
} }
/// <inheritdoc/>
/// <summary> /// <summary>
/// Adds a handler type to the collection. /// Adds a handler type to the collection.
/// </summary> /// </summary>
@@ -112,7 +91,6 @@ namespace Telegrator.Providers
return this; return this;
} }
/// <inheritdoc/>
/// <summary> /// <summary>
/// Adds a handler type to the collection. /// Adds a handler type to the collection.
/// </summary> /// </summary>
@@ -138,7 +116,6 @@ namespace Telegrator.Providers
return this; return this;
} }
/// <inheritdoc/>
/// <summary> /// <summary>
/// Gets or creates a descriptor list for the specified update type. /// Gets or creates a descriptor list for the specified update type.
/// </summary> /// </summary>
@@ -155,7 +132,6 @@ namespace Telegrator.Providers
return list; return list;
} }
/// <inheritdoc/>
/// <summary> /// <summary>
/// Checks for intersecting command aliases and handles them according to configuration. /// Checks for intersecting command aliases and handles them according to configuration.
/// </summary> /// </summary>
+1 -1
View File
@@ -17,7 +17,7 @@
<EnableNETAnalyzers>True</EnableNETAnalyzers> <EnableNETAnalyzers>True</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.1</Version> <Version>1.0.2</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+39 -2
View File
@@ -1,7 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.Payments; using Telegram.Bot.Types.Payments;
@@ -166,8 +165,46 @@ namespace Telegrator
/// Extension methods for handlers collections. /// Extension methods for handlers collections.
/// Provides convenient methods for creating implicit handlers. /// Provides convenient methods for creating implicit handlers.
/// </summary> /// </summary>
public static class HandlersCollectionExtensions public static partial class HandlersCollectionExtensions
{ {
private static readonly string[] skippingAssemblies = ["System", "Microsoft", "Telegrator"];
/// <summary>
/// Collects all handlers from the current app domain.
/// Scans for types that implement handlers and adds them to the collection.
/// </summary>
/// <returns>This collection instance for method chaining.</returns>
/// <exception cref="Exception">Thrown when the entry assembly cannot be found.</exception>
public static IHandlersCollection CollectHandlersDomainWide(this IHandlersCollection handlers)
{
AppDomain.CurrentDomain
.GetAssemblies()
.Where(ass => skippingAssemblies.All(skip => !ass.FullName.Contains(skip)))
.SelectMany(ass => ass.GetExportedTypes())
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
.Where(type => type.IsHandlerRealization())
.ForEach(type => handlers.AddHandler(type));
return handlers;
}
/// <summary>
/// Collects all handlers from the calling this function assembly.
/// Scans for types that implement handlers and adds them to the collection.
/// </summary>
/// <returns>This collection instance for method chaining.</returns>
/// <exception cref="Exception">Thrown when the entry assembly cannot be found.</exception>
public static IHandlersCollection CollectHandlersAssemblyWide(this IHandlersCollection handlers)
{
Assembly.GetCallingAssembly()
.GetExportedTypes()
.Where(type => type.GetCustomAttribute<DontCollectAttribute>() == null)
.Where(type => type.IsHandlerRealization())
.ForEach(type => handlers.AddHandler(type));
return handlers;
}
/// <summary> /// <summary>
/// Creates a handler builder for a specific update type. /// Creates a handler builder for a specific update type.
/// </summary> /// </summary>