class Apress::ChangeLogger

Constants

COMMIT_URL
EXCLUDE_MERGE
GIT_LOG_CMD
ISSUE_REGEXP
ISSUE_URL
JIRA_REGEXP
JIRA_URL

Public Class Methods

new(file_name = 'CHANGELOG.md', from = nil, to = nil) click to toggle source

initialize

@param [String] file_name with path

# File lib/apress/changelogger.rb, line 18
def initialize(file_name = 'CHANGELOG.md', from = nil, to = nil)
  @file_name = file_name
  @tag_from = from
  @tag_to = to
end

Public Instance Methods

log_changes() click to toggle source
# File lib/apress/changelogger.rb, line 24
def log_changes
  return unless git_repository?

  output = parse_change_log
  write_file output unless output.empty?
end

Private Instance Methods

git_repository?() click to toggle source
# File lib/apress/changelogger.rb, line 33
def git_repository?
  initialized = `git rev-parse --is-inside-work-tree`.chomp

  if initialized != 'true'
    initialized = false
    puts 'Exiting. Nothing to create log file.'
  end
  initialized
end
parse_change_log() click to toggle source
# File lib/apress/changelogger.rb, line 49
def parse_change_log
  output = []

  tags = `git for-each-ref --sort='*authordate' --format='%(tag)' refs/tags | grep -v '^$'#`

  tags = tags.split
  prev_begin = nil

  if (!@tag_from.nil? && !tags.include?(@tag_from)) || (!@tag_to.nil? && !tags.include?(@tag_to))
    show_not_found_message(tags)
    return output
  end

  if !@tag_from.nil? && !@tag_to.nil?
    from = tags.index(@tag_from)
    to = tags.index(@tag_to)
    tags = tags[from..to]
  elsif !@tag_from.nil?
    from = tags.index @tag_from
    prev_begin = tags[from - 1]
    tags = tags[from..-1]
  elsif !@tag_to.nil?
    to = tags.index @tag_to
    tags = tags[0..to]
  end

  tags.reverse!

  output << "\n#### [Current]" if @tag_to.nil?

  previous_tag = ''
  tags.each do |tag|
    current_tag = tag

    output << "\n#### #{previous_tag}" unless previous_tag.empty?

    if !previous_tag.empty? || @tag_to.nil?
      output << `#{GIT_LOG_CMD} "#{current_tag}".."#{previous_tag}" | #{EXCLUDE_MERGE}`
    end

    previous_tag = current_tag
  end

  output << "\n#### #{previous_tag}"

  if prev_begin.nil?
    output << `#{GIT_LOG_CMD} #{previous_tag} | #{EXCLUDE_MERGE}`
  else
    output << `#{GIT_LOG_CMD} "#{prev_begin}".."#{previous_tag}" | #{EXCLUDE_MERGE}`
  end

  output.each do |line|
    line.encode!('utf-8', 'utf-8', invalid: :replace, undef: :replace, replace: '')

    if line.index(ISSUE_REGEXP)
      line.gsub!(ISSUE_REGEXP) { |s| "[#{s}](#{ISSUE_URL}#{s[-(s.length - 1)..-1]})" }
    end

    if line.index(JIRA_REGEXP)
      line.gsub!(JIRA_REGEXP) { |s| "[#{s}](#{JIRA_URL}#{s[-(s.length - 1)..-1]})" }
    end
  end

  output
end
show_not_found_message(tags) click to toggle source
# File lib/apress/changelogger.rb, line 115
def show_not_found_message(tags)
  puts 'Could not find the given tags. Make sure that given tags exist.'
  puts 'Listing found tags:'
  puts tags
end
write_file(output) click to toggle source
# File lib/apress/changelogger.rb, line 43
def write_file(output)
  File.open(@file_name, 'w') do |file|
    file.puts(output)
  end
end