# class Chef::Provider::Package::Rubygems::GemEnvironment

### Constants

DEFAULT_UNINSTALLER_OPTS

### Public Instance Methods

candidate_version_from_file(gem_dependency, source) click to toggle source

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+

    # File lib/chef/provider/package/rubygems.rb, line 139
    def candidate_version_from_file(gem_dependency, source)
      spec = spec_from_file(source)
      if spec.satisfies_requirement?(gem_dependency)
        logger.debug {"#{@new_resource} found candidate gem version #{spec.version} from local gem package #{source}"}
        spec.version
      else
        # This is probably going to end badly...
        logger.warn { "#{@new_resource} gem package #{source} does not satisfy the requirements #{gem_dependency.to_s}" }
        nil
      end
    end

candidate_version_from_remote(gem_dependency, *sources) click to toggle source

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

    # File lib/chef/provider/package/rubygems.rb, line 160
    def candidate_version_from_remote(gem_dependency, *sources)
      raise NotImplementedError
    end

dependency_installer(opts={}) click to toggle source

    # File lib/chef/provider/package/rubygems.rb, line 227
    def dependency_installer(opts={})
      Gem::DependencyInstaller.new(opts)
    end

find_newest_remote_version(gem_dependency, *sources) click to toggle source

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

    # File lib/chef/provider/package/rubygems.rb, line 167
    def find_newest_remote_version(gem_dependency, *sources)
      available_gems = dependency_installer.find_gems_with_sources(gem_dependency)
      spec, source = if available_gems.respond_to?(:last)
        # DependencyInstaller sorts the results such that the last one is
        # always the one it considers best.
        spec_with_source = available_gems.last
        spec_with_source && spec_with_source
      else
        # Rubygems 2.0 returns a Gem::Available set, which is a
        # collection of AvailableSet::Tuple structs
        available_gems.pick_best!
        best_gem = available_gems.set.first
        best_gem && [best_gem.spec, best_gem.source]
      end

      version = spec && spec.version
      if version
        logger.debug { "#{@new_resource} 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 { "#{@new_resource} failed to find gem #{gem_dependency} from #{source_list}" }
        nil
      end
    end

gem_paths() click to toggle source

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

    # File lib/chef/provider/package/rubygems.rb, line 61
    def gem_paths
      raise NotImplementedError
    end

gem_source_index() click to toggle source

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

### Returns¶ ↑

Gem::SourceIndex

    # File lib/chef/provider/package/rubygems.rb, line 71
    def gem_source_index
      raise NotImplementedError
    end

gem_specification() click to toggle source

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

    # File lib/chef/provider/package/rubygems.rb, line 82
    def gem_specification
      raise NotImplementedError
    end

install(gem_dependency, options={}) click to toggle source

Installs a gem via the rubygems ruby API.

### Options¶ ↑

:sources rubygems servers to use Other options are passed to Gem::DependencyInstaller.new

    # File lib/chef/provider/package/rubygems.rb, line 198
    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) click to toggle source

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

    # File lib/chef/provider/package/rubygems.rb, line 94
    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) click to toggle source

Extracts the gemspec from a (on-disk) gem package.

### Returns¶ ↑

Gem::Specification

    # File lib/chef/provider/package/rubygems.rb, line 123
    def spec_from_file(file)
      if defined?(Gem::Format) and 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={}) click to toggle source

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

    # File lib/chef/provider/package/rubygems.rb, line 212
    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={}) click to toggle source

    # File lib/chef/provider/package/rubygems.rb, line 231
    def uninstaller(gem_name, opts={})
      Gem::Uninstaller.new(gem_name, DEFAULT_UNINSTALLER_OPTS.merge(opts))
    end

with_correct_verbosity() { || ... } click to toggle source

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

    # File lib/chef/provider/package/rubygems.rb, line 222
    def with_correct_verbosity
      Gem::DefaultUserInteraction.ui = Chef::Log.debug? ? Gem::ConsoleUI.new : Gem::SilentUI.new
      yield
    end

with_gem_sources(*sources) { || ... } click to toggle source

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.

    # File lib/chef/provider/package/rubygems.rb, line 106
    def with_gem_sources(*sources)
      sources.compact!
      original_sources = Gem.sources
      Gem.sources = sources unless sources.empty?
      yield
    ensure
      Gem.sources = original_sources
    end