Initial commit
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
|
||||
internal class Attributes
|
||||
{
|
||||
public const string AttributesNamespace = nameof(InterfaceGenerator);
|
||||
|
||||
public const string GenerateAutoInterfaceClassname = "GenerateAutoInterfaceAttribute";
|
||||
public const string AutoInterfaceIgnoreAttributeClassname = "AutoInterfaceIgnoreAttribute";
|
||||
|
||||
public const string VisibilityModifierPropName = "VisibilityModifier";
|
||||
public const string InterfaceNamePropName = "Name";
|
||||
|
||||
public static readonly string AttributesSourceCode = $@"
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace {AttributesNamespace}
|
||||
{{
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
|
||||
[Conditional(""CodeGeneration"")]
|
||||
public sealed class {GenerateAutoInterfaceClassname} : Attribute
|
||||
{{
|
||||
public string? {VisibilityModifierPropName} {{ get; init; }}
|
||||
public string? {InterfaceNamePropName} {{ get; init; }}
|
||||
|
||||
public {GenerateAutoInterfaceClassname}()
|
||||
{{
|
||||
}}
|
||||
}}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
|
||||
[Conditional(""CodeGeneration"")]
|
||||
public sealed class {AutoInterfaceIgnoreAttributeClassname} : Attribute
|
||||
{{
|
||||
}}
|
||||
}}
|
||||
";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
using System;
|
||||
using System.CodeDom.Compiler;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
[Generator]
|
||||
public class AutoInterfaceGenerator : ISourceGenerator
|
||||
{
|
||||
private INamedTypeSymbol _generateAutoInterfaceAttribute = null!;
|
||||
private INamedTypeSymbol _ignoreAttribute = null!;
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
|
||||
}
|
||||
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
// setting the culture to invariant prevents errors such as emitting a decimal comma (0,1) instead of
|
||||
// a decimal point (0.1) in certain cultures
|
||||
var prevCulture = Thread.CurrentThread.CurrentCulture;
|
||||
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
|
||||
GenerateAttributes(context);
|
||||
GenerateInterfaces(context);
|
||||
|
||||
Thread.CurrentThread.CurrentCulture = prevCulture;
|
||||
}
|
||||
|
||||
private static void GenerateAttributes(GeneratorExecutionContext context)
|
||||
{
|
||||
context.AddSource(
|
||||
Attributes.GenerateAutoInterfaceClassname,
|
||||
SourceText.From(Attributes.AttributesSourceCode, Encoding.UTF8));
|
||||
}
|
||||
|
||||
private void GenerateInterfaces(GeneratorExecutionContext context)
|
||||
{
|
||||
if (context.SyntaxReceiver is not SyntaxReceiver receiver)
|
||||
return;
|
||||
|
||||
var compilation = GetCompilation(context);
|
||||
InitAttributes(compilation);
|
||||
|
||||
var classSymbols = GetImplTypeSymbols(compilation, receiver);
|
||||
|
||||
foreach (var implTypeSymbol in classSymbols)
|
||||
{
|
||||
if (!implTypeSymbol.TryGetAttribute(_generateAutoInterfaceAttribute, out var attributes))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var attribute = attributes.Single();
|
||||
var source = SourceText.From(GenerateInterfaceCode(implTypeSymbol, attribute), Encoding.UTF8);
|
||||
context.AddSource($"{implTypeSymbol.Name}_AutoInterface.cs", source);
|
||||
}
|
||||
}
|
||||
|
||||
private string GetVisibilityModifier(INamedTypeSymbol implTypeSymbol, AttributeData attributeData)
|
||||
{
|
||||
var pair = attributeData.NamedArguments.FirstOrDefault(x => x.Key == Attributes.VisibilityModifierPropName);
|
||||
string? result = pair.Value.Value?.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return implTypeSymbol.DeclaredAccessibility switch
|
||||
{
|
||||
Accessibility.Public => "public",
|
||||
var _ => "internal",
|
||||
};
|
||||
}
|
||||
|
||||
private string GetInterfaceName(INamedTypeSymbol implTypeSymbol, AttributeData attributeData)
|
||||
{
|
||||
var pair = attributeData.NamedArguments.FirstOrDefault(x => x.Key == Attributes.InterfaceNamePropName);
|
||||
string? result = pair.Value.Value?.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
return "I" + implTypeSymbol.Name;
|
||||
}
|
||||
|
||||
private string GenerateInterfaceCode(INamedTypeSymbol implTypeSymbol, AttributeData attributeData)
|
||||
{
|
||||
using var stream = new MemoryStream();
|
||||
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
|
||||
var codeWriter = new IndentedTextWriter(streamWriter, GeneratorConsts.Indent);
|
||||
|
||||
var namespaceName = implTypeSymbol.ContainingNamespace.ToDisplayString();
|
||||
var interfaceName = GetInterfaceName(implTypeSymbol, attributeData);
|
||||
var visibilityModifier = GetVisibilityModifier(implTypeSymbol, attributeData);
|
||||
|
||||
codeWriter.WriteLine("namespace {0}", namespaceName);
|
||||
codeWriter.WriteLine("{");
|
||||
|
||||
++codeWriter.Indent;
|
||||
codeWriter.Write("{0} partial interface {1}", visibilityModifier, interfaceName);
|
||||
WriteTypeGenericsIfNeeded(codeWriter, implTypeSymbol);
|
||||
codeWriter.WriteLine();
|
||||
codeWriter.WriteLine("{");
|
||||
|
||||
++codeWriter.Indent;
|
||||
GenerateInterfaceMemberDefinitions(codeWriter, implTypeSymbol);
|
||||
--codeWriter.Indent;
|
||||
|
||||
codeWriter.WriteLine("}");
|
||||
--codeWriter.Indent;
|
||||
|
||||
codeWriter.WriteLine("}");
|
||||
|
||||
codeWriter.Flush();
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8, true);
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
|
||||
private void WriteTypeGenericsIfNeeded(IndentedTextWriter writer, INamedTypeSymbol implTypeSymbol)
|
||||
{
|
||||
if (!implTypeSymbol.IsGenericType)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
writer.Write("<");
|
||||
writer.WriteJoin(", ", implTypeSymbol.TypeParameters.Select(x => x.Name));
|
||||
writer.Write(">");
|
||||
|
||||
WriteTypeParameterConstraints(writer, implTypeSymbol.TypeParameters);
|
||||
}
|
||||
|
||||
private void GenerateInterfaceMemberDefinitions(IndentedTextWriter writer, INamedTypeSymbol implTypeSymbol)
|
||||
{
|
||||
foreach (var member in implTypeSymbol.GetMembers())
|
||||
{
|
||||
if (member.DeclaredAccessibility != Accessibility.Public)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (member.HasAttribute(_ignoreAttribute))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
GenerateInterfaceMemberDefinition(writer, member);
|
||||
}
|
||||
}
|
||||
|
||||
private static void GenerateInterfaceMemberDefinition(IndentedTextWriter writer, ISymbol member)
|
||||
{
|
||||
switch (member)
|
||||
{
|
||||
case IPropertySymbol propertySymbol:
|
||||
GeneratePropertyDefinition(writer, propertySymbol);
|
||||
break;
|
||||
case IMethodSymbol methodSymbol:
|
||||
GenerateMethodDefinition(writer, methodSymbol);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteMemberDocs(IndentedTextWriter writer, ISymbol member)
|
||||
{
|
||||
var xml = member.GetDocumentationCommentXml();
|
||||
if (string.IsNullOrWhiteSpace(xml))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// omit the fist and last lines to skip the <member> tag
|
||||
|
||||
var reader = new StringReader(xml);
|
||||
var lines = new List<string>();
|
||||
|
||||
while (true)
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
if (line is null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
lines.Add(line);
|
||||
}
|
||||
|
||||
for (int i = 1; i < lines.Count - 1; i++)
|
||||
{
|
||||
var line = lines[i];
|
||||
writer.WriteLine("/// {0}", line);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsPublicOrInternal(IMethodSymbol methodSymbol)
|
||||
{
|
||||
return methodSymbol.DeclaredAccessibility == Accessibility.Public ||
|
||||
methodSymbol.DeclaredAccessibility == Accessibility.Internal;
|
||||
}
|
||||
|
||||
private static void GeneratePropertyDefinition(IndentedTextWriter writer, IPropertySymbol propertySymbol)
|
||||
{
|
||||
bool hasPublicGetter = propertySymbol.GetMethod is not null &&
|
||||
IsPublicOrInternal(propertySymbol.GetMethod);
|
||||
|
||||
bool hasPublicSetter = propertySymbol.SetMethod is not null &&
|
||||
IsPublicOrInternal(propertySymbol.SetMethod);
|
||||
|
||||
if (!hasPublicGetter && !hasPublicSetter)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
WriteMemberDocs(writer, propertySymbol);
|
||||
|
||||
if (propertySymbol.IsIndexer)
|
||||
{
|
||||
writer.Write("{0} this[", propertySymbol.Type);
|
||||
//writer.WriteJoin(", ", propertySymbol.Parameters, (w, param) => { w.Write("{0} {1}", param.Type, param.Name); });
|
||||
writer.WriteJoin(", ", propertySymbol.Parameters, WriteMethodParam);
|
||||
writer.Write("] ");
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write("{0} {1} ", propertySymbol.Type, propertySymbol.Name); // ex. int Foo
|
||||
}
|
||||
|
||||
writer.Write("{ ");
|
||||
|
||||
if (hasPublicGetter)
|
||||
{
|
||||
writer.Write("get; ");
|
||||
}
|
||||
|
||||
if (hasPublicSetter)
|
||||
{
|
||||
writer.Write("set; ");
|
||||
}
|
||||
|
||||
writer.WriteLine("}");
|
||||
}
|
||||
|
||||
private static void GenerateMethodDefinition(IndentedTextWriter writer, IMethodSymbol methodSymbol)
|
||||
{
|
||||
if (methodSymbol.MethodKind != MethodKind.Ordinary || methodSymbol.IsStatic)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
WriteMemberDocs(writer, methodSymbol);
|
||||
|
||||
writer.Write("{0} {1}", methodSymbol.ReturnType, methodSymbol.Name); // ex. int Foo
|
||||
|
||||
if (methodSymbol.IsGenericMethod)
|
||||
{
|
||||
writer.Write("<");
|
||||
writer.WriteJoin(", ", methodSymbol.TypeParameters.Select(x => x.Name));
|
||||
writer.Write(">");
|
||||
}
|
||||
|
||||
writer.Write("(");
|
||||
writer.WriteJoin(", ", methodSymbol.Parameters, WriteMethodParam);
|
||||
|
||||
writer.Write(")");
|
||||
|
||||
if (methodSymbol.IsGenericMethod)
|
||||
{
|
||||
WriteTypeParameterConstraints(writer, methodSymbol.TypeParameters);
|
||||
}
|
||||
|
||||
writer.WriteLine(";");
|
||||
}
|
||||
|
||||
private static void WriteMethodParam(TextWriter writer, IParameterSymbol param)
|
||||
{
|
||||
if (param.IsParams)
|
||||
{
|
||||
writer.Write("params ");
|
||||
}
|
||||
|
||||
switch (param.RefKind)
|
||||
{
|
||||
case RefKind.Ref:
|
||||
writer.Write("ref ");
|
||||
break;
|
||||
case RefKind.Out:
|
||||
writer.Write("out ");
|
||||
break;
|
||||
case RefKind.In:
|
||||
writer.Write("in ");
|
||||
break;
|
||||
}
|
||||
|
||||
writer.Write("{0} {1}", param.Type, param.Name);
|
||||
|
||||
if (param.HasExplicitDefaultValue)
|
||||
{
|
||||
WriteParamExplicitDefaultValue(writer, param);
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteParamExplicitDefaultValue(TextWriter writer, IParameterSymbol param)
|
||||
{
|
||||
if (param.ExplicitDefaultValue is null)
|
||||
{
|
||||
writer.Write(" = default");
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (param.Type.Name)
|
||||
{
|
||||
case nameof(String):
|
||||
writer.Write(" = \"{0}\"", param.ExplicitDefaultValue);
|
||||
break;
|
||||
case nameof(Single):
|
||||
writer.Write(" = {0}f", param.ExplicitDefaultValue);
|
||||
break;
|
||||
case nameof(Double):
|
||||
writer.Write(" = {0}d", param.ExplicitDefaultValue);
|
||||
break;
|
||||
case nameof(Decimal):
|
||||
writer.Write(" = {0}m", param.ExplicitDefaultValue);
|
||||
break;
|
||||
default:
|
||||
writer.Write(" = {0}", param.ExplicitDefaultValue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteTypeParameterConstraints(
|
||||
IndentedTextWriter writer,
|
||||
IEnumerable<ITypeParameterSymbol> typeParameters)
|
||||
{
|
||||
foreach (var typeParameter in typeParameters)
|
||||
{
|
||||
var constraints = typeParameter.EnumGenericConstraints().ToList();
|
||||
if (constraints.Count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
writer.Write(" where {0} : ", typeParameter.Name);
|
||||
writer.WriteJoin(", ", constraints);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitAttributes(Compilation compilation)
|
||||
{
|
||||
_generateAutoInterfaceAttribute = compilation.GetTypeByMetadataName(
|
||||
$"{Attributes.AttributesNamespace}.{Attributes.GenerateAutoInterfaceClassname}")!;
|
||||
|
||||
_ignoreAttribute = compilation.GetTypeByMetadataName(
|
||||
$"{Attributes.AttributesNamespace}.{Attributes.AutoInterfaceIgnoreAttributeClassname}")!;
|
||||
}
|
||||
|
||||
private static IEnumerable<INamedTypeSymbol> GetImplTypeSymbols(Compilation compilation, SyntaxReceiver receiver)
|
||||
{
|
||||
return receiver.CandidateClasses.Select(candidate => GetClassSymbol(compilation, candidate));
|
||||
}
|
||||
|
||||
private static INamedTypeSymbol GetClassSymbol(Compilation compilation, ClassDeclarationSyntax @class)
|
||||
{
|
||||
var model = compilation.GetSemanticModel(@class.SyntaxTree);
|
||||
var classSymbol = ModelExtensions.GetDeclaredSymbol(model, @class)!;
|
||||
return (INamedTypeSymbol)classSymbol;
|
||||
}
|
||||
|
||||
private static Compilation GetCompilation(GeneratorExecutionContext context)
|
||||
{
|
||||
var options = context.Compilation.SyntaxTrees.First().Options as CSharpParseOptions;
|
||||
|
||||
var compilation = context.Compilation.AddSyntaxTrees(
|
||||
CSharpSyntaxTree.ParseText(
|
||||
SourceText.From(Attributes.AttributesSourceCode, Encoding.UTF8), options));
|
||||
|
||||
return compilation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
internal class GeneratorConsts
|
||||
{
|
||||
public const string Indent = " ";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<Authors>R. David</Authors>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
internal static class SymbolExtensions
|
||||
{
|
||||
public static bool TryGetAttribute(
|
||||
this ISymbol symbol,
|
||||
INamedTypeSymbol attributeType,
|
||||
out IEnumerable<AttributeData> attributes)
|
||||
{
|
||||
attributes = symbol.GetAttributes()
|
||||
.Where(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeType));
|
||||
return attributes.Any();
|
||||
}
|
||||
|
||||
public static bool HasAttribute(this ISymbol symbol, INamedTypeSymbol attributeType)
|
||||
{
|
||||
return symbol.GetAttributes()
|
||||
.Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeType));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
internal class SyntaxReceiver : ISyntaxReceiver
|
||||
{
|
||||
public IList<ClassDeclarationSyntax> CandidateClasses { get; } = new List<ClassDeclarationSyntax>();
|
||||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax &&
|
||||
classDeclarationSyntax.AttributeLists.Count > 0)
|
||||
{
|
||||
CandidateClasses.Add(classDeclarationSyntax);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
internal static class TextWriterExtensions
|
||||
{
|
||||
|
||||
public static void WriteJoin<T>(
|
||||
this TextWriter writer,
|
||||
string separator,
|
||||
IEnumerable<T> values)
|
||||
{
|
||||
writer.WriteJoin(separator, values, (w, x) => w.Write(x));
|
||||
}
|
||||
|
||||
public static void WriteJoin<T>(
|
||||
this TextWriter writer,
|
||||
string separator,
|
||||
IEnumerable<T> values,
|
||||
Action<TextWriter, T> writeAction)
|
||||
{
|
||||
string.Join("", Enumerable.Empty<string>());
|
||||
|
||||
using var enumerator = values.GetEnumerator();
|
||||
|
||||
if (!enumerator.MoveNext())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
writeAction(writer, enumerator.Current);
|
||||
|
||||
if (!enumerator.MoveNext())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
writer.Write(separator);
|
||||
writeAction(writer, enumerator.Current);
|
||||
} while (enumerator.MoveNext());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace InterfaceGenerator
|
||||
{
|
||||
internal static class TypeParameterSymbolExtensions
|
||||
{
|
||||
public static IEnumerable<string> EnumGenericConstraints(this ITypeParameterSymbol symbol)
|
||||
{
|
||||
// the class/struct/unmanaged/notnull constraint has to be the last
|
||||
if (symbol.HasNotNullConstraint)
|
||||
{
|
||||
yield return "notnull";
|
||||
}
|
||||
|
||||
if (symbol.HasValueTypeConstraint)
|
||||
{
|
||||
yield return "struct";
|
||||
}
|
||||
|
||||
if (symbol.HasUnmanagedTypeConstraint)
|
||||
{
|
||||
yield return "unmanaged";
|
||||
}
|
||||
|
||||
if (symbol.HasReferenceTypeConstraint)
|
||||
{
|
||||
yield return symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated
|
||||
? "class?"
|
||||
: "class";
|
||||
}
|
||||
|
||||
|
||||
// types go in the middle
|
||||
foreach (var constraintType in symbol.ConstraintTypes)
|
||||
{
|
||||
yield return constraintType.ToDisplayString();
|
||||
}
|
||||
|
||||
|
||||
// the new() constraint has to be the last
|
||||
if (symbol.HasConstructorConstraint)
|
||||
{
|
||||
yield return "new()";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user