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