class Changelog::Generator

Public Class Methods

new(title, type, author, issue, merge_request, amend, force = false) click to toggle source
# File lib/changelog/generator.rb, line 15
def initialize(title, type, author, issue, merge_request, amend, force = false)
  @entry = Changelog::Entry.new(
    parse_title(title), 
    type, 
    parse_author(author), 
    parse_issue(issue), 
    merge_request
  )

  @amend = amend
  @force = force
end

Public Instance Methods

execute(dry_run=false) click to toggle source
# File lib/changelog/generator.rb, line 28
def execute(dry_run=false)
  assert_feature_branch!
  assert_title! unless editor
  assert_new_file!

  $stdout.puts "#{'create'.green} #{file_path}"
  $stdout.puts contents

  unless dry_run
    write
    amend_commit if @amend
  end

  if editor
    system("#{editor} '#{file_path}'")
  end
end

Private Instance Methods

amend_commit() click to toggle source
# File lib/changelog/generator.rb, line 65
def amend_commit
  fail_with "git add failed" unless system(*%W[git add #{file_path}])

  Kernel.exec(*%w[git commit --amend])
end
assert_feature_branch!() click to toggle source
# File lib/changelog/generator.rb, line 71
def assert_feature_branch!
  return unless branch_name == 'master' and not @force

  fail_with "Create a branch first!"
end
assert_new_file!() click to toggle source
# File lib/changelog/generator.rb, line 77
def assert_new_file!
  return unless File.exist?(file_path)
  return if @force

  fail_with "#{file_path} already exists! Use `--force` to overwrite."
end
assert_title!() click to toggle source
# File lib/changelog/generator.rb, line 84
def assert_title!
  return if @entry.title.length > 0 || @amend

  fail_with "Provide a title for the changelog entry or use `--amend`" \
    " to use the title from the previous commit."
end
assert_valid_type!() click to toggle source
# File lib/changelog/generator.rb, line 91
def assert_valid_type!
  return unless @entry.type && @entry.type == INVALID_TYPE

  fail_with 'Invalid category given!'
end
branch_name() click to toggle source
# File lib/changelog/generator.rb, line 141
def branch_name
  @branch_name ||= capture_stdout(%w[git symbolic-ref --short HEAD]).strip
end
contents() click to toggle source
# File lib/changelog/generator.rb, line 48
def contents
  @entry.to_yml
end
editor() click to toggle source
# File lib/changelog/generator.rb, line 61
def editor
  ENV['EDITOR']
end
file_path() click to toggle source
# File lib/changelog/generator.rb, line 128
def file_path
  base_path = File.join(
    unreleased_path,
    branch_name.gsub(/[^\w-]/, '-'))

  # Add padding for .yml extension
  base_path[0..MAX_FILENAME_LENGTH - 5] + '.yml'
end
git_user_name() click to toggle source
# File lib/changelog/generator.rb, line 115
def git_user_name
  capture_stdout(%w[git config user.name]).strip
end
issue_nr_from_branch() click to toggle source
# File lib/changelog/generator.rb, line 123
def issue_nr_from_branch
  found = branch_name.scan(/\/?(\d+)-/).first
  found.first unless found.nil?
end
last_commit_subject() click to toggle source
# File lib/changelog/generator.rb, line 119
def last_commit_subject
  capture_stdout(%w[git log --format=%s -1]).strip
end
parse_author(author) click to toggle source
# File lib/changelog/generator.rb, line 103
def parse_author(author)
  return author unless author.nil?

  git_user_name
end
parse_issue(issue) click to toggle source
# File lib/changelog/generator.rb, line 109
def parse_issue(issue)
  return issue unless issue.nil?

  issue_nr_from_branch
end
parse_title(title) click to toggle source
# File lib/changelog/generator.rb, line 97
def parse_title(title)
  return title unless title.empty?
  
  last_commit_subject
end
unreleased_path() click to toggle source
# File lib/changelog/generator.rb, line 137
def unreleased_path
  File.join('changelogs', 'unreleased')
end
write() click to toggle source
# File lib/changelog/generator.rb, line 52
def write
  dirname = File.dirname(file_path)
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end

  File.write(file_path, contents)
end