Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kant2002/sourcegeneratorskit
Utility library for faster writing source generators
https://github.com/kant2002/sourcegeneratorskit
source-generators
Last synced: 21 days ago
JSON representation
Utility library for faster writing source generators
- Host: GitHub
- URL: https://github.com/kant2002/sourcegeneratorskit
- Owner: kant2002
- Created: 2021-05-05T16:31:31.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-05-06T03:54:51.000Z (over 3 years ago)
- Last Synced: 2024-10-03T12:31:16.062Z (about 1 month ago)
- Topics: source-generators
- Language: C#
- Homepage:
- Size: 7.81 KB
- Stars: 59
- Watchers: 5
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Source Generators Toolkit
=========================![Nuget](https://img.shields.io/nuget/v/SourceGeneratorsKit)
Toolkit for writing source generators, and specifically collect important information about project to aid generation itself.
# Getting started
```
[Generator]
public class MagicGenerator : ISourceGenerator
{
SyntaxReceiver syntaxReceiver = new MethodsWithAttributeReceiver("MagicAttribute");///
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => syntaxReceiver);
}public void Execute(GeneratorExecutionContext context)
{
// Retrieve the populated receiver
if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
{
return;
}foreach (IMethodSymbol methodSymbol in this.syntaxReceiver.Methods)
{
// process your method here.
}
}
}
```# Find all classes which implements specific interface
```
[Generator]
public class MagicGenerator : ISourceGenerator
{
SyntaxReceiver syntaxReceiver = new ClassesWithInterfacesReceiver("IEnumerable");///
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => syntaxReceiver);
}public void Execute(GeneratorExecutionContext context)
{
// Retrieve the populated receiver
if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
{
return;
}foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver.Classes)
{
// process your class here.
}
}
}
```# Find all classes which derived from well-known type
```
[Generator]
public class MagicGenerator : ISourceGenerator
{
SyntaxReceiver syntaxReceiver = new DerivedClassesReceiver("DbConnection");///
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => syntaxReceiver);
}public void Execute(GeneratorExecutionContext context)
{
// Retrieve the populated receiver
if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
{
return;
}foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver.Classes)
{
// process your class here.
}
}
}
```# Find all members with given attribute
```
[Generator]
public class MagicGenerator : ISourceGenerator
{
SyntaxReceiver syntaxReceiver = new MethodsWithAttributeReceiver("MagicAttribute");///
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => syntaxReceiver);
}public void Execute(GeneratorExecutionContext context)
{
// Retrieve the populated receiver
if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
{
return;
}foreach (IMethodSymbol methodSymbol in this.syntaxReceiver.Methods)
{
// process your method here.
var parameters = methodSymbol.Parameters;
foreach (var parameter in parameters)
{
// Do your parameter magic here.
}
}
}
}
```# Work with multiple receivers
```
[Generator]
public class AggregateMagicGenerator : ISourceGenerator
{
SyntaxReceiver syntaxReceiver1 = new MethodsWithAttributeReceiver("MagicAttribute");
SyntaxReceiver syntaxReceiver2 = new DerivedClassesReceiver("DbConnection");
SyntaxReceiver agregateSyntaxReceiver;public MagicGenerator()
{
this.agregateSyntaxReceiver = new AggregateSyntaxContextReceiver(this.syntaxReceiver1, this.syntaxReceiver2);
}///
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => agregateSyntaxReceiver);
}public void Execute(GeneratorExecutionContext context)
{
// Retrieve the populated receiver
if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
{
return;
}foreach (IMethodSymbol methodSymbol in this.syntaxReceiver1.Methods)
{
// process your method here.
var parameters = methodSymbol.Parameters;
foreach (var parameter in parameters)
{
// Do your parameter magic here.
}
}foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver2.Classes)
{
// process your class here.
}
}
}
```