Class: Capsium::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/capsium/registry.rb,
lib/capsium/registry/local.rb,
lib/capsium/registry/remote.rb,
sig/capsium/registry.rbs,
sig/capsium/registry/local.rbs,
sig/capsium/registry/remote.rbs

Overview

A static package registry (directory or https base URL with an index.json): Capsium::Registry::Local (read-write) and Capsium::Registry::Remote (read-only). See lib/capsium/registry.rb.

Defined Under Namespace

Classes: ChecksumMismatchError, Entry, FetchError, InvalidPackageError, InvalidRegistryError, Local, PackageNotFoundError, RegistryError, RegistryNotConfiguredError, Remote, UnsatisfiableConstraintError

Constant Summary collapse

INDEX_FILE =

Returns:

  • (String)
"index.json"
ENV_VAR =

Returns:

  • (String)
"CAPSIUM_REGISTRY"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



78
79
80
# File 'lib/capsium/registry.rb', line 78

def initialize
  @index = nil
end

Class Method Details

.defaultRegistry?

The registry named by the CAPSIUM_REGISTRY environment variable, or nil when unset.

Returns:



72
73
74
75
# File 'lib/capsium/registry.rb', line 72

def default
  ref = ENV.fetch(ENV_VAR, nil)
  ref.nil? || ref.empty? ? nil : fetch(ref)
end

.fetch(ref) ⇒ Registry

The registry at the given reference: a Local directory or a Remote http(s) base URL. Raises RegistryNotConfiguredError when no reference is given.

Parameters:

  • ref (String, nil)

Returns:



61
62
63
64
65
66
67
68
# File 'lib/capsium/registry.rb', line 61

def fetch(ref)
  if ref.nil? || ref.to_s.empty?
    raise RegistryNotConfiguredError,
          "no registry configured (pass --registry or set #{ENV_VAR})"
  end

  ref.to_s.match?(%r{\Ahttps?://}) ? Remote.new(ref.to_s) : Local.new(ref.to_s)
end

Instance Method Details

#install(guid, constraint = "*", store:) ⇒ Object

Resolves, fetches, checksum-verifies and installs the newest satisfying .cap into the package store, returning the store path.



104
105
106
107
108
109
110
# File 'lib/capsium/registry.rb', line 104

def install(guid, constraint = "*", store:)
  entry = resolve(guid, constraint)
  with_entry_file(entry) do |path|
    verify_checksum!(entry, path)
    store_for(store).install(path, guid: entry.guid, file_name: entry.cap_file_name)
  end
end

#locationString

Where this registry lives (directory path or base URL).

Returns:

  • (String)


84
# File 'lib/capsium/registry.rb', line 84

def location = raise(NotImplementedError)

#push(_cap_path) ⇒ Entry

Only Local registries are writable.

Parameters:

  • cap_path (String)

Returns:



113
114
115
# File 'lib/capsium/registry.rb', line 113

def push(_cap_path)
  raise RegistryError, "registry is read-only: #{location}"
end

#resolve(guid, constraint = "*") ⇒ Entry

The newest indexed version of the package GUID satisfying the semver constraint.

Parameters:

  • guid (String)
  • constraint (String) (defaults to: "*")

Returns:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/capsium/registry.rb', line 89

def resolve(guid, constraint = "*")
  listing = packages[guid]
  raise PackageNotFoundError, "no package #{guid} in registry #{location}" if listing.nil?

  versions = listing_versions(guid, listing)
  range = Package::VersionRange.parse(constraint)
  satisfying = versions.keys.select { |version| range.satisfied_by?(version) }
  raise_unsatisfiable(guid, constraint, versions) if satisfying.empty?

  entry_for(guid, listing, satisfying.max)
end