Module: Bootsnap::LoadPathCache::PathScanner

Defined in:
lib/bootsnap/load_path_cache/path_scanner.rb

Constant Summary collapse

REQUIRABLE_EXTENSIONS =
[DOT_RB] + DL_EXTENSIONS
NORMALIZE_NATIVE_EXTENSIONS =
!DL_EXTENSIONS.include?(LoadPathCache::DOT_SO)
ALTERNATIVE_NATIVE_EXTENSIONS_PATTERN =
/\.(o|bundle|dylib)\z/.freeze
BUNDLE_PATH =
if Bootsnap.bundler?
  (Bundler.bundle_path.cleanpath.to_s << LoadPathCache::SLASH).freeze
else
  ""
end

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.ignored_directoriesObject

Returns the value of attribute ignored_directories.



21
22
23
# File 'lib/bootsnap/load_path_cache/path_scanner.rb', line 21

def ignored_directories
  @ignored_directories
end

Class Method Details

.call(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bootsnap/load_path_cache/path_scanner.rb', line 23

def call(path)
  path = File.expand_path(path.to_s).freeze
  return [[], []] unless File.directory?(path)

  # If the bundle path is a descendent of this path, we do additional
  # checks to prevent recursing into the bundle path as we recurse
  # through this path. We don't want to scan the bundle path because
  # anything useful in it will be present on other load path items.
  #
  # This can happen if, for example, the user adds '.' to the load path,
  # and the bundle path is '.bundle'.
  contains_bundle_path = BUNDLE_PATH.start_with?(path)

  dirs = []
  requirables = []
  walk(path, nil) do |relative_path, absolute_path, is_directory|
    if is_directory
      dirs << os_path(relative_path)
      !contains_bundle_path || !absolute_path.start_with?(BUNDLE_PATH)
    elsif relative_path.end_with?(*REQUIRABLE_EXTENSIONS)
      requirables << os_path(relative_path)
    end
  end
  [requirables, dirs]
end

.walk(absolute_dir_path, relative_dir_path, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bootsnap/load_path_cache/path_scanner.rb', line 49

def walk(absolute_dir_path, relative_dir_path, &block)
  Dir.foreach(absolute_dir_path) do |name|
    next if name.start_with?(".")

    relative_path = relative_dir_path ? File.join(relative_dir_path, name) : name

    absolute_path = "#{absolute_dir_path}/#{name}"
    if File.directory?(absolute_path)
      next if ignored_directories.include?(name) || ignored_directories.include?(absolute_path)

      if yield relative_path, absolute_path, true
        walk(absolute_path, relative_path, &block)
      end
    else
      yield relative_path, absolute_path, false
    end
  end
end

Instance Method Details

#os_path(path) ⇒ Object



69
70
71
# File 'lib/bootsnap/load_path_cache/path_scanner.rb', line 69

def os_path(path)
  path.freeze
end