Sundew.Generator 3.1.22

This package has a SemVer 2.0.0 package version: 3.1.22+b7c6c8fba5.
dotnet add package Sundew.Generator --version 3.1.22                
NuGet\Install-Package Sundew.Generator -Version 3.1.22                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sundew.Generator" Version="3.1.22" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sundew.Generator --version 3.1.22                
#r "nuget: Sundew.Generator, 3.1.22"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sundew.Generator as a Cake Addin
#addin nuget:?package=Sundew.Generator&version=3.1.22

// Install Sundew.Generator as a Cake Tool
#tool nuget:?package=Sundew.Generator&version=3.1.22                

Sundew.Generator

Sundew.Generator is code generator aiming to provide an alternative or replacement for T4 templates.

Generators are simple C# console applications that execute themselves after being built.

The concept separates generators from models and output, meaning it is possible to have several generators operate on the same input (models) and have the result placed in one or more outputs (writers).

Getting started

  1. Create a new project (Console application).
  2. Add the Sundew.Generator or Sundew.Generator.Code NuGet package.
  3. Implement a generator
  4. Setup
  5. Implement the main method, something like this:
public static Task Main()
{
    return GeneratorFacade.RunAsync(new MySetupsFactory());
}
  1. Build (and it will run)

<a id="implement_a_generator"></a>Implementing a generator

A generator can be added by implementing the generic Sundew.Generator.IGenerator<in TSetup, in TGeneratorSetup, in TTarget, in TModel, TRun, out TOutput> interface. The interface take quite a few generic parameters, which the developer can decide and requires the implementation of two methods.

  • Prepare must return a number IRuns, which will cause the Generate method to be called for each run. Typically prepare will return a single run.

  • Generate can output anything as long as the writer accepts the output, but typically this would be text, such as TextOutput.

Sundew.Generator does not support any form of preprocessed text templates like T4. For readability string interpolation combined with methods may be used.

public interface IGenerator<in TSetup, in TGeneratorSetup, in TTarget, in TModel, TRun, out TOutput> : IGenerator
    where TTarget : ITarget
    where TRun : IRun
{
    IReadOnlyList<TRun> Prepare(TSetup setup, TGeneratorSetup generatorSetup, TTarget target, TModel model, string modelPath);

    TOutput Generate(TSetup setup, TGeneratorSetup generatorSetup, TTarget target, TModel model, TRun run, long index);
}

Generator Example

QuantityGenerator

<a id="setup"></a>Setup

The setup is used to configure models, generators and writers. The Sundew.Generator.Setup allows setting a ModelSetup, one of more GeneratorSetups and one or more WriterSetups.

ModelSetup

A model setup allows to specify a ModelProvider and the ModelType with the Sundew.Generator.Input.ModelSetup class. If neither a ModelProvider or ModelType is specified an Sundew.Generator.ModelProviders.EmptyModelProvider<object> will be used. If only a model type is specified Sundew.Generator.ModelProviders.JsonModelProvider<TModel> will be used.

Model Providers

Sundew.Generator ships with three model providers out the box:

Sundew.Generator.ModelProviders.XmlModelProvider<TModel>
Sundew.Generator.ModelProviders.JsonModelProvider<TModel>
Sundew.Generator.ModelProviders.EmptyModelProvider<TModel>

XmlModelProvider and JsonModelProvider can use an Sundew.Generator.Input.FolderModelSetup to specify folder and file pattern to search for model files.

EmptyModelProvider provides a single model through its default constructor.

Implementing a model provider

To implement a model provider use the Sundew.Generator.Input.IModelProvider<in TSetup, in TModelSetup, TModel> interface.

The interface consists of a single async method that results a list of models. GetModelsAsync takes two parameters, the setup with which the generation was setup and a model setup.

public interface IModelProvider<in TSetup, in TModelSetup, TModel> : IModelProvider
    where TModelSetup : class
    where TModel : class
{
    Task<IReadOnlyList<IModelInfo<TModel>>> GetModelsAsync(TSetup setup, TModelSetup modelSetup);
}
Model Provider Example

JsonModelProvider

GeneratorSetups

Sundew.Generator.GeneratorSetup specify which generators to run and also allow to add generator specific settings. Additionally, they may also specify any number of WriterSetups that will only be used for this particular generator and a boolean indicating whether the global WriterSetups should skip the generator (default false).

WriterSetups

Writer setups help configure what happens with a generator's output. Sundew.Generator.Output.WriterSetup provides a target (any string) and a writer.

There is also the Sundew.Generator.Output.FileWriterSetup which can be used to specify a filename suffix and extension.

Writers
  • Sundew.Generator.Writers.TextFileWriter writes text output of each run to a file and uses the WriterSetup.Target to specify the folder to write to.
  • Sundew.Generator.Code.ProjectTextFileWriter is available in the Sundew.Generator.Code NuGet package and can be used with the provided Sundew.Generator.Code.CodeSetup to generate C# code. It expects the WriterSetup.Target to be csproj or vbproj to determine the default namespace and root folder to store generated files.
Implementing a writer

Similar to generators and model providers, a custom writer can be added be implementing the Sundew.Generator.Output.IWriter<in TWriterSetup, TTarget, in TRun, in TOutput> interface.

This is by far the most complex interface because it can manage the output of a generator.

  • GetTargetAsync runs in the very beginning of the generation process and provides a target for the generators prepare method. For example the Sundew.Generator.Writers.TextFileWriter builds the target folder path.
  • PrepareTargetAsync runs after the generation process.
  • ApplyContentToTargetAsync runs for each of the generated outputs of the generator and allows to gather the output or persist it.
  • CompleteTargetAsync runs once as a last step in the generation process and may be used to persist gathered output if necessary.
public interface IWriter<in TWriterSetup, TTarget, in TRun, in TOutput> : IWriter
    where TWriterSetup : IWriterSetup
    where TTarget : ITarget
    where TRun : IRun
{
    Task<TTarget> GetTargetAsync(TWriterSetup writerSetup);

    Task PrepareTargetAsync(TTarget target, TWriterSetup writerSetup);

    Task<string> ApplyContentToTargetAsync(TTarget target, TRun run, TWriterSetup writerSetup, TOutput output);

    Task CompleteTargetAsync(ITargetCompletionTracker targetCompletionTracker);
}
Writer Example

TextFileWriter

Additional info

Including generated code in a build

With SDK-style projects files can be included with a wildcard an ItemGroup, while the WriterSetup can be configured to output the generated files to a .generated folder.

<ItemGroup>
  <Compile Include=".generated\**\*.cs" />
</ItemGroup>

It might also be suitable to add the .generated folder to .gitignore to avoid checking in generated files.

.generated/

Configuring build order

In Visual Studio the Build Dependencies feature can be used to ensure that the generator runs before any consuming project without creating a project reference.

Right click the solution in Solution Explorer - Build Dependencies - Build Dependencies to set up the build order.

Number of runs

The number times each generator runs is determined the following way: #WriterSetups * #Models * #Runs

Json Serialized Setups

Sundew.Generator support using json files to setup up the generator. Use the RunAsync overload on the GeneratorFacade and specify the directory and file pattern to search for setup files. GeneratorFacade.RunAsync(string directory, string pattern, GeneratorOptions generatorOptions = null)

In a json setup file a "Type" property may be specified with the fully qualified name to tell the deserializer, which type to use for Setup, GeneratorSetup, ModelSetup and WriterSetup.

License:

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Sundew.Generator:

Package Downloads
Sundew.Generator.Code

Sundew.Generator is code generator aiming to provide an alternative or replacement for T4 templates.

Sundew.Generator.CommandLine

Console application for generation.

Sundew.Generator.CodeAnalysis.MSBuildWorkspace

Generation library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.22 84 4/22/2024
3.1.21 89 2/17/2024
3.1.20 64 2/15/2024
3.1.20-u20231218-221954-ci 85 12/18/2023
3.1.19 130 11/30/2023
3.1.18 69 11/26/2023
3.1.18-u20231126-231229-ci 55 11/26/2023
3.1.17 74 11/26/2023
3.1.17-u20231126-092633-ci 60 11/26/2023
3.1.16 150 3/20/2023
3.1.16-u20230320-003925-ci 90 3/20/2023
3.1.15 163 11/29/2022
3.1.15-u20221129-223403-ci 85 11/29/2022
3.1.14 330 8/21/2022
3.1.14-u20220821-115829-ci 131 8/21/2022
3.1.14-u20220810-205134-ci 117 8/10/2022
3.1.14-u20220802-202608-ci 127 8/2/2022
3.1.14-u20220714-205556-ci 129 7/14/2022
3.1.14-u20220711-200610-ci 136 7/11/2022
3.1.14-u20220629-182659-ci 132 6/29/2022
3.1.14-u20220629-182545-ci 122 6/29/2022
3.1.14-u20220623-220949-ci 210 6/23/2022
3.1.14-u20220623-220839-ci 122 6/23/2022
3.1.14-u20220623-220733-ci 132 6/23/2022
3.1.14-u20220623-220616-ci 122 6/23/2022
3.1.14-u20220623-220241-ci 114 6/23/2022
3.1.14-u20220623-215919-ci 116 6/23/2022
3.1.14-u20220623-215800-ci 120 6/23/2022
3.1.14-u20220623-215644-ci 123 6/23/2022
3.1.14-u20220623-215524-ci 116 6/23/2022
3.1.14-u20220623-214910-ci 128 6/23/2022
3.1.14-u20220623-214758-ci 127 6/23/2022
3.1.14-u20220623-213902-ci 120 6/23/2022
3.1.14-u20220623-213726-ci 127 6/23/2022
3.1.14-u20220621-210538-ci 128 6/21/2022
3.1.14-u20220621-210428-ci 132 6/21/2022
3.1.14-u20220621-210321-ci 127 6/21/2022
3.1.14-u20220621-210207-ci 120 6/21/2022
3.1.14-u20220620-205341-ci 138 6/20/2022
3.1.14-u20220620-205235-ci 139 6/20/2022
3.1.14-u20220620-205117-ci 130 6/20/2022
3.1.14-u20220620-205010-ci 132 6/20/2022
3.1.14-u20220620-204902-ci 131 6/20/2022
3.1.13 175 6/20/2022
3.1.13-u20220619-215231-ci 119 6/19/2022
3.1.13-u20220619-215121-ci 120 6/19/2022
3.1.13-u20220619-214928-ci 117 6/19/2022
3.1.13-u20220619-214739-ci 113 6/19/2022
3.1.13-u20220619-214629-ci 115 6/19/2022
3.1.13-u20220619-214237-ci 113 6/19/2022
3.1.12 508 11/20/2021
3.1.12-u20211120-045513-ci 416 11/20/2021
3.1.12-u20211120-045410-ci 415 11/20/2021
3.1.11 437 11/20/2021
3.1.11-u20211120-044240-ci 421 11/20/2021
3.1.10 282 6/18/2021
3.1.9 210 6/18/2021
3.1.8 1,192 5/2/2021
3.1.6 502 5/2/2021
3.1.5 533 4/7/2021
3.1.4 586 3/5/2021
3.1.3 517 3/4/2021
3.1.2 510 3/4/2021
3.1.1 530 3/3/2021
3.1.0 661 3/3/2021
3.0.20 518 3/3/2021
3.0.19 542 3/1/2021
3.0.18 506 3/1/2021
3.0.17 679 3/1/2021
3.0.16 518 3/1/2021
3.0.15 527 3/1/2021
3.0.14 522 3/1/2021
3.0.13 527 3/1/2021
3.0.12 525 3/1/2021
3.0.11 531 3/1/2021
3.0.10 526 3/1/2021
3.0.9 508 3/1/2021
3.0.8 517 3/1/2021
3.0.7 652 2/1/2021
3.0.6 873 12/6/2020
3.0.5 815 11/9/2020
3.0.4 783 10/29/2020
3.0.3 755 10/26/2020
3.0.2 830 10/25/2020
3.0.1 732 10/25/2020
3.0.0 815 10/24/2020
2.0.1-pre001 513 2/1/2019
2.0.1-pre000 546 2/1/2019
2.0.0 1,363 3/12/2017

3.0 - Changed to .NET Standard 2.0