e227b8
#!/usr/bin/ruby
e227b8
e227b8
require 'rubygems/package'
e227b8
e227b8
module RubyGemsReq
e227b8
  module Helpers
e227b8
    # Keep only '!=' requirements.
e227b8
    def self.conflicts(requirements)
e227b8
      conflicts = requirements.select {|r| r.first == '!='}
e227b8
    end
e227b8
e227b8
    # Converts Gem::Requirement into array of requirements strings compatible
e227b8
    # with RPM .spec file.
e227b8
    def self.requirement_versions_to_rpm(requirement)
e227b8
      self.conflicts(requirement.requirements).map do |op, version|
e227b8
        version == Gem::Version.new(0) ? "" : "= #{version}"
e227b8
      end
e227b8
    end
e227b8
  end
e227b8
e227b8
  # Report conflicting gem dependencies including their version.
e227b8
  def self.gem_depenencies(specification)
e227b8
    specification.runtime_dependencies.each do |dependency|
e227b8
      conflict_strings = Helpers::requirement_versions_to_rpm(dependency.requirement).map do |requirement|
e227b8
        requirement_string = "rubygem(#{dependency.name}) #{requirement}"
e227b8
      end
e227b8
      if conflict_strings.length > 0
e227b8
        conflict_string = conflict_strings.join(' with ')
e227b8
        conflict_string.prepend('(').concat(')') if conflict_strings.length > 1
e227b8
        puts conflict_string
e227b8
      end
e227b8
    end
e227b8
  end
e227b8
e227b8
  # Reports all conflicts specified by all provided .gemspec files.
e227b8
  def self.conflicts
e227b8
    while filename = gets
e227b8
      filename.strip!
e227b8
      begin
e227b8
        specification = Gem::Specification.load filename
e227b8
e227b8
        gem_depenencies(specification)
e227b8
      rescue => e
e227b8
        # Ignore all errors.
e227b8
      end
e227b8
    end
e227b8
  end
e227b8
end
e227b8
e227b8
if __FILE__ == $0
e227b8
  RubyGemsReq::conflicts
e227b8
end