This commit is contained in:
Stef Heyenrath
2021-07-25 10:41:29 +02:00
parent 97fddc2f3f
commit 9326899151
12 changed files with 116 additions and 118 deletions
@@ -1,20 +1,15 @@
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)
public BaseGenerator(Context context)
{
_context = context;
_candidateInterfaces = candidateInterfaces;
}
protected INamedTypeSymbol GetType(string name)
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
@@ -14,14 +11,14 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
private readonly List<FileData> files = new List<FileData>();
public PartialInterfacesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces) :
base(context, candidateInterfaces)
public PartialInterfacesGenerator(Context context) :
base(context)
{
}
public IEnumerable<FileData> GenerateFiles()
{
foreach (var ci in _candidateInterfaces)
foreach (var ci in _context.CandidateInterfaces)
{
var file = GenerateFile(ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
files.Add(file);
@@ -61,52 +58,42 @@ namespace {symbol.ContainingNamespace}
var str = new StringBuilder();
// SimpleProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.Type.IsValueType || p.Type.ToString() == "string"))
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.ValueTypeOrString))
{
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
}
// InterfaceProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol,
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind == TypeKind.Interface)
)
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Interface))
{
str.AppendLine($" {property.ToPropertyText()}");
str.AppendLine();
}
// ComplexProperties
var complexFilters = new List<Func<IPropertySymbol, bool>>
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Complex))
{
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind != TypeKind.Interface
};
//if (proxyAll)
//{
// var existing = _context.GeneratedData
// .FirstOrDefault(x => x.ClassName == $"{property.Name}Proxy" || x.InterfaceName == $"I{property.Name}");
foreach (var property in MemberHelper.GetPublicProperties(symbol, complexFilters.ToArray()))
{
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()}");
}
// 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();
}
@@ -118,11 +105,11 @@ namespace {symbol.ContainingNamespace}
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
{
str.AppendLine($" {method.ToMethodTextForInterface()};");
str.AppendLine($" {method.ToMethodText()};");
str.AppendLine();
}
return "// Methods"; // str.ToString();
return str.ToString();
}
}
}
@@ -16,7 +16,7 @@ namespace ProxyInterfaceGenerator
public Type Type {{ get; }}
public bool ProxyAll {{ get; }}
public {ClassName}(Type type, bool proxyAll = true)
public {ClassName}(Type type, bool proxyAll = false)
{{
Type = type;
ProxyAll = proxyAll;
@@ -1,32 +1,44 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ProxyInterfaceSourceGenerator.Enums;
using ProxyInterfaceSourceGenerator.Extensions;
using ProxyInterfaceSourceGenerator.SyntaxReceiver;
using ProxyInterfaceSourceGenerator.Utils;
namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal class ProxyClassesGenerator : BaseGenerator, IFilesGenerator
{
public ProxyClassesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces) :
base(context, candidateInterfaces)
private readonly List<FileData> files = new List<FileData>();
public ProxyClassesGenerator(Context context) :
base(context)
{
}
public IEnumerable<FileData> GenerateFiles()
{
foreach (var ci in _candidateInterfaces)
foreach (var ci in _context.CandidateInterfaces)
{
var symbol = GetType(ci.Value.TypeName);
yield return new FileData(
$"{ci.Value.ClassName}Proxy.cs",
CreateProxyClassCode(symbol, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.ProxyAll)
);
var file = GenerateFile(ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
files.Add(file);
}
return files;
}
private FileData GenerateFile(string interfaceName, string className, string typeName, bool proxyAll)
{
var symbol = GetType(typeName);
var file = new FileData(
$"{className}Proxy.cs",
CreateProxyClassCode(symbol, interfaceName, className, proxyAll)
);
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = className, FileData = file });
return file;
}
private string CreateProxyClassCode(INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
@@ -35,12 +47,11 @@ namespace {symbol.ContainingNamespace}
{{
public class {className}Proxy : {interfaceName}
{{
private {className} _instance;
{GeneratePrivateComplexInterfaceFields(symbol,proxyAll)}
public {className} _Instance {{ get; }}
public {className}Proxy({className} instance)
{{
_instance = instance;
_Instance = instance;
}}
{GeneratePublicProperties(symbol, proxyAll)}
@@ -48,46 +59,27 @@ namespace {symbol.ContainingNamespace}
{GeneratePublicMethods(symbol)}
}}
}}";
private string GeneratePrivateComplexInterfaceFields(INamedTypeSymbol symbol, bool proxyAll)
{
if (!proxyAll)
{
return string.Empty;
}
var str = new StringBuilder();
foreach (var property in GetComplexProperties(symbol, proxyAll))
{
str.AppendLine($" private {property.Type} _{property.Name};");
}
return str.ToString();
}
private string GeneratePublicProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var str = new StringBuilder();
// SimpleProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.Type.IsValueType || p.Type.ToString() == "string"))
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.ValueTypeOrString))
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
// InterfaceProperties
foreach (var property in MemberHelper.GetPublicProperties(symbol,
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind == TypeKind.Interface)
)
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Interface))
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
// ComplexProperties
foreach (var property in GetComplexProperties(symbol, proxyAll))
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.GetTypeEnum() == TypeEnum.Complex))
{
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
@@ -96,22 +88,6 @@ namespace {symbol.ContainingNamespace}
return str.ToString();
}
private IEnumerable<IPropertySymbol> GetComplexProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var complexFilters = new List<Func<IPropertySymbol, bool>>
{
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind != TypeKind.Interface
};
if (proxyAll)
{
}
return MemberHelper.GetPublicProperties(symbol, complexFilters.ToArray());
}
private string GeneratePublicMethods(INamedTypeSymbol symbol)
{
var str = new StringBuilder();