Class: Textus::Infra::Port::SentinelStore

Inherits:
Object
  • Object
show all
Includes:
Interface
Defined in:
lib/textus/infra/port/sentinel_store.rb

Overview

Persistence adapter for sentinel files. Owns the on-disk JSON shape, the path layout (<store_root>/track/sentinels/.textus-managed.json — runtime, git-ignored, ADR 0070), and all File/FileUtils I/O. Core::Sentinel is a pure value object that depends on this port for reads and writes.

Defined Under Namespace

Modules: Interface

Constant Summary collapse

SUFFIX =
".textus-managed.json".freeze

Instance Method Summary collapse

Constructor Details

#initialize(file_system: Infra::FileSystem.new) ⇒ SentinelStore

Returns a new instance of SentinelStore.



20
21
22
# File 'lib/textus/infra/port/sentinel_store.rb', line 20

def initialize(file_system: Infra::FileSystem.new)
  @file_system = file_system
end

Instance Method Details

#load(path, repo_root) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/textus/infra/port/sentinel_store.rb', line 36

def load(path, repo_root)
  raw = JSON.parse(@file_system.read(path))
  Textus::Infra::Port::Sentinel.new(
    target: absolutize(raw["target"], repo_root),
    source: absolutize(raw["source"], repo_root),
    sha256: raw["sha256"],
    mode: raw["mode"],
  )
rescue JSON::ParserError, Errno::ENOENT
  nil
end

#sentinel_path(target, store_root) ⇒ Object



48
49
50
51
52
# File 'lib/textus/infra/port/sentinel_store.rb', line 48

def sentinel_path(target, store_root)
  repo_root = File.dirname(store_root)
  rel = relative_to(target, repo_root) || File.basename(target)
  File.join(Textus::Protocol::Layout.new(store_root).sentinels_root, rel + SUFFIX)
end

#targets_under(target_dir, store_root) ⇒ Object

Absolute target paths of every sentinel recorded under target_dir.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/textus/infra/port/sentinel_store.rb', line 55

def targets_under(target_dir, store_root)
  repo_root = File.dirname(store_root)
  rel = relative_to(target_dir, repo_root) or return []
  root = Textus::Protocol::Layout.new(store_root).sentinels_root
  sdir = File.join(root, rel)
  return [] unless @file_system.directory?(sdir)

  prefix = root + "/"
  @file_system.glob(File.join(sdir, "**", "*#{SUFFIX}")).map do |spath|
    # strip the sentinel-store prefix and the .textus-managed.json suffix to recover the repo-relative target path
    trel = spath.delete_prefix(prefix).delete_suffix(SUFFIX)
    File.join(repo_root, trel)
  end
end

#write!(target:, source:, store_root:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/textus/infra/port/sentinel_store.rb', line 24

def write!(target:, source:, store_root:)
  path = sentinel_path(target, store_root)
  @file_system.mkdir_p(File.dirname(path))
  repo_root = File.dirname(store_root)
  @file_system.write(path, JSON.generate(
                             "source" => rel_or_abs(source, repo_root),
                             "target" => rel_or_abs(target, repo_root),
                             "sha256" => Digest::SHA256.hexdigest(@file_system.read(target)),
                             "mode" => "copy",
                           ))
end