Module: MilkTea::ModuleRoots

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

Class Method Summary collapse

Class Method Details

.add_env_roots!(roots, env) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/milk_tea/core/module_roots.rb', line 74

def self.add_env_roots!(roots, env)
  env_keys = %w[MILK_TEA_MODULE_ROOT MILK_TEA_STD_ROOT]
  env_keys.each do |key|
    value = env[key]
    next if value.nil? || value.strip.empty?

    value.split(File::PATH_SEPARATOR).each do |entry|
      roots << entry unless entry.strip.empty?
    end
  end
end

.normalize_root(root) ⇒ Object



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

def self.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



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

def self.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



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

def self.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



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

def self.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



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

def self.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