Skip to content

mavfi-foundation/source-generators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MavFiFoundation.SourceGenerators

An extendable source generator that builds on Roslyn's support for incremental generators.

Basic functionality

Source generation can be triggered by specifying configuration information using:

  • Type Attributes
  • Yaml Files
  • Json Files
  • XML Files

The following template languages can be used to specify generated source code:

Basic usage

Adding the following trigger file would generate an EF Core DbContext for all classes in the project that include an attribute named DbEntity.

Trigger file (ExampleContext.CodeGen.yml)

srcLocatorType: MFFAttributeTypeLocator
srcLocatorInfo: Example.Data.DbEntityAttribute
genOutputInfos:
  - fileNameBuilderInfo: 'ExampleDbContext.g.cs'
    sourceBuilderType: MFFScribanBuilder
    sourceBuilderInfo: |-
      #nullable enable

      using Microsoft.EntityFrameworkCore;

      namespace Example.Data

      public partial class ExampleContext : DbContext
      {
        public ExampleContext(DbContextOptions<ExampleContext> options) : base(options)
        {
        }

      {{- for srcType in srcTypes }}
        public DbSet<{{ srcType.Name }}> {{ srcType.Name }}s { get; set; }

      {{- end }}
      }

Generated file (ExampleDbContext.g.cs)

#nullable enable

using Microsoft.EntityFrameworkCore;

namespace Example.Data

public partial class ExampleContext : DbContext
{
    public ExampleContext(DbContextOptions<ExampleContext> options) : base(options)
    {

    }

    public DbSet<Example> Examples { get; set; }

    public DbSet<AdditionalExample> AdditionalExamples { get; set; }

}

Releases

No releases published

Packages

No packages published