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