Cleanup some code
This commit is contained in:
@@ -12,7 +12,10 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="Mapster" Version="7.3.0" />
|
||||
<PackageReference Include="ProxyInterfaceGenerator" Version="0.0.32" />
|
||||
<PackageReference Include="ProxyInterfaceGenerator" Version="0.0.38">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -46,18 +46,12 @@ internal static class NamedTypeSymbolExtensions
|
||||
$"{namedTypeSymbol.Name}Proxy<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(ta => ta.Name))}>";
|
||||
}
|
||||
|
||||
public static string ResolveFullProxyClassName(this INamedTypeSymbol namedTypeSymbol)
|
||||
{
|
||||
return !namedTypeSymbol.IsGenericType ?
|
||||
$"{namedTypeSymbol}Proxy" :
|
||||
$"{namedTypeSymbol}Proxy<{string.Join(", ", namedTypeSymbol.TypeArguments.Select(ta => ta.Name))}>";
|
||||
}
|
||||
|
||||
public static List<INamedTypeSymbol> ResolveImplementedInterfaces(this INamedTypeSymbol symbol, bool proxyBaseClasses)
|
||||
{
|
||||
//Members implemented by us or base classes should go here.
|
||||
// Members implemented by us or base classes should go here.
|
||||
var publicMembers = symbol.GetMembers().Where(m => m.DeclaredAccessibility == Accessibility.Public).ToList();
|
||||
//Direct interfaces, recursive interfaces or base class interfaces should go here.
|
||||
|
||||
// Direct interfaces, recursive interfaces or base class interfaces should go here.
|
||||
var interfaces = new List<INamedTypeSymbol>(symbol.Interfaces);
|
||||
var baseType = symbol.BaseType;
|
||||
while (proxyBaseClasses && baseType != null && baseType.SpecialType != SpecialType.System_Object)
|
||||
@@ -67,12 +61,12 @@ internal static class NamedTypeSymbolExtensions
|
||||
baseType = baseType.BaseType;
|
||||
}
|
||||
|
||||
//Filter explicitly implemented interfaces.
|
||||
// Filter explicitly implemented interfaces.
|
||||
var realizedInterfaces = new List<INamedTypeSymbol>();
|
||||
foreach (var iface in interfaces)
|
||||
foreach (var @interface in interfaces)
|
||||
{
|
||||
var isRealized = true;
|
||||
var allMembers = iface.AllInterfaces.Aggregate(iface.GetMembers(), (xs, x) => xs.AddRange(x.GetMembers()));
|
||||
var allMembers = @interface.AllInterfaces.Aggregate(@interface.GetMembers(), (xs, x) => xs.AddRange(x.GetMembers()));
|
||||
foreach (var member in allMembers)
|
||||
{
|
||||
var implementation = symbol.FindImplementationForInterfaceMember(member);
|
||||
@@ -82,11 +76,13 @@ internal static class NamedTypeSymbolExtensions
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isRealized)
|
||||
{
|
||||
realizedInterfaces.Add(iface);
|
||||
realizedInterfaces.Add(@interface);
|
||||
}
|
||||
}
|
||||
|
||||
return realizedInterfaces;
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ internal static class SymbolExtensions
|
||||
}
|
||||
|
||||
//https://stackoverflow.com/questions/27105909/get-fully-qualified-metadata-name-in-roslyn
|
||||
public static string GetFullMetadataName(this ISymbol s)
|
||||
public static string GetFullMetadataName(this ISymbol? s)
|
||||
{
|
||||
if (s == null || IsRootNamespace(s))
|
||||
{
|
||||
@@ -69,9 +69,6 @@ internal static class SymbolExtensions
|
||||
public static bool IsPublic(this ISymbol? symbol) =>
|
||||
symbol is { DeclaredAccessibility: Accessibility.Public };
|
||||
|
||||
private static bool IsRootNamespace(ISymbol symbol)
|
||||
{
|
||||
INamespaceSymbol s = null;
|
||||
return ((s = symbol as INamespaceSymbol) != null) && s.IsGlobalNamespace;
|
||||
}
|
||||
private static bool IsRootNamespace(ISymbol symbol) =>
|
||||
symbol is INamespaceSymbol { IsGlobalNamespace: true };
|
||||
}
|
||||
@@ -23,11 +23,6 @@ internal static class TypeSymbolExtensions
|
||||
public static bool IsString(this ITypeSymbol ts) =>
|
||||
ts.ToString().ToLowerInvariant() == "string" || ts.ToString().ToLowerInvariant() == "string?";
|
||||
|
||||
public static string ToFullyQualifiedDisplayString(this ITypeSymbol property)
|
||||
{
|
||||
return property.ToDisplayString(NullableFlowState.None, SymbolDisplayFormat.FullyQualifiedFormat);
|
||||
}
|
||||
|
||||
internal static bool IsClass(this ITypeSymbol ts) =>
|
||||
ts.IsReferenceType && ts.TypeKind == TypeKind.Class;
|
||||
public static string ToFullyQualifiedDisplayString(this ITypeSymbol property) =>
|
||||
property.ToDisplayString(NullableFlowState.None, SymbolDisplayFormat.FullyQualifiedFormat);
|
||||
}
|
||||
@@ -11,9 +11,9 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators;
|
||||
|
||||
internal class PartialInterfacesGenerator : BaseGenerator, IFilesGenerator
|
||||
{
|
||||
private IReadOnlyCollection<INamedTypeSymbol> ImplementedInterfaces = new List<INamedTypeSymbol>();
|
||||
public PartialInterfacesGenerator(Context context, bool supportsNullable) :
|
||||
base(context, supportsNullable)
|
||||
private IReadOnlyCollection<INamedTypeSymbol> _implementedInterfaces = new List<INamedTypeSymbol>();
|
||||
|
||||
public PartialInterfacesGenerator(Context context, bool supportsNullable) : base(context, supportsNullable)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -59,8 +59,8 @@ internal class PartialInterfacesGenerator : BaseGenerator, IFilesGenerator
|
||||
ProxyData proxyData)
|
||||
{
|
||||
var extendsProxyClasses = GetExtendsProxyData(proxyData, classSymbol);
|
||||
ImplementedInterfaces = classSymbol.Symbol.ResolveImplementedInterfaces(proxyData.ProxyBaseClasses);
|
||||
var implementedInterfacesNames = ImplementedInterfaces.Select(i => i.ToFullyQualifiedDisplayString());
|
||||
_implementedInterfaces = classSymbol.Symbol.ResolveImplementedInterfaces(proxyData.ProxyBaseClasses);
|
||||
var implementedInterfacesNames = _implementedInterfaces.Select(i => i.ToFullyQualifiedDisplayString()).ToArray();
|
||||
var implements = implementedInterfacesNames.Any() ? $" : {string.Join(", ", implementedInterfacesNames)}" : string.Empty;
|
||||
var @new = extendsProxyClasses.Any() ? "new " : string.Empty;
|
||||
var (namespaceStart, namespaceEnd) = NamespaceBuilder.Build(ns);
|
||||
@@ -96,16 +96,17 @@ methods}
|
||||
private Func<T, bool> InterfaceFilter<T>() where T : ISymbol
|
||||
{
|
||||
var hashSet = new HashSet<string>();
|
||||
foreach (var iface in ImplementedInterfaces)
|
||||
foreach (var @interface in _implementedInterfaces)
|
||||
{
|
||||
var members = iface.AllInterfaces.Aggregate(iface.GetMembers(), (xs, x) => xs.AddRange(x.GetMembers()));
|
||||
var members = @interface.AllInterfaces.Aggregate(@interface.GetMembers(), (xs, x) => xs.AddRange(x.GetMembers()));
|
||||
foreach (var member in members)
|
||||
{
|
||||
hashSet.Add(member.Name);
|
||||
}
|
||||
}
|
||||
//Member is not already implemented in another interface.
|
||||
return (T t) => !hashSet.Contains(t.Name);
|
||||
|
||||
// Member is not already implemented in another interface.
|
||||
return t => !hashSet.Contains(t.Name);
|
||||
}
|
||||
|
||||
private string GenerateProperties(ClassSymbol targetClassSymbol, bool proxyBaseClasses)
|
||||
|
||||
@@ -7,8 +7,6 @@ internal record Context
|
||||
{
|
||||
public GeneratorExecutionContext GeneratorExecutionContext { get; init; }
|
||||
|
||||
// public List<ContextData> GeneratedData { get; } = new List<ContextData>();
|
||||
|
||||
public IDictionary<InterfaceDeclarationSyntax, ProxyData> Candidates { get; init; } = default!;
|
||||
|
||||
public Dictionary<string, string> ReplacedTypes { get; } = new();
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace ProxyInterfaceSourceGenerator.Models;
|
||||
|
||||
internal record ContextData
|
||||
{
|
||||
public string? InterfaceName { get; init; }
|
||||
|
||||
public string? ClassName { get; init; }
|
||||
|
||||
public FileData FileData { get; init; } = default!;
|
||||
}
|
||||
@@ -4,6 +4,26 @@ namespace ProxyInterfaceSourceGenerator.Models;
|
||||
|
||||
internal class ProxyData
|
||||
{
|
||||
public string Namespace { get; }
|
||||
|
||||
public string NamespaceDot { get; }
|
||||
|
||||
public string ShortInterfaceName { get; }
|
||||
|
||||
public string FullInterfaceName { get; }
|
||||
|
||||
public string FullQualifiedTypeName { get; }
|
||||
|
||||
public string ShortMetadataName { get; }
|
||||
|
||||
public string FullMetadataTypeName { get; }
|
||||
|
||||
public List<string> Usings { get; }
|
||||
|
||||
public bool ProxyBaseClasses { get; }
|
||||
|
||||
public ProxyClassAccessibility Accessibility { get; }
|
||||
|
||||
public ProxyData(string @namespace,
|
||||
string namespaceDot,
|
||||
string shortInterfaceName,
|
||||
@@ -26,24 +46,4 @@ internal class ProxyData
|
||||
ProxyBaseClasses = proxyBaseClasses;
|
||||
Accessibility = accessibility;
|
||||
}
|
||||
|
||||
public string Namespace { get; }
|
||||
|
||||
public string NamespaceDot { get; }
|
||||
|
||||
public string ShortInterfaceName { get; }
|
||||
|
||||
public string FullInterfaceName { get; }
|
||||
|
||||
public string FullQualifiedTypeName { get; }
|
||||
|
||||
public string ShortMetadataName { get; }
|
||||
|
||||
public string FullMetadataTypeName { get; }
|
||||
|
||||
public List<string> Usings { get; }
|
||||
|
||||
public bool ProxyBaseClasses { get; }
|
||||
|
||||
public ProxyClassAccessibility Accessibility { get; }
|
||||
}
|
||||
Reference in New Issue
Block a user