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