|
| 1 | +# Code Refactoring: Extracted Version Helper |
| 2 | + |
| 3 | +## Problem |
| 4 | +The GitHub Actions workflow in `.github/workflows/create-release-pr.yml` contained inline Ruby code that duplicated version bumping logic from the Rakefile: |
| 5 | + |
| 6 | +```ruby |
| 7 | +# Duplicated inline Ruby in GitHub Actions |
| 8 | +NEXT_VERSION=$(ruby -e " |
| 9 | + require_relative 'Rakefile' |
| 10 | + current = '$CURRENT_VERSION' == 'none' ? nil : '$CURRENT_VERSION' |
| 11 | + puts bump_version(current, '$RELEASE_TYPE') |
| 12 | +") |
| 13 | +``` |
| 14 | + |
| 15 | +This created several issues: |
| 16 | +- **Code duplication** between Rakefile and GitHub Actions |
| 17 | +- **Maintenance burden** when updating version logic |
| 18 | +- **Fragile inline code** that's hard to test and debug |
| 19 | +- **Poor separation of concerns** |
| 20 | + |
| 21 | +## Solution |
| 22 | +Created a dedicated `VersionHelper` module and CLI script to centralize all version-related logic: |
| 23 | + |
| 24 | +### 1. New Files Created |
| 25 | + |
| 26 | +#### `lib/version_helper.rb` |
| 27 | +- **Centralized version logic** for semantic versioning operations |
| 28 | +- **Conventional commit analysis** for automatic version bump detection |
| 29 | +- **Changelog generation** with proper categorization |
| 30 | +- **Comprehensive error handling** and validation |
| 31 | + |
| 32 | +#### `scripts/version.rb` |
| 33 | +- **CLI interface** for version operations |
| 34 | +- **GitHub Actions friendly** with simple command interface |
| 35 | +- **Executable script** that can be called from any environment |
| 36 | + |
| 37 | +### 2. Key Refactoring Changes |
| 38 | + |
| 39 | +#### Before (Duplicated): |
| 40 | +```ruby |
| 41 | +# In Rakefile |
| 42 | +def bump_version(version, bump_type) |
| 43 | + # version logic here |
| 44 | +end |
| 45 | + |
| 46 | +# In GitHub Actions (inline Ruby) |
| 47 | +NEXT_VERSION=$(ruby -e "complex inline code") |
| 48 | +``` |
| 49 | + |
| 50 | +#### After (DRY): |
| 51 | +```ruby |
| 52 | +# In lib/version_helper.rb |
| 53 | +module VersionHelper |
| 54 | + def self.bump_version(current_version, release_type) |
| 55 | + # centralized logic |
| 56 | + end |
| 57 | +end |
| 58 | + |
| 59 | +# In Rakefile |
| 60 | +def bump_version(version, bump_type) |
| 61 | + VersionHelper.bump_version(version, bump_type) |
| 62 | +end |
| 63 | + |
| 64 | +# In GitHub Actions |
| 65 | +NEXT_VERSION=$(ruby scripts/version.rb next "$RELEASE_TYPE") |
| 66 | +``` |
| 67 | + |
| 68 | +### 3. Updated Components |
| 69 | + |
| 70 | +#### Rakefile |
| 71 | +- **Simplified helper methods** that delegate to VersionHelper |
| 72 | +- **Removed duplicate logic** for commit analysis and version bumping |
| 73 | +- **Enhanced preview task** using centralized version info |
| 74 | + |
| 75 | +#### GitHub Actions Workflow |
| 76 | +- **Replaced inline Ruby** with clean CLI script calls |
| 77 | +- **Improved maintainability** with external script dependency |
| 78 | +- **Better error handling** and debugging capabilities |
| 79 | + |
| 80 | +### 4. CLI Interface Examples |
| 81 | + |
| 82 | +```bash |
| 83 | +# Get current version |
| 84 | +ruby scripts/version.rb current |
| 85 | + |
| 86 | +# Get next version (auto-detect from commits) |
| 87 | +ruby scripts/version.rb next auto |
| 88 | + |
| 89 | +# Get specific version bump |
| 90 | +ruby scripts/version.rb next patch |
| 91 | + |
| 92 | +# Analyze commits for suggested release type |
| 93 | +ruby scripts/version.rb analyze |
| 94 | + |
| 95 | +# Get comprehensive version information |
| 96 | +ruby scripts/version.rb info |
| 97 | +``` |
| 98 | + |
| 99 | +## Benefits |
| 100 | + |
| 101 | +### ✅ **Eliminated Code Duplication** |
| 102 | +- Single source of truth for version logic |
| 103 | +- Consistent behavior across all tools |
| 104 | + |
| 105 | +### ✅ **Improved Maintainability** |
| 106 | +- Changes only need to be made in one place |
| 107 | +- Easier to test and debug version logic |
| 108 | + |
| 109 | +### ✅ **Better Separation of Concerns** |
| 110 | +- Version logic isolated in dedicated module |
| 111 | +- CLI script provides clean interface |
| 112 | + |
| 113 | +### ✅ **Enhanced Testability** |
| 114 | +- Helper module can be unit tested independently |
| 115 | +- CLI script provides clear interfaces |
| 116 | + |
| 117 | +### ✅ **GitHub Actions Optimization** |
| 118 | +- Cleaner workflow YAML without inline Ruby |
| 119 | +- Better error messages and debugging |
| 120 | + |
| 121 | +## Testing |
| 122 | + |
| 123 | +The refactoring maintains full backward compatibility: |
| 124 | + |
| 125 | +```bash |
| 126 | +# All existing Rake tasks still work |
| 127 | +bundle exec rake release:preview |
| 128 | +bundle exec rake release:create[patch] |
| 129 | + |
| 130 | +# New CLI interface works |
| 131 | +ruby scripts/version.rb info |
| 132 | +# Current version: none |
| 133 | +# Suggested release type: minor |
| 134 | +# Next patch: 1.0.0 |
| 135 | +# Next minor: 1.0.0 |
| 136 | +# Next major: 1.0.0 |
| 137 | +``` |
| 138 | + |
| 139 | +## Files Modified |
| 140 | + |
| 141 | +### New Files |
| 142 | +- `lib/version_helper.rb` - Centralized version management module |
| 143 | +- `scripts/version.rb` - CLI interface for version operations |
| 144 | + |
| 145 | +### Modified Files |
| 146 | +- `Rakefile` - Updated to use VersionHelper instead of duplicated logic |
| 147 | +- `.github/workflows/create-release-pr.yml` - Replaced inline Ruby with CLI script calls |
| 148 | + |
| 149 | +This refactoring follows the **DRY (Don't Repeat Yourself)** principle and provides a more maintainable, testable, and robust version management system. |
0 commit comments