Fix namespace (#1)

* Fix NS

* .
This commit is contained in:
Stef Heyenrath
2021-07-25 19:22:24 +02:00
committed by GitHub
parent a38d8c8ef0
commit 0f58ce480d
15 changed files with 216 additions and 85 deletions
@@ -22,12 +22,17 @@ namespace ProxyInterfaceSourceGenerator.Extensions
public static TypeEnum GetTypeEnum(this IParameterSymbol p)
{
if (p.Type.IsValueType || p.Type.ToString() == "string")
return GetTypeEnum(p.Type);
}
public static TypeEnum GetTypeEnum(this ITypeSymbol ts)
{
if (ts.IsValueType || ts.ToString() == "string")
{
return TypeEnum.ValueTypeOrString;
}
if (p.Type.TypeKind == TypeKind.Interface)
if (ts.TypeKind == TypeKind.Interface)
{
return TypeEnum.Interface;
}
@@ -0,0 +1,40 @@
using Microsoft.CodeAnalysis;
namespace ProxyInterfaceSourceGenerator.Extensions
{
internal static class SyntaxNodeUtils
{
// https://stackoverflow.com/questions/20458457/getting-class-fullname-including-namespace-from-roslyn-classdeclarationsyntax
public static bool TryGetParentSyntax<T>(this SyntaxNode syntaxNode, out T result) where T : SyntaxNode
{
result = null;
if (syntaxNode is null)
{
return false;
}
try
{
syntaxNode = syntaxNode.Parent;
if (syntaxNode is null)
{
return false;
}
if (syntaxNode.GetType() == typeof(T))
{
result = syntaxNode as T;
return true;
}
return TryGetParentSyntax(syntaxNode, out result);
}
catch
{
return false;
}
}
}
}
@@ -56,61 +56,103 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
protected string GetPropertyType(IPropertySymbol property)
{
var propertyTypeAsString = property.Type.ToString();
return GetReplacedType(property.Type);
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
if (existing is not null)
{
if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
{
_context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
}
//var propertyTypeAsString = property.Type.ToString();
return existing.InterfaceName;
}
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
//if (existing is not null)
//{
// if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
// {
// _context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
// }
if (property.Type is INamedTypeSymbol namedTypedSymbol)
{
var propertyTypeAsStringToBeModified = propertyTypeAsString;
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
{
var typeArgumentAsString = typeArgument.ToString();
var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
if (exist is not null)
{
if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
{
_context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
}
// return existing.InterfaceName;
//}
propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
}
}
//if (property.Type is INamedTypeSymbol namedTypedSymbol)
//{
// var propertyTypeAsStringToBeModified = propertyTypeAsString;
// foreach (var typeArgument in namedTypedSymbol.TypeArguments)
// {
// var typeArgumentAsString = typeArgument.ToString();
// var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
// if (exist is not null)
// {
// if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
// {
// _context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
// }
return propertyTypeAsStringToBeModified;
}
// propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
// }
// }
return propertyTypeAsString;
// return propertyTypeAsStringToBeModified;
//}
//return propertyTypeAsString;
}
protected string GetParameterType(IParameterSymbol property)
{
var propertyTypeAsString = property.Type.ToString();
return GetReplacedType(property.Type);
//var propertyTypeAsString = property.Type.ToString();
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
//var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == propertyTypeAsString);
//if (existing is not null)
//{
// if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
// {
// _context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
// }
// return existing.InterfaceName;
//}
//if (property.Type is INamedTypeSymbol namedTypedSymbol)
//{
// var propertyTypeAsStringToBeModified = propertyTypeAsString;
// foreach (var typeArgument in namedTypedSymbol.TypeArguments)
// {
// var typeArgumentAsString = typeArgument.ToString();
// var exist = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeArgumentAsString);
// if (exist is not null)
// {
// if (!_context.ReplacedTypes.ContainsKey(typeArgumentAsString))
// {
// _context.ReplacedTypes.Add(typeArgumentAsString, exist.InterfaceName);
// }
// propertyTypeAsStringToBeModified = propertyTypeAsStringToBeModified.Replace(typeArgumentAsString, exist.InterfaceName);
// }
// }
// return propertyTypeAsStringToBeModified;
//}
//return propertyTypeAsString;
}
protected string GetReplacedType(ITypeSymbol property)
{
var typeSymbolAsString = property.ToString();
var existing = _context.CandidateInterfaces.Values.FirstOrDefault(x => x.TypeName == typeSymbolAsString);
if (existing is not null)
{
if (!_context.ReplacedTypes.ContainsKey(propertyTypeAsString))
if (!_context.ReplacedTypes.ContainsKey(typeSymbolAsString))
{
_context.ReplacedTypes.Add(propertyTypeAsString, existing.InterfaceName);
_context.ReplacedTypes.Add(typeSymbolAsString, existing.InterfaceName);
}
return existing.InterfaceName;
}
if (property.Type is INamedTypeSymbol namedTypedSymbol)
if (property is INamedTypeSymbol namedTypedSymbol)
{
var propertyTypeAsStringToBeModified = propertyTypeAsString;
var propertyTypeAsStringToBeModified = typeSymbolAsString;
foreach (var typeArgument in namedTypedSymbol.TypeArguments)
{
var typeArgumentAsString = typeArgument.ToString();
@@ -129,7 +171,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
return propertyTypeAsStringToBeModified;
}
return propertyTypeAsString;
return typeSymbolAsString;
}
protected INamedTypeSymbol GetType(string name)
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using ProxyInterfaceSourceGenerator.Enums;
@@ -19,17 +18,17 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.TypeName, ci.Value.ProxyAll);
}
}
private FileData GenerateFile(string interfaceName, string typeName, bool proxyAll)
private FileData GenerateFile(string ns, string interfaceName, string typeName, bool proxyAll)
{
var symbol = GetType(typeName);
var file = new FileData(
$"{interfaceName}.cs",
CreatePartialInterfaceCode(symbol, interfaceName, proxyAll)
CreatePartialInterfaceCode(ns, symbol, interfaceName, proxyAll)
);
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = null, FileData = file });
@@ -37,9 +36,9 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
return file;
}
private string CreatePartialInterfaceCode(INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
private string CreatePartialInterfaceCode(string ns, INamedTypeSymbol symbol, string interfaceName, bool proxyAll) => $@"using System;
namespace {symbol.ContainingNamespace}
namespace {ns}
{{
public partial interface {interfaceName}
{{
@@ -97,11 +96,8 @@ namespace {symbol.ContainingNamespace}
}
}
str.AppendLine($" {method.ReturnType} {method.Name}({string.Join(", ", methodParameters)});");
str.AppendLine($" {GetReplacedType(method.ReturnType)} {method.Name}({string.Join(", ", methodParameters)});");
str.AppendLine();
//str.AppendLine($" {method.ToMethodText()};");
//str.AppendLine();
}
return str.ToString();
@@ -17,17 +17,17 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
foreach (var ci in _context.CandidateInterfaces)
{
yield return GenerateFile(ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
yield return GenerateFile(ci.Value.Namespace, ci.Value.InterfaceName, ci.Value.ClassName, ci.Value.TypeName, ci.Value.ProxyAll);
}
}
private FileData GenerateFile(string interfaceName, string className, string typeName, bool proxyAll)
private FileData GenerateFile(string ns, string interfaceName, string className, string typeName, bool proxyAll)
{
var symbol = GetType(typeName);
var file = new FileData(
$"{className}Proxy.cs",
CreateProxyClassCode(symbol, interfaceName, className, proxyAll)
CreateProxyClassCode(ns, symbol, interfaceName, className, proxyAll)
);
_context.GeneratedData.Add(new() { InterfaceName = interfaceName, ClassName = className, FileData = file });
@@ -35,22 +35,22 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
return file;
}
private string CreateProxyClassCode(INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
private string CreateProxyClassCode(string ns, INamedTypeSymbol symbol, string interfaceName, string className, bool proxyAll) => $@"using System;
using AutoMapper;
namespace {symbol.ContainingNamespace}
namespace {ns}
{{
public class {className}Proxy : {interfaceName}
{{
private readonly IMapper _mapper;
private readonly IMapper? _mapper;
public {className} _Instance {{ get; }}
public {symbol} _Instance {{ get; }}
{GeneratePublicProperties(symbol, proxyAll)}
{GeneratePublicMethods(symbol)}
public {className}Proxy({className} instance)
public {className}Proxy({symbol} instance)
{{
_Instance = instance;
@@ -61,6 +61,11 @@ namespace {symbol.ContainingNamespace}
private string GenerateAutoMapper()
{
if (_context.ReplacedTypes.Count == 0)
{
return string.Empty;
}
var str = new StringBuilder();
str.AppendLine(" _mapper = new MapperConfiguration(cfg =>");
@@ -110,33 +115,38 @@ namespace {symbol.ContainingNamespace}
foreach (var method in MemberHelper.GetPublicMethods(symbol))
{
var methodParameters = new List<string>();
foreach (var ps in method.Parameters)
{
if (ps.GetTypeEnum() == TypeEnum.Complex)
{
var type = GetParameterType(ps);
methodParameters.Add($"{type} {ps.Name}");
}
else
{
methodParameters.Add($"{ps.Type} {ps.Name}");
}
}
var invokeParameters = new List<string>();
foreach (var ps in method.Parameters)
{
if (ps.GetTypeEnum() == TypeEnum.Complex)
{
methodParameters.Add($"{GetParameterType(ps)} {ps.Name}");
invokeParameters.Add($"_mapper.Map<{ps.Type}>({ps.Name})");
}
else
{
methodParameters.Add($"{ps.Type} {ps.Name}");
invokeParameters.Add($"{ps.Name}");
}
}
str.AppendLine($" public {method.ReturnType} {method.Name}({string.Join(", ", methodParameters)}) => _Instance.{method.Name}({string.Join(", ", invokeParameters)});");
string returnTypeAsString;
string call;
if (method.ReturnType.GetTypeEnum() == TypeEnum.Complex)
{
returnTypeAsString = GetReplacedType(method.ReturnType);
call = $"_mapper.Map<{returnTypeAsString}>(_Instance.{method.Name}({string.Join(", ", invokeParameters)}))";
}
else
{
returnTypeAsString = method.ReturnType.ToString();
call = $"_Instance.{method.Name}({string.Join(", ", invokeParameters)})";
}
str.AppendLine($" public {returnTypeAsString} {method.Name}({string.Join(", ", methodParameters)}) => {call};");
str.AppendLine();
}
@@ -28,25 +28,35 @@ namespace ProxyInterfaceSourceGenerator
return;
}
var context = new Context
var context1 = new Context
{
GeneratorExecutionContext = ctx,
CandidateInterfaces = receiver.CandidateInterfaces
};
var attributeData = _proxyAttributeGenerator.GenerateFile();
context.GeneratorExecutionContext.AddSource(attributeData.FileName, SourceText.From(attributeData.Text, Encoding.UTF8));
context1.GeneratorExecutionContext.AddSource(attributeData.FileName, SourceText.From(attributeData.Text, Encoding.UTF8));
var partialInterfacesGenerator = new PartialInterfacesGenerator(context);
var context2 = new Context
{
GeneratorExecutionContext = ctx,
CandidateInterfaces = receiver.CandidateInterfaces
};
var partialInterfacesGenerator = new PartialInterfacesGenerator(context2);
foreach (var data in partialInterfacesGenerator.GenerateFiles())
{
context.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
context2.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
}
var proxyClassesGenerator = new ProxyClassesGenerator(context);
var context3 = new Context
{
GeneratorExecutionContext = ctx,
CandidateInterfaces = receiver.CandidateInterfaces
};
var proxyClassesGenerator = new ProxyClassesGenerator(context3);
foreach (var data in proxyClassesGenerator.GenerateFiles())
{
context.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
context3.GeneratorExecutionContext.AddSource(data.FileName, SourceText.From(data.Text, Encoding.UTF8));
}
}
}
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>0.0.2</Version>
<Version>0.0.3</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
@@ -2,7 +2,7 @@
namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
{
internal record ProxyData(string InterfaceName, string TypeName, bool ProxyAll)
internal record ProxyData(string Namespace, string InterfaceName, string TypeName, bool ProxyAll)
{
public string ClassName => TypeName.Split('.').Last();
}
@@ -2,6 +2,7 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ProxyInterfaceSourceGenerator.Extensions;
namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
{
@@ -19,7 +20,7 @@ namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
private static bool TryGet(InterfaceDeclarationSyntax interfaceDeclarationSyntax, out ProxyData data)
{
data = new(string.Empty, string.Empty, false);
data = new(string.Empty, string.Empty, string.Empty, false);
// TODO : how to check if the InterfaceDeclarationSyntax has 'partial' ?
var attributeLists = interfaceDeclarationSyntax.AttributeLists.FirstOrDefault(x => x.Attributes.Any(a => a.Name.ToString().Equals("ProxyInterfaceGenerator.Proxy")));
@@ -34,8 +35,15 @@ namespace ProxyInterfaceSourceGenerator.SyntaxReceiver
return false;
}
string ns = string.Empty;
if (SyntaxNodeUtils.TryGetParentSyntax(interfaceDeclarationSyntax, out NamespaceDeclarationSyntax namespaceDeclarationSyntax))
{
ns = namespaceDeclarationSyntax.Name.ToString();
}
data = new
(
ns,
interfaceDeclarationSyntax.Identifier.ToString(),
argumentList.Arguments[0].Expression.ChildNodes().First().GetText().ToString(),
false //bool.Parse(argumentList.Arguments[1].Expression.GetText().ToString())