Adding source generator to add all API methods to the DeviceSession
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
||||
<OutputItemType>Analyzer</OutputItemType>
|
||||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,174 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace TelegramBotBase
|
||||
{
|
||||
|
||||
[Generator(LanguageNames.CSharp)]
|
||||
public class TelegramDeviceExtensionGenerator : IIncrementalGenerator
|
||||
{
|
||||
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
var provider = context.SyntaxProvider.CreateSyntaxProvider(
|
||||
predicate: (c, _) => c is ClassDeclarationSyntax,
|
||||
transform: (n, _) => (ClassDeclarationSyntax)n.Node)
|
||||
.Where(a => a is not null);
|
||||
|
||||
|
||||
var compilation = context.CompilationProvider;
|
||||
|
||||
context.RegisterSourceOutput(compilation, (spc, source) => Execute(spc, source));
|
||||
|
||||
}
|
||||
|
||||
private void Execute(SourceProductionContext context, Compilation compilation)
|
||||
{
|
||||
//if (!Debugger.IsAttached) Debugger.Launch();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
var telegram_package = compilation.References.FirstOrDefault(a => a.Display != null && a.Display.Contains("Telegram.Bot"));
|
||||
if (telegram_package == null)
|
||||
return;
|
||||
|
||||
var assemblySymbol = compilation.GetAssemblyOrModuleSymbol(telegram_package) as IAssemblySymbol;
|
||||
|
||||
if (assemblySymbol == null)
|
||||
return;
|
||||
|
||||
if (assemblySymbol.Name != "Telegram.Bot")
|
||||
return;
|
||||
|
||||
var apiClass = assemblySymbol.GetTypeByMetadataName("Telegram.Bot.TelegramBotClientExtensions");
|
||||
if (apiClass == null)
|
||||
return;
|
||||
|
||||
var methods = apiClass.GetMembers().OfType<IMethodSymbol>().ToList();
|
||||
|
||||
foreach (var method in methods)
|
||||
{
|
||||
if (!method.Parameters.Any(a => a.Type.Name == "ITelegramBotClient"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!method.Parameters.Any(a => a.Type.Name == "ChatId"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (method.Name == ".ctor") continue;
|
||||
|
||||
String parameters = "";
|
||||
String subCallParameters = "";
|
||||
foreach (var par in method.Parameters)
|
||||
{
|
||||
if (par.Name == "botClient")
|
||||
continue;
|
||||
|
||||
if (!string.IsNullOrEmpty(parameters))
|
||||
{
|
||||
parameters += ", ";
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(subCallParameters))
|
||||
{
|
||||
subCallParameters += ", ";
|
||||
}
|
||||
|
||||
if (par.Name != "chatId")
|
||||
{
|
||||
subCallParameters += $"{par.Name}";
|
||||
parameters += $"{par.Type.ToDisplayString()} {par.Name}";
|
||||
}
|
||||
else
|
||||
{
|
||||
subCallParameters += $"device.DeviceId";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
var returnStatement = "";
|
||||
|
||||
if (method.ReturnType is INamedTypeSymbol namedType && namedType.IsGenericType && namedType.ConstructedFrom.Name == "Task" && namedType.ContainingNamespace.ToDisplayString() == "System.Threading.Tasks")
|
||||
{
|
||||
returnStatement = "return await";
|
||||
}
|
||||
else if (method.ReturnType.Name == "Task" && method.ReturnType.ContainingNamespace.ToDisplayString() == "System.Threading.Tasks")
|
||||
{
|
||||
returnStatement = "await";
|
||||
}
|
||||
else if (method.ReturnsVoid)
|
||||
{
|
||||
returnStatement = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
returnStatement = "return ";
|
||||
}
|
||||
|
||||
sb.AppendLine($$""""
|
||||
|
||||
|
||||
public static async {{method.ReturnType.ToDisplayString()}} {{method.Name}}(this DeviceSession device, {{parameters}}){
|
||||
|
||||
|
||||
{{returnStatement}} device.Client.TelegramClient.{{method.Name}}({{subCallParameters}});
|
||||
}
|
||||
|
||||
|
||||
"""");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Der generierte Code
|
||||
var sourceCode = $$"""
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Sessions;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Extensions;
|
||||
using Telegram.Bot.Requests;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegram.Bot.Types.InlineQueryResults;
|
||||
using Telegram.Bot.Types.Payments;
|
||||
using Telegram.Bot.Types.ReplyMarkups;
|
||||
using File = Telegram.Bot.Types.File;
|
||||
|
||||
#nullable enable
|
||||
namespace TelegramBotBase;
|
||||
|
||||
public static class DeviceExtensions
|
||||
{
|
||||
{{sb.ToString()}}
|
||||
}
|
||||
|
||||
""";
|
||||
|
||||
//Cleanup
|
||||
|
||||
sourceCode = sourceCode.Replace("System.Threading.Tasks.", "");
|
||||
//sourceCode = sourceCode.Replace("Telegram.Bot.", "");
|
||||
//sourceCode = sourceCode.Replace("Telegram.Bot.Types.", "");
|
||||
|
||||
|
||||
// Fügen Sie den generierten Code der Compilation hinzu
|
||||
context.AddSource("DeviceExtensions.g.cs", SourceText.From(sourceCode, Encoding.UTF8));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user