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