FractalDataWorks.SmartGenerators.CodeBuilders
0.1.4-alpha-g4415be49cc
This is a prerelease version of FractalDataWorks.SmartGenerators.CodeBuilders.
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package FractalDataWorks.SmartGenerators.CodeBuilders --version 0.1.4-alpha-g4415be49cc
NuGet\Install-Package FractalDataWorks.SmartGenerators.CodeBuilders -Version 0.1.4-alpha-g4415be49cc
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="FractalDataWorks.SmartGenerators.CodeBuilders" Version="0.1.4-alpha-g4415be49cc"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FractalDataWorks.SmartGenerators.CodeBuilders" Version="0.1.4-alpha-g4415be49cc" />
<PackageReference Include="FractalDataWorks.SmartGenerators.CodeBuilders"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FractalDataWorks.SmartGenerators.CodeBuilders --version 0.1.4-alpha-g4415be49cc
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: FractalDataWorks.SmartGenerators.CodeBuilders, 0.1.4-alpha-g4415be49cc"
#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.
#:package FractalDataWorks.SmartGenerators.CodeBuilders@0.1.4-alpha-g4415be49cc
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FractalDataWorks.SmartGenerators.CodeBuilders&version=0.1.4-alpha-g4415be49cc&prerelease
#tool nuget:?package=FractalDataWorks.SmartGenerators.CodeBuilders&version=0.1.4-alpha-g4415be49cc&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
FractalDataWorks Smart Generators
Part of the FractalDataWorks toolkit.
Build Status
Release Status
Package Status
License
A comprehensive toolkit for building, testing, and deploying Roslyn source generators with a fluent code generation API.
Overview
FractalDataWorks Smart Generators provides three core packages:
- FractalDataWorks.SmartGenerators: Base classes and utilities for building incremental source generators
- FractalDataWorks.SmartGenerators.CodeBuilders: Fluent API for generating C# code programmatically
- FractalDataWorks.SmartGenerators.TestUtilities: Testing framework for source generators with expectations API
Installation
dotnet add package FractalDataWorks.SmartGenerators
dotnet add package FractalDataWorks.SmartGenerators.CodeBuilders
dotnet add package FractalDataWorks.SmartGenerators.TestUtilities
Key Features
Source Generator Base Classes
IncrementalGeneratorBase<T>
- Simplified base class for incremental generators- Built-in assembly scanning for cross-project type discovery
- Attribute source registration helpers
- Diagnostic reporting utilities
Code Builders
- Fluent API for building C# code structures
- Builders for classes, interfaces, records, enums, methods, properties, and more
- Automatic formatting and proper indentation
- XML documentation support
Testing Framework
SourceGeneratorTestHelper
for running generators in tests- Expectations API for asserting generated code structure
- Compilation verification
- Multi-generator test support
Quick Start
Creating an Incremental Generator
using FractalDataWorks.SmartGenerators;
using Microsoft.CodeAnalysis;
[Generator]
public class MyGenerator : IncrementalGeneratorBase<MyInputInfo>
{
protected override bool IsRelevantSyntax(SyntaxNode syntaxNode)
{
// Filter to classes with specific attributes
return syntaxNode is ClassDeclarationSyntax;
}
protected override MyInputInfo? TransformSyntax(GeneratorSyntaxContext context)
{
// Transform syntax into your input model
return new MyInputInfo { /* ... */ };
}
protected override void RegisterSourceOutput(
IncrementalGeneratorInitializationContext context,
IncrementalValuesProvider<MyInputInfo?> syntaxProvider)
{
context.RegisterSourceOutput(syntaxProvider, (spc, input) =>
{
if (input is null) return;
// Generate code using input
var code = GenerateCode(input);
spc.AddSource($"{input.Name}.g.cs", code);
});
}
}
Using Code Builders
using FractalDataWorks.SmartGenerators.CodeBuilders;
var classBuilder = new ClassBuilder("PersonDto")
.MakePublic()
.MakePartial()
.WithSummary("Data transfer object for Person entity")
.AddProperty("Id", "int", property => property
.WithXmlDocSummary("Gets or sets the unique identifier"))
.AddProperty("Name", "string", property => property
.WithXmlDocSummary("Gets or sets the person's name")
.WithInitSetter())
.AddMethod("ToString", "string", method => method
.MakePublic()
.MakeOverride()
.WithBody("return $\"Person {{ Id = {Id}, Name = {Name} }}\";"));
var code = new NamespaceBuilder("MyApp.Models")
.AddMember(classBuilder)
.Build();
Testing Your Generator
using FractalDataWorks.SmartGenerators.TestUtilities;
using Xunit;
using Shouldly;
public class MyGeneratorTests
{
[Fact]
public void GeneratesExpectedCode()
{
// Arrange
var source = @"
[GenerateDto]
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}";
// Act
var generator = new MyGenerator();
var output = SourceGeneratorTestHelper.RunGenerator(
generator,
new[] { source },
out var diagnostics);
// Assert
diagnostics.ShouldBeEmpty();
output.Count.ShouldBe(1);
// Use expectations API for structural assertions
ExpectationsFactory.ExpectCode(output.Values.First())
.HasNamespace("MyApp.Models")
.HasClass("PersonDto", c => c
.HasModifier("public")
.HasModifier("partial")
.HasProperty("Id", p => p.HasType("int"))
.HasProperty("Name", p => p.HasType("string"))
.HasMethod("ToString", m => m.HasReturnType("string")))
.Assert();
}
}
Assembly Scanner Usage
Enable assembly scanning in your project:
[assembly: FractalDataWorks.SmartGenerators.EnableAssemblyScanner]
Then use it in your generator:
var scanner = AssemblyScannerService.Get(compilation);
if (scanner != null)
{
var allTypes = scanner.AllNamedTypes;
// Process types across the compilation
}
Documentation
Requirements
- .NET Standard 2.0 (for generators)
- .NET SDK 8.0 or later (for development)
- C# 12.0 or later
License
This project is licensed under the Apache License 2.0.
Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.14.0)
- System.Text.RegularExpressions (>= 4.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.1.5-alpha-g8cae162c25 | 482 | 7/23/2025 |
0.1.4-alpha-g4415be49cc | 277 | 7/21/2025 |
0.1.3-alpha-g2dc2da0077 | 268 | 7/20/2025 |
0.1.2-alpha-gf6ea635473 | 125 | 7/14/2025 |
0.1.1-alpha-gc64e6ae799 | 119 | 7/14/2025 |
0.1.1-alpha-g64f4a16f7b | 112 | 7/14/2025 |