/SourceGenerators

Provides utility functions and helpers to aid in writing source files for source generators based on T4 templates

Primary LanguageC#MIT LicenseMIT

SourceGenerators

Build status OpenSSF Scorecard GitHub license

Provides utility functions and helpers to aid in writing source files for source generators based on T4 templates

🏃 Quickstart   |   📗 Guide   |   📦 NuGet


Provides a high performance T4 base class on compiled library or source files package.

NuGet Packages

  • NuGet Raiqub.Generators.T4CodeWriter: provides a compiled library with a high performance T4 base class
  • NuGet Raiqub.Generators.T4CodeWriter.Sources: provides source files with a high performance T4 base class

Compatibility

Raiqub T4 Code Writer library requires .NET Standard 2.0 runtime to run, and the source files requires at least the .NET 6 SDK to run, but you can target earlier frameworks.

Quickstart

Add the package to your application using

dotnet add package Raiqub.Generators.T4CodeWriter.Sources

Adding the package the base class CodeWriterBase<T> will be available as a base class for T4 templates.

Guide

CodeWriterBase

To override the base class of a T4 template set the inherits attribute. For example:

<#@ template debug="false" linePragmas="false" hostspecific="false" language="C#" inherits="CodeWriterBase<MyModel>" #>

The constructor of CodeWriterBase class need to receive a StringBuilder instance, then T4 templates that inherits from CodeWriterBase need to define a constructor passing a StringBuilder instance to base class. For example:

<#+
    public MyWriter(StringBuilder builder) : base(builder)
    {
    }
#>

Additionally the method GetFileName must be implemented to define the name of file of the generated source. For example:

<#+
    public override string GetFileName() => $"{Model.Namespace ?? "_"}.{Model.Name}Extensions.g.cs";
#>

Optionally the method CanGenerateFor can be implemented to determine if source code should be generated according with the specified model state. For example:

<#+
    protected override bool CanGenerateFor(MyModel model) => model.Properties.Count > 0;
#>

CodeWriterDispatcher

The CodeWriterDispatcher is a dispatcher for code writers that generate compilation source. As it does not mutate any internal state is safe to define it as a static readonly field. Example:

private static readonly CodeWriterDispatcher<MyModel> s_dispatcher =
    new(
        sb => new MyWriter1(sb),
        sb => new MyWriter2(sb));

Then just call the GenerateSources method to generate source files using the SourceProductionContext. For example:

private static void Emit(
    Compilation compilation,
    SourceProductionContext context,
    ImmutableArray<MyModel> types)
{
    // ...
    s_dispatcher.GenerateSources(typesToGenerate, context);
}

Contributing

If something is not working for you or if you think that the source file should change, feel free to create an issue or Pull Request. I will be happy to discuss and potentially integrate your ideas!

License

See the LICENSE file for details.