Class: Capsium::Package::Store
- Inherits:
-
Object
- Object
- Capsium::Package::Store
- Defined in:
- lib/capsium/package/store.rb,
sig/capsium/package/store.rbs
Overview
A package store directory (ARCHITECTURE.md section 4a):
"
Defined Under Namespace
Classes: CatalogEntry
Constant Summary collapse
- INDEX_FILE =
"index.json"- CAP_GLOB =
"*.cap"
Instance Attribute Summary collapse
-
#dir ⇒ String
readonly
Returns the value of attribute dir.
Class Method Summary collapse
-
.default ⇒ Store?
The store configured via the CAPSIUM_STORE environment variable, or nil when unset.
Instance Method Summary collapse
-
#catalog ⇒ Array[CatalogEntry]
Every .cap in the store with its metadata identity (memoized).
-
#find(guid, range_string) ⇒ String
The newest .cap providing the dependency GUID whose version satisfies the range string.
-
#initialize(dir) ⇒ Store
constructor
A new instance of Store.
-
#install(source_path, guid:, file_name:) ⇒ Object
Installs the .cap at source_path into the store as file_name, records guid -> file_name in index.json (atomically) and returns the installed path.
Constructor Details
#initialize(dir) ⇒ Store
Returns a new instance of Store.
30 31 32 33 34 35 36 |
# File 'lib/capsium/package/store.rb', line 30 def initialize(dir) unless File.directory?(dir) raise DependencyError, "Package store directory not found: #{dir}" end @dir = dir end |
Instance Attribute Details
#dir ⇒ String (readonly)
Returns the value of attribute dir.
21 22 23 |
# File 'lib/capsium/package/store.rb', line 21 def dir @dir end |
Class Method Details
.default ⇒ Store?
The store configured via the CAPSIUM_STORE environment variable, or nil when unset.
25 26 27 28 |
# File 'lib/capsium/package/store.rb', line 25 def self.default dir = ENV.fetch("CAPSIUM_STORE", nil) dir.nil? || dir.empty? ? nil : new(dir) end |
Instance Method Details
#catalog ⇒ Array[CatalogEntry]
Every .cap in the store with its metadata identity (memoized).
61 62 63 64 65 |
# File 'lib/capsium/package/store.rb', line 61 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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/capsium/package/store.rb', line 40 def find(guid, range_string) range = VersionRange.parse(range_string) indexed = indexed_path(guid) return (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 = candidates.select { |entry| range.satisfied_by?(entry.version) } return .max_by(&:version).path unless .empty? versions = candidates.map { |entry| entry.version.to_s }.sort.join(", ") raise UnsatisfiableDependencyError, "no version of #{guid} satisfies '#{range_string}' " \ "(store has: #{versions})" end |
#install(source_path, guid:, file_name:) ⇒ Object
Installs the .cap at source_path into the store as file_name, records guid -> file_name in index.json (atomically) and returns the installed path.
72 73 74 75 76 77 78 79 |
# File 'lib/capsium/package/store.rb', line 72 def install(source_path, guid:, file_name:) destination = File.join(@dir, file_name) tmp = "#{destination}.tmp-#{Process.pid}" FileUtils.cp(source_path, tmp) File.rename(tmp, destination) record_index(guid, file_name) destination end |