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