Skip to content
Purview Dev logo

SourceGenerator Framework

Stable

A typed framework for building incremental C# source generators.

analyzers code-generation compile-time compiler csharp developer-experience developer-tools devex dotnet incremental-generator metaprogramming nuget roslyn source-generator source-generators testing

The problem

A strongly typed framework for building, testing, and maintaining incremental C# source generators with Roslyn. Includes structured code generation, attribute data models, type libraries, and a step-cache test runner for verifying incremental behaviour.

From the repository

A strongly typed framework for building, testing, and maintaining incremental C# source generators with Roslyn.

Why it became a project

Building several incremental generators exposed the same needs for typed models, structured output, testing, and cache verification, so those foundations became a shared framework.

Where it fits

Use it when

You are building a non-trivial incremental C# source generator and want reusable modelling, code-writing, packaging, and testing infrastructure.

It may not fit when

Your generator is small enough for the Roslyn APIs directly, or adopting a framework would hide behaviour your implementation needs to control precisely.

Use cases

Concrete examples of what SourceGenerator Framework does, and who each one is for.

Contributor

Build an incremental generator on typed foundations

You are writing a non-trivial incremental C# source generator and need typed attribute models, structured code output, and packaging that Roslyn will actually load.

xml
<ProjectReference
  Include="..\MyGenerator\MyGenerator.csproj"
  PrivateAssets="all"
  OutputItemType="Analyzer"
  ReferenceOutputAssembly="false"
/>

What you get: The framework supplies attribute-data models, a structured code writer, a type library, and packaging helpers so the generator stays focused on its own logic.

The Purview generators themselves are built on the framework, so it is exercised by real generators rather than being an untested abstraction.

Read the guide →
Contributor

Verify incremental behaviour in tests

You need to prove a generator is incremental and produces the code you expect, without assembling a Roslyn driver by hand in every test.

What you get: The testing package provides SourceGeneratorTestRunner<TGenerator>, a framework-agnostic test base, and assertions such as AssertSingleGeneratedSource and AssertNoCompilationErrors.

Step-cache tests and a TUnit integration ship alongside the runner, so incremental regressions surface in CI.

Read the guide →

Packages

Install

Purview.SourceGeneratorFramework primary

.NET CLI
dotnet add package Purview.SourceGeneratorFramework

Purview.SourceGeneratorFramework.Testing

.NET CLI
dotnet add package Purview.SourceGeneratorFramework.Testing

Purview.SourceGeneratorFramework.Testing.TUnit

.NET CLI
dotnet add package Purview.SourceGeneratorFramework.Testing.TUnit