Adding xml loader to source generator to add comments from nuget package

This commit is contained in:
Florian Zevedei 2024-07-15 18:51:30 +02:00
parent 9bdea52df9
commit a1c4629351
4 changed files with 15431 additions and 2 deletions

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,10 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\Telegram.Bot.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>

View File

@ -1,12 +1,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using TelegramBotBase.SourceGenerators;
namespace TelegramBotBase
{
@ -14,6 +19,8 @@ namespace TelegramBotBase
[Generator(LanguageNames.CSharp)]
public class TelegramDeviceExtensionGenerator : IIncrementalGenerator
{
XmlDocumentationLoader xml;
public void Initialize(IncrementalGeneratorInitializationContext context)
{
@ -27,8 +34,11 @@ namespace TelegramBotBase
context.RegisterSourceOutput(compilation, (spc, source) => Execute(spc, source));
}
private void Execute(SourceProductionContext context, Compilation compilation)
{
//if (!Debugger.IsAttached) Debugger.Launch();
@ -41,6 +51,15 @@ namespace TelegramBotBase
if (telegram_package == null)
return;
//Load only once
if (xml == null)
{
xml = new XmlDocumentationLoader();
xml.ReadEmbeddedXml("Telegram.Bot.xml");
}
var assemblySymbol = compilation.GetAssemblyOrModuleSymbol(telegram_package) as IAssemblySymbol;
if (assemblySymbol == null)
@ -57,6 +76,7 @@ namespace TelegramBotBase
//Get existing list of methods
var methods = apiClass.GetMembers().OfType<IMethodSymbol>().ToList();
foreach (var method in methods)
{
if (!method.Parameters.Any(a => a.Type.Name == "ITelegramBotClient"))
@ -65,7 +85,7 @@ namespace TelegramBotBase
if (!method.Parameters.Any(a => a.Type.Name == "ChatId"))
continue;
if (method.Name == ".ctor")
if (method.Name == ".ctor")
continue;
String parameters = "";
@ -158,10 +178,23 @@ namespace TelegramBotBase
}
private static String GenerateMethod(IMethodSymbol? method, string parameters, string subCallParameters, string returnStatement)
/// <summary>
/// Test
/// </summary>
/// <param name="method"></param>
/// <param name="parameters"></param>
/// <param name="subCallParameters"></param>
/// <param name="returnStatement"></param>
/// <returns></returns>
private String GenerateMethod(IMethodSymbol? method, string parameters, string subCallParameters, string returnStatement)
{
//Adding xml comments from embedded xml file (Workaround)
String xml_comments = xml?.GetDocumentationLinesForSymbol(method);
StringBuilder sb = new StringBuilder();
sb.AppendLine(xml_comments);
sb.AppendLine($" public static async {method.ReturnType.ToDisplayString()} {method.Name}(this DeviceSession device, {parameters})");
sb.AppendLine($" {{");
@ -172,6 +205,8 @@ namespace TelegramBotBase
sb.AppendLine();
sb.AppendLine();
return sb.ToString();
}
}

View File

@ -0,0 +1,89 @@
using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;
namespace TelegramBotBase.SourceGenerators
{
public class XmlDocumentationLoader
{
XDocument xDocument;
public string GetDocumentationLinesForSymbol(ISymbol symbol)
{
var docElement = xDocument?.Descendants("member")
.FirstOrDefault(e => e.Attribute("name")?.Value == GetDocumentationCommentId(symbol));
StringBuilder sb = new StringBuilder();
XNode first = docElement.FirstNode;
do
{
sb.AppendLine(first.ToString());
first = first.NextNode;
}
while (first.NextNode != null);
var lines = sb.ToString().Split('\n');
sb = new StringBuilder();
foreach (var line in lines)
{
if (line == "")
continue;
sb.AppendLine($" /// {line.Trim()}");
}
return sb.ToString().Trim();
}
private string GetDocumentationCommentId(ISymbol symbol)
{
// Returns the documentation comment ID for a symbol
return symbol.GetDocumentationCommentId();
}
public XDocument ReadEmbeddedXml(string resourceName)
{
// Get the assembly where the resource is embedded
Assembly assembly = Assembly.GetExecutingAssembly();
// Construct the full resource name
string fullResourceName = $"{assembly.GetName().Name}.Resources.{resourceName}";
var names = assembly.GetManifestResourceNames();
if (!names.Contains(fullResourceName))
return null;
// Open a stream to the embedded resource
using (Stream stream = assembly.GetManifestResourceStream(fullResourceName))
{
if (stream == null)
{
//throw new FileNotFoundException("Resource not found", fullResourceName);
return null;
}
xDocument = XDocument.Load(stream);
// Load the stream into an XDocument
return xDocument;
}
}
}
}