pvalena / rpms / ruby

Forked from rpms/ruby 6 years ago
Clone
724ffdb
#!/usr/bin/ruby
724ffdb
724ffdb
require 'rubygems/package'
724ffdb
724ffdb
module RubyGemsReq
724ffdb
  module Helpers
724ffdb
    # Expands '~>' and '!=' gem requirements.
724ffdb
    def self.expand_requirement(requirements)
724ffdb
      requirements.inject([]) do |output, r|
724ffdb
        output.concat case r.first
724ffdb
        when '~>'
724ffdb
          expand_pessimistic_requirement(r)
724ffdb
        when '!='
f571c55
          # If there is only the conflict requirement, we still need to depend
f571c55
          # on the specified gem.
f571c55
          if requirements.size == 1
f571c55
            Gem::Requirement.default.requirements
f571c55
          else
f571c55
            []
f571c55
          end
724ffdb
        else
724ffdb
          [r]
724ffdb
        end
637f46e
      end.reject {|r| r.empty? }
724ffdb
    end
724ffdb
724ffdb
    # Expands the pessimistic version operator '~>' into equivalent '>=' and
724ffdb
    # '<' pair.
724ffdb
    def self.expand_pessimistic_requirement(requirement)
724ffdb
      next_version = Gem::Version.create(requirement.last).bump
724ffdb
      return ['>=', requirement.last], ['<', next_version]
724ffdb
    end
724ffdb
724ffdb
    # Converts Gem::Requirement into array of requirements strings compatible
724ffdb
    # with RPM .spec file.
724ffdb
    def self.requirement_versions_to_rpm(requirement)
724ffdb
      self.expand_requirement(requirement.requirements).map do |op, version|
724ffdb
        version == Gem::Version.new(0) ? "" : "#{op} #{version}"
724ffdb
      end
724ffdb
    end
724ffdb
  end
724ffdb
724ffdb
  # Report RubyGems dependency, versioned if required.
724ffdb
  def self.rubygems_dependency(specification)
724ffdb
    Helpers::requirement_versions_to_rpm(specification.required_rubygems_version).each do |requirement|
724ffdb
      dependency_string = "ruby(rubygems)"
75ea12f
      dependency_string += " #{specification.required_rubygems_version}" if requirement&.length > 0
724ffdb
      puts dependency_string
724ffdb
    end
724ffdb
  end
724ffdb
724ffdb
  # Report all gem dependencies including their version.
724ffdb
  def self.gem_depenencies(specification)
724ffdb
    specification.runtime_dependencies.each do |dependency|
b15350c
      dependency_strings = Helpers::requirement_versions_to_rpm(dependency.requirement).map do |requirement|
b15350c
        requirement_string = "rubygem(#{dependency.name})"
75ea12f
        requirement_string += " #{requirement}" if requirement&.length > 0
b15350c
        requirement_string
724ffdb
      end
b15350c
      dependency_string = dependency_strings.join(' with ')
b15350c
      dependency_string.prepend('(').concat(')') if dependency_strings.length > 1
b15350c
      puts dependency_string
724ffdb
    end
724ffdb
  end
724ffdb
724ffdb
  # Reports all requirements specified by all provided .gemspec files.
724ffdb
  def self.requires
724ffdb
    while filename = gets
724ffdb
      filename.strip!
724ffdb
      begin
724ffdb
        specification = Gem::Specification.load filename
724ffdb
724ffdb
        rubygems_dependency(specification)
724ffdb
        gem_depenencies(specification)
724ffdb
      rescue => e
724ffdb
        # Ignore all errors.
724ffdb
      end
724ffdb
    end
724ffdb
  end
724ffdb
end
724ffdb
724ffdb
if __FILE__ == $0
724ffdb
  RubyGemsReq::requires
724ffdb
end