Class: Beni::Vendor::Tarball
- Inherits:
-
Object
- Object
- Beni::Vendor::Tarball
- Defined in:
- lib/beni/vendor/tarball.rb,
sig/beni/vendor/tarball.rbs
Overview
Unpacks a vendored tarball into final_dir, idempotent on a
.beni-version marker stamped inside the tree. One instance per
(tarball, top_level_dir, final_dir, version) configuration; reuse is
not supported and not needed by Beni::Tasks.
A version mismatch (toolchain bump) forces a clean re-extract, so the
unpacked tree never lags the pinned version. Public contract is the
single #prepare entry point; the staging-directory step is internal.
Constant Summary collapse
- VERSION_MARKER =
Marker stamped inside
final_dirafter a successful unpack; a matching value short-circuits#prepare, a mismatch forces re-extract. ".beni-version"
Instance Method Summary collapse
- #extract_to_staging(staging) ⇒ void
-
#initialize(tarball:, top_level_dir:, final_dir:, version:) ⇒ Tarball
constructor
A new instance of Tarball.
-
#installed_version ⇒ String?
Version recorded by the last successful unpack, or
nilwhen the tree is absent or predates version stamping (forcing a re-extract). -
#prepare ⇒ void
Extract the tarball into a staging sibling of
final_dir, then atomically move thetop_level_dirsubtree into place and stamp the version marker. -
#promote(staging) ⇒ void
Move the expected
top_level_dirsubtree out ofstagingintofinal_dirand stamp the version marker.
Constructor Details
#initialize(tarball:, top_level_dir:, final_dir:, version:) ⇒ Tarball
Returns a new instance of Tarball.
20 21 22 23 24 25 |
# File 'lib/beni/vendor/tarball.rb', line 20 def initialize(tarball:, top_level_dir:, final_dir:, version:) @tarball = tarball @top_level_dir = top_level_dir @final_dir = final_dir @version = version end |
Instance Method Details
#extract_to_staging(staging) ⇒ void
This method returns an undefined value.
53 54 55 56 57 |
# File 'lib/beni/vendor/tarball.rb', line 53 def extract_to_staging(staging) FileUtils.rm_rf(staging) FileUtils.mkdir_p(staging) system("tar", "-xzf", @tarball, "-C", staging, exception: true) end |
#installed_version ⇒ String?
Version recorded by the last successful unpack, or nil when the tree
is absent or predates version stamping (forcing a re-extract).
48 49 50 51 |
# File 'lib/beni/vendor/tarball.rb', line 48 def installed_version marker = File.join(@final_dir, VERSION_MARKER) File.read(marker).strip if File.exist?(marker) end |
#prepare ⇒ void
This method returns an undefined value.
Extract the tarball into a staging sibling of final_dir, then
atomically move the top_level_dir subtree into place and stamp the
version marker. A no-op when the stamped version already matches.
Raises if the tarball does not contain the expected top_level_dir
root. The staging tree is removed whether or not extraction succeeds.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/beni/vendor/tarball.rb', line 32 def prepare return if installed_version == @version staging = "#{@final_dir}.staging" begin extract_to_staging(staging) promote(staging) ensure FileUtils.rm_rf(staging) end end |
#promote(staging) ⇒ void
This method returns an undefined value.
Move the expected top_level_dir subtree out of staging into
final_dir and stamp the version marker.
61 62 63 64 65 66 67 68 69 |
# File 'lib/beni/vendor/tarball.rb', line 61 def promote(staging) src = File.join(staging, @top_level_dir) raise Error, "[beni] expected #{src} after extracting #{@tarball}, missing" unless File.directory?(src) FileUtils.rm_rf(@final_dir) FileUtils.mkdir_p(File.dirname(@final_dir)) FileUtils.mv(src, @final_dir) File.write(File.join(@final_dir, VERSION_MARKER), "#{@version}\n") end |