Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 122 lines (101 sloc) 3.579 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
gemfile = File.expand_path(File.join(__FILE__, '..', 'Gemfile'))
if File.exist?(gemfile) && ENV['BUNDLE_GEMFILE'].nil?
  puts "Respawning with 'bundle exec rake'"
  exec("bundle", "exec", "rake", *ARGV)
end

require 'bundler'
Bundler::GemHelper.install_tasks

require 'rake'
require 'rake/testtask'

task :my_release => ['changelog', 'release'] do
end

task :changelog do

  changelog_file = 'CHANGELOG'
  entries = ""
  
  helper = Bundler::GemHelper.new(Dir.pwd)
  current_version = "v#{helper.gemspec.version}"
  starting_version = nil
  ending_version = nil, ending_version_name = nil

  if ENV['VERSION']
    ver = ENV['VERSION']
    first_ver, second_ver = ver.split("..")
    starting_version = "v#{first_ver.gsub(/^[^\d]*/, '')}" if ! first_ver.nil? && first_ver.size > 0
    ending_version = "v#{second_ver.gsub(/^[^\d]*/, '')}" if ! second_ver.nil? && second_ver.size > 0
    ending_version_name = ending_version if ending_version
  end
  
  # If we already have a changelog, make the starting_version be the
  # last one in the changelog
  #
  if ! starting_version && File.exist?(changelog_file)
    entries = File.read(changelog_file)
    head = entries.split.first
    if head =~ /(\d\.\d\.\d).*/
      starting_version = "v#{$1}"
      
      if current_version == starting_version
        puts "WARN: gemspec version is the same as most recent changelog: #{current_version}"
      end
    end
  end
  
   # Get a list of current tags
  tags = `git tag -l`.split
  tags = tags.sort_by {|t| t[1..-1].split(".").collect {|s| s.to_i } }
  newest_tag = tags[-1]

  if current_version == newest_tag
    # When generating CHANGELOG after release, we want the last tag as the ending version
    ending_version = newest_tag
    ending_version_name = newest_tag
  else
    # When generating CHANGELOG before release, we want the current ver as the ending version
    ending_version = "HEAD"
    ending_version_name = current_version
  end

  if starting_version
    version_selector = "#{starting_version}..#{ending_version}"
  else
    puts "WARN: No starting version, dumping entire history, try: rake changelog VERSION=v1.2.3"
    version_selector = ""
  end
  
  # Generate changelog from repo
  puts "Generating a changelog for #{version_selector}"
  log=`git log --pretty='format:%s <%h>' #{version_selector}`.lines.to_a

  # Strip out maintenance entries
  log = log.delete_if do |l|
     l =~ /^Regenerated? gemspec/ ||
         l =~ /^version bump/i ||
         l =~ /^bump version/i ||
         l =~ /^Updated changelog/ ||
         l =~ /^Merged? branch/
  end
  
  # Add templates user needs to run vulcanize for
  log = log.collect do |l|
    if l =~ /<(.+)>/
      ver = $1
      files = `git diff --name-only #{ver}^1 #{ver}`.lines.to_a
      templates = files.collect {|f| f =~ /templates\/([^\/]+)\// ? $1 : nil}.compact.sort.uniq
      templates << 'core' if templates.size == 0
      l = "[#{templates.join(", ")}] #{l}"
    end
    l
  end
  
  # sort so core comes first
  log = log.sort_by {|s| s =~ /\[.*core.*\]/ ? "" : s }
  
  # Write out changelog file
  File.open(changelog_file, 'w') do |out|
    ver_title = ending_version_name.gsub(/^v/, '') + " (#{Time.now.strftime("%m/%d/%Y")})"
    out.puts ver_title
    out.puts "-" * ver_title.size
    out.puts "\n"
    out.puts log
    out.puts "\n"
    out.puts entries
  end

end

desc 'Test the rubber plugin.'
Rake::TestTask.new(:test) do |t|
  t.libs << 'lib'
  t.libs << 'test'
  t.pattern = 'test/**/*_test.rb'
  t.verbose = true
end

desc 'Default: run unit tests.'
task :default => :test
Something went wrong with that request. Please try again.