class Logchange::Release

Releases new changes to a markdown log.

Public Class Methods

new(tag) click to toggle source
# File lib/logchange/release.rb, line 7
def initialize(tag)
  @tag = tag
end

Public Instance Methods

execute() click to toggle source
# File lib/logchange/release.rb, line 11
def execute
  return if no_unreleased_changes?

  release_list = { 'timestamp' => Time.now.utc.iso8601 }
  release_list['tag'] = @tag unless @tag.nil?

  release_list['changes'] = unreleased_changelogs.map do |changelog_path|
    YAML.safe_load(File.read(changelog_path))
  end

  # Sort the list of changes by timestamp, in reverse.
  release_list['changes'] = release_list['changes'].sort_by do |changelog|
    Time.parse(changelog['timestamp'])
  end.reverse

  update_release_file(release_list)

  # Delete changelogs from unreleased directory.
  unreleased_changelogs.each { |changelog_path| FileUtils.rm(changelog_path) }
end

Private Instance Methods

no_unreleased_changes?() click to toggle source
# File lib/logchange/release.rb, line 55
def no_unreleased_changes?
  return false if unreleased_changelogs.any?
  puts 'There are no unreleased changes.'
  true
end
release_path() click to toggle source
# File lib/logchange/release.rb, line 50
def release_path
  filename = "#{Time.now.utc.year}.yaml"
  File.join(Logchange.configuration.changelog_directory_path, filename)
end
unreleased_changelogs() click to toggle source
# File lib/logchange/release.rb, line 61
def unreleased_changelogs
  @unreleased_changelogs ||= Dir[File.join(Logchange.configuration.changelog_directory_path, 'unreleased', '*.yaml')]
end
update_release_file(release) click to toggle source
# File lib/logchange/release.rb, line 34
def update_release_file(release)
  existing_release = if File.exist?(release_path)
    YAML.safe_load(File.read(release_path))
  else
    { 'changelog' => [] }
  end

  # Add latest release to the top of existing release's changelog.
  existing_release['changelog'] = [release] + existing_release['changelog']

  # Now write updated release back.
  File.write(release_path, YAML.dump(existing_release))

  puts "Released changes to #{release_path}."
end