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