Sha256: 02224c01828c43c57731bd205b1d2aa955d579908cc6d61790ce1b527a78216d

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require "core"
require "dry/monads"
require "versionaire"

module Milestoner
  module Commits
    # Calculates next version based on commit trailer version keys.
    class Versioner
      include Import[:git]
      include Dry::Monads[:result]

      using Versionaire::Cast

      DEFAULTS = {trailer_key: "Milestone", fallback: Versionaire::Version.new}.freeze

      def initialize(defaults: DEFAULTS, collector: Collector.new, **)
        @defaults = defaults
        @collector = collector
        super(**)
      end

      def call
        trailer_milestones.then { |milestones| bump milestones }
                          .value_or(fallback)
      end

      private

      attr_reader :defaults, :collector

      def trailer_milestones
        collector.call.value_or(Core::EMPTY_ARRAY).each.with_object [] do |commit, values|
          commit.trailer_value_for(trailer_key).bind { |milestone| values.append milestone.to_sym }
        end
      end

      def bump milestones
        target = fallback.members.intersection(milestones).first
        git.tag_last.fmap { |tag| target ? Version(tag).bump(target) : fallback }
      end

      def trailer_key = defaults.fetch __method__

      def fallback = defaults.fetch __method__
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
milestoner-17.10.0 lib/milestoner/commits/versioner.rb
milestoner-17.9.0 lib/milestoner/commits/versioner.rb