Rakefile in cutting_edge-0.2.1 vs Rakefile in cutting_edge-0.3

- old
+ new

@@ -1,7 +1,8 @@ require 'rubygems' require 'date' +require 'tempfile' require 'rspec/core/rake_task' task :default => :rspec desc "Run specs." @@ -24,10 +25,18 @@ def version line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/] line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1] end +def latest_changes_file + 'LATEST_CHANGES.md' +end + +def history_file + 'HISTORY.md' +end + # assumes x.y.z all digit version def next_version # x.y.z v = version.split '.' # bump z @@ -84,18 +93,19 @@ # ############################################################################# desc 'Create a release build and push to rubygems' task :release => :build do - unless `git branch` =~ /^\* master$/ - puts "You must be on the master branch to release!" + unless `git branch` =~ /^\* main$/ + puts "You must be on the main branch to release!" exit! end + Rake::Task[:changelog].execute sh "git commit --allow-empty -a -m 'Release #{version}'" - sh "git pull --rebase origin master" + sh "git pull --rebase origin main" sh "git tag v#{version}" - sh "git push origin master" + sh "git push origin main" sh "git push origin v#{version}" sh "gem push pkg/#{name}-#{version}.gem" end desc 'Publish to rubygems. Same as release' @@ -150,5 +160,45 @@ unless Dir['VERSION*'].empty? puts "A `VERSION` file at root level violates Gem best practices." exit! end end + +desc 'Build changelog' +task :changelog do + [latest_changes_file, history_file].each do |f| + unless File.exists?(f) + puts "#{f} does not exist but is required to build a new release." + exit! + end + end + + latest_changes = File.open(latest_changes_file) + version_pattern = "# #{version}" + + if !`grep "#{version_pattern}" #{history_file}`.empty? + puts "#{version} is already described in #{history_file}" + exit! + end + + begin + unless latest_changes.readline.chomp! =~ %r{#{version_pattern}} + puts "#{latest_changes_file} should begin with '#{version_pattern}'" + exit! + end + rescue EOFError + puts "#{latest_changes_file} is empty!" + exit! + end + + body = latest_changes.read + body.scan(/\s*#\s+\d\.\d.*/) do |match| + puts "#{latest_changes_file} may not contain multiple markdown headers!" + exit! + end + + temp = Tempfile.new + temp.puts("#{version_pattern} / #{date}\n#{body}\n") + temp.close + `cat #{history_file} >> #{temp.path}` + `cat #{temp.path} > #{history_file}` +end \ No newline at end of file