Adding xml loader to source generator to add comments from nuget package
This commit is contained in:
parent
9bdea52df9
commit
a1c4629351
15301
TelegramBotBase.SourceGenerators/Resources/Telegram.Bot.xml
Normal file
15301
TelegramBotBase.SourceGenerators/Resources/Telegram.Bot.xml
Normal file
File diff suppressed because it is too large
Load Diff
@ -10,6 +10,10 @@
|
|||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<EmbeddedResource Include="Resources\Telegram.Bot.xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
|
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
|||||||
@ -1,12 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Resources;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
using Microsoft.CodeAnalysis.Text;
|
using Microsoft.CodeAnalysis.Text;
|
||||||
|
using TelegramBotBase.SourceGenerators;
|
||||||
|
|
||||||
namespace TelegramBotBase
|
namespace TelegramBotBase
|
||||||
{
|
{
|
||||||
@ -14,6 +19,8 @@ namespace TelegramBotBase
|
|||||||
[Generator(LanguageNames.CSharp)]
|
[Generator(LanguageNames.CSharp)]
|
||||||
public class TelegramDeviceExtensionGenerator : IIncrementalGenerator
|
public class TelegramDeviceExtensionGenerator : IIncrementalGenerator
|
||||||
{
|
{
|
||||||
|
XmlDocumentationLoader xml;
|
||||||
|
|
||||||
|
|
||||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||||
{
|
{
|
||||||
@ -27,8 +34,11 @@ namespace TelegramBotBase
|
|||||||
|
|
||||||
context.RegisterSourceOutput(compilation, (spc, source) => Execute(spc, source));
|
context.RegisterSourceOutput(compilation, (spc, source) => Execute(spc, source));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void Execute(SourceProductionContext context, Compilation compilation)
|
private void Execute(SourceProductionContext context, Compilation compilation)
|
||||||
{
|
{
|
||||||
//if (!Debugger.IsAttached) Debugger.Launch();
|
//if (!Debugger.IsAttached) Debugger.Launch();
|
||||||
@ -41,6 +51,15 @@ namespace TelegramBotBase
|
|||||||
if (telegram_package == null)
|
if (telegram_package == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Load only once
|
||||||
|
if (xml == null)
|
||||||
|
{
|
||||||
|
xml = new XmlDocumentationLoader();
|
||||||
|
xml.ReadEmbeddedXml("Telegram.Bot.xml");
|
||||||
|
}
|
||||||
|
|
||||||
var assemblySymbol = compilation.GetAssemblyOrModuleSymbol(telegram_package) as IAssemblySymbol;
|
var assemblySymbol = compilation.GetAssemblyOrModuleSymbol(telegram_package) as IAssemblySymbol;
|
||||||
|
|
||||||
if (assemblySymbol == null)
|
if (assemblySymbol == null)
|
||||||
@ -57,6 +76,7 @@ namespace TelegramBotBase
|
|||||||
//Get existing list of methods
|
//Get existing list of methods
|
||||||
var methods = apiClass.GetMembers().OfType<IMethodSymbol>().ToList();
|
var methods = apiClass.GetMembers().OfType<IMethodSymbol>().ToList();
|
||||||
|
|
||||||
|
|
||||||
foreach (var method in methods)
|
foreach (var method in methods)
|
||||||
{
|
{
|
||||||
if (!method.Parameters.Any(a => a.Type.Name == "ITelegramBotClient"))
|
if (!method.Parameters.Any(a => a.Type.Name == "ITelegramBotClient"))
|
||||||
@ -65,7 +85,7 @@ namespace TelegramBotBase
|
|||||||
if (!method.Parameters.Any(a => a.Type.Name == "ChatId"))
|
if (!method.Parameters.Any(a => a.Type.Name == "ChatId"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (method.Name == ".ctor")
|
if (method.Name == ".ctor")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
String parameters = "";
|
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();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.AppendLine(xml_comments);
|
||||||
|
|
||||||
sb.AppendLine($" public static async {method.ReturnType.ToDisplayString()} {method.Name}(this DeviceSession device, {parameters})");
|
sb.AppendLine($" public static async {method.ReturnType.ToDisplayString()} {method.Name}(this DeviceSession device, {parameters})");
|
||||||
|
|
||||||
sb.AppendLine($" {{");
|
sb.AppendLine($" {{");
|
||||||
@ -172,6 +205,8 @@ namespace TelegramBotBase
|
|||||||
|
|
||||||
sb.AppendLine();
|
sb.AppendLine();
|
||||||
|
|
||||||
|
sb.AppendLine();
|
||||||
|
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
89
TelegramBotBase.SourceGenerators/XmlDocumentationLoader.cs
Normal file
89
TelegramBotBase.SourceGenerators/XmlDocumentationLoader.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user