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