2014-03-03 16:49:42 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
require 'rubygems/package'
|
|
|
|
|
|
|
|
module RubyGemsReq
|
|
|
|
module Helpers
|
|
|
|
# Expands '~>' and '!=' gem requirements.
|
|
|
|
def self.expand_requirement(requirements)
|
|
|
|
requirements.inject([]) do |output, r|
|
|
|
|
output.concat case r.first
|
|
|
|
when '~>'
|
|
|
|
expand_pessimistic_requirement(r)
|
|
|
|
when '!='
|
2018-04-10 07:07:02 +00:00
|
|
|
# If there is only the conflict requirement, we still need to depend
|
|
|
|
# on the specified gem.
|
|
|
|
if requirements.size == 1
|
|
|
|
Gem::Requirement.default.requirements
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2014-03-03 16:49:42 +00:00
|
|
|
else
|
|
|
|
[r]
|
|
|
|
end
|
2018-02-01 18:22:44 +00:00
|
|
|
end.reject {|r| r.empty? }
|
2014-03-03 16:49:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Expands the pessimistic version operator '~>' into equivalent '>=' and
|
|
|
|
# '<' pair.
|
|
|
|
def self.expand_pessimistic_requirement(requirement)
|
|
|
|
next_version = Gem::Version.create(requirement.last).bump
|
|
|
|
return ['>=', requirement.last], ['<', next_version]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Converts Gem::Requirement into array of requirements strings compatible
|
|
|
|
# with RPM .spec file.
|
|
|
|
def self.requirement_versions_to_rpm(requirement)
|
|
|
|
self.expand_requirement(requirement.requirements).map do |op, version|
|
2019-01-24 09:36:55 +00:00
|
|
|
version == Gem::Version.new(0) ? "" : " #{op} #{version}"
|
2014-03-03 16:49:42 +00:00
|
|
|
end
|
|
|
|
end
|
2019-01-24 09:36:55 +00:00
|
|
|
|
|
|
|
# Compose dependency together with its requirements in RPM rich dependency
|
|
|
|
# string.
|
|
|
|
def self.compose_dependency_string(name, requirements)
|
|
|
|
dependency_strings = requirements.map { |requirement| name + requirement }
|
|
|
|
dependency_string = dependency_strings.join(' with ')
|
|
|
|
dependency_string.prepend('(').concat(')') if dependency_strings.length > 1
|
|
|
|
dependency_string
|
|
|
|
end
|
2014-03-03 16:49:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Report RubyGems dependency, versioned if required.
|
|
|
|
def self.rubygems_dependency(specification)
|
2019-01-24 09:36:55 +00:00
|
|
|
dependency_name = "ruby(rubygems)"
|
|
|
|
requirements = Helpers::requirement_versions_to_rpm(specification.required_rubygems_version)
|
|
|
|
|
|
|
|
puts Helpers::compose_dependency_string(dependency_name, requirements)
|
2014-03-03 16:49:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Report all gem dependencies including their version.
|
|
|
|
def self.gem_depenencies(specification)
|
|
|
|
specification.runtime_dependencies.each do |dependency|
|
2019-01-24 09:36:55 +00:00
|
|
|
dependency_name = "rubygem(#{dependency.name})"
|
|
|
|
requirements = Helpers::requirement_versions_to_rpm(dependency.requirement)
|
|
|
|
|
|
|
|
puts Helpers::compose_dependency_string(dependency_name, requirements)
|
2014-03-03 16:49:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reports all requirements specified by all provided .gemspec files.
|
|
|
|
def self.requires
|
|
|
|
while filename = gets
|
|
|
|
filename.strip!
|
|
|
|
begin
|
|
|
|
specification = Gem::Specification.load filename
|
|
|
|
|
|
|
|
rubygems_dependency(specification)
|
|
|
|
gem_depenencies(specification)
|
|
|
|
rescue => e
|
|
|
|
# Ignore all errors.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if __FILE__ == $0
|
|
|
|
RubyGemsReq::requires
|
|
|
|
end
|