Sha256: f97ea23546a1e4d3a6f708050f5934cca3eff9f0e48718a442f8db7b2cfe2259

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Codelog
  module Command
    class Bump
      CHANGELOG_RELEASES_PATH = 'changelogs/releases/'.freeze
      CHANGELOG_RELEASE_REGEXP = /^\d*\.{1}\d*\.{1}\d*$/
      VALID_VERSION_TYPES = ['major', 'minor', 'patch'].freeze
      INITIAL_RELEASE_VERSION = '0.0.0'.freeze

      def self.run(version_type, release_date, options)
        Codelog::Command::Bump.new.run version_type, release_date, options
      end

      def run(version_type, release_date, options)
        unless VALID_VERSION_TYPES.include?(version_type.downcase)
          abort(Codelog::Message::Error.invalid_version_type(version_type))
        end

        if options[:preview]
          Codelog::Command::Preview.run(next_version(version_type), release_date)
        else
          Codelog::Command::Release.run(next_version(version_type), release_date)
        end
      end

      private

      def next_version(version_type)
        last_version = last_created_changelog.split('.').map(&:to_i)

        case version_type.downcase
        when 'major'
          last_version = [(last_version[0] + 1), 0, 0]
        when 'minor'
          last_version = [last_version.first, (last_version[1] + 1), 0]
        when 'patch'
          last_version[2] += 1
        end
        last_version.join('.')
      end

      def last_created_changelog
        released_versions = Dir.glob(File.join(CHANGELOG_RELEASES_PATH, '*.md*')).map do |file_path|
          file_path.gsub(CHANGELOG_RELEASES_PATH, '').gsub('.md', '')
        end.grep(CHANGELOG_RELEASE_REGEXP)

        released_versions.max_by do |version_string|
          Gem::Version.new(version_string)
        end || INITIAL_RELEASE_VERSION
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codelog-0.8.0 lib/codelog/command/bump.rb