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