Module: Bootsnap::ExplicitRequire

Defined in:
lib/bootsnap/explicit_require.rb

Constant Summary collapse

ARCHDIR =
RbConfig::CONFIG["archdir"]
RUBYLIBDIR =
RbConfig::CONFIG["rubylibdir"]
DLEXT =
RbConfig::CONFIG["DLEXT"]

Class Method Summary collapse

Class Method Details

.from_archdir(feature) ⇒ Object



17
18
19
# File 'lib/bootsnap/explicit_require.rb', line 17

def self.from_archdir(feature)
  require(File.join(ARCHDIR, "#{feature}.#{DLEXT}"))
end

.from_rubylibdir(feature) ⇒ Object



13
14
15
# File 'lib/bootsnap/explicit_require.rb', line 13

def self.from_rubylibdir(feature)
  require(File.join(RUBYLIBDIR, "#{feature}.rb"))
end

.from_self(feature) ⇒ Object



9
10
11
# File 'lib/bootsnap/explicit_require.rb', line 9

def self.from_self(feature)
  require_relative("../#{feature}")
end

.with_gems(*gems) ⇒ Object

Given a set of gems, run a block with the LOAD_PATH narrowed to include only core ruby source paths and these gems – that is, roughly, temporarily remove all gems not listed in this call from the LOAD_PATH.

This is useful before bootsnap is fully-initialized to load gems that it depends on, without forcing full LOAD_PATH traversals.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bootsnap/explicit_require.rb', line 27

def self.with_gems(*gems)
  # Ensure the gems are activated (their paths are in $LOAD_PATH)
  gems.each do |gem_name|
    gem gem_name
  end

  orig = $LOAD_PATH.dup
  $LOAD_PATH.clear
  gems.each do |gem|
    pat = %r{
      /
      (gems|extensions/[^/]+/[^/]+)          # "gems" or "extensions/x64_64-darwin16/2.3.0"
      /
      #{Regexp.escape(gem)}-(\h{12}|(\d+\.)) # msgpack-1.2.3 or msgpack-1234567890ab
    }x
    $LOAD_PATH.concat(orig.grep(pat))
  end
  $LOAD_PATH << ARCHDIR
  $LOAD_PATH << RUBYLIBDIR
  begin
    yield
  rescue LoadError
    $LOAD_PATH.replace(orig)
    yield
  end
ensure
  $LOAD_PATH.replace(orig)
end