This commit is contained in:
Stef Heyenrath
2021-07-23 22:10:08 +02:00
parent d129f52441
commit 36fdd645b1
12 changed files with 112 additions and 69 deletions
@@ -1,9 +1,6 @@
namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal record Data
internal record Data(string FileName, string Text)
{
public string FileName { get; init; }
public string Text { get; init; }
}
}
@@ -12,10 +12,10 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal class PartialInterfacesGenerator : IFilesGenerator
{
private readonly GeneratorExecutionContext _context;
private readonly Context _context;
private readonly IDictionary<InterfaceDeclarationSyntax, ProxyData> _candidateInterfaces;
public PartialInterfacesGenerator(GeneratorExecutionContext context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces)
public PartialInterfacesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces)
{
_context = context;
_candidateInterfaces = candidateInterfaces;
@@ -25,7 +25,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _candidateInterfaces)
{
var symbol = _context.Compilation.GetTypeByMetadataName(ci.Value.TypeName);
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(ci.Value.TypeName);
if (symbol is null)
{
throw new Exception($"The type '{ci.Value.TypeName}' is not found.");
@@ -33,15 +33,11 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
string interfaceName = $"I{ci.Value.TypeName.Split('.').Last()}";
yield return new Data
{
FileName = $"I{interfaceName}.cs",
Text = CreatePartialInterfaceCode(symbol, interfaceName)
};
yield return new Data($"I{interfaceName}.cs", CreatePartialInterfaceCode(symbol, interfaceName, ci.Value.ProxyAll));
}
}
private string CreatePartialInterfaceCode(INamedTypeSymbol symbol, string interfaceName) => $@"using System;
private string CreatePartialInterfaceCode(INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
namespace {symbol.ContainingNamespace}
{{
@@ -49,6 +45,10 @@ namespace {symbol.ContainingNamespace}
{{
{GenerateSimpleProperties(symbol)}
{GenerateInterfaceProperties(symbol)}
{GenerateComplexProperties(symbol, proxyAll)}
{GenerateMethods(symbol)}
}}
}}";
@@ -58,19 +58,57 @@ namespace {symbol.ContainingNamespace}
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.Type.IsValueType || p.Type.ToString() == "string"))
{
str.AppendLine($" {property.ToCode()}");
str.AppendLine($" {property.ToPropertyTextForInterface()}");
str.AppendLine();
}
return str.ToString();
}
private string GenerateInterfaceProperties(INamedTypeSymbol symbol)
{
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol,
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind == TypeKind.Interface)
)
{
str.AppendLine($" {property.ToPropertyTextForInterface()}");
str.AppendLine();
}
return "// GenerateInterfaceProperties";// str.ToString();
}
private string GenerateComplexProperties(INamedTypeSymbol symbol, bool proxyAll)
{
var filters = new List<Func<IPropertySymbol, bool>>
{
p => !(p.Type.IsValueType || p.Type.ToString() == "string"),
p => p.Type.TypeKind != TypeKind.Interface
};
if (proxyAll)
{
}
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol, filters.ToArray()))
{
str.AppendLine($" {property.ToPropertyTextForInterface()}");
str.AppendLine();
}
return "// GenerateComplexProperties";// str.ToString();
}
private string GenerateMethods(INamedTypeSymbol symbol)
{
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
{
str.AppendLine($" {method.ToCode()};");
str.AppendLine($" {method.ToMethodTextForInterface()};");
str.AppendLine();
}
@@ -6,10 +6,7 @@
public Data GenerateFile()
{
return new Data
{
FileName = $"{ClassName}.cs",
Text = $@"using System;
return new Data($"{ClassName}.cs", $@"using System;
namespace ProxyInterfaceGenerator
{{
@@ -25,8 +22,7 @@ namespace ProxyInterfaceGenerator
ProxyAll = proxyAll;
}}
}}
}}"
};
}}");
}
}
}
@@ -12,10 +12,10 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal class ProxyClassesGenerator : IFilesGenerator
{
private readonly GeneratorExecutionContext _context;
private readonly Context _context;
private readonly IDictionary<InterfaceDeclarationSyntax, ProxyData> _candidateInterfaces;
public ProxyClassesGenerator(GeneratorExecutionContext context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces)
public ProxyClassesGenerator(Context context, IDictionary<InterfaceDeclarationSyntax, ProxyData> candidateInterfaces)
{
_context = context;
_candidateInterfaces = candidateInterfaces;
@@ -25,7 +25,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _candidateInterfaces)
{
var symbol = _context.Compilation.GetTypeByMetadataName(ci.Value.TypeName);
var symbol = _context.GeneratorExecutionContext.Compilation.GetTypeByMetadataName(ci.Value.TypeName);
if (symbol is null)
{
throw new Exception($"The type '{ci.Value.TypeName}' is not found.");
@@ -33,11 +33,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
string className = $"{ci.Value.TypeName.Split('.').Last()}";
yield return new Data
{
FileName = $"{className}Proxy.cs",
Text = CreateProxyClassCode(symbol, className)
};
yield return new Data($"{className}Proxy.cs", CreateProxyClassCode(symbol, className));
}
}
@@ -65,7 +61,7 @@ namespace {symbol.ContainingNamespace}
var str = new StringBuilder();
foreach (var property in MemberHelper.GetPublicProperties(symbol, p => p.Type.IsValueType || p.Type.ToString() == "string"))
{
str.AppendLine($" public {property.ToProxyCode()}");
str.AppendLine($" public {property.ToPropertyTextForClass()}");
str.AppendLine();
}
@@ -77,7 +73,7 @@ namespace {symbol.ContainingNamespace}
var str = new StringBuilder();
foreach (var method in MemberHelper.GetPublicMethods(symbol))
{
str.AppendLine($" public {method.ToProxyCode()}");
str.AppendLine($" public {method.ToMethodTextForClass()}");
str.AppendLine();
}