Sha256: 813b945ed6686b224dd938ff88496ceaa048b289a051bb606a13cb54109b59d4

Contents?: true

Size: 1.38 KB

Versions: 12

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Nanoc
  module Int
    # Yields item reps to compile.
    #
    # @api private
    class ItemRepSelector
      def initialize(reps)
        @reps = reps
      end

      class MicroGraph
        def initialize(reps)
          @reps = Set.new(reps)
          @stack = []
        end

        def next
          if @stack.any?
            @stack.last
          elsif @reps.any?
            @reps.each { |rep| break rep }.tap do |rep|
              @reps.delete(rep)
              @stack.push(rep)
            end
          else
            nil
          end
        end

        def mark_ok
          @stack.pop
        end

        def mark_failed(dep)
          if @stack.include?(dep)
            raise Nanoc::Int::Errors::DependencyCycle.new(@stack + [dep])
          end

          @reps.delete(dep)
          @stack.push(dep)
        end
      end

      def each
        mg = MicroGraph.new(@reps)

        loop do
          rep = mg.next
          break if rep.nil?

          begin
            yield(rep)
            mg.mark_ok
          rescue => e
            actual_error = e.is_a?(Nanoc::Int::Errors::CompilationError) ? e.unwrap : e

            if actual_error.is_a?(Nanoc::Int::Errors::UnmetDependency)
              mg.mark_failed(actual_error.rep)
            else
              raise(e)
            end
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
nanoc-4.11.12 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.11 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.10 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.9 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.8 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.7 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.6 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.5 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.4 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.3 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.2 lib/nanoc/base/services/item_rep_selector.rb
nanoc-4.11.1 lib/nanoc/base/services/item_rep_selector.rb