Sha256: f4f545a977016608e2cb3185e22857b76dcc105d86c167e231e125f50aff269f

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require 'json'
module Chchchanges
  class Generator
    attr_accessor :changelog_hash, :changelog

    def initialize
      @changelog_hash = {}
      @changelog = "# Change Log\n"
    end

    def call
      read_changelog_entry_files
      write_to_changelog_file
    end

    private

    def read_changelog_entry_files
      Dir.foreach('.changelog_entries') do |json_file|
        next if json_file == '.' or json_file == '..'
        entry = File.read(".changelog_entries/#{json_file}")
        entry_hash = JSON.parse(entry)
        version = entry_hash["version"]
        type = entry_hash["type"]
        description = entry_hash["description"]
        ticket = entry_hash["ticket"]
        @changelog_hash[version] = {} unless changelog_hash[version]
        @changelog_hash[version][type] = [] unless changelog_hash[version][type]
        @changelog_hash[version][type] << "- [#{ticket}] #{description}\n"
      end
    end

    def write_to_changelog_file
      versions = @changelog_hash.keys.sort_by{|v| Gem::Version.new(v)}.reverse
      versions.each do |version|
        change_types = changelog_hash[version]
        @changelog << "\n## [#{version}]\n"
        change_types.sort.each do |type, changes|
          @changelog << "### #{type}\n"
          changes.sort.each do |change|
            @changelog << change
          end
        end
      end
      File.write('CHANGELOG.md', @changelog)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chchchanges-0.1.1 lib/chchchanges/generator.rb
chchchanges-0.1.0 lib/chchchanges/generator.rb