Module: MilkTea::ModuleRoots

Defined in:
lib/milk_tea/core/module_roots.rb

Class Method Summary collapse

Class Method Details

.normalize_root(root) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/milk_tea/core/module_roots.rb', line 64

def normalize_root(root)
  return nil if root.nil? || root.to_s.strip.empty?

  expanded = File.expand_path(root.to_s)
  if File.basename(expanded) == 'std'
    File.dirname(expanded)
  else
    expanded
  end
end

.package_root_for_path(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/milk_tea/core/module_roots.rb', line 50

def package_root_for_path(path)
  return nil unless path

  current = File.expand_path(File.directory?(path) ? path : File.dirname(path))
  loop do
    return current if File.file?(File.join(current, 'package.toml'))

    parent = File.dirname(current)
    return nil if parent == current

    current = parent
  end
end

.package_roots_for_path(path, locked: false) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/milk_tea/core/module_roots.rb', line 27

def package_roots_for_path(path, locked: false)
  PackageGraph.load(path, locked:).source_roots
rescue PackageManifestError
  package_root = package_root_for_path(path)
  return [] unless package_root

  [package_root]
end

.project_root_for_path(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/milk_tea/core/module_roots.rb', line 36

def project_root_for_path(path)
  return nil unless path

  current = File.expand_path(File.directory?(path) ? path : File.dirname(path))
  loop do
    return current if File.directory?(File.join(current, 'std'))

    parent = File.dirname(current)
    return nil if parent == current

    current = parent
  end
end

.roots_for_path(path, env: ENV, locked: false) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/milk_tea/core/module_roots.rb', line 7

def roots_for_path(path, env: ENV, locked: false)
  roots = []

  add_env_roots!(roots, env)

  package_roots_for_path(path, locked:).each do |root|
    roots << root
  end

  project_root = project_root_for_path(path)
  roots << project_root if project_root

  roots << MilkTea.root.to_s

  roots.map { |root| normalize_root(root) }
       .compact
       .uniq
       .select { |root| File.directory?(root) }
end