class TagChangelog::Generate

Attributes

commit_messages_filter[R]
config[R]
filter[R]
group[R]
options[R]
output[R]
tags_list[R]

Public Class Methods

new(project_config = {}, options = {}) click to toggle source
# File lib/tag_changelog/generate.rb, line 8
def initialize(project_config = {}, options = {})
  @project_config = project_config
  @options = options
  @config = build_configuration
  @output = open_output_file
  @tags_list = build_tags_list
  @filter = Regexp.new(config["filter"], true)
  @commit_messages_filter = set_commits_filter
  @group = config["group"]
end
run(project_config, options) click to toggle source
# File lib/tag_changelog/generate.rb, line 4
def self.run(project_config, options)
  new(project_config, options).run
end

Public Instance Methods

run() click to toggle source
# File lib/tag_changelog/generate.rb, line 19
def run
  output << "# Changelog\n\n"
  tags_list.each_cons(2) do |current_tag, previous_tag|
    tag = Git::Tag.new(current_tag)
    messages = get_commit_messages(previous_tag, current_tag)
    output << "## #{tag.version}" + " (#{tag.date})\n"
    output << messages.to_text
    output << "\n"
  end
end

Private Instance Methods

build_categories() click to toggle source
# File lib/tag_changelog/generate.rb, line 98
def build_categories
  categories = config["categories"].dup
  categories.push({ "bullet" => "[U]", "header" => "Uncategorized" })
  categories.each { |category| category["messages"] = [] }
  categories
end
build_configuration() click to toggle source
# File lib/tag_changelog/generate.rb, line 40
def build_configuration
  default_config
    .merge(@project_config)
    .merge(@options.delete_if { |_, v| v.nil? })
end
build_tags_list() click to toggle source
# File lib/tag_changelog/generate.rb, line 59
def build_tags_list
  Git::TagList.new(config["head"]).list.reject do |tag|
    tag if config["skip"].include?(tag)
  end
end
categorize_messages(messages, categories) click to toggle source
# File lib/tag_changelog/generate.rb, line 80
def categorize_messages(messages, categories)
  uncategorized = categories.detect { |cat| cat["header"] == "Uncategorized" }
  messages.each do |msg|
    matching_category = categories.detect do |category|
      next unless category["filters"]
      category["filters"].map { |ftr| msg.include?(ftr) }.include?(true)
    end
    if matching_category
      msg = msg.gsub!(filter, matching_category["bullet"])
      matching_category["messages"].push(msg)
    else
      uncategorized["messages"].push("#{msg}")
    end
  end

  categories
end
default_config() click to toggle source
# File lib/tag_changelog/generate.rb, line 46
def default_config
  YAML.load_file(@project_config["config_file"])
end
get_commit_messages(previous_tag, current_tag) click to toggle source
# File lib/tag_changelog/generate.rb, line 69
def get_commit_messages(previous_tag, current_tag)
  messages = Git::Git.get_filtered_messages(previous_tag,
                                            current_tag,
                                            commit_messages_filter).split("\n")
  # if not filtering merged pull requests only
  # we need to remove the commit sha1
  messages = messages.map { |msg| msg.split(" ")[1..-1].join(" ") } unless commit_messages_filter
  messages = categorize_messages(messages, build_categories) if group
  messages = MessageList.new(messages, group)
end
open_output_file() click to toggle source
# File lib/tag_changelog/generate.rb, line 54
def open_output_file
  puts "#{config['output']} doesn't exist in #{options[:dir]}... creating it" unless output_file_exists?
  File.open(config["output"], "w+")
end
output_file_exists?() click to toggle source
# File lib/tag_changelog/generate.rb, line 50
def output_file_exists?
  File.exists?(config["output"])
end
set_commits_filter() click to toggle source
# File lib/tag_changelog/generate.rb, line 65
def set_commits_filter
  config["pull-requests-only"] ? 'Merge pull request' : nil
end