Class: FPM::Source::Gem

Inherits:
FPM::Source show all
Defined in:
lib/fpm/source/gem.rb

Instance Attribute Summary

Attributes inherited from FPM::Source

#paths, #root

Instance Method Summary collapse

Methods inherited from FPM::Source

#[], #[]=, #dependencies, #initialize, #metadata, #package

Constructor Details

This class inherits a constructor from FPM::Source

Instance Method Details

#can_recurse_dependenciesObject

def get_source



20
21
22
# File 'lib/fpm/source/gem.rb', line 20

def can_recurse_dependencies
  true
end

#download(gem_name, version = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fpm/source/gem.rb', line 24

def download(gem_name, version=nil)
  # This code mostly mutated from rubygem's fetch_command.rb
  # Code use permissible by rubygems's "GPL or these conditions below"
  # http://rubygems.rubyforge.org/rubygems-update/LICENSE_txt.html

  puts "Trying to download #{gem_name} (version=#{version})"
  dep = ::Gem::Dependency.new gem_name, version
  # How to handle prerelease? Some extra magic options?
  #dep.prerelease = options[:prerelease]
  
  if ::Gem::SpecFetcher.fetcher.respond_to?(:fetch_with_errors)
    specs_and_sources, errors =
      ::Gem::SpecFetcher.fetcher.fetch_with_errors(dep, true, true, false)
  else
    specs_and_sources = 
      ::Gem::SpecFetcher.fetcher.fetch(dep, true)
    errors = "???"
  end
  spec, source_uri = specs_and_sources.sort_by { |s,| s.version }.last

  if spec.nil? then
    raise "Invalid gem? Name: #{gem_name}, Version: #{version}, Errors: #{errors}"
  end

  path = ::Gem::RemoteFetcher.fetcher.download spec, source_uri
  FileUtils.mv path, spec.file_name
  @paths = [spec.file_name]
end

#get_metadataObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fpm/source/gem.rb', line 53

def 
  File.open(@paths.first, 'r') do |f|
    ::Gem::Package.open(f, 'r') do |gem|
      spec = gem.
      %w(
        description
        license
        summary
        version
      ).each do |field|
        self[field.to_sym] = spec.send(field) rescue "unknown"
      end

      self[:name] = "rubygem#{self[:suffix]}-#{spec.name}"
      self[:maintainer] = spec.author
      self[:url] = spec.homepage

      # TODO [Jay]: this will be different for different
      # package managers.  Need to decide how to handle this.
      self[:category] = 'Languages/Development/Ruby'

      self[:dependencies] = []
      spec.runtime_dependencies.map do |dep|
        # rubygems 1.3.5 doesn't have 'Gem::Dependency#requirement'
        if dep.respond_to?(:requirement)
          reqs = dep.requirement.to_s.gsub(/,/, '')
        else
          reqs = dep.version_requirements
        end

        # Some reqs can be ">= a, < b" versions, let's handle that.
        reqs.to_s.split(/, */).each do |req|
          self[:dependencies] << "rubygem#{self[:suffix]}-#{dep.name} #{req}"
        end
      end # runtime_dependencies
    end # ::Gem::Package
  end # File.open (the gem)
end

#get_source(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fpm/source/gem.rb', line 8

def get_source(params)
  gem = @paths.first
  looks_like_name_re = /^[A-Za-z0-9_-]+$/
  if !File.exists?(gem) 
    if gem =~ looks_like_name_re
      download(gem, params[:version])
    else
      raise "Path '#{gem}' is not a file and does not appear to be the name of a rubygem."
    end
  end
end

#make_tarball!(tar_path, builddir) ⇒ Object

def get_metadata



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fpm/source/gem.rb', line 92

def make_tarball!(tar_path, builddir)
  tmpdir = "#{tar_path}.dir"
  installdir = "#{tmpdir}/#{::Gem::dir}"
  ::FileUtils.mkdir_p(installdir)
  args = ["gem", "install", "--quiet", "--no-ri", "--no-rdoc",
     "--install-dir", installdir, "--ignore-dependencies", @paths.first]
  system(*args)
  
  @paths = [ ::Gem::dir ]
  tar(tar_path, ".#{@paths.first}", tmpdir)
  FileUtils.rm_r(tmpdir)

  # TODO(sissel): Make a helper method.
  system(*["gzip", "-f", tar_path])
end