Class: Gembrew::GithubArchiveStore

Inherits:
Object
  • Object
show all
Defined in:
lib/gembrew/github_archive_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache_path:, reporter: Reporter.new, command_runner: nil) ⇒ GithubArchiveStore

Returns a new instance of GithubArchiveStore.



9
10
11
12
13
14
# File 'lib/gembrew/github_archive_store.rb', line 9

def initialize(cache_path:, reporter: Reporter.new, command_runner: nil)
  @cache_path = Pathname(cache_path)
  @reporter = reporter
  @command_runner = command_runner || method(:run_command)
  @cache_path.mkpath
end

Instance Method Details

#extract(archive, destination) ⇒ Object



33
34
35
36
37
38
# File 'lib/gembrew/github_archive_store.rb', line 33

def extract(archive, destination)
  destination = Pathname(destination)
  destination.mkpath
  command_runner.call 'tar', '-xzf', archive.to_s, '-C', destination.to_s, chdir: destination
  destination
end

#fetch(repository, tag) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gembrew/github_archive_store.rb', line 16

def fetch(repository, tag)
  name = repository.tr '/', '-'
  path = @cache_path/"#{name}-#{tag.tr '/', '-'}.tar.gz"
  if path.file? && !path.empty?
    reporter.archive :github_cache, repository, tag
    return path
  end

  reporter.archive :downloading, repository, tag
  url = "https://github.com/#{repository}/archive/refs/tags/#{tag}.tar.gz"
  command_runner.call 'curl', '--fail', '--location', '--silent', '--show-error',
    '--output', path.to_s, url, chdir: @cache_path
  raise Error, "GitHub archive is empty: #{url}" unless path.file? && !path.empty?

  path
end