Class: Chef::Provider::Package::Rubygems::GemEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/provider/package/rubygems.rb

Constant Summary collapse

DEFAULT_UNINSTALLER_OPTS =
{ ignore: true, executables: true }.freeze

Instance Method Summary collapse

Instance Method Details

#candidate_version_from_file(gem_dependency, source) ⇒ Object

Determines the candidate version for a gem from a .gem file on disk and checks if it matches the version constraints in +gem_dependency+ === Returns Gem::Version a singular gem version object is returned if the gem is available nil returns nil if the gem on disk doesn't match the version constraints for +gem_dependency+


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/chef/provider/package/rubygems.rb', line 133

def candidate_version_from_file(gem_dependency, source)
  spec = spec_from_file(source)
  if spec.satisfies_requirement?(gem_dependency)
    logger.debug { "found candidate gem version #{spec.version} from local gem package #{source}" }
    spec.version
  else
    # This is probably going to end badly...
    logger.warn { "gem package #{source} does not satisfy the requirements #{gem_dependency}" }
    nil
  end
end

#candidate_version_from_remote(gem_dependency, *sources) ⇒ Object

Finds the newest version that satisfies the constraints of +gem_dependency+. The version is determined from the cache or a round-trip to the server as needed. The architecture and gem sources will be set before making the query. === Returns Gem::Version a singular gem version object is returned if the gem is available nil returns nil if the gem could not be found

Raises:

  • (NotImplementedError)

154
155
156
# File 'lib/chef/provider/package/rubygems.rb', line 154

def candidate_version_from_remote(gem_dependency, *sources)
  raise NotImplementedError
end

#dependency_installer(opts = {}) ⇒ Object


224
225
226
# File 'lib/chef/provider/package/rubygems.rb', line 224

def dependency_installer(opts = {})
  Gem::DependencyInstaller.new(opts)
end

#find_newest_remote_version(gem_dependency, *sources) ⇒ Object

Find the newest gem version available from Gem.sources that satisfies the constraints of +gem_dependency+


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/chef/provider/package/rubygems.rb', line 161

def find_newest_remote_version(gem_dependency, *sources)
  spec, source =
    if Chef::Config[:rubygems_cache_enabled]
      # This code caches every gem on rubygems.org and uses lots of RAM
      available_gems = dependency_installer.find_gems_with_sources(gem_dependency)
      available_gems.pick_best!
      best_gem = available_gems.set.first
      best_gem && [best_gem.spec, best_gem.source]
    else
      # Use the API that 'gem install' calls which does not pull down the rubygems universe
      begin
        rs = dependency_installer.resolve_dependencies gem_dependency.name, gem_dependency.requirement
        rs.specs.select { |s| s.name == gem_dependency.name }.first
      rescue Gem::UnsatisfiableDependencyError
        nil
      end
    end

  version = spec && spec.version
  if version
    logger.debug { "found gem #{spec.name} version #{version} for platform #{spec.platform} from #{source}" }
    version
  else
    source_list = sources.compact.empty? ? "[#{Gem.sources.to_a.join(', ')}]" : "[#{sources.join(', ')}]"
    logger.warn { "failed to find gem #{gem_dependency} from #{source_list}" }
    nil
  end
end

#gem_pathsObject

The paths where rubygems should search for installed gems. Implemented by subclasses.

Raises:

  • (NotImplementedError)

55
56
57
# File 'lib/chef/provider/package/rubygems.rb', line 55

def gem_paths
  raise NotImplementedError
end

#gem_source_indexObject

A rubygems source index containing the list of gemspecs for all available gems in the gem installation. Implemented by subclasses === Returns Gem::SourceIndex

Raises:

  • (NotImplementedError)

65
66
67
# File 'lib/chef/provider/package/rubygems.rb', line 65

def gem_source_index
  raise NotImplementedError
end

#gem_specificationObject

A rubygems specification object containing the list of gemspecs for all available gems in the gem installation. Implemented by subclasses For rubygems >= 1.8.0 === Returns Gem::Specification

Raises:

  • (NotImplementedError)

76
77
78
# File 'lib/chef/provider/package/rubygems.rb', line 76

def gem_specification
  raise NotImplementedError
end

#install(gem_dependency, options = {}) ⇒ Object

Installs a gem via the rubygems ruby API. === Options :sources rubygems servers to use Other options are passed to Gem::DependencyInstaller.new


195
196
197
198
199
200
201
# File 'lib/chef/provider/package/rubygems.rb', line 195

def install(gem_dependency, options = {})
  with_gem_sources(*options.delete(:sources)) do
    with_correct_verbosity do
      dependency_installer(options).install(gem_dependency)
    end
  end
end

#installed_versions(gem_dep) ⇒ Object

Lists the installed versions of +gem_name+, constrained by the version spec in +gem_dep+ === Arguments Gem::Dependency +gem_dep+ is a Gem::Dependency object, its version specification constrains which gems are returned. === Returns [Gem::Specification] an array of Gem::Specification objects


88
89
90
91
92
93
94
# File 'lib/chef/provider/package/rubygems.rb', line 88

def installed_versions(gem_dep)
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new("1.8.0")
    gem_specification.find_all_by_name(gem_dep.name, gem_dep.requirement)
  else
    gem_source_index.search(gem_dep)
  end
end

#spec_from_file(file) ⇒ Object

Extracts the gemspec from a (on-disk) gem package. === Returns Gem::Specification

-- Compatibility note: Rubygems 1.x uses Gem::Format, 2.0 moved this code into Gem::Package.


117
118
119
120
121
122
123
# File 'lib/chef/provider/package/rubygems.rb', line 117

def spec_from_file(file)
  if defined?(Gem::Format) && Gem::Package.respond_to?(:open)
    Gem::Format.from_file_by_path(file).spec
  else
    Gem::Package.new(file).spec
  end
end

#uninstall(gem_name, gem_version = nil, opts = {}) ⇒ Object

Uninstall the gem +gem_name+ via the rubygems ruby API. If +gem_version+ is provided, only that version will be uninstalled. Otherwise, all versions are uninstalled. === Options Options are passed to Gem::Uninstaller.new


209
210
211
212
213
214
# File 'lib/chef/provider/package/rubygems.rb', line 209

def uninstall(gem_name, gem_version = nil, opts = {})
  gem_version ? opts[:version] = gem_version : opts[:all] = true
  with_correct_verbosity do
    uninstaller(gem_name, opts).uninstall
  end
end

#uninstaller(gem_name, opts = {}) ⇒ Object


228
229
230
# File 'lib/chef/provider/package/rubygems.rb', line 228

def uninstaller(gem_name, opts = {})
  Gem::Uninstaller.new(gem_name, DEFAULT_UNINSTALLER_OPTS.merge(opts))
end

#with_correct_verbosityObject

Set rubygems' user interaction to ConsoleUI or SilentUI depending on our current debug level


219
220
221
222
# File 'lib/chef/provider/package/rubygems.rb', line 219

def with_correct_verbosity
  Gem::DefaultUserInteraction.ui = Chef::Log.debug? ? Gem::ConsoleUI.new : Gem::SilentUI.new
  yield
end

#with_gem_sources(*sources) ⇒ Object

Yields to the provided block with rubygems' source list set to the list provided. Always resets the list when the block returns or raises an exception.


100
101
102
103
104
105
106
107
# File 'lib/chef/provider/package/rubygems.rb', line 100

def with_gem_sources(*sources)
  sources.compact!
  original_sources = Gem.sources
  Gem.sources = sources unless sources.empty?
  yield
ensure
  Gem.sources = original_sources
end