Sha256: ba4b410a142bd7f22dd445d037641dec27f923db4039ce85853e9e4624f795ac

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

require 'semantic'

module Schmersion
  class VersionCalculator

    def initialize(current, commits, **options)
      if current.is_a?(String)
        current = Semantic::Version.new(current)
      end

      @current = current || Semantic::Version.new('1.0.0')
      @commits = commits
      @options = options
    end

    def calculate
      if @current.pre && pre?
        new_version = @current.dup
      elsif breaking_changes? && consider_breaking_changes_major? && @current.major.positive?
        new_version = @current.increment!(:major)
      elsif features? || breaking_changes?
        new_version = @current.increment!(:minor)
      else
        new_version = @current.increment!(:patch)
      end

      add_pre_to_version(new_version) if @options[:pre]

      new_version
    end

    private

    def pre?
      !!@options[:pre]
    end

    def features?
      @commits.any? { |c| c.message.type == 'feat' }
    end

    def breaking_changes?
      @commits.any? { |c| c.message.breaking_change? }
    end

    def consider_breaking_changes_major?
      return false if @options[:breaking_change_not_major]

      true
    end

    def add_pre_to_version(version)
      prefix = @options[:pre]
      prefix = 'pre' unless prefix.is_a?(String)

      current_prefix, current_sequence = @current&.pre&.split('.', 2)
      if current_prefix == prefix
        # If the current version's prefix is the same as the one
        # that's been selected, we'll increment its integer
        pre_sequence = current_sequence.to_i + 1
      else
        pre_sequence = 1
      end
      version.pre = "#{prefix}.#{pre_sequence}"
      version
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
schmersion-1.1.3 lib/schmersion/version_calculator.rb
schmersion-1.1.2 lib/schmersion/version_calculator.rb
schmersion-1.1.0 lib/schmersion/version_calculator.rb
schmersion-1.0.1 lib/schmersion/version_calculator.rb
schmersion-1.0.0 lib/schmersion/version_calculator.rb