pvalena / rpms / ruby

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