Sha256: 9b59c8d0aa05cf10a91d786aeb5cb63bb9b3e0fe4872298588ac120adebe4938

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require "lapidarius/gem"
require "lapidarius/command"

module Lapidarius
  class Cutter
    DEVELOPMENT = "development"

    class GemNotCreatedError < StandardError; end

    def initialize(gem:, version: nil, cmd_klass: Command)
      @gem = gem
      @version = version
      @cmd = cmd_klass.new
    end

    def call(src = cmd, gem = nil)
      tokens = tokenize(src)
      token = tokens.shift
      gem ||= Gem.factory(token)
      fail GemNotCreatedError, "unable to create a gem from #{token}" unless gem
      tokens.each do |t|
        dep = Gem.factory(t)
        next unless dep
        gem << dep
        call(cmd(dep.name), dep)
      end
      gem
    end

    private def tokenize(src)
      data = src.split(/\n\n/).map!(&:strip)
      by_version(data).split("\n").tap do |tokens|
        tokens.map!(&:strip)
        tokens.reject! { |token| token.match(/#{DEVELOPMENT}/) }
      end
    end

    private def by_version(data)
      data.detect { |d| d.match(/^Gem #{@gem}-#{@version}/) } || data.first
    end

    private def cmd(gem = @gem)
      @cmd.call(gem)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lapidarius-2.0.1 lib/lapidarius/cutter.rb