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