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}
@@_gem_source_index_reloaded =
false

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 contraints 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+


107
108
109
110
111
112
113
114
115
116
117
# File 'lib/chef/provider/package/rubygems.rb', line 107

def candidate_version_from_file(gem_dependency, source)
  spec = Gem::Format.from_file_by_path(source).spec
  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 { "The gem package #{source} does not satisfy the requirements #{gem_dependency.to_s}" }
    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)


128
129
130
# File 'lib/chef/provider/package/rubygems.rb', line 128

def candidate_version_from_remote(gem_dependency, *sources)
  raise NotImplementedError
end

#dependency_installer(opts = {}) ⇒ Object



186
187
188
# File 'lib/chef/provider/package/rubygems.rb', line 186

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/provider/package/rubygems.rb', line 135

def find_newest_remote_version(gem_dependency, *sources)
  # DependencyInstaller sorts the results such that the last one is
  # always the one it considers best.
  spec_with_source = dependency_installer.find_gems_with_sources(gem_dependency).last

  spec = spec_with_source && spec_with_source[0]
  version = spec && spec_with_source[0].version
  if version
    logger.debug { "Found gem #{spec.name} version #{version} for platform #{spec.platform} from #{spec_with_source[1]}" }
    version
  else
    source_list = sources.compact.empty? ? "[#{Gem.sources.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)


53
54
55
# File 'lib/chef/provider/package/rubygems.rb', line 53

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)


63
64
65
# File 'lib/chef/provider/package/rubygems.rb', line 63

def gem_source_index
  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



157
158
159
160
161
162
163
# File 'lib/chef/provider/package/rubygems.rb', line 157

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



75
76
77
78
79
80
81
82
83
# File 'lib/chef/provider/package/rubygems.rb', line 75

def installed_versions(gem_dep)
  # workaround for Bundler ...
  # if we are running under bundler we still want to use the orignal Gem SourceIndex in order to find gems
  if defined?(Bundler) && ::File.exists?("/usr/local/lib/site_ruby/1.8/rubygems/source_index.rb") && !@@_gem_source_index_reloaded
    load '/usr/local/lib/site_ruby/1.8/rubygems/source_index.rb'
    @@_gem_source_index_reloaded = true
  end
  gem_source_index.search(gem_dep)
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



171
172
173
174
175
176
# File 'lib/chef/provider/package/rubygems.rb', line 171

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



190
191
192
# File 'lib/chef/provider/package/rubygems.rb', line 190

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



181
182
183
184
# File 'lib/chef/provider/package/rubygems.rb', line 181

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.



90
91
92
93
94
95
96
97
# File 'lib/chef/provider/package/rubygems.rb', line 90

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