Sundew.Generator 3.1.18-u20231126-231229-ci

This is a prerelease version of Sundew.Generator.
This package has a SemVer 2.0.0 package version: 3.1.18-u20231126-231229-ci+66672848c8.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Sundew.Generator --version 3.1.18-u20231126-231229-ci                
NuGet\Install-Package Sundew.Generator -Version 3.1.18-u20231126-231229-ci                
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.18-u20231126-231229-ci" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sundew.Generator --version 3.1.18-u20231126-231229-ci                
#r "nuget: Sundew.Generator, 3.1.18-u20231126-231229-ci"                
#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.18-u20231126-231229-ci&prerelease

// Install Sundew.Generator as a Cake Tool
#tool nuget:?package=Sundew.Generator&version=3.1.18-u20231126-231229-ci&prerelease                

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 90 4/22/2024
3.1.21 91 2/17/2024
3.1.20 66 2/15/2024
3.1.20-u20231218-221954-ci 87 12/18/2023
3.1.19 132 11/30/2023
3.1.18 71 11/26/2023
3.1.18-u20231126-231229-ci 57 11/26/2023
3.1.17 76 11/26/2023
3.1.17-u20231126-092633-ci 62 11/26/2023
3.1.16 152 3/20/2023
3.1.16-u20230320-003925-ci 92 3/20/2023
3.1.15 167 11/29/2022
3.1.15-u20221129-223403-ci 87 11/29/2022
3.1.14 334 8/21/2022
3.1.14-u20220821-115829-ci 133 8/21/2022
3.1.14-u20220810-205134-ci 119 8/10/2022
3.1.14-u20220802-202608-ci 129 8/2/2022
3.1.14-u20220714-205556-ci 131 7/14/2022
3.1.14-u20220711-200610-ci 138 7/11/2022
3.1.14-u20220629-182659-ci 134 6/29/2022
3.1.14-u20220629-182545-ci 124 6/29/2022
3.1.14-u20220623-220949-ci 212 6/23/2022
3.1.14-u20220623-220839-ci 124 6/23/2022
3.1.14-u20220623-220733-ci 134 6/23/2022
3.1.14-u20220623-220616-ci 124 6/23/2022
3.1.14-u20220623-220241-ci 116 6/23/2022
3.1.14-u20220623-215919-ci 118 6/23/2022
3.1.14-u20220623-215800-ci 122 6/23/2022
3.1.14-u20220623-215644-ci 125 6/23/2022
3.1.14-u20220623-215524-ci 118 6/23/2022
3.1.14-u20220623-214910-ci 130 6/23/2022
3.1.14-u20220623-214758-ci 129 6/23/2022
3.1.14-u20220623-213902-ci 122 6/23/2022
3.1.14-u20220623-213726-ci 129 6/23/2022
3.1.14-u20220621-210538-ci 130 6/21/2022
3.1.14-u20220621-210428-ci 134 6/21/2022
3.1.14-u20220621-210321-ci 129 6/21/2022
3.1.14-u20220621-210207-ci 122 6/21/2022
3.1.14-u20220620-205341-ci 140 6/20/2022
3.1.14-u20220620-205235-ci 141 6/20/2022
3.1.14-u20220620-205117-ci 132 6/20/2022
3.1.14-u20220620-205010-ci 134 6/20/2022
3.1.14-u20220620-204902-ci 133 6/20/2022
3.1.13 179 6/20/2022
3.1.13-u20220619-215231-ci 121 6/19/2022
3.1.13-u20220619-215121-ci 122 6/19/2022
3.1.13-u20220619-214928-ci 119 6/19/2022
3.1.13-u20220619-214739-ci 115 6/19/2022
3.1.13-u20220619-214629-ci 117 6/19/2022
3.1.13-u20220619-214237-ci 115 6/19/2022
3.1.12 512 11/20/2021
3.1.12-u20211120-045513-ci 418 11/20/2021
3.1.12-u20211120-045410-ci 417 11/20/2021
3.1.11 441 11/20/2021
3.1.11-u20211120-044240-ci 423 11/20/2021
3.1.10 286 6/18/2021
3.1.9 214 6/18/2021
3.1.8 1,196 5/2/2021
3.1.6 506 5/2/2021
3.1.5 537 4/7/2021
3.1.4 590 3/5/2021
3.1.3 521 3/4/2021
3.1.2 514 3/4/2021
3.1.1 534 3/3/2021
3.1.0 665 3/3/2021
3.0.20 522 3/3/2021
3.0.19 546 3/1/2021
3.0.18 510 3/1/2021
3.0.17 683 3/1/2021
3.0.16 523 3/1/2021
3.0.15 531 3/1/2021
3.0.14 526 3/1/2021
3.0.13 531 3/1/2021
3.0.12 529 3/1/2021
3.0.11 535 3/1/2021
3.0.10 531 3/1/2021
3.0.9 512 3/1/2021
3.0.8 521 3/1/2021
3.0.7 656 2/1/2021
3.0.6 878 12/6/2020
3.0.5 819 11/9/2020
3.0.4 787 10/29/2020
3.0.3 759 10/26/2020
3.0.2 834 10/25/2020
3.0.1 736 10/25/2020
3.0.0 819 10/24/2020
2.0.1-pre001 518 2/1/2019
2.0.1-pre000 549 2/1/2019
2.0.0 1,368 3/12/2017

3.0 - Changed to .NET Standard 2.0