-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
10 deletions.
There are no files selected for viewing
45 changes: 35 additions & 10 deletions
45
src/Shiny.Mediator.SourceGenerators/MediatorSourceGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Shiny.Mediator.SourceGenerators/RegisterHandlerAttributeSyntaxReceiver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |