Skip to content

Commit

Permalink
Starting on source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed May 31, 2024
1 parent 56a9d5b commit 2006a23
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/Shiny.Mediator.SourceGenerators/MediatorSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
using Microsoft.CodeAnalysis;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using SourceGeneratorsKit;

namespace Shiny.Mediator.SourceGenerators;


[Generator]
public class MediatorSourceGenerator : ISourceGenerator
{
readonly SyntaxReceiver syntaxReceiver = new RegisterHandlerAttributeSyntaxReceiver();
// SyntaxReceiver syntaxReceiver = new ClassesWithInterfacesReceiver("IEnumerable");

public void Initialize(GeneratorInitializationContext context)
{
// context.RegisterForSyntaxNotifications(() => syntaxReceiver);
context.RegisterForPostInitialization(x => x.AddSource("RegisterHandlerAttribute.g.cs", SourceText.From(
"""
namespace Shiny.Mediator;
[System.AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class RegisterHandlerAttribute : System.Attribute {}
"""
)));
context.RegisterForSyntaxNotifications(() => syntaxReceiver);
}


public void Execute(GeneratorExecutionContext context)
{
// if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
// {
// return;
// }
// foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver.Classes)
// {
// // process your class here.
// }
if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
return;

var sb = new StringBuilder();
sb
.AppendLine("namespace Shiny.Mediator;")
.AppendLine()
.AppendLine("public static class __ShinyMediatorSourceGenExtensions {")
.AppendLine(
"\tpublic static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddDiscoveredMediatorHandlers(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services) {");

foreach (var classSymbol in this.syntaxReceiver.Classes)
sb.AppendLine($"\t\tservices.AddSingletonAsImplementedInterfaces<{classSymbol.ToDisplayString()}>();");

sb
.AppendLine("\treturn services;")
.AppendLine("\t}")
.AppendLine("}");

context.AddSource("__MediatorHandlersRegistration.g.cs", SourceText.From(sb.ToString()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis;
using SourceGeneratorsKit;

namespace Shiny.Mediator.SourceGenerators;


public class RegisterHandlerAttributeSyntaxReceiver : SyntaxReceiver
{
public override bool CollectClassSymbol => true;

protected override bool ShouldCollectClassSymbol(INamedTypeSymbol classSymbol)
{
var hasAttribute = classSymbol.HasAttribute("RegisterHandlerAttribute");
if (!hasAttribute)
return false;

if (classSymbol.IsImplements("Shiny.Mediator.IEventHandler`1"))
return false;

if (classSymbol.IsImplements("Shiny.Mediator.IRequestHandler`1"))
return false;

return false;
}
}

0 comments on commit 2006a23

Please sign in to comment.