Class: MilkTea::PackageSourceCache

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/packages/source_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: self.class.default_root) ⇒ PackageSourceCache

Returns a new instance of PackageSourceCache.



20
21
22
# File 'lib/milk_tea/packages/source_cache.rb', line 20

def initialize(root: self.class.default_root)
  @root = File.expand_path(root)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



18
19
20
# File 'lib/milk_tea/packages/source_cache.rb', line 18

def root
  @root
end

Class Method Details

.default_root(env: ENV, home: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/milk_tea/packages/source_cache.rb', line 7

def self.default_root(env: ENV, home: nil)
  xdg_cache_home = env.fetch("XDG_CACHE_HOME", "").to_s.strip
  base_root = if xdg_cache_home.empty?
                File.join(home || Dir.home, ".cache")
              else
                File.expand_path(xdg_cache_home)
              end

  File.join(base_root, "milk_tea", "package_sources")
end

Instance Method Details

#manifest_path_for(source_or_identity) ⇒ Object



53
54
55
# File 'lib/milk_tea/packages/source_cache.rb', line 53

def manifest_path_for(source_or_identity)
  File.join(materialized_root_for(source_or_identity), "package.toml")
end

#materialized?(source_or_identity) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/milk_tea/packages/source_cache.rb', line 57

def materialized?(source_or_identity)
  File.file?(manifest_path_for(source_or_identity))
end

#materialized_root_for(source_or_identity) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/milk_tea/packages/source_cache.rb', line 34

def materialized_root_for(source_or_identity)
  identity = extract_identity(source_or_identity)
  base_path = path_for(identity)

  if identity.respond_to?(:subdir) && identity.subdir
    segments = identity.subdir.split(/[\\\/]+/).reject(&:empty?)
    materialized_path = File.expand_path(File.join(base_path, *segments))
    expanded_base_path = File.expand_path(base_path)
    unless materialized_path == expanded_base_path || materialized_path.start_with?(expanded_base_path + File::SEPARATOR)
      raise PackageSourceCacheError,
            "git source subdir escapes the materialized package root: #{identity.subdir.inspect}"
    end

    return materialized_path
  end

  base_path
end

#path_for(source_or_identity) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/milk_tea/packages/source_cache.rb', line 24

def path_for(source_or_identity)
  identity = extract_identity(source_or_identity)
  cache_key = identity.cache_key
  unless cache_key
    raise PackageSourceCacheError, "package source kind #{identity.kind.inspect} does not use the shared source cache"
  end

  File.join(@root, *cache_key.split("/"))
end