Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions lib/gem2rpm/rpm_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

module Gem2Rpm
class RpmDependency < Gem2Rpm::Dependency
attr_reader :boolean

def initialize(dependency)
@boolean = dependency.boolean if dependency.respond_to? :boolean

if dependency.respond_to? :__getobj__
super dependency.__getobj__
else
Expand All @@ -13,36 +17,66 @@ def initialize(dependency)
# Convert to rubygem() virtual provide dependency.
def virtualize
clone.tap do |dep|
dep.name = "rubygem(#{dep.name})"
if dep.boolean
dep.instance_variable_set('@boolean', "rubygem(#{dep.name})")
else
dep.name = "rubygem(#{dep.name})"
end
end
end

# Output dependency with RPM requires tag.
def with_requires
clone.tap do |dep|
dep.name = case dep.type
prefix = case dep.type
when :development
"BuildRequires: #{dep.name}"
"BuildRequires:"
else
"Requires: #{dep.name}"
"Requires:"
end

if dep.boolean
dep.instance_variable_set('@boolean', "#{prefix} #{dep.boolean}")
else
dep.name = "#{prefix} #{dep.name}"
end
end
end

# Comment out the dependency.
def comment_out
clone.tap do |dep|
dep.name = "# #{dep.name}"
if dep.boolean
dep.instance_variable_set('@boolean', "# #{dep.boolean}")
else
dep.name = "# #{dep.name}"
end
end
end

# Represent as boolean RPM dependency.
def booleanize
clone.tap do |dep|
rpm_dependencies = requirement.map do |version|
version = nil if version && version.to_s.empty?
[name, version].compact.join(' ')
end

rpm_dependencies = rpm_dependencies.join(' with ')
if requirement.size > 1
rpm_dependencies = "(#{rpm_dependencies})"
end

dep.instance_variable_set('@boolean', rpm_dependencies)
end
end

# Returns string with entry suitable for RPM .spec file.
def to_rpm
rpm_dependencies = requirement.map do |version|
boolean || requirement.map do |version|
version = nil if version && version.to_s.empty?
[name, version].compact.join(' ')
end
rpm_dependencies.join("\n")
end.join("\n")
end
end
end
7 changes: 7 additions & 0 deletions lib/gem2rpm/rpm_dependency_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def comment_out
self.class.new dep_list
end

# Represent as boolean RPM dependency.
def booleanize
dep_list = self.map(&:booleanize)

self.class.new dep_list
end

# Returns string with all dependencies from the list converted into entries
# suitable for RPM .spec file. Thise entries should include all necessary
# macros depending on file categorization.
Expand Down
5 changes: 5 additions & 0 deletions test/test_rpm_dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ def test_comment_out
assert_equal "# #{results.first}",
Gem2Rpm::RpmDependency.new(entries.first).comment_out.to_rpm
end

def test_booleanize
assert_equal %|(#{results[1].split("\n").join(' with ')})|,
Gem2Rpm::RpmDependency.new(entries[1]).booleanize.to_rpm
end
end
16 changes: 16 additions & 0 deletions test/test_rpm_dependency_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,20 @@ def test_comment_out

assert_equal result, @dependency_list.comment_out.to_rpm
end

def test_booleanize
result =
"empty_requirement\n" \
"(pessimistic_constraint >= 1.0 with pessimistic_constraint < 2)\n" \
"development_dependency\n"
assert_equal result, @dependency_list.booleanize.to_rpm
end

def test_full_conversion_chain
result =
"# Requires: rubygem(empty_requirement)\n" \
"# Requires: (rubygem(pessimistic_constraint) >= 1.0 with rubygem(pessimistic_constraint) < 2)\n" \
"# BuildRequires: rubygem(development_dependency)\n"
assert_equal result, @dependency_list.virtualize.booleanize.with_requires.comment_out.to_rpm
end
end