Skip to content

refactor: Consolidate duplicated generator logic into GeneratorUtils#1468

Merged
thomhurst merged 9 commits into
mainfrom
fix/issue-1456-generator-dry
Dec 30, 2025
Merged

refactor: Consolidate duplicated generator logic into GeneratorUtils#1468
thomhurst merged 9 commits into
mainfrom
fix/issue-1456-generator-dry

Conversation

@thomhurst
Copy link
Copy Markdown
Owner

Summary

  • Moved shared code generation logic from 4 generator files to GeneratorUtils.cs
  • Added 4 new utility methods: GenerateCliAttributeString(), GenerateValidationAttributes(), GenerateXmlDocumentation(), GenerateMethodNameFromCommandParts(), and GenerateMethodNameFromLastCommandPart()
  • Reduced ~80 lines of duplicated code across OptionsClassGenerator.cs, GlobalOptionsBaseGenerator.cs, ServiceInterfaceGenerator.cs, ServiceImplementationGenerator.cs, and SubDomainClassGenerator.cs

Details

The following duplicated logic has been consolidated:

Method Description Previously Duplicated In
GenerateCliAttributeString() Generates CliFlag/CliOption attribute strings OptionsClassGenerator, GlobalOptionsBaseGenerator
GenerateValidationAttributes() Generates Range/RegularExpression attributes OptionsClassGenerator, GlobalOptionsBaseGenerator
GenerateXmlDocumentation() Generates XML summary blocks All 5 generator files
GenerateMethodNameFromCommandParts() Converts command parts to PascalCase method name ServiceInterfaceGenerator, ServiceImplementationGenerator
GenerateMethodNameFromLastCommandPart() Generates method name from last command segment SubDomainClassGenerator

Fixes #1456

Test plan

  • Build succeeds: dotnet build tools/ModularPipelines.OptionsGenerator
  • All existing tests pass
  • Generated code output is identical (no behavioral changes)

🤖 Generated with Claude Code

thomhurst and others added 9 commits December 29, 2025 18:39
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move shared code generation logic from 4 generator files to GeneratorUtils.cs:
- GenerateCliAttributeString(): CLI attribute generation (42 lines)
- GenerateValidationAttributes(): Validation attribute generation (14 lines)
- GenerateXmlDocumentation(): XML doc comment generation (10+ locations)
- GenerateMethodNameFromCommandParts(): Full method name from command parts
- GenerateMethodNameFromLastCommandPart(): Method name from last segment

This reduces ~80 lines of duplicated code across:
- OptionsClassGenerator.cs
- GlobalOptionsBaseGenerator.cs
- ServiceInterfaceGenerator.cs
- ServiceImplementationGenerator.cs
- SubDomainClassGenerator.cs

Fixes #1456

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

Summary

Consolidates ~80 lines of duplicated generator code into shared utility methods in GeneratorUtils.cs.

Critical Issues

None found ✅

Suggestions

1. Minor case handling inconsistency in GenerateMethodNameFromCommandParts (GeneratorUtils.cs:207)

The new shared method uses .ToLowerInvariant() for the rest of the string with a length check:

.Select(p => char.ToUpperInvariant(p[0]) + (p.Length > 1 ? p[1..].ToLowerInvariant() : ""))

But the original implementation in ServiceImplementationGenerator.cs used:

.Select(p => char.ToUpperInvariant(p[0]) + p[1..].ToLowerInvariant())

The new version adds a length check which is safer and prevents potential index out of bounds if a part is a single character. This is actually an improvement, but means the generated code output may differ in edge cases with single-character command parts. Verify this doesn't break existing generated output.

2. Consider adding unit tests for the new utility methods

The PR consolidates critical code generation logic. Adding unit tests for these methods would:

  • Prevent future regressions
  • Document expected behavior with examples
  • Verify edge cases (empty strings, special characters, single-character parts)

Example test cases:

  • GenerateMethodNameFromCommandParts(["build", "server"]) should return "BuildServer"
  • GenerateMethodNameFromCommandParts(["a", "b-c"]) should return "ABC"
  • GenerateMethodNameFromLastCommandPart(...) with various inputs

Verdict

APPROVE - Well-executed refactoring with proper DRY principles. The code consolidation is sound and maintains the original behavior (with one minor safety improvement noted above).

@thomhurst thomhurst merged commit 9374bfc into main Dec 30, 2025
10 of 12 checks passed
@thomhurst thomhurst deleted the fix/issue-1456-generator-dry branch December 30, 2025 02:51
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.

HIGH: Code generators duplicate 80+ lines of logic across 4 files

1 participant