Class: MilkTea::ModulePathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/core/module_path_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(module_roots:, platform:, package_graph: nil, source_overrides: nil, package_manifest_cache: nil) ⇒ ModulePathResolver

Returns a new instance of ModulePathResolver.



5
6
7
8
9
10
11
# File 'lib/milk_tea/core/module_path_resolver.rb', line 5

def initialize(module_roots:, platform:, package_graph: nil, source_overrides: nil, package_manifest_cache: nil)
  @module_roots = module_roots.map { |root| File.expand_path(root.to_s) }
  @platform = platform
  @package_graph = package_graph
  @source_overrides = source_overrides || {}
  @package_manifest_cache = package_manifest_cache || {}
end

Instance Method Details

#resolve_module_path(module_name, importer_path: nil, importer_module_name: nil) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/milk_tea/core/module_path_resolver.rb', line 13

def resolve_module_path(module_name, importer_path: nil, importer_module_name: nil)
  package_candidate = resolve_package_module_path(module_name, importer_path:)
  return package_candidate if package_candidate

  relative_path = File.join(*module_name.split(".")) + ".mt"
  blocked = false
  candidate = @module_roots.lazy.map { |root| ModuleLoader.resolve_source_path(File.join(root, relative_path), platform: @platform) }.find do |path|
    next false unless source_path_available?(path)

    allowed = import_allowed?(module_name, importer_path, path)
    blocked ||= !allowed
    allowed
  end
  raise ModuleLoadError.new("package dependency not declared", path: module_name) if blocked
  unless candidate
    message = namespace_hint_for_missing_module(module_name, importer_path:, importer_module_name:) || "module not found"
    raise ModuleLoadError.new(message, path: module_name)
  end

  File.expand_path(candidate)
end

#source_path_available?(path) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/milk_tea/core/module_path_resolver.rb', line 35

def source_path_available?(path)
  resolved_path = File.expand_path(path.to_s)
  @source_overrides.key?(resolved_path) || File.file?(resolved_path)
end