Add support for base (proxy) class (#29)

This commit is contained in:
Stef Heyenrath
2022-02-04 11:33:26 +01:00
committed by GitHub
parent 94d322cfb3
commit d7483d6b7e
38 changed files with 400 additions and 309 deletions
@@ -3,6 +3,7 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using ProxyInterfaceSourceGenerator.FileGenerators;
using ProxyInterfaceSourceGenerator.Models;
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
namespace ProxyInterfaceSourceGenerator;
@@ -10,7 +11,7 @@ namespace ProxyInterfaceSourceGenerator;
[Generator]
internal class ProxyInterfaceCodeGenerator : ISourceGenerator
{
private readonly ProxyAttributeGenerator _proxyAttributeGenerator = new ProxyAttributeGenerator();
private readonly ProxyAttributeGenerator _proxyAttributeGenerator = new ();
public void Initialize(GeneratorInitializationContext context)
{
@@ -24,21 +25,21 @@ internal class ProxyInterfaceCodeGenerator : ISourceGenerator
public void Execute(GeneratorExecutionContext context)
{
if (context.ParseOptions is not CSharpParseOptions csharpParseOptions)
{
throw new NotSupportedException("Only C# is supported.");
}
if (context.SyntaxReceiver is not ProxySyntaxReceiver receiver)
{
return;
}
// https://github.com/reactiveui/refit/blob/main/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs
var supportsNullable = csharpParseOptions.LanguageVersion >= LanguageVersion.CSharp8;
try
{
if (context.ParseOptions is not CSharpParseOptions csharpParseOptions)
{
throw new NotSupportedException("Only C# is supported.");
}
if (context.SyntaxReceiver is not ProxySyntaxReceiver receiver)
{
throw new NotSupportedException($"Only {nameof(ProxySyntaxReceiver)} is supported.");
}
// https://github.com/reactiveui/refit/blob/main/InterfaceStubGenerator.Core/InterfaceStubGenerator.cs
var supportsNullable = csharpParseOptions.LanguageVersion >= LanguageVersion.CSharp8;
GenerateProxyAttribute(context, receiver);
GeneratePartialInterfaces(context, receiver, supportsNullable);
GenerateProxyClasses(context, receiver, supportsNullable);
@@ -51,7 +52,7 @@ internal class ProxyInterfaceCodeGenerator : ISourceGenerator
private void GenerateError(GeneratorExecutionContext context, Exception exception)
{
var message = $"/*\r\n{nameof(ProxyInterfaceCodeGenerator)}\r\n\r\n{exception}\r\n\r\n{exception.StackTrace}*/";
var message = $"/*\r\n{nameof(ProxyInterfaceCodeGenerator)}\r\n\r\n[Exception]\r\n{exception}\r\n\r\n[StackTrace]\r\n{exception.StackTrace}*/";
context.AddSource("Error.g", SourceText.From(message, Encoding.UTF8));
}
@@ -76,9 +77,9 @@ internal class ProxyInterfaceCodeGenerator : ISourceGenerator
};
var partialInterfacesGenerator = new PartialInterfacesGenerator(context, supportsNullable);
foreach (var data in partialInterfacesGenerator.GenerateFiles())
foreach (var (fileName, text) in partialInterfacesGenerator.GenerateFiles())
{
context.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
context.GeneratorExecutionContext.AddSource(fileName, SourceText.From(text, Encoding.UTF8));
}
}
@@ -91,9 +92,9 @@ internal class ProxyInterfaceCodeGenerator : ISourceGenerator
};
var proxyClassesGenerator = new ProxyClassesGenerator(context, supportsNullable);
foreach (var data in proxyClassesGenerator.GenerateFiles())
foreach (var (fileName, text) in proxyClassesGenerator.GenerateFiles())
{
context.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
context.GeneratorExecutionContext.AddSource(fileName, SourceText.From(text, Encoding.UTF8));
}
}
}