public void Execute(GeneratorExecutionContext context) { //if (!System.Diagnostics.Debugger.IsAttached) // System.Diagnostics.Debugger.Launch(); //System.Diagnostics.Debugger.Break(); MySyntaxReceiver syntaxReceiver = (MySyntaxReceiver)context.SyntaxReceiver; var metadataLoadContext = new MetadataLoadContext(context.Compilation); var assembly = metadataLoadContext.MainAssembly; foreach ((MemberAccessExpressionSyntax _, ArgumentSyntax ArgSyntax)bindOverload in syntaxReceiver.BindOverloads) { SemanticModel compilationSemanticModel = context.Compilation.GetSemanticModel(bindOverload.ArgSyntax.SyntaxTree); ITypeSymbol typeSymbol = compilationSemanticModel.GetTypeInfo(bindOverload.ArgSyntax.Expression).Type; var accessibility = typeSymbol.DeclaredAccessibility; if (accessibility == Accessibility.Private) { // TODO support continue; } Type type = new TypeWrapper(typeSymbol, metadataLoadContext); if (type.IsGenericType) { // TODO support continue; } if (!(TypesToBind ??= new Dictionary <string, Type>()).ContainsKey(type.FullName)) { TypesToBind[type.FullName] = type; } } if (TypesToBind == null) { return; } foreach (KeyValuePair <string, Type> pair in TypesToBind) { Type type = pair.Value; if (type.FullName == typeof(object).FullName) { // Bind overloads for object already available in the framework continue; } StringBuilder sb = new(); sb.Append(@$ "using System; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace {type.Namespace}
public void Execute(SourceGeneratorContext context) { // the generator infrastructure will create a receiver and populate it // we can retrieve the populated instance via the context MySyntaxReceiver syntaxReceiver = (MySyntaxReceiver)context.SyntaxReceiver; // get the recorded user class ClassDeclarationSyntax userClass = syntaxReceiver.ClassToAugment; if (userClass is null) { // if we didn't find the user class, there is nothing to do return; } // add the generated implementation to the compilation SourceText sourceText = SourceText.From($@" public partial class {userClass.Identifier} {{ public void GeneratedMethod() {{ // generated code }} }}", Encoding.UTF8); context.AddSource("ExpressionHolder.Generated.cs", sourceText); }
/// <summary> /// Called to perform source generation. /// </summary> /// <param name="context"></param> public void Execute(GeneratorExecutionContext context) { MySyntaxReceiver syntaxReceiver = (MySyntaxReceiver)context.SyntaxReceiver; GenerateCommandClass(context, syntaxReceiver); GenerateQueryClass(context, syntaxReceiver); }
public void Execute(GeneratorExecutionContext context) { // the generator infrastructure will create a receiver and populate it // we can retrieve the populated instance via the context MySyntaxReceiver syntaxReceiver = (MySyntaxReceiver)context.SyntaxReceiver; // get the recorded user class //ClassDeclarationSyntax userClass = syntaxReceiver.ClassToAugment; foreach (var userClass in syntaxReceiver.CandidateClasses) { if (userClass is null) { // if we didn't find the user class, there is nothing to do return; } var model = context.Compilation.GetSemanticModel(userClass.SyntaxTree); var classInfo = model.GetDeclaredSymbol(userClass); var namespaceName = GetFullyQualifiedNamespace(classInfo.ContainingNamespace); // Full class generation StringBuilder stringBuilder = new StringBuilder( $@" using System; using System.Net; using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Distributed; // reference to synca.lib using synca.lib.Hosted.Service; using synca.lib.Background; using synca.lib.Services; namespace {namespaceName} {{ public partial class {userClass.Identifier} : ControllerBase {{" ); stringBuilder.Append(GenerateMethods(userClass.Members)); // Closing bracket for class stringBuilder.Append("}"); // Closing bracket for namespace stringBuilder.Append("}"); SourceText sourceText = SourceText.From(stringBuilder.ToString(), Encoding.UTF8); context.AddSource($"{userClass.Identifier}.Generated.cs", sourceText); } }
/// <summary> /// Generate the API controller class to handle the query commands /// </summary> /// <param name="context"></param> /// <param name="syntaxReceiver"></param> private void GenerateQueryClass(GeneratorExecutionContext context, MySyntaxReceiver syntaxReceiver) { string QueryClassTemplate = LoadTemplate(context, filename: "QueryClassTemplate.txt"); StringBuilder commandSource = new StringBuilder(); foreach (var query in syntaxReceiver.Queries) { var queryCommandName = query.Identifier.ValueText; var queryReturnType = LookupIRequestGenericType(query); var commandComments = query.GetLeadingTrivia().ToString(); commandSource.AppendLine(@$ " {commandComments}