Module: Pray::Archive

Defined in:
lib/pray/archive.rb

Class Method Summary collapse

Class Method Details

.build_package_archive_bytes(package) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pray/archive.rb', line 12

def build_package_archive_bytes(package)
  prayspec_path = Resolve.find_prayspec_file(package.root)
  prayspec_name = File.basename(prayspec_path)
   = (package)

  Dir.mktmpdir("pray-package-") do |staging|
    File.write(File.join(staging, "metadata.json"), )
    File.write(File.join(staging, prayspec_name), File.binread(prayspec_path))
    package.spec.files.each do |file|
      destination = File.join(staging, file)
      FileUtils.mkdir_p(File.dirname(destination))
      File.binwrite(destination, File.binread(File.join(package.root, file)))
    end

    tar_bytes, status = Open3.capture2("tar", "-cf", "-", "-C", staging, ".")
    raise Error.integrity("failed to build package tar archive") unless status.success?

    zstd_bytes, status = Open3.capture2("zstd", "-q", "-c", stdin_data: tar_bytes)
    unless status.success?
      raise Error.unsupported("zstd is required to build package archives")
    end

    zstd_bytes
  end
end

.package_archive_path(package_name, version) ⇒ Object



54
55
56
57
# File 'lib/pray/archive.rb', line 54

def package_archive_path(package_name, version)
  slug = package_name.tr("/", "-")
  File.join(".pray", "packages", "#{slug}-#{version}.praypkg")
end

.package_metadata_json(package) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/pray/archive.rb', line 59

def (package)
  JSON.generate(
    "name" => package.spec.name,
    "version" => package.spec.version,
    "tree_hash" => package.tree_hash,
    "exports" => package.selected_exports
  )
end

.unpack_praypkg(artifact_bytes, output_directory) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/pray/archive.rb', line 43

def unpack_praypkg(artifact_bytes, output_directory)
  FileUtils.mkdir_p(output_directory)
  tar_bytes, status = Open3.capture2("zstd", "-d", "-q", "-c", stdin_data: artifact_bytes)
  unless status.success?
    raise Error.unsupported("zstd is required to unpack package archives")
  end

  _stdout, _stderr, status = Open3.capture3("tar", "-xf", "-", "-C", output_directory, stdin_data: tar_bytes)
  raise Error.integrity("failed to unpack package archive") unless status.success?
end

.write_package_archive(package, output_path) ⇒ Object



38
39
40
41
# File 'lib/pray/archive.rb', line 38

def write_package_archive(package, output_path)
  FileUtils.mkdir_p(File.dirname(output_path))
  File.binwrite(output_path, build_package_archive_bytes(package))
end