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