class Changelog::Updater

Public Class Methods

new(path, dry_run = false) click to toggle source
# File lib/changelog/updater.rb, line 6
def initialize(path, dry_run = false)
    @path = path
    @dry_run = dry_run
end

Public Instance Methods

insert(version, markdown) click to toggle source
# File lib/changelog/updater.rb, line 11
def insert(version, markdown) 

    create_file unless File.file?(@path)

    versions_found = false

    contents = File.read(@path).lines if File.exist?(@path)
    contents ||= []
    contents.each_with_index do |line, index|
    
        if line =~ /\A## v?(\d+\.\d+\.\d+)/
            header = Changelog::SemanticVersion.new($1)

            if version == header
                entries = markdown.lines
                entries.shift(2) # Remove the header and the blank line
                entries.pop      # Remove the trailing blank line

                # Insert the entries below the existing header and its blank line
                contents.insert(index + 2, entries)
                versions_found = true
                
                break
            elsif version >= header
                contents.insert(index, *markdown.lines)
                versions_found = true

                break
            end

        end

    end

    contents.push(*markdown.lines) unless versions_found

    contents
        .flatten
        .map { |line| line.force_encoding(Encoding::UTF_8) }
        .join

    File.open(@path, "w") { |f| f.puts contents } unless @dry_run
    $stdout.puts "#{'CHANGELOG:'.green}\n\n #{contents.join('')}\n" if @dry_run
    
end

Private Instance Methods

create_file() click to toggle source
# File lib/changelog/updater.rb, line 59
def create_file
    File.open(@path, "w") { |f| f.write header } unless @dry_run
    $stdout.puts "#{'create'.green} #{@path}" if @dry_run

end
header() click to toggle source
# File lib/changelog/updater.rb, line 65
def header 
    str = +""
    str << "**Note:** This file is automatically generated.\n\n"
    str << "# Changelog\n\n"
end