Skip to content

Commit

Permalink
Merge pull request #116 from ekohl/respect-doc-rules
Browse files Browse the repository at this point in the history
Introduce a template context for proper loading
  • Loading branch information
voxik authored Aug 5, 2022
2 parents 91b38c8 + 644410b commit 331c674
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 32 deletions.
31 changes: 3 additions & 28 deletions lib/gem2rpm.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'erb'
require 'socket'
require 'gem2rpm/context'
require 'gem2rpm/distro'
require 'gem2rpm/gem/format'
require 'gem2rpm/gem/package'
Expand All @@ -12,8 +13,6 @@
require 'gem2rpm/test_suite'

module Gem2Rpm
extend Gem2Rpm::TemplateHelpers

Gem2Rpm::VERSION = "1.0.2".freeze

class Exception < RuntimeError; end
Expand Down Expand Up @@ -64,31 +63,7 @@ def self.find_download_url(name, version)

def self.convert(fname, template, out = $stdout, nongem = true, local = false,
doc_subpackage = true)
package = Gem2Rpm::Package.new(fname)
# Deprecate, kept just for backward compatibility.
format = Gem2Rpm::Format.new(package)
spec = Gem2Rpm::Specification.new(package.spec)

config = Configuration.instance.reset

runtime_dependencies = Gem2Rpm::RpmDependencyList.new(spec.runtime_dependencies)
development_dependencies = Gem2Rpm::RpmDependencyList.new(spec.development_dependencies)

tests = TestSuite.new(spec)

files = RpmFileList.new(spec.files)
main_files = files.top_level_entries.main_entries
doc_files = files.top_level_entries.doc_entries

download_path = ""
unless local
begin
download_path = find_download_url(spec.name, spec.version)
rescue DownloadUrlError => e
$stderr.puts "Warning: Could not retrieve full URL for #{spec.name}\nWarning: Edit the specfile and enter the full download URL as 'Source0' manually"
$stderr.puts e.inspect
end
end
context = Gem2Rpm::Context.new(fname, nongem, local, doc_subpackage)

# Check if keyword arguments are used. The condition could let go as soon
# as only Ruby 2.6+ is supported.
Expand All @@ -98,7 +73,7 @@ def self.convert(fname, template, out = $stdout, nongem = true, local = false,
else
ERB.new(template.read, 0, '-')
end
out.puts erb.result(binding)
out.puts erb.result(context.instance_eval { binding })
rescue Gem::Exception => e
puts e
end
Expand Down
54 changes: 54 additions & 0 deletions lib/gem2rpm/context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'gem2rpm/template_helpers'

module Gem2Rpm
class Context
include Gem2Rpm::TemplateHelpers

attr_reader :nongem, :local, :doc_subpackage, :package, :format, :spec,
:config, :runtime_dependencies, :development_dependencies, :tests,
:files, :download_path

def initialize(fname, nongem = true, local = false, doc_subpackage = true)
@nongem = nongem
@local = local
@doc_subpackage = doc_subpackage

@package = Gem2Rpm::Package.new(fname)
# Deprecate, kept just for backward compatibility.
@format = Gem2Rpm::Format.new(@package)
@spec = Gem2Rpm::Specification.new(@package.spec)

@config = Configuration.instance.reset

@runtime_dependencies = Gem2Rpm::RpmDependencyList.new(@spec.runtime_dependencies)
@development_dependencies = Gem2Rpm::RpmDependencyList.new(@spec.development_dependencies)

@tests = TestSuite.new(spec)

# Ruby 2.0 doesn't have sorted files
@files = RpmFileList.new(spec.files.sort)

@download_path = ""
unless @local
begin
@download_path = Gem2Rpm.find_download_url(@spec.name, @spec.version)
rescue DownloadUrlError => e
$stderr.puts "Warning: Could not retrieve full URL for #{@spec.name}\nWarning: Edit the specfile and enter the full download URL as 'Source0' manually"
$stderr.puts e.inspect
end
end
end

def main_files
@files.top_level_entries.main_entries
end

def doc_files
@files.top_level_entries.doc_entries
end

def packager
Gem2Rpm.packager
end
end
end
2 changes: 2 additions & 0 deletions lib/gem2rpm/rpm_dependency.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'gem2rpm/gem/dependency'

module Gem2Rpm
class RpmDependency < Gem2Rpm::Dependency
def initialize(dependency)
Expand Down
11 changes: 11 additions & 0 deletions test/templates/fake_files/config-override.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%-
# Move runtime to the doc subpackage
config.rules[:misc] << 'runtime'
# %exclude Gemfile
config.rules[:ignore] << 'Gemfile'
-%>
%files
<%= main_files.to_rpm %>

%files doc
<%= doc_files.to_rpm %>
25 changes: 25 additions & 0 deletions test/templates/test_config_override.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'helper'

class TestConfigOverride < Minitest::Test
def test_rules_respected
template = Gem2Rpm::Template.new(File.join(__dir__, 'fake_files', 'config-override.erb'))
out = StringIO.new
Gem2Rpm.convert(gem_path, template, out, false)

expected = <<-EXPECTED.gsub(/^ */, '')
%files
%exclude %{gem_instdir}/.travis.yml
%{gem_instdir}/exe
%{gem_instdir}/ext
%{gem_libdir}
%files doc
%exclude %{gem_instdir}/Gemfile
%doc %{gem_instdir}/README
%{gem_instdir}/Rakefile
%{gem_instdir}/runtime
%{gem_instdir}/testing_gem.gemspec
EXPECTED
assert_equal(expected, out.string)
end
end
12 changes: 8 additions & 4 deletions test/test_template_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
require 'helper'
require 'gem2rpm/template_helpers'

class ConcreteTemplateHelpers
extend Gem2Rpm::TemplateHelpers
end

class TestTemplateHelpers < Minitest::Test
def test_requirement
assert_equal "rubygem(foo)", Gem2Rpm.requirement("rubygem(foo)")
assert_equal "rubygem(foo) >= 1.0", Gem2Rpm.requirement("rubygem(foo)", ">= 1.0")
assert_equal "rubygem(foo)", Gem2Rpm.requirement("rubygem(foo)", "")
assert_equal "rubygem(foo)", ConcreteTemplateHelpers.requirement("rubygem(foo)")
assert_equal "rubygem(foo) >= 1.0", ConcreteTemplateHelpers.requirement("rubygem(foo)", ">= 1.0")
assert_equal "rubygem(foo)", ConcreteTemplateHelpers.requirement("rubygem(foo)", "")

artificial_object = Object.new
assert_equal "rubygem(foo)", Gem2Rpm.requirement("rubygem(foo)", artificial_object)
assert_equal "rubygem(foo)", ConcreteTemplateHelpers.requirement("rubygem(foo)", artificial_object)
end
end

0 comments on commit 331c674

Please sign in to comment.