vondruch / 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 '!='
724ffd
          expand_not_equal_requirement(r)
724ffd
        else
724ffd
          [r]
724ffd
        end
724ffd
      end
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
    # Expands the not equal version operator '!=' into equivalent '<' and
724ffd
    # '>' pair.
724ffd
    def self.expand_not_equal_requirement(requirement)
724ffd
      return ['<', requirement.last], ['>', requirement.last]
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|
724ffd
        version == Gem::Version.new(0) ? "" : "#{op} #{version}"
724ffd
      end
724ffd
    end
724ffd
  end
724ffd
724ffd
  # Report RubyGems dependency, versioned if required.
724ffd
  def self.rubygems_dependency(specification)
724ffd
    Helpers::requirement_versions_to_rpm(specification.required_rubygems_version).each do |requirement|
724ffd
      dependency_string = "ruby(rubygems)"
699faa
      dependency_string += " #{specification.required_rubygems_version}" if requirement && requirement.length > 0
724ffd
      puts dependency_string
724ffd
    end
724ffd
  end
724ffd
724ffd
  # Report all gem dependencies including their version.
724ffd
  def self.gem_depenencies(specification)
724ffd
    specification.runtime_dependencies.each do |dependency|
724ffd
      Helpers::requirement_versions_to_rpm(dependency.requirement).each do |requirement|
724ffd
        dependency_string = "rubygem(#{dependency.name})"
724ffd
        dependency_string += " #{requirement}" if requirement && requirement.length > 0
724ffd
        puts dependency_string
724ffd
      end
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