Module: LibGems

Defined in:
lib/libgems/gem_openssl.rb,
lib/libgems/defaults.rb,
lib/libgems/source_index.rb,
lib/libgems/source_index.rb,
lib/libgems.rb

Overview

:stopdoc:

Defined Under Namespace

Modules: Commands, DefaultUserInteraction, Ext, GemcutterUtilities, InstallUpdateOptions, LocalRemoteOptions, Package, RequirePathsBuilder, SSL, Security, Text, UserInteraction, VersionOption Classes: Builder, Command, CommandLineError, CommandManager, ConfigFile, ConsoleUI, Dependency, DependencyError, DependencyInstaller, DependencyList, DependencyRemovalException, DocManager, DocumentError, EndOfYAMLException, ErrorReason, Exception, FakeFetcher, FileOperations, FilePermissionError, Format, FormatException, GemNotFoundException, GemNotInHomeException, GemPathSearcher, Indexer, InstallError, Installer, InvalidSpecificationException, LoadError, OldFormat, OperationNotSupportedError, PackageTask, Platform, PlatformMismatch, RemoteError, RemoteFetcher, RemoteInstallationCancelled, RemoteInstallationSkipped, RemoteSourceException, Requirement, Server, SilentUI, SourceIndex, SourceInfoCache, SourceInfoCacheEntry, SpecFetcher, Specification, StreamUI, SystemExitException, Uninstaller, Validator, VerificationError, Version

Constant Summary collapse

Cache =

Cache is an alias for SourceIndex to allow older YAMLized source index objects to load properly.

SourceIndex
NAME =
'LibGems'
GEM_NAME =
'libgems'
VERSION =
'1.3.9.2'
LIBGEMS_VERSION =
'0.1.3'
ConfigMap =

Configuration settings from ::RbConfig

{}
DIRECTORIES =

Default directories in a gem repository

%w[cache doc gems specifications]
RubyGemsPackageVersion =
VERSION
WIN_PATTERNS =

An Array of Regexps that match windows ruby platforms.

[
  /bccwin/i,
  /cygwin/i,
  /djgpp/i,
  /mingw/i,
  /mswin/i,
  /wince/i,
]
MARSHAL_SPEC_DIR =

Location of Marshal quick gemspecs on remote repositories

"quick/Marshal.#{LibGems.marshal_version}/"
YAML_SPEC_DIR =

Location of legacy YAML quick gemspecs on remote repositories

'quick/'
@@source_index =
nil
@@win_platform =
nil

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loaded_specsObject (readonly)

Hash of loaded LibGems::Specification keyed by name



1137
1138
1139
# File 'lib/libgems.rb', line 1137

def loaded_specs
  @loaded_specs
end

.post_build_hooksObject (readonly)

The list of hooks to be run before LibGems::Install#install finishes installation



1143
1144
1145
# File 'lib/libgems.rb', line 1143

def post_build_hooks
  @post_build_hooks
end

.post_install_hooksObject (readonly)

The list of hooks to be run before LibGems::Install#install does any work



1148
1149
1150
# File 'lib/libgems.rb', line 1148

def post_install_hooks
  @post_install_hooks
end

.post_uninstall_hooksObject (readonly)

The list of hooks to be run before LibGems::Uninstall#uninstall does any work



1154
1155
1156
# File 'lib/libgems.rb', line 1154

def post_uninstall_hooks
  @post_uninstall_hooks
end

.pre_install_hooksObject (readonly)

The list of hooks to be run after LibGems::Install#install is finished



1159
1160
1161
# File 'lib/libgems.rb', line 1159

def pre_install_hooks
  @pre_install_hooks
end

.pre_uninstall_hooksObject (readonly)

The list of hooks to be run after LibGems::Uninstall#uninstall is finished



1164
1165
1166
# File 'lib/libgems.rb', line 1164

def pre_uninstall_hooks
  @pre_uninstall_hooks
end

.ssl_available=(value) ⇒ Object (writeonly)

Is SSL available?



26
27
28
# File 'lib/libgems/gem_openssl.rb', line 26

def ssl_available=(value)
  @ssl_available = value
end

Class Method Details

.activate(gem, *version_requirements) ⇒ Object

Activates an installed gem matching gem. The gem must satisfy version_requirements.

Returns true if the gem is activated, false if it is already loaded, or an exception otherwise.

LibGems#activate adds the library paths in gem to $LOAD_PATH. Before a LibGems is activated its required Gems are activated. If the version information is omitted, the highest version LibGems of the supplied name is loaded. If a LibGems is not found that meets the version requirements or a required LibGems is not found, a LibGems::LoadError is raised.

More information on version requirements can be found in the LibGems::Requirement and LibGems::Version documentation.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/libgems.rb', line 248

def self.activate(gem, *version_requirements)
  if version_requirements.last.is_a?(Hash)
    options = version_requirements.pop
  else
    options = {}
  end

  sources = options[:sources] || []

  if version_requirements.empty? then
    version_requirements = LibGems::Requirement.default
  end

  unless gem.respond_to?(:name) and
         gem.respond_to?(:requirement) then
    gem = LibGems::Dependency.new(gem, version_requirements)
  end

  matches = LibGems.source_index.find_name(gem.name, gem.requirement)
  report_activate_error(gem) if matches.empty?

  if @loaded_specs[gem.name] then
    # This gem is already loaded.  If the currently loaded gem is not in the
    # list of candidate gems, then we have a version conflict.
    existing_spec = @loaded_specs[gem.name]

    unless matches.any? { |spec| spec.version == existing_spec.version } then
       sources_message = sources.map { |spec| spec.full_name }
       stack_message = @loaded_stacks[gem.name].map { |spec| spec.full_name }

       msg = "can't activate #{gem} for #{sources_message.inspect}, "
       msg << "already activated #{existing_spec.full_name} for "
       msg << "#{stack_message.inspect}"

       e = LibGems::LoadError.new msg
       e.name = gem.name
       e.version_requirement = gem.requirement

       raise e
    end

    return false
  end

  # new load
  spec = matches.last
  return false if spec.loaded?

  spec.loaded = true
  @loaded_specs[spec.name] = spec
  @loaded_stacks[spec.name] = sources.dup

  # Load dependent gems first
  spec.runtime_dependencies.each do |dep_gem|
    activate dep_gem, :sources => [spec, *sources]
  end

  # bin directory must come before library directories
  spec.require_paths.unshift spec.bindir if spec.bindir

  require_paths = spec.require_paths.map do |path|
    File.join spec.full_gem_path, path
  end

  # gem directories must come after -I and ENV['RUBYLIB']
  insert_index = load_path_insert_index

  if insert_index then
    # gem directories must come after -I and ENV['RUBYLIB']
    $LOAD_PATH.insert(insert_index, *require_paths)
  else
    # we are probably testing in core, -I and RUBYLIB don't apply
    $LOAD_PATH.unshift(*require_paths)
  end

  return true
end

.all_load_pathsObject

An Array of all possible load paths for all versions of all gems in the LibGems installation.



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/libgems.rb', line 330

def self.all_load_paths
  result = []

  LibGems.path.each do |gemdir|
    each_load_path all_partials(gemdir) do |load_path|
      result << load_path
    end
  end

  result
end

.available?(gem, *requirements) ⇒ Boolean

See if a given gem is available.

Returns:

  • (Boolean)


354
355
356
357
358
359
360
361
362
363
# File 'lib/libgems.rb', line 354

def self.available?(gem, *requirements)
  requirements = LibGems::Requirement.default if requirements.empty?

  unless gem.respond_to?(:name) and
         gem.respond_to?(:requirement) then
    gem = LibGems::Dependency.new gem, requirements
  end

  !LibGems.source_index.search(gem).empty?
end

.bin_path(name, exec_name = nil, *version_requirements) ⇒ Object

Find the full path to the executable for gem name. If the exec_name is not given, the gem’s default_executable is chosen, otherwise the specified executable’s path is returned. version_requirements allows you to specify specific gem versions.



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/libgems.rb', line 371

def self.bin_path(name, exec_name = nil, *version_requirements)
  version_requirements = LibGems::Requirement.default if
    version_requirements.empty?
  specs = LibGems.source_index.find_name(name, version_requirements)

  raise LibGems::GemNotFoundException,
        "can't find gem #{name} (#{version_requirements})" if specs.empty?

  specs = specs.find_all do |spec|
    spec.executables.include?(exec_name)
  end if exec_name

  unless spec = specs.last
    msg = "can't find gem #{name} (#{version_requirements}) with executable #{exec_name}"
    raise LibGems::GemNotFoundException, msg
  end

  exec_name ||= spec.default_executable

  unless exec_name
    msg = "no default executable for #{spec.full_name} and none given"
    raise LibGems::Exception, msg
  end

  File.join(spec.full_gem_path, spec.bindir, exec_name)
end

.binary_modeObject

The mode needed to read a file as straight binary.



401
402
403
# File 'lib/libgems.rb', line 401

def self.binary_mode
  'rb'
end

.bindir(install_dir = LibGems.dir) ⇒ Object

The path where gem executables are to be installed.



408
409
410
411
412
# File 'lib/libgems.rb', line 408

def self.bindir(install_dir=LibGems.dir)
  return File.join(install_dir, 'bin') unless
    install_dir.to_s == LibGems.default_dir
  LibGems.default_bindir
end

.clear_pathsObject

Reset the dir and path values. The next time dir or path is requested, the values will be calculated from scratch. This is mainly used by the unit tests to provide test isolation.



419
420
421
422
423
424
425
426
427
# File 'lib/libgems.rb', line 419

def self.clear_paths
  @gem_home = nil
  @gem_path = nil
  @user_home = nil

  @@source_index = nil

  @searcher = nil
end

.config_fileObject

The path to standard location of the user’s .gemrc file.



432
433
434
# File 'lib/libgems.rb', line 432

def self.config_file
  File.join LibGems.user_home, '.gemrc'
end

.configurationObject

The standard configuration object for gems.



439
440
441
# File 'lib/libgems.rb', line 439

def self.configuration
  @configuration || load_configuration
end

.configuration=(config) ⇒ Object

Use the given configuration object (which implements the ConfigFile protocol) as the standard configuration object.



447
448
449
# File 'lib/libgems.rb', line 447

def self.configuration=(config)
  load_configuration(config)
end

.datadir(gem_name) ⇒ Object

The path the the data directory specified by the gem name. If the package is not available as a gem, return nil.



463
464
465
466
467
# File 'lib/libgems.rb', line 463

def self.datadir(gem_name)
  spec = @loaded_specs[gem_name]
  return nil if spec.nil?
  File.join(spec.full_gem_path, 'data', gem_name)
end

.default_bindirObject

The default directory for binaries



67
68
69
70
71
72
73
# File 'lib/libgems/defaults.rb', line 67

def self.default_bindir
  if defined? RUBY_FRAMEWORK_VERSION then # mac framework support
    '/usr/bin'
  else # generic install
    ConfigMap[:bindir]
  end
end

.default_dirObject

Default home directory path to be used if an alternate value is not specified in the environment



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/libgems/defaults.rb', line 19

def self.default_dir
  if defined? RUBY_FRAMEWORK_VERSION then
    File.join File.dirname(ConfigMap[:sitedir]), 'Gems',
              ConfigMap[:ruby_version]
  elsif ConfigMap[:rubylibprefix] then
    File.join(ConfigMap[:rubylibprefix], 'gems',
              ConfigMap[:ruby_version])
  else
    File.join(ConfigMap[:libdir], ruby_engine, 'gems',
              ConfigMap[:ruby_version])
  end
end

.default_exec_formatObject

Deduce Ruby’s –program-prefix and –program-suffix from its install name



53
54
55
56
57
58
59
60
61
62
# File 'lib/libgems/defaults.rb', line 53

def self.default_exec_format
  exec_format = ConfigMap[:ruby_install_name].sub('ruby', '%s') rescue '%s'

  unless exec_format =~ /%s/ then
    raise LibGems::Exception,
      "[BUG] invalid exec_format #{exec_format.inspect}, no %s"
  end

  exec_format
end

.default_pathObject

Default gem load path



42
43
44
45
46
47
48
# File 'lib/libgems/defaults.rb', line 42

def self.default_path
  if File.exist? LibGems.user_home then
    [user_dir, default_dir]
  else
    [default_dir]
  end
end

.default_sourcesObject

An Array of the default sources that come with SlimGems



11
12
13
# File 'lib/libgems/defaults.rb', line 11

def self.default_sources
  %w[http://rubygems.org/]
end

.default_system_source_cache_dirObject

The default system-wide source info cache directory



78
79
80
# File 'lib/libgems/defaults.rb', line 78

def self.default_system_source_cache_dir
  File.join LibGems.dir, 'source_cache'
end

.default_user_source_cache_dirObject

The default user-specific source info cache directory



85
86
87
# File 'lib/libgems/defaults.rb', line 85

def self.default_user_source_cache_dir
  File.join LibGems.user_home, '.gem', 'source_cache'
end

.deflate(data) ⇒ Object

A Zlib::Deflate.deflate wrapper



472
473
474
475
# File 'lib/libgems.rb', line 472

def self.deflate(data)
  require 'zlib'
  Zlib::Deflate.deflate data
end

.dirObject

The path where gems are to be installed.



480
481
482
483
# File 'lib/libgems.rb', line 480

def self.dir
  set_home(ENV['LIBGEMS_HOME'] || ENV['GEM_HOME'] || LibGems.configuration.home || default_dir) unless @gem_home
  @gem_home
end

.ensure_gem_subdirectories(gemdir) ⇒ Object

Quietly ensure the named LibGems directory contains all the proper subdirectories. If we can’t create a directory due to a permission problem, then we will silently continue.



512
513
514
515
516
517
518
519
# File 'lib/libgems.rb', line 512

def self.ensure_gem_subdirectories(gemdir)
  require 'fileutils'

  LibGems::DIRECTORIES.each do |filename|
    fn = File.join gemdir, filename
    FileUtils.mkdir_p fn rescue nil unless File.exist? fn
  end
end

.ensure_ssl_availableObject

Ensure that SSL is available. Throw an exception if it is not.



31
32
33
34
35
# File 'lib/libgems/gem_openssl.rb', line 31

def ensure_ssl_available
  unless ssl_available?
    raise LibGems::Exception, "SSL is not installed on this system"
  end
end

.find_files(glob, check_load_path = true) ⇒ Object

Returns a list of paths matching glob that can be used by a gem to pick up features from other gems. For example:

LibGems.find_files('rdoc/discover').each do |path| load path end

if check_load_path is true (the default), then find_files also searches $LOAD_PATH for files as well as gems.

Note that find_files will return all files even if they are from different versions of the same gem.



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/libgems.rb', line 533

def self.find_files(glob, check_load_path=true)
  files = []

  if check_load_path
    files = $LOAD_PATH.map { |load_path|
      Dir["#{File.expand_path glob, load_path}#{LibGems.suffix_pattern}"]
    }.flatten.select { |file| File.file? file.untaint }
  end

  specs = searcher.find_all glob

  specs.each do |spec|
    files.concat searcher.matching_files(spec, glob)
  end

  # $LOAD_PATH might contain duplicate entries or reference
  # the spec dirs directly, so we prune.
  files.uniq! if check_load_path

  return files
end

.gunzip(data) ⇒ Object

Zlib::GzipReader wrapper that unzips data.



592
593
594
595
596
597
598
# File 'lib/libgems.rb', line 592

def self.gunzip(data)
  require 'stringio'
  require 'zlib'
  data = StringIO.new data

  Zlib::GzipReader.new(data).read
end

.gzip(data) ⇒ Object

Zlib::GzipWriter wrapper that zips data.



603
604
605
606
607
608
609
610
611
# File 'lib/libgems.rb', line 603

def self.gzip(data)
  require 'stringio'
  require 'zlib'
  zipped = StringIO.new

  Zlib::GzipWriter.wrap zipped do |io| io.write data end

  zipped.string
end

.hostObject

Get the default SlimGems API host. This is normally https://rubygems.org.



625
626
627
# File 'lib/libgems.rb', line 625

def self.host
  @host ||= "https://rubygems.org"
end

.host=(host) ⇒ Object

Set the default SlimGems API host.



631
632
633
# File 'lib/libgems.rb', line 631

def self.host= host
  @host = host
end

.inflate(data) ⇒ Object

A Zlib::Inflate#inflate wrapper



616
617
618
619
# File 'lib/libgems.rb', line 616

def self.inflate(data)
  require 'zlib'
  Zlib::Inflate.inflate data
end

.latest_load_pathsObject

Return a list of all possible load paths for the latest version for all gems in the LibGems installation.



639
640
641
642
643
644
645
646
647
648
649
# File 'lib/libgems.rb', line 639

def self.latest_load_paths
  result = []

  LibGems.path.each do |gemdir|
    each_load_path(latest_partials(gemdir)) do |load_path|
      result << load_path
    end
  end

  result
end

.load_configuration(config = nil) ⇒ Object



451
452
453
454
455
456
457
# File 'lib/libgems.rb', line 451

def self.load_configuration(config=nil)
  @configuration = config || LibGems::ConfigFile.new([])
  LibGems.use_paths(@configuration[:gemhome], @configuration[:gempath])
  LibGems::Command.extra_args = @configuration[:gem]
  LibGems::DocManager.configured_args = @configuration[:rdoc]
  @configuration
end

.load_env_pluginsObject

Find all ‘rubygems_plugin’ files in $LOAD_PATH and load them



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'lib/libgems.rb', line 1117

def self.load_env_plugins
  path = "libgems_plugin"

  files = []
  $LOAD_PATH.each do |load_path|
    globbed = Dir["#{File.expand_path path, load_path}#{LibGems.suffix_pattern}"]

    globbed.each do |load_path_file|
      files << load_path_file if File.file?(load_path_file.untaint)
    end
  end

  load_plugin_files files
end

.load_path_insert_indexObject

The index to insert activated gem paths into the $LOAD_PATH.

Defaults to the site lib directory unless gem_prelude.rb has loaded paths, then it inserts the activated gem’s paths before the gem_prelude.rb paths so you can override the gem_prelude.rb default $LOAD_PATH paths.



678
679
680
# File 'lib/libgems.rb', line 678

def self.load_path_insert_index
  index = $LOAD_PATH.index ConfigMap[:sitelibdir]
end

.load_plugin_files(plugins) ⇒ Object

Find all ‘rubygems_plugin’ files and load them



1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
# File 'lib/libgems.rb', line 1090

def self.load_plugin_files(plugins)
  plugins.each do |plugin|

    # Skip older versions of the LibGemsCutter plugin: Its commands are in
    # SlimGems proper now.

    next if plugin =~ /gemcutter-0\.[0-3]/

    begin
      load plugin
    rescue ::Exception => e
      details = "#{plugin.inspect}: #{e.message} (#{e.class})"
      warn "Error loading #{LibGems::NAME} plugin #{details}"
    end
  end
end

.load_pluginsObject

Find all ‘rubygems_plugin’ files in installed gems and load them



1110
1111
1112
# File 'lib/libgems.rb', line 1110

def self.load_plugins
  load_plugin_files(find_files('libgems_plugin', false) + find_files('rubygems_plugin', false))
end

.load_yamlObject

Loads YAML, preferring Psych



691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/libgems.rb', line 691

def self.load_yaml
  using_psych = false
  begin
    unless ENV['TEST_SYCK']
      require 'psych'
      using_psych = true
    end
  rescue ::LoadError
  ensure
    require 'yaml'
    if defined?(YAML::ENGINE)
      YAML::ENGINE.yamler = using_psych ? 'psych' : 'syck'
    end
  end

  # Hack to handle syck's DefaultKey bug with psych.
  # See the note at the top of lib/rubygems/requirement.rb for
  # why we end up defining DefaultKey more than once.
  if !defined? YAML::Syck
    YAML.module_eval do
      const_set 'Syck', Syck
    end
  end
end

.location_of_callerObject

The file name and line number of the caller of the caller of this method.



719
720
721
722
723
724
725
# File 'lib/libgems.rb', line 719

def self.location_of_caller
  caller[1] =~ /(.*?):(\d+).*?$/i
  file = $1
  lineno = $2.to_i

  [file, lineno]
end

.marshal_versionObject

The version of the Marshal format for your Ruby.



730
731
732
# File 'lib/libgems.rb', line 730

def self.marshal_version
  "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
end

.pathObject

Array of paths to search for Gems.



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/libgems.rb', line 737

def self.path
  @gem_path ||= nil

  unless @gem_path then
    paths = [ENV['LIBGEMS_PATH'] || ENV['GEM_PATH'] || LibGems.configuration.path || default_path]

    if defined?(APPLE_GEM_HOME) and not ENV['GEM_PATH'] then
      paths << APPLE_GEM_HOME
    end

    set_paths paths.compact.join(File::PATH_SEPARATOR)
  end

  @gem_path
end

.platformsObject

Array of platforms this SlimGems supports.



763
764
765
766
767
768
769
# File 'lib/libgems.rb', line 763

def self.platforms
  @platforms ||= []
  if @platforms.empty?
    @platforms = [LibGems::Platform::RUBY, LibGems::Platform.local]
  end
  @platforms
end

.platforms=(platforms) ⇒ Object

Set array of platforms this SlimGems supports (primarily for testing).



756
757
758
# File 'lib/libgems.rb', line 756

def self.platforms=(platforms)
  @platforms = platforms
end

.post_build(&hook) ⇒ Object

Adds a post-build hook that will be passed an LibGems::Installer instance when LibGems::Installer#install is called. The hook is called after the gem has been extracted and extensions have been built but before the executables or gemspec has been written. If the hook returns false then the gem’s files will be removed and the install will be aborted.



778
779
780
# File 'lib/libgems.rb', line 778

def self.post_build(&hook)
  @post_build_hooks << hook
end

.post_install(&hook) ⇒ Object

Adds a post-install hook that will be passed an LibGems::Installer instance when LibGems::Installer#install is called



786
787
788
# File 'lib/libgems.rb', line 786

def self.post_install(&hook)
  @post_install_hooks << hook
end

.post_uninstall(&hook) ⇒ Object

Adds a post-uninstall hook that will be passed a LibGems::Uninstaller instance and the spec that was uninstalled when LibGems::Uninstaller#uninstall is called



795
796
797
# File 'lib/libgems.rb', line 795

def self.post_uninstall(&hook)
  @post_uninstall_hooks << hook
end

.pre_install(&hook) ⇒ Object

Adds a pre-install hook that will be passed an LibGems::Installer instance when LibGems::Installer#install is called



803
804
805
# File 'lib/libgems.rb', line 803

def self.pre_install(&hook)
  @pre_install_hooks << hook
end

.pre_uninstall(&hook) ⇒ Object

Adds a pre-uninstall hook that will be passed an LibGems::Uninstaller instance and the spec that will be uninstalled when LibGems::Uninstaller#uninstall is called



812
813
814
# File 'lib/libgems.rb', line 812

def self.pre_uninstall(&hook)
  @pre_uninstall_hooks << hook
end

.prefixObject

The directory prefix this SlimGems was installed at.



819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/libgems.rb', line 819

def self.prefix
  dir = File.dirname File.expand_path(__FILE__)
  prefix = File.dirname dir

  if prefix == File.expand_path(ConfigMap[:sitelibdir]) or
     prefix == File.expand_path(ConfigMap[:libdir]) or
     'lib' != File.basename(dir) then
    nil
  else
    prefix
  end
end

.promote_load_path(gem_name, over_name) ⇒ Object

Promotes the load paths of the gem_name over the load paths of over_name. Useful for allowing one gem to override features in another using #find_files.

Raises:

  • (ArgumentError)


837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'lib/libgems.rb', line 837

def self.promote_load_path(gem_name, over_name)
  gem = LibGems.loaded_specs[gem_name]
  over = LibGems.loaded_specs[over_name]

  raise ArgumentError, "gem #{gem_name} is not activated" if gem.nil?
  raise ArgumentError, "gem #{over_name} is not activated" if over.nil?

  last_gem_path = File.join gem.full_gem_path, gem.require_paths.last

  over_paths = over.require_paths.map do |path|
    File.join over.full_gem_path, path
  end

  over_paths.each do |path|
    $LOAD_PATH.delete path
  end

  gem = $LOAD_PATH.index(last_gem_path) + 1

  $LOAD_PATH.insert(gem, *over_paths)
end

.read_binary(path) ⇒ Object

Safely read a file in binary mode on all platforms.



871
872
873
# File 'lib/libgems.rb', line 871

def self.read_binary(path)
  File.open path, binary_mode do |f| f.read end
end

.refreshObject

Refresh source_index from disk and clear searcher.



862
863
864
865
866
# File 'lib/libgems.rb', line 862

def self.refresh
  source_index.refresh!

  @searcher = nil
end

.remove_prelude_pathsObject



682
683
684
685
686
# File 'lib/libgems.rb', line 682

def self.remove_prelude_paths
  LibGems::QuickLoader::GemLoadPaths.each do |path|
    $LOAD_PATH.delete(path)
  end
end

.required_location(gemname, libfile, *requirements) ⇒ Object

Full path to libfile in gemname. Searches for the latest gem unless requirements is given.



903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
# File 'lib/libgems.rb', line 903

def self.required_location(gemname, libfile, *requirements)
  requirements = LibGems::Requirement.default if requirements.empty?

  matches = LibGems.source_index.find_name gemname, requirements

  return nil if matches.empty?

  spec = matches.last
  spec.require_paths.each do |path|
    result = File.join spec.full_gem_path, path, libfile
    return result if File.exist? result
  end

  nil
end

.rubyObject

The path to the running Ruby interpreter.



922
923
924
925
926
927
928
929
930
931
932
933
# File 'lib/libgems.rb', line 922

def self.ruby
  if @ruby.nil? then
    @ruby = File.join(ConfigMap[:bindir],
                      ConfigMap[:ruby_install_name])
    @ruby << ConfigMap[:EXEEXT]

    # escape string in case path to ruby executable contain spaces.
    @ruby.sub!(/.*\s.*/m, '"\&"')
  end

  @ruby
end

.ruby_engineObject

A wrapper around RUBY_ENGINE const that may not be defined



92
93
94
95
96
97
98
# File 'lib/libgems/defaults.rb', line 92

def self.ruby_engine
  if defined? RUBY_ENGINE then
    RUBY_ENGINE
  else
    'ruby'
  end
end

.ruby_versionObject

A LibGems::Version for the currently running ruby.



938
939
940
941
942
943
944
945
946
947
948
949
# File 'lib/libgems.rb', line 938

def self.ruby_version
  return @ruby_version if defined? @ruby_version
  version = RUBY_VERSION.dup

  if defined?(RUBY_PATCHLEVEL) && RUBY_PATCHLEVEL != -1 then
    version << ".#{RUBY_PATCHLEVEL}"
  elsif defined?(RUBY_REVISION) then
    version << ".dev.#{RUBY_REVISION}"
  end

  @ruby_version = LibGems::Version.new version
end

.rubygems_compat?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/libgems.rb', line 204

def self.rubygems_compat?
  @using_rubygems_compat ||= false
end

.searcherObject

The LibGemsPathSearcher object used to search for matching installed gems.



954
955
956
# File 'lib/libgems.rb', line 954

def self.searcher
  @searcher ||= LibGems::GemPathSearcher.new
end

.source_indexObject Also known as: cache

Returns the LibGems::SourceIndex of specifications that are in the LibGems.path



995
996
997
# File 'lib/libgems.rb', line 995

def self.source_index
  @@source_index ||= SourceIndex.from_installed_gems
end

.sourcesObject

Returns an Array of sources to fetch remote gems from. If the sources list is empty, attempts to load the “sources” gem, then uses default_sources if it is not installed.



1004
1005
1006
1007
1008
1009
1010
# File 'lib/libgems.rb', line 1004

def self.sources
  if !@sources || @sources.empty? then
    @sources = default_sources
  end

  @sources
end

.sources=(new_sources) ⇒ Object

Need to be able to set the sources without calling LibGems.sources.replace since that would cause an infinite loop.



1016
1017
1018
# File 'lib/libgems.rb', line 1016

def self.sources=(new_sources)
  @sources = new_sources
end

.ssl_available?Boolean

Is SSL (used by the signing commands) available on this platform?

Returns:

  • (Boolean)


19
20
21
# File 'lib/libgems/gem_openssl.rb', line 19

def ssl_available?
  @ssl_available
end

.suffix_patternObject

Glob pattern for require-able path suffixes.



1023
1024
1025
# File 'lib/libgems.rb', line 1023

def self.suffix_pattern
  @suffix_pattern ||= "{#{suffixes.join(',')}}"
end

.suffixesObject

Suffixes for require-able paths.



1030
1031
1032
# File 'lib/libgems.rb', line 1030

def self.suffixes
  ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
end

.time(msg, width = 0, display = LibGems.configuration.verbose) ⇒ Object

Prints the amount of time the supplied block takes to run using the debug UI output.



1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/libgems.rb', line 1038

def self.time(msg, width = 0, display = LibGems.configuration.verbose)
  now = Time.now

  value = yield

  elapsed = Time.now - now

  ui.say "%2$*1$s: %3$3.3fs" % [-width, msg, elapsed] if display

  value
end

.try_activate(path) ⇒ Object



1287
1288
1289
1290
1291
1292
1293
# File 'lib/libgems.rb', line 1287

def try_activate(path)
  spec = LibGems.searcher.find(path)
  return false unless spec

  LibGems.activate(spec.name, "= #{spec.version}")
  return true
end

.uiObject

Lazily loads DefaultUserInteraction and returns the default UI.



1053
1054
1055
1056
1057
# File 'lib/libgems.rb', line 1053

def self.ui
  require 'libgems/user_interaction'

  LibGems::DefaultUserInteraction.ui
end

.use_paths(home, paths = []) ⇒ Object

Use the home and paths values for LibGems.dir and LibGems.path. Used mainly by the unit tests to provide environment isolation.



1063
1064
1065
1066
1067
# File 'lib/libgems.rb', line 1063

def self.use_paths(home, paths=[])
  clear_paths
  set_home(home) if home
  set_paths(paths.join(File::PATH_SEPARATOR)) if paths
end

.user_dirObject

Path for gems in the user’s home directory



35
36
37
# File 'lib/libgems/defaults.rb', line 35

def self.user_dir
  File.join LibGems.user_home, '.gem', ruby_engine, ConfigMap[:ruby_version]
end

.user_homeObject

The home directory for the user.



1072
1073
1074
# File 'lib/libgems.rb', line 1072

def self.user_home
  @user_home ||= find_home
end

.win_platform?Boolean

Is this a windows platform?

Returns:

  • (Boolean)


1079
1080
1081
1082
1083
1084
1085
# File 'lib/libgems.rb', line 1079

def self.win_platform?
  if @@win_platform.nil? then
    @@win_platform = !!WIN_PATTERNS.find { |r| RUBY_PLATFORM =~ r }
  end

  @@win_platform
end

.with_rubygems_compatObject

Run code with Gem = LibGems



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/libgems.rb', line 211

def self.with_rubygems_compat
  unless LibGems.rubygems_compat?
    begin
      @using_rubygems_compat = true
      libgems_original_gem = nil
      if Object.const_defined?(:Gem)
        libgems_original_gem = Object::Gem
        Object.send(:remove_const, :Gem)
      end
      Object.const_set(:Gem, LibGems)
      yield
    ensure
      Object.send(:remove_const, :Gem)
      Object.const_set(:Gem, libgems_original_gem) if libgems_original_gem
      @using_rubygems_compat = false
    end
  else
    yield
  end
end