Skip to content

Conversation

@ncipollina
Copy link
Contributor

Summary

This PR migrates the code generation infrastructure from StringBuilder to Scriban 6.5.0 templates for improved maintainability and readability. Version bumped to 1.0.5-beta.

What's Changed

🎨 Template-based Code Generation

  • ✅ Unified template generates all interceptor code for 8 registration kinds
  • ✅ Conditional logic handles variations (keyed, factory, instance, two-type-params)
  • ✅ Template caching with ConcurrentDictionary<string, Template> for performance
  • ✅ Strongly-typed RegistrationModel (readonly record struct) for zero-boxing overhead

🧹 Code Cleanup

  • ✅ Removed 570+ lines of StringBuilder emission code
  • ✅ Simplified InterceptorEmitter.cs from 440+ lines to ~70 lines
  • ✅ Removed individual template files (Common/InterceptsLocationAttribute, DecoratorKeys, DecoratorFactory)
  • ✅ Single unified template: DecoWeaverInterceptors.scriban
  • ✅ Cleaned up unused TemplateConstants

📝 Comment Reduction

  • ✅ Removed generic "Register X" comments (obvious from code)
  • ✅ Kept only valuable WHY comments:
    • "Create nested key to avoid circular resolution" - explains nested key purpose
    • "Compose decorators (innermost to outermost)" - explains decorator ordering

📦 Dependencies

  • Added Scriban 6.5.0
  • Added Microsoft.CSharp 4.7.0 (for Scriban's dynamic features)

📚 Documentation

  • Updated Directory.Build.props to version 1.0.5-beta
  • Updated docs/changelog.md with comprehensive changes
  • Updated releasenotes.props with new release notes

Technical Details

  • Template resource format: Templates.{FileName}.scriban embedded in assembly
  • No functional changes - generated code is equivalent (except comment reduction)
  • All 49 tests passing with updated snapshots
  • Build performance unchanged - template compilation is one-time cost with caching
  • Template uses Scriban's {{- syntax for whitespace control

Test Results

  • ✅ All 49 tests passing
  • ✅ All snapshots updated to reflect new output (comment changes only)
  • ✅ Release build successful
  • ✅ No performance regression

Breaking Changes

None - this is a purely internal refactoring. Generated code is functionally equivalent.

🤖 Generated with Claude Code

ncipollina and others added 3 commits November 14, 2025 08:51
Add Scriban templating support to the source generator project.

Changes:
- Add Scriban 6.5.0 package with source inclusion
- Add Microsoft.CSharp 4.7.0 for dynamic binding support
- Create Templates/ and OutputGenerators/ folder structure
- Add TemplateHelper.cs for loading templates from embedded resources
- Add TemplateConstants.cs for template resource paths
- Configure .csproj to embed .scriban files as resources

This establishes the foundation for migrating from StringBuilder-based
code generation to maintainable Scriban templates.

Phase 1 of Scriban migration plan - infrastructure setup complete.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Add Scriban 6.5.0 and Microsoft.CSharp 4.7.0 dependencies
- Create TemplateHelper with caching and improved error messages (ChatGPT feedback)
- Create TemplateConstants for template resource paths
- Migrate InterceptsLocationAttribute, DecoratorKeys, and DecoratorFactory to Scriban templates
- Create CommonSources with individual generator methods for clean separation
- Update InterceptorEmitter to use CommonSources for template rendering
- All 49 tests passing

This is Phase 2 of the Scriban migration. Common helper code now uses templates instead of StringBuilder.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
This commit migrates the code generation infrastructure from StringBuilder
to Scriban 6.5.0 templates for improved maintainability and readability.

## Changes

### Template-based Code Generation
- Unified template generates all interceptor code for 8 registration kinds
- Conditional logic handles variations (keyed, factory, instance, two-type-params)
- Template caching with ConcurrentDictionary<string, Template> for performance
- Strongly-typed RegistrationModel (readonly record struct) for zero-boxing

### Code Cleanup
- Removed 570+ lines of StringBuilder emission code
- Simplified InterceptorEmitter.cs from 440+ lines to ~70 lines
- Removed individual template files (Common/*)
- Single unified template: DecoWeaverInterceptors.scriban
- Cleaned up unused TemplateConstants

### Comment Reduction
- Removed generic "Register X" comments
- Kept only valuable WHY comments:
  - "Create nested key to avoid circular resolution"
  - "Compose decorators (innermost to outermost)"

### Dependencies
- Added Scriban 6.5.0
- Added Microsoft.CSharp 4.7.0 (for Scriban's dynamic features)

### Version and Documentation
- Bumped version to 1.0.5-beta
- Updated changelog and release notes
- All 49 tests passing with updated snapshots

## Technical Details
- Template resource format: Templates.{FileName}.scriban embedded in assembly
- No functional changes - generated code is equivalent (except comment reduction)
- Build performance unchanged - template compilation is one-time cost with caching

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@ncipollina ncipollina requested review from Copilot and j-d-ha November 15, 2025 00:20
@ncipollina
Copy link
Contributor Author

@codex review

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR migrates the code generation infrastructure from StringBuilder to Scriban 6.5.0 templates, simplifying the codebase by removing 570+ lines of manual string building code while maintaining functional equivalence. The refactoring consolidates eight different registration patterns into a single unified template with conditional logic.

Key changes:

  • Replaced StringBuilder-based code generation with Scriban template system featuring caching and strongly-typed models
  • Reduced generated code comments to retain only meaningful explanations of non-obvious logic
  • Updated package versions including Scriban 6.5.0, Microsoft.CSharp 4.7.0, and Polyfill 9.0.3

Reviewed Changes

Copilot reviewed 51 out of 51 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/LayeredCraft.DecoWeaver.Generators/Templates/DecoWeaverInterceptors.scriban New unified Scriban template generating all interceptor code patterns
src/LayeredCraft.DecoWeaver.Generators/OutputGenerators/DecoWeaverInterceptorsSources.cs New helper for template model creation and rendering coordination
src/LayeredCraft.DecoWeaver.Generators/Emit/TemplateHelper.cs New template loading/caching infrastructure with concurrent dictionary
src/LayeredCraft.DecoWeaver.Generators/Emit/TemplateConstants.cs New constants defining template resource paths
src/LayeredCraft.DecoWeaver.Generators/Emit/InterceptorEmitter.cs Simplified from 440+ to ~70 lines by delegating to template system
src/LayeredCraft.DecoWeaver.Generators/LayeredCraft.DecoWeaver.Generators.csproj Added Scriban and Microsoft.CSharp dependencies, embedded template resources
src/LayeredCraft.DecoWeaver.Attributes/LayeredCraft.DecoWeaver.Attributes.csproj Updated Polyfill from 9.0.2 to 9.0.3
Directory.Build.props Version bump to 1.0.5-beta
releasenotes.props Added release notes for 1.0.5-beta
docs/changelog.md Documented migration changes and technical details
test/LayeredCraft.DecoWeaver.Generator.Tests/Snapshots/*.verified.cs Updated snapshots reflecting comment removal in generated code

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link
Collaborator

@j-d-ha j-d-ha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I think the template helps readability a lot.

@ncipollina ncipollina merged commit 1adf375 into main Nov 15, 2025
3 checks passed
@ncipollina ncipollina deleted the feat/scriban-templates branch November 15, 2025 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants