class Chmd::Make

class for making changelog.md

Attributes

remote_url[R]
tags[R]

Public Class Methods

check_git_command_exist() click to toggle source
# File lib/chmd.rb, line 93
def check_git_command_exist
  return if find_executable 'git'

  warn 'Error: Git is not installed.'
  exit 1
end
check_git_remote_is_github() click to toggle source
# File lib/chmd.rb, line 100
def check_git_remote_is_github
  system 'git rev-parse --show-toplevel', %i[out err] => File::NULL
  return if $CHILD_STATUS.success?

  warn 'Error: You are not in a git repo.'
  exit 1
end
get_prev_tag_name(tags, current_tag) click to toggle source
# File lib/chmd.rb, line 50
def get_prev_tag_name(tags, current_tag)
  (
    tags.keys[tags.keys.index(current_tag) + 1] \
    ||= tags.values.last.last.first
  ).sub('tag: ', '')
end
new() click to toggle source
# File lib/chmd.rb, line 27
def initialize
  @remote_url = self.class.set_remote
  @tags = self.class.set_tags
end
parse_tag_commits(commits, remote_url) click to toggle source
# File lib/chmd.rb, line 57
      def parse_tag_commits(commits, remote_url)
        commits.map do |hash, _, msg|
          if msg =~ /changelog/i
            nil
          else
            <<~A
              - [#{hash}](#{remote_url}/commit/#{hash})
                - #{msg}
            A
          end
        end
      end
parse_tag_info(tag_name, prev_tag_name, remote_url, timestamp) click to toggle source
# File lib/chmd.rb, line 45
def parse_tag_info(tag_name, prev_tag_name, remote_url, timestamp)
  "## [#{tag_name}](#{remote_url}/releases/tag/#{tag_name}) " \
      + "- #{timestamp} ([compare](#{remote_url}/compare/#{prev_tag_name}...#{tag_name}))"
end
set_remote() click to toggle source
# File lib/chmd.rb, line 82
def set_remote
  check_git_command_exist
  check_git_remote_is_github
  remote_urls = `git remote get-url origin`.split("\n").grep(/github\.com/)
  unless remote_urls.size == 1
    warn 'Error: remote origin is not on github.com.'
    exit 1
  end
  remote_urls[0].sub(/.git$/, '')
end
set_tags() click to toggle source
# File lib/chmd.rb, line 70
def set_tags
  ref = nil
  tags = {}
  logs = `git log --pretty=format:"%H     %ad %s      %D" \
          --date=format:'%Y-%m-%d %H:%M:%S'`.split "\n"
  logs.map { _1.split "\t" }.each do |c|
    ref = c[-1] unless c[3].nil?
    tags[ref] = tags[ref].nil? ? [c[0, 3]] : tags[ref] + [c[0, 3]]
  end
  tags
end

Public Instance Methods

make() click to toggle source
# File lib/chmd.rb, line 32
def make
  ['# Changelog'] | @tags.map do |tag, commits|
    next unless tag =~ /tag: /

    _, timestamp, = commits.first.map(&:strip)
    tag_name = tag.scan(/(?<=tag: )[^,]+/).first
    prev_tag_name = self.class.get_prev_tag_name(@tags, tag)
    tag_info = self.class.parse_tag_info(tag_name, prev_tag_name, @remote_url, timestamp)
    tag_commits_info = self.class.parse_tag_commits(commits, @remote_url).compact
    ['', tag_info, '', tag_commits_info]
  end.compact
end