Repository navigation
Description
Summary
Generators/GeneratorBase.cs is a general-purpose "JSON metadata file in, generated source out" IIncrementalGenerator base. It knows nothing about physics: it finds an AdditionalTexts entry whose path ends with a given filename, deserializes it to T with case-insensitive property matching, and hands the result to an abstract Generate. Every generator in this repo derives from it, and any metadata-driven generator in any repo could.
It should move to a shared toolkit package so it is not re-implemented per project.
What moves
GeneratorBase<T>(string metadataFilename)— registration, discovery, deserialization, JSON error reporting.WriteHeaderTo/WriteSourceFileTo— the file preamble, with the hard-coded copyright replaced by configuration (see below).- The
LoadJson<TMeta>helper currently privately duplicated insideQuantitiesGenerator(see item 2).
Problems to fix during the move
1. The copyright header is hard-coded.
protected static void WriteHeaderTo(CodeBlocker codeBlocker)
{
codeBlocker.WriteLine("// Copyright (c) 2023-2026 ktsu-dev contributors");
codeBlocker.WriteLine("// <auto-generated />");
codeBlocker.NewLine();
}CLAUDE.md already notes that this must be kept in step with .editorconfig's file_header_template by hand, and that a drift shows up as a committed-source diff rather than a build error. In a shared package the literal is untenable — it must be a virtual property or a constructor parameter, ideally defaulted from the consuming compilation.
2. The base only supports one metadata file, so the largest generator bypasses it.
QuantitiesGenerator needs both dimensions.json and units.json, so it overrides Initialize entirely, adds its own LoadJson<TMeta> + CombinedMetadata plumbing, and leaves the inherited abstract Generate as a dead shim:
/// The legacy abstract GeneratorBase{T}.Generate entry point is unused: the
/// overridden Initialize handles registration and calls GenerateInner directly.
/// This shim exists to satisfy the abstract contract.
protected override void Generate(SourceProductionContext context, DimensionsMetadata metadata, CodeBlocker codeBlocker)
=> GenerateInner(context, metadata, new UnitsMetadata());Multi-file metadata is the normal case, not the exception. The extracted base should support declaring N metadata files and combining them, so no consumer has to reimplement Initialize.
3. JSON failures are reported inconsistently.
The base reports parse failures as CONV001 / category "SourceGenerator" — a leftover from when it served only ConversionsGenerator, and inconsistent with the SEM00x / Semantics.SourceGenerators scheme every derived generator uses. QuantitiesGenerator.LoadJson meanwhile swallows JsonException and returns null, so a malformed units.json produces no diagnostic at all — the generator just silently emits identity conversions. The extracted base needs one consistent, configurable diagnostic prefix and category, and must never swallow a parse failure. See the diagnostics-helper issue.
4. Metadata discovery is by filename suffix.
file.Path.EndsWith(metadataFilename) matches any path ending in that name, so two units.json files anywhere in the compilation both match, and .Collect().FirstOrDefault() then picks one arbitrarily. Worth tightening — match on a designated AdditionalFiles metadata item group or on a relative path — and worth a diagnostic when the expected file is missing entirely rather than silently generating nothing.
Where it goes
Per the open question in #181: recommended target is a new ktsu-dev/SourceGeneratorToolkit repository, keeping the Roslyn dependency and analyzer-packaging rules out of ktsu.CodeBlocker. That decision blocks the final packaging step, not the refactor — the code can be shaped for extraction in place first.
Acceptance criteria
-
GeneratorBasesupports N metadata files with no consumer needing to overrideInitialize. -
QuantitiesGeneratordrops its privateLoadJson,CombinedMetadata,Initializeoverride and deadGenerateshim. - The emitted header is configurable, and a test asserts the configured value appears in the output.
- A malformed or missing metadata file produces a diagnostic in every code path.
- Committed generator output under
Semantics.Quantities/Generated/is unchanged (verify-generatedstays green).
Context
Part of #181.
Activity
fix: satisfy the IDE style rules in the new test files [patch]
refactor: filter explicitly instead of inside the loop body [patch]
refactor: shape the generator infrastructure for extraction [minor]
fix: point SEM001 and SEM003 at the metadata they are complaining abo…
build: silence RS2002 for the generator project [patch]