Class: Chef::Provider::Package::Rubygems::AlternateGemEnvironment

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

Constant Summary collapse

JRUBY_PLATFORM =
/(:?universal|x86_64|x86)\-java\-[0-9\.]+/

Constants inherited from GemEnvironment

GemEnvironment::DEFAULT_UNINSTALLER_OPTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Methods inherited from GemEnvironment

#candidate_version_from_file, #dependency_installer, #find_newest_remote_version, #install, #installed_versions, #spec_from_file, #uninstall, #uninstaller, #with_correct_verbosity, #with_gem_sources

Constructor Details

#initialize(gem_binary_location) ⇒ AlternateGemEnvironment

Returns a new instance of AlternateGemEnvironment


277
278
279
# File 'lib/chef/provider/package/rubygems.rb', line 277

def initialize(gem_binary_location)
  @gem_binary_location = gem_binary_location
end

Instance Attribute Details

#gem_binary_locationObject (readonly)

Returns the value of attribute gem_binary_location


275
276
277
# File 'lib/chef/provider/package/rubygems.rb', line 275

def gem_binary_location
  @gem_binary_location
end

Class Method Details

.gempath_cacheObject


265
266
267
# File 'lib/chef/provider/package/rubygems.rb', line 265

def self.gempath_cache
  @gempath_cache ||= {}
end

.platform_cacheObject


269
270
271
# File 'lib/chef/provider/package/rubygems.rb', line 269

def self.platform_cache
  @platform_cache ||= {}
end

Instance Method Details

#candidate_version_from_remote(gem_dependency, *sources) ⇒ Object


342
343
344
345
346
347
348
# File 'lib/chef/provider/package/rubygems.rb', line 342

def candidate_version_from_remote(gem_dependency, *sources)
  with_gem_sources(*sources) do
    with_gem_platforms(*gem_platforms) do
      find_newest_remote_version(gem_dependency, *sources)
    end
  end
end

#gem_pathsObject


281
282
283
284
285
286
287
288
289
290
291
# File 'lib/chef/provider/package/rubygems.rb', line 281

def gem_paths
  if self.class.gempath_cache.key?(@gem_binary_location)
    self.class.gempath_cache[@gem_binary_location]
  else
    # shellout! is a fork/exec which won't work on windows
    shell_style_paths = shell_out!("#{@gem_binary_location} env gempath").stdout
    # on windows, the path separator is (usually? always?) semicolon
    paths = shell_style_paths.split(::File::PATH_SEPARATOR).map(&:strip)
    self.class.gempath_cache[@gem_binary_location] = paths
  end
end

#gem_platformsObject

Attempt to detect the correct platform settings for the target gem environment.

In practice, this only makes a difference if different versions are available depending on platform, and only if the target gem environment has a radically different platform (i.e., jruby), so we just try to detect jruby and fall back to the current platforms (Gem.platforms) if we don't detect it.

=== Returns [String|Gem::Platform] returns an array of Gem::Platform-compatible objects, i.e., Strings that are valid for Gem::Platform or actual Gem::Platform objects.


320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/chef/provider/package/rubygems.rb', line 320

def gem_platforms
  if self.class.platform_cache.key?(@gem_binary_location)
    self.class.platform_cache[@gem_binary_location]
  else
    gem_environment = shell_out!("#{@gem_binary_location} env").stdout
    self.class.platform_cache[@gem_binary_location] = if jruby = gem_environment[JRUBY_PLATFORM]
                                                        ["ruby", Gem::Platform.new(jruby)]
                                                      else
                                                        Gem.platforms
                                                      end
  end
end

#gem_source_indexObject


293
294
295
# File 'lib/chef/provider/package/rubygems.rb', line 293

def gem_source_index
  @source_index ||= Gem::SourceIndex.from_gems_in(*gem_paths.map { |p| p + "/specifications" })
end

#gem_specificationObject


297
298
299
300
301
302
303
304
# File 'lib/chef/provider/package/rubygems.rb', line 297

def gem_specification
  # Only once, dirs calls a reset
  unless @specification
    Gem::Specification.dirs = gem_paths
    @specification = Gem::Specification
  end
  @specification
end

#with_gem_platforms(*alt_gem_platforms) ⇒ Object


333
334
335
336
337
338
339
340
# File 'lib/chef/provider/package/rubygems.rb', line 333

def with_gem_platforms(*alt_gem_platforms)
  alt_gem_platforms.flatten!
  original_gem_platforms = Gem.platforms
  Gem.platforms = alt_gem_platforms
  yield
ensure
  Gem.platforms = original_gem_platforms
end