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