Skip to content

Move CodeGen/ into a shared source-generator toolkit package #192

Description

@matt-edmondson

Summary

The last outstanding piece of #181. Semantics.SourceGenerators/CodeGen/ holds the Roslyn-side infrastructure that has nothing to do with physical quantities. It is already shaped for extraction — a one-way dependency, nothing physics-specific, public where it needs to be — but it still lives in this repository, so a second project cannot use it without depending on ktsu.Semantics.

Until this lands, #181's definition of done is not met.

What moves

File What it is
CodeGen/GeneratorBase.cs IIncrementalGenerator base driven by N JSON metadata files supplied as AdditionalFiles; finds them, hands them to Generate, reports anything missing or malformed
CodeGen/MetadataFile.cs MetadataFile / MetadataSet — deserialization, and FindLocation for pointing a diagnostic at a position in the metadata
CodeGen/DiagnosticCatalog.cs Descriptor allocation under one prefix and category, plus the Report/ReportAt helpers
CodeGen/CSharpKeywords.cs The generic C# vocabulary left over from splitting Emit
Semantics.Test/Quantities/GeneratorHarness.cs The CSharpGeneratorDriver harness — supplies metadata the way MSBuild does, and can run a generator twice to check it reuses its output

What stays: Models/, Metadata/, Generators/, SemanticsDiagnostics, SemanticsGenerator/SemanticsMultiFileGenerator, and the Semantics-specific half of Emit.

The decision this needs first

  • (a) A new ktsu-dev/SourceGeneratorToolkit repo — recommended. Keeps the Roslyn dependency and the analyzer-packaging rules (IsRoslynComponent, EnforceExtendedAnalyzerRules, the GetDependencyTargetPaths bundling target) out of ktsu.CodeBlocker, which ships 8 target frameworks down to netstandard2.0 and has one small dependency.
  • (b) A second package in the CodeBlocker repo (ktsu.CodeBlocker.SourceGenerators) — fewer repos, but couples CodeBlocker's release cadence to Roslyn's, and Roslyn moves fast enough that this repository already pins Microsoft.CodeAnalysis 5.9.

Notes for whoever picks it up

  • The two Semantics base classes exist precisely to keep the toolkit consumer-agnostic: SemanticsGenerator<T> binds the diagnostic catalogue and the file header in one place. A second consumer writes its own equivalent — that is the seam, and it should stay.
  • WriteFileHeader/WriteSourceFile take the copyright as a parameter rather than hard-coding it, so nothing about this repository leaks into the package.
  • The harness carries the one piece of hard-won packaging knowledge worth not rediscovering: a project that references the generator as a plain library rather than as an analyzer must opt out of dependency bundling with AdditionalProperties="BundleAnalyzerDependencies=false", or the bundled netstandard2.0 facades collide with the in-box types (CS0433 on ReadOnlySpan<T>, Vector4, and friends).
  • Semantics.SourceGenerators.csproj uses <Project Sdk="Microsoft.NET.Sdk"> rather than ktsu.Sdk. Worth deciding deliberately whether the new package follows suit.

Acceptance criteria

  • Packaging decision made and recorded here.
  • CodeGen/ and GeneratorHarness live in the chosen package; this repository consumes them.
  • Semantics.SourceGenerators contains only Models/, Metadata/, Generators/ and the Semantics-specific bindings.
  • Generated output under Semantics.Quantities/Generated/ unchanged (Generated files up to date stays green).
  • Epic: extract and generalize the code generation stack so other projects can share it #181 can be closed.

Context

Part of #181. #182 shaped this code for extraction; #187 removed the template half of it into ktsu.CodeBlocker 1.3.0. This is the remainder.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions