Class: Capsium::Package::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/capsium/package/store.rb,
sig/capsium/package/store.rbs

Overview

A package store directory (ARCHITECTURE.md section 4a): "-.cap" files plus an optional index.json mapping dependency GUID -> file name. Dependencies resolve to the newest version satisfying their semver range.

Defined Under Namespace

Classes: CatalogEntry

Constant Summary collapse

INDEX_FILE =

Returns:

  • (String)
"index.json"
CAP_GLOB =

Returns:

  • (String)
"*.cap"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Store

Returns a new instance of Store.

Parameters:

  • dir (String)


29
30
31
32
33
34
35
# File 'lib/capsium/package/store.rb', line 29

def initialize(dir)
  unless File.directory?(dir)
    raise DependencyError, "Package store directory not found: #{dir}"
  end

  @dir = dir
end

Instance Attribute Details

#dirString (readonly)

Returns the value of attribute dir.

Returns:

  • (String)


20
21
22
# File 'lib/capsium/package/store.rb', line 20

def dir
  @dir
end

Class Method Details

.defaultStore?

The store configured via the CAPSIUM_STORE environment variable, or nil when unset.

Returns:



24
25
26
27
# File 'lib/capsium/package/store.rb', line 24

def self.default
  dir = ENV.fetch("CAPSIUM_STORE", nil)
  dir.nil? || dir.empty? ? nil : new(dir)
end

Instance Method Details

#catalogArray[CatalogEntry]

Every .cap in the store with its metadata identity (memoized).

Returns:



60
61
62
63
64
# File 'lib/capsium/package/store.rb', line 60

def catalog
  @catalog ||= Dir.glob(File.join(@dir, CAP_GLOB)).map do |path|
    catalog_entry(path)
  end
end

#find(guid, range_string) ⇒ String

The newest .cap providing the dependency GUID whose version satisfies the range string. Raises DependencyNotFoundError or UnsatisfiableDependencyError when it cannot.

Parameters:

  • guid (String)
  • range_string (String)

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/capsium/package/store.rb', line 39

def find(guid, range_string)
  range = VersionRange.parse(range_string)
  indexed = indexed_path(guid)
  return indexed_satisfying(indexed, guid, range_string, range) if indexed

  candidates = catalog.select { |entry| entry.guid == guid }
  if candidates.empty?
    raise DependencyNotFoundError,
          "no package for dependency #{guid} in store #{@dir}"
  end

  satisfying = candidates.select { |entry| range.satisfied_by?(entry.version) }
  return satisfying.max_by(&:version).path unless satisfying.empty?

  versions = candidates.map { |entry| entry.version.to_s }.sort.join(", ")
  raise UnsatisfiableDependencyError,
        "no version of #{guid} satisfies '#{range_string}' " \
        "(store has: #{versions})"
end