Module: LibGems

Defined in:
lib/libgems.rb,
lib/libgems/defaults.rb,
lib/libgems/gem_openssl.rb,
lib/libgems/source_index.rb,
lib/libgems/source_index.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

NAME =
'LibGems'
GEM_NAME =
'libgems'
VERSION =
'1.3.9.2'
LIBGEMS_VERSION =
'0.1.0'
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/'
Cache =

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

SourceIndex
@@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



1105
1106
1107
# File 'lib/libgems.rb', line 1105

def loaded_specs
  @loaded_specs
end

.post_build_hooksObject (readonly)

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



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

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



1116
1117
1118
# File 'lib/libgems.rb', line 1116

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



1122
1123
1124
# File 'lib/libgems.rb', line 1122

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



1127
1128
1129
# File 'lib/libgems.rb', line 1127

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



1132
1133
1134
# File 'lib/libgems.rb', line 1132

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.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
# File 'lib/libgems.rb', line 221

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.



303
304
305
306
307
308
309
310
311
312
313
# File 'lib/libgems.rb', line 303

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)


327
328
329
330
331
332
333
334
335
336
# File 'lib/libgems.rb', line 327

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.



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/libgems.rb', line 344

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.



374
375
376
# File 'lib/libgems.rb', line 374

def self.binary_mode
  'rb'
end

.bindir(install_dir = LibGems.dir) ⇒ Object

The path where gem executables are to be installed.



381
382
383
384
385
# File 'lib/libgems.rb', line 381

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.



392
393
394
395
396
397
398
399
400
# File 'lib/libgems.rb', line 392

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.



405
406
407
# File 'lib/libgems.rb', line 405

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

.configurationObject

The standard configuration object for gems.



412
413
414
# File 'lib/libgems.rb', line 412

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.



420
421
422
# File 'lib/libgems.rb', line 420

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.



436
437
438
439
440
# File 'lib/libgems.rb', line 436

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



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

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

.dirObject

The path where gems are to be installed.



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

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.



485
486
487
488
489
490
491
492
# File 'lib/libgems.rb', line 485

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.



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/libgems.rb', line 506

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.



565
566
567
568
569
570
571
# File 'lib/libgems.rb', line 565

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.



576
577
578
579
580
581
582
583
584
# File 'lib/libgems.rb', line 576

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.



598
599
600
# File 'lib/libgems.rb', line 598

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

.host=(host) ⇒ Object

Set the default SlimGems API host.



604
605
606
# File 'lib/libgems.rb', line 604

def self.host= host
  @host = host
end

.inflate(data) ⇒ Object

A Zlib::Inflate#inflate wrapper



589
590
591
592
# File 'lib/libgems.rb', line 589

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.



612
613
614
615
616
617
618
619
620
621
622
# File 'lib/libgems.rb', line 612

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



424
425
426
427
428
429
430
# File 'lib/libgems.rb', line 424

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



1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'lib/libgems.rb', line 1085

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.



651
652
653
# File 'lib/libgems.rb', line 651

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



1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/libgems.rb', line 1058

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



1078
1079
1080
# File 'lib/libgems.rb', line 1078

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

.load_yamlObject

Loads YAML, preferring Psych



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/libgems.rb', line 664

def self.load_yaml
  begin
    require 'psych' unless ENV['TEST_SYCK']
  rescue ::LoadError
  ensure
    require 'yaml'
  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', Module.new {
          const_set 'DefaultKey', Class.new
        }
      end
  end
end

.location_of_callerObject

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



687
688
689
690
691
692
693
# File 'lib/libgems.rb', line 687

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.



698
699
700
# File 'lib/libgems.rb', line 698

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

.pathObject

Array of paths to search for Gems.



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/libgems.rb', line 705

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.



731
732
733
734
735
736
737
# File 'lib/libgems.rb', line 731

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).



724
725
726
# File 'lib/libgems.rb', line 724

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.



746
747
748
# File 'lib/libgems.rb', line 746

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



754
755
756
# File 'lib/libgems.rb', line 754

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



763
764
765
# File 'lib/libgems.rb', line 763

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



771
772
773
# File 'lib/libgems.rb', line 771

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



780
781
782
# File 'lib/libgems.rb', line 780

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

.prefixObject

The directory prefix this SlimGems was installed at.



787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/libgems.rb', line 787

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)


805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/libgems.rb', line 805

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.



839
840
841
# File 'lib/libgems.rb', line 839

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.



830
831
832
833
834
# File 'lib/libgems.rb', line 830

def self.refresh
  source_index.refresh!

  @searcher = nil
end

.remove_prelude_pathsObject



655
656
657
658
659
# File 'lib/libgems.rb', line 655

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.



871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'lib/libgems.rb', line 871

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.



890
891
892
893
894
895
896
897
898
899
900
901
# File 'lib/libgems.rb', line 890

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.



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

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)


177
178
179
# File 'lib/libgems.rb', line 177

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

.searcherObject

The LibGemsPathSearcher object used to search for matching installed gems.



922
923
924
# File 'lib/libgems.rb', line 922

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



963
964
965
# File 'lib/libgems.rb', line 963

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.



972
973
974
975
976
977
978
# File 'lib/libgems.rb', line 972

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.



984
985
986
# File 'lib/libgems.rb', line 984

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.



991
992
993
# File 'lib/libgems.rb', line 991

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

.suffixesObject

Suffixes for require-able paths.



998
999
1000
# File 'lib/libgems.rb', line 998

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.



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/libgems.rb', line 1006

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



1255
1256
1257
1258
1259
1260
1261
# File 'lib/libgems.rb', line 1255

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.



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

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.



1031
1032
1033
1034
1035
# File 'lib/libgems.rb', line 1031

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.



1040
1041
1042
# File 'lib/libgems.rb', line 1040

def self.user_home
  @user_home ||= find_home
end

.win_platform?Boolean

Is this a windows platform?

Returns:

  • (Boolean)


1047
1048
1049
1050
1051
1052
1053
# File 'lib/libgems.rb', line 1047

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/libgems.rb', line 184

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