lib/gem2rpm.rb in gem2rpm-0.5.3 vs lib/gem2rpm.rb in gem2rpm-0.6.0
- old
+ new
@@ -1,10 +1,19 @@
require 'erb'
require 'socket'
require 'rubygems/format'
-require 'rubygems/remote_installer'
+# Adapt to the differences between rubygems < 1.0.0 and after
+# Once we can be reasonably certain that everybody has version >= 1.0.0
+# all this logic should be killed
+GEM_VERSION = Gem::Version.create(Gem::RubyGemsVersion)
+HAS_REMOTE_INSTALLER = GEM_VERSION < Gem::Version.create("1.0.0")
+
+if HAS_REMOTE_INSTALLER
+ require 'rubygems/remote_installer'
+end
+
# Extend String with a word_wrap method, which we use in the ERB template
# below. Taken with modification from the word_wrap method in ActionPack.
# Text::Format does the smae thing better.
class String
def word_wrap(line_width = 80)
@@ -28,24 +37,41 @@
end
end
module Gem2Rpm
- Gem2Rpm::VERSION = "0.5.3"
+ Gem2Rpm::VERSION = "0.6.0"
- def Gem2Rpm.convert(fname, template=TEMPLATE, out=$stdout,
+ if HAS_REMOTE_INSTALLER
+ def self.find_download_url(name, version)
+ installer = Gem::RemoteInstaller.new
+ dummy, download_path = installer.find_gem_to_install(name, "=#{version}")
+ download_path += "/gems/" if download_path.to_s != ""
+ return download_path
+ end
+ else
+ def self.find_download_url(name, version)
+ dep = Gem::Dependency.new(name, "=#{version}")
+ fetcher = Gem::SpecFetcher.fetcher
+ dummy, download_path = fetcher.find_matching(dep, false, false).first
+ download_path += "gems/" if download_path.to_s != ""
+ return download_path
+ end
+ end
+
+ def Gem2Rpm.convert(fname, template=TEMPLATE, out=$stdout,
nongem=true, local=false)
format = Gem::Format.from_file_by_path(fname)
spec = format.spec
spec.description ||= spec.summary
download_path = ""
unless local
begin
- dummy, download_path = Gem::RemoteInstaller.new.find_gem_to_install(spec.name, "=#{spec.version}")
- download_path += "/gems/" if download_path.to_s != ""
- rescue Gem::Exception
+ download_path = find_download_url(spec.name, spec.version)
+ rescue Gem::Exception => 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
template = ERB.new(template, 0, '<>')
out.puts template.result(binding)
end
@@ -59,10 +85,10 @@
packager = "#{Etc::getpwnam(Etc::getlogin).gecos} <#{Etc::getlogin}@#{Socket::gethostname}>"
end
packager
end
- TEMPLATE =
+ TEMPLATE =
%q{# Generated from <%= File::basename(format.gem_path) %> by gem2rpm -*- rpm-spec -*-
%define ruby_sitelib %(ruby -rrbconfig -e "puts Config::CONFIG['sitelibdir']")
%define gemdir %(ruby -rubygems -e 'puts Gem::dir' 2>/dev/null)
%define gemname <%= spec.name %>
%define geminstdir %{gemdir}/gems/%{gemname}-%{version}