|
| 1 | +namespace :version do |
| 2 | + desc "Bump the gem version, update changelog, build and release the gem" |
| 3 | + task :bump, [:version] => :environment do |t, args| |
| 4 | + version = args[:version] |
| 5 | + raise "Please specify a version. Usage: rake version:bump[0.1.5]" unless version |
| 6 | + |
| 7 | + gemspec_file = Dir.glob("*.gemspec").first |
| 8 | + raise "No gemspec file found" unless gemspec_file |
| 9 | + |
| 10 | + # Update the gemspec version |
| 11 | + gemspec_content = File.read(gemspec_file) |
| 12 | + new_gemspec_content = gemspec_content.gsub(/(spec\.version\s*=\s*")\d+\.\d+\.\d+("?)/, "\\1#{version}\\2") |
| 13 | + File.write(gemspec_file, new_gemspec_content) |
| 14 | + puts "Updated gemspec version to #{version}" |
| 15 | + |
| 16 | + # Update the changelog |
| 17 | + changelog_file = "CHANGELOG.md" |
| 18 | + raise "CHANGELOG.md not found" unless File.exist?(changelog_file) |
| 19 | + |
| 20 | + changelog_content = File.read(changelog_file) |
| 21 | + new_changelog_content = changelog_content.sub("## [Unreleased]", "## [Unreleased]\n\n## [#{version}] - #{Time.now.strftime('%Y-%m-%d')}") |
| 22 | + File.write(changelog_file, new_changelog_content) |
| 23 | + puts "Updated CHANGELOG.md with version #{version}" |
| 24 | + |
| 25 | + # Commit changes and create a git tag |
| 26 | + `git add #{gemspec_file} #{changelog_file}` |
| 27 | + `git commit -m "Bump version to #{version}"` |
| 28 | + `git tag -a v#{version} -m "Version #{version}"` |
| 29 | + puts "Committed and tagged version #{version}" |
| 30 | + |
| 31 | + # Build the gem |
| 32 | + `gem build #{gemspec_file}` |
| 33 | + puts "Built gem version #{version}" |
| 34 | + |
| 35 | + # Release the gem |
| 36 | + gem_file = Dir.glob("*.gem").max_by { |f| File.mtime(f) } |
| 37 | + raise "No gem file found" unless gem_file |
| 38 | + `gem push #{gem_file}` |
| 39 | + puts "Released gem #{gem_file}" |
| 40 | + end |
| 41 | +end |
0 commit comments