ProxyAll for interface part 1...
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
{
|
||||
internal abstract class BaseGenerator
|
||||
{
|
||||
protected readonly Context _context;
|
||||
protected readonly IDictionary<InterfaceDeclarationSyntax, ProxyData> _candidateInterfaces;
|
||||
|
||||
public BaseGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces)
|
||||
{
|
||||
_context = context;
|
||||
_candidateInterfaces = candidateInterfaces;
|
||||
}
|
||||
|
||||
protected INamedTypeSymbol GetType(string name)
|
||||
{
|
||||
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(name);
|
||||
if (symbol is null)
|
||||
{
|
||||
throw new Exception($"The type '{name}' is not found.");
|
||||
}
|
||||
|
||||
return symbol;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,32 +10,38 @@ using ProxyInterfaceSourceGenerator.Utils;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
{
|
||||
internal class PartialInterfacesGenerator : IFilesGenerator
|
||||
internal class PartialInterfacesGenerator : BaseGenerator, IFilesGenerator
|
||||
{
|
||||
private readonly Context _context;
|
||||
private readonly IDictionary<InterfaceDeclarationSyntax, ProxyData> _candidateInterfaces;
|
||||
private readonly List<FileData> files = new List<FileData>();
|
||||
|
||||
public PartialInterfacesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces)
|
||||
public PartialInterfacesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces) :
|
||||
base(context, candidateInterfaces)
|
||||
{
|
||||
_context = context;
|
||||
_candidateInterfaces = candidateInterfaces;
|
||||
}
|
||||
|
||||
public IEnumerable<FileData> GenerateFiles()
|
||||
{
|
||||
foreach (var ci in _candidateInterfaces)
|
||||
{
|
||||
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(ci.Value.TypeName);
|
||||
if (symbol is null)
|
||||
{
|
||||
throw new Exception($"The type '{ci.Value.TypeName}' is not found.");
|
||||
}
|
||||
|
||||
yield return new FileData(
|
||||
$"{ci.Value.InterfaceName}.cs",
|
||||
CreatePartialInterfaceCode(symbol, ci.Value.InterfaceName, ci.Value.ProxyAll)
|
||||
);
|
||||
var file = GenerateFile(ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
|
||||
files.Add(file);
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
private FileData GenerateFile(string interfaceName, string typeName, bool proxyAll)
|
||||
{
|
||||
var symbol = GetType(typeName);
|
||||
|
||||
var file = new FileData(
|
||||
$"{interfaceName}.cs",
|
||||
CreatePartialInterfaceCode(symbol, interfaceName, proxyAll)
|
||||
);
|
||||
|
||||
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = null, FileData = file });
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
private string CreatePartialInterfaceCode(INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
|
||||
@@ -78,14 +84,29 @@ namespace {symbol.ContainingNamespace}
|
||||
p => p.Type.TypeKind != TypeKind.Interface
|
||||
};
|
||||
|
||||
if (proxyAll)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
foreach (var property in MemberHelper.GetPublicProperties(symbol, complexFilters.ToArray()))
|
||||
{
|
||||
str.AppendLine($" {property.ToPropertyText()}");
|
||||
if (proxyAll)
|
||||
{
|
||||
var existing = _context.GeneratedData
|
||||
.FirstOrDefault(x => x.ClassName == $"{property.Name}Proxy" || x.InterfaceName == $"I{property.Name}");
|
||||
|
||||
if (existing is not null)
|
||||
{
|
||||
str.AppendLine($" {property.ToPropertyText(existing.InterfaceName)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create new
|
||||
var typeName = $"{property.Type}";
|
||||
var file = GenerateFile($"I{property.Name}", typeName, false);
|
||||
str.AppendLine($" // {property.ToPropertyText($"I{property.Name}")}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
str.AppendLine($" {property.ToPropertyText()}");
|
||||
}
|
||||
str.AppendLine();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,26 +9,18 @@ using ProxyInterfaceSourceGenerator.Utils;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.FileGenerators
|
||||
{
|
||||
internal class ProxyClassesGenerator : IFilesGenerator
|
||||
internal class ProxyClassesGenerator : BaseGenerator, IFilesGenerator
|
||||
{
|
||||
private readonly Context _context;
|
||||
private readonly IDictionary<InterfaceDeclarationSyntax, ProxyData> _candidateInterfaces;
|
||||
|
||||
public ProxyClassesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces)
|
||||
public ProxyClassesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces) :
|
||||
base(context, candidateInterfaces)
|
||||
{
|
||||
_context = context;
|
||||
_candidateInterfaces = candidateInterfaces;
|
||||
}
|
||||
|
||||
public IEnumerable<FileData> GenerateFiles()
|
||||
{
|
||||
foreach (var ci in _candidateInterfaces)
|
||||
{
|
||||
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(ci.Value.TypeName);
|
||||
if (symbol is null)
|
||||
{
|
||||
throw new Exception($"The type '{ci.Value.TypeName}' is not found.");
|
||||
}
|
||||
var symbol = GetType(ci.Value.TypeName);
|
||||
|
||||
yield return new FileData(
|
||||
$"{ci.Value.ClassName}Proxy.cs",
|
||||
@@ -44,20 +36,25 @@ namespace {symbol.ContainingNamespace}
|
||||
public class {className}Proxy : {interfaceName}
|
||||
{{
|
||||
private {className} _instance;
|
||||
{GenerateComplexFields(symbol,proxyAll)}
|
||||
{GeneratePrivateComplexInterfaceFields(symbol,proxyAll)}
|
||||
|
||||
public {className}Proxy({className} instance)
|
||||
{{
|
||||
_instance = instance;
|
||||
}}
|
||||
|
||||
{GenerateProperties(symbol, proxyAll)}
|
||||
{GeneratePublicProperties(symbol, proxyAll)}
|
||||
|
||||
{GenerateMethods(symbol)}
|
||||
{GeneratePublicMethods(symbol)}
|
||||
}}
|
||||
}}";
|
||||
private string GenerateComplexFields(INamedTypeSymbol symbol, bool proxyAll)
|
||||
private string GeneratePrivateComplexInterfaceFields(INamedTypeSymbol symbol, bool proxyAll)
|
||||
{
|
||||
if (!proxyAll)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var str = new StringBuilder();
|
||||
|
||||
foreach (var property in GetComplexProperties(symbol, proxyAll))
|
||||
@@ -68,7 +65,7 @@ namespace {symbol.ContainingNamespace}
|
||||
return str.ToString();
|
||||
}
|
||||
|
||||
private string GenerateProperties(INamedTypeSymbol symbol, bool proxyAll)
|
||||
private string GeneratePublicProperties(INamedTypeSymbol symbol, bool proxyAll)
|
||||
{
|
||||
var str = new StringBuilder();
|
||||
|
||||
@@ -115,7 +112,7 @@ namespace {symbol.ContainingNamespace}
|
||||
return MemberHelper.GetPublicProperties(symbol, complexFilters.ToArray());
|
||||
}
|
||||
|
||||
private string GenerateMethods(INamedTypeSymbol symbol)
|
||||
private string GeneratePublicMethods(INamedTypeSymbol symbol)
|
||||
{
|
||||
var str = new StringBuilder();
|
||||
foreach (var method in MemberHelper.GetPublicMethods(symbol))
|
||||
|
||||
Reference in New Issue
Block a user