e227b8
#!/usr/bin/ruby
e227b8
e227b8
require 'rubygems/package'
e227b8
e227b8
module RubyGemsReq
e227b8
  module Helpers
e227b8
    # Expands '~>' and '!=' gem requirements.
e227b8
    def self.expand_requirement(requirements)
e227b8
      requirements.inject([]) do |output, r|
e227b8
        output.concat case r.first
e227b8
        when '~>'
e227b8
          expand_pessimistic_requirement(r)
e227b8
        when '!='
e227b8
          # If there is only the conflict requirement, we still need to depend
e227b8
          # on the specified gem.
e227b8
          if requirements.size == 1
e227b8
            Gem::Requirement.default.requirements
e227b8
          else
e227b8
            []
e227b8
          end
e227b8
        else
e227b8
          [r]
e227b8
        end
e227b8
      end.reject {|r| r.empty? }
e227b8
    end
e227b8
e227b8
    # Expands the pessimistic version operator '~>' into equivalent '>=' and
e227b8
    # '<' pair.
e227b8
    def self.expand_pessimistic_requirement(requirement)
e227b8
      next_version = Gem::Version.create(requirement.last).bump
e227b8
      return ['>=', requirement.last], ['<', next_version]
e227b8
    end
e227b8
e227b8
    # Converts Gem::Requirement into array of requirements strings compatible
e227b8
    # with RPM .spec file.
e227b8
    def self.requirement_versions_to_rpm(requirement)
e227b8
      self.expand_requirement(requirement.requirements).map do |op, version|
e227b8
        version == Gem::Version.new(0) ? "" : " #{op} #{version}"
e227b8
      end
e227b8
    end
e227b8
e227b8
    # Compose dependency together with its requirements in RPM rich dependency
e227b8
    # string.
e227b8
    def self.compose_dependency_string(name, requirements)
e227b8
      dependency_strings = requirements.map { |requirement| name + requirement }
e227b8
      dependency_string = dependency_strings.join(' with ')
e227b8
      dependency_string.prepend('(').concat(')') if dependency_strings.length > 1
e227b8
      dependency_string
e227b8
    end
e227b8
  end
e227b8
e227b8
  # Report RubyGems dependency, versioned if required.
e227b8
  def self.rubygems_dependency(specification)
e227b8
    dependency_name = "ruby(rubygems)"
e227b8
    requirements = Helpers::requirement_versions_to_rpm(specification.required_rubygems_version)
e227b8
e227b8
    puts Helpers::compose_dependency_string(dependency_name, requirements)
e227b8
  end
e227b8
e227b8
  # Report all gem dependencies including their version.
e227b8
  def self.gem_depenencies(specification)
e227b8
    specification.runtime_dependencies.each do |dependency|
e227b8
      dependency_name = "rubygem(#{dependency.name})"
e227b8
      requirements = Helpers::requirement_versions_to_rpm(dependency.requirement)
e227b8
e227b8
      puts Helpers::compose_dependency_string(dependency_name, requirements)
e227b8
    end
e227b8
  end
e227b8
e227b8
  # Reports all requirements specified by all provided .gemspec files.
e227b8
  def self.requires
e227b8
    while filename = gets
e227b8
      filename.strip!
e227b8
      begin
e227b8
        specification = Gem::Specification.load filename
e227b8
e227b8
        rubygems_dependency(specification)
e227b8
        gem_depenencies(specification)
e227b8
      rescue => e
e227b8
        # Ignore all errors.
e227b8
      end
e227b8
    end
e227b8
  end
e227b8
end
e227b8
e227b8
if __FILE__ == $0
e227b8
  RubyGemsReq::requires
e227b8
end