Class: FPM::Package::Gem

Inherits:
FPM::Package show all
Defined in:
lib/fpm/package/gem.rb

Overview

A rubygems package.

This does not currently support ‘output’

The following attributes are supported:

  • :gem_bin_path

  • :gem_package_name_prefix

  • :gem_gem

Instance Attribute Summary

Attributes inherited from FPM::Package

#architecture, #attributes, #attrs, #category, #config_files, #conflicts, #dependencies, #description, #directories, #epoch, #iteration, #license, #maintainer, #name, #provides, #replaces, #scripts, #url, #vendor, #version

Instance Method Summary collapse

Methods inherited from FPM::Package

apply_options, #build_path, #cleanup, #cleanup_build, #cleanup_staging, #convert, #converted_from, default_attributes, #edit_file, #files, inherited, #initialize, option, #output, #script, #staging_path, #to_s, #type, type, types

Methods included from Util

#copied_entries, #copy_entry, #copy_metadata, #default_shell, #execmd, #expand_pessimistic_constraints, #logger, #mknod_w, #program_exists?, #program_in_path?, #safesystem, #safesystemout, #tar_cmd

Constructor Details

This class inherits a constructor from FPM::Package

Instance Method Details

#download(gem_name, gem_version = nil) ⇒ Object

def download_if_necessary



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fpm/package/gem.rb', line 72

def download(gem_name, gem_version=nil)

  logger.info("Trying to download", :gem => gem_name, :version => gem_version)

  gem_fetch = [ "#{attributes[:gem_gem]}", "fetch", gem_name]

  gem_fetch += ["--prerelease"] if attributes[:gem_prerelease?]
  gem_fetch += ["--version", gem_version] if gem_version

  download_dir = build_path(gem_name)
  FileUtils.mkdir(download_dir) unless File.directory?(download_dir)

  ::Dir.chdir(download_dir) do |dir|
    logger.debug("Downloading in directory #{dir}")
    safesystem(*gem_fetch)
  end

  gem_files = ::Dir.glob(File.join(download_dir, "*.gem"))

  if gem_files.length != 1
    raise "Unexpected number of gem files in #{download_dir},  #{gem_files.length} should be 1"
  end

  return gem_files.first
end

#download_if_necessary(gem, gem_version) ⇒ Object

def input



62
63
64
65
66
67
68
69
70
# File 'lib/fpm/package/gem.rb', line 62

def download_if_necessary(gem, gem_version)
  path = gem
  if !File.exist?(path)
    path = download(gem, gem_version)
  end

  logger.info("Using gem file", :path => path)
  return path
end

#fix_name(name) ⇒ Object

Sanitize package name. This prefixes the package name with ‘rubygem’ (but depends on the attribute :gem_package_name_prefix



239
240
241
# File 'lib/fpm/package/gem.rb', line 239

def fix_name(name)
  return [attributes[:gem_package_name_prefix], name].join("-")
end

#input(gem) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fpm/package/gem.rb', line 50

def input(gem)
  # 'arg'  is the name of the rubygem we should unpack.
  path_to_gem = download_if_necessary(gem, version)

  # Got a good gem now (downloaded or otherwise)
  #
  # 1. unpack it into staging_path
  # 2. take the metadata from it and update our wonderful package with it.
  load_package_info(path_to_gem)
  install_to_staging(path_to_gem)
end

#install_to_staging(gem_path) ⇒ Object

def load_package_info



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/fpm/package/gem.rb', line 173

def install_to_staging(gem_path)
  if attributes.include?(:prefix) && ! attributes[:prefix].nil?
    installdir = "#{staging_path}/#{attributes[:prefix]}"
  else
    gemdir = safesystemout(*[attributes[:gem_gem], 'env', 'gemdir']).chomp
    installdir = File.join(staging_path, gemdir)
  end

  ::FileUtils.mkdir_p(installdir)
  # TODO(sissel): Allow setting gem tool path
  args = [attributes[:gem_gem], "install", "--quiet", "--no-ri", "--no-rdoc",
     "--no-user-install", "--install-dir", installdir, "--ignore-dependencies"]
  if attributes[:gem_env_shebang?]
    args += ["-E"]
  end

  if attributes.include?(:gem_bin_path) && ! attributes[:gem_bin_path].nil?
    bin_path = File.join(staging_path, attributes[:gem_bin_path])
  else
    gem_env  = safesystemout(*[attributes[:gem_gem], 'env']).split("\n")
    gem_bin  = gem_env.select{ |line| line =~ /EXECUTABLE DIRECTORY/ }.first.split(': ').last
    bin_path = File.join(staging_path, gem_bin)
  end

  args += ["--bindir", bin_path]
  ::FileUtils.mkdir_p(bin_path)
  args << gem_path
  safesystem(*args)

  # Replace the shebangs in the executables
  if attributes[:gem_shebang]
    ::Dir.entries(bin_path).each do |file_name|
      # exclude . and ..
      next if ['.', '..'].include?(file_name)
      # exclude everything which is not a file
      file_path = File.join(bin_path, file_name)
      next unless File.ftype(file_path) == 'file'
      # replace shebang in files if there is one
      file = File.read(file_path)
      if file.gsub!(/\A#!.*$/, "#!#{attributes[:gem_shebang]}")
        File.open(file_path, 'w'){|f| f << file}
      end
    end
  end

  # Delete bin_path if it's empty, and any empty parents (#612)
  # Above, we mkdir_p bin_path because rubygems aborts if the parent
  # directory doesn't exist, for example:
  #   ERROR:  While executing gem ... (Errno::ENOENT)
  #       No such file or directory - /tmp/something/weird/bin
  tmp = bin_path
  while ::Dir.entries(tmp).size == 2 || tmp == "/"  # just [ "..", "." ] is an empty directory
    logger.info("Deleting empty bin_path", :path => tmp)
    ::Dir.rmdir(tmp)
    tmp = File.dirname(tmp)
  end
  if attributes[:gem_version_bins?] and File.directory?(bin_path)
    (::Dir.entries(bin_path) - ['.','..']).each do |bin|
      FileUtils.mv("#{bin_path}/#{bin}", "#{bin_path}/#{bin}-#{self.version}")
    end
  end
end

#load_package_info(gem_path) ⇒ Object

def download



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fpm/package/gem.rb', line 98

def load_package_info(gem_path)

  spec = YAML.load(%x{#{attributes[:gem_gem]} specification #{gem_path} --yaml})

  if !attributes[:gem_package_prefix].nil?
    attributes[:gem_package_name_prefix] = attributes[:gem_package_prefix]
  end

  # name prefixing is optional, if enabled, a name 'foo' will become
  # 'rubygem-foo' (depending on what the gem_package_name_prefix is)
  self.name = spec.name
  if attributes[:gem_fix_name?]
    self.name = fix_name(spec.name)
  end

  #self.name = [attributes[:gem_package_name_prefix], spec.name].join("-")
  self.license = (spec.license or "no license listed in #{File.basename(gem_path)}")

  # expand spec's version to match RationalVersioningPolicy to prevent cases
  # where missing 'build' number prevents correct dependency resolution by target
  # package manager. Ie. for dpkg 1.1 != 1.1.0
  m = spec.version.to_s.scan(/(\d+)\.?/)
  self.version = m.flatten.fill('0', m.length..2).join('.')

  self.vendor = spec.author
  self.url = spec.homepage
  self.category = "Languages/Development/Ruby"

  # if the gemspec has C extensions defined, then this should be a 'native' arch.
  if !spec.extensions.empty?
    self.architecture = "native"
  else
    self.architecture = "all"
  end

  # make sure we have a description
  description_options = [ spec.description, spec.summary, "#{spec.name} - no description given" ]
  self.description = description_options.find { |d| !(d.nil? or d.strip.empty?) }

  # Upstream rpms seem to do this, might as well share.
  # TODO(sissel): Figure out how to hint this only to rpm?
  # maybe something like attributes[:rpm_provides] for rpm specific stuff?
  # Or just ignore it all together.
  #self.provides << "rubygem(#{self.name})"

  # By default, we'll usually automatically provide this, but in the case that we are
  # composing multiple packages, it's best to explicitly include it in the provides list.
  self.provides << "#{self.name} = #{self.version}"

  if !attributes[:no_auto_depends?]
    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
      else
        reqs = dep.version_requirements
      end

      # Some reqs can be ">= a, < b" versions, let's handle that.
      reqs.to_s.split(/, */).each do |req|
        if attributes[:gem_disable_dependencies]
          next if attributes[:gem_disable_dependencies].include?(dep.name)
        end

        if attributes[:gem_fix_dependencies?]
          name = fix_name(dep.name)
        else
          name = dep.name
        end
        self.dependencies << "#{name} #{req}"
      end
    end # runtime_dependencies
  end #no_auto_depends
end