Class: Librarian::Puppet::Source::GitHubTarball::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/librarian/puppet/source/githubtarball.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, name) ⇒ Repo

Returns a new instance of Repo.



14
15
16
17
# File 'lib/librarian/puppet/source/githubtarball.rb', line 14

def initialize(source, name)
  self.source = source
  self.name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/librarian/puppet/source/githubtarball.rb', line 11

def name
  @name
end

#sourceObject

Returns the value of attribute source.



11
12
13
# File 'lib/librarian/puppet/source/githubtarball.rb', line 11

def source
  @source
end

Instance Method Details

#cache_pathObject



65
66
67
# File 'lib/librarian/puppet/source/githubtarball.rb', line 65

def cache_path
  @cache_path ||= source.cache_path.join(name)
end

#cache_version_unpacked!(version) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/librarian/puppet/source/githubtarball.rb', line 77

def cache_version_unpacked!(version)
  path = version_unpacked_cache_path(version)
  return if path.directory?

  path.mkpath

  target = vendored?(source.uri, version) ? vendored_path(source.uri, version) : name

  `tar xzf #{target} -C #{path}`
end

#clean_up_old_cached_versions(name) ⇒ Object



105
106
107
108
109
# File 'lib/librarian/puppet/source/githubtarball.rb', line 105

def clean_up_old_cached_versions(name)
  Dir["#{environment.vendor_cache}/#{name.sub('/', '-')}*.tar.gz"].each do |old_version|
    FileUtils.rm old_version
  end
end

#environmentObject



61
62
63
# File 'lib/librarian/puppet/source/githubtarball.rb', line 61

def environment
  source.environment
end

#hexdigest(value) ⇒ Object



73
74
75
# File 'lib/librarian/puppet/source/githubtarball.rb', line 73

def hexdigest(value)
  Digest::MD5.hexdigest(value)
end

#install_version!(version, install_path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/librarian/puppet/source/githubtarball.rb', line 44

def install_version!(version, install_path)
  if environment.local? && !vendored?(source.uri, version)
    raise Error, "Could not find a local copy of #{source.uri} at #{version}."
  end

  vendor_cache(source.uri, version) unless vendored?(source.uri, version)

  cache_version_unpacked! version

  if install_path.exist?
    install_path.rmtree
  end

  unpacked_path = version_unpacked_cache_path(version).children.first
  FileUtils.cp_r(unpacked_path, install_path)
end

#manifestsObject



38
39
40
41
42
# File 'lib/librarian/puppet/source/githubtarball.rb', line 38

def manifests
  versions.map do |version|
    Manifest.new(source, name, version)
  end
end

#vendor_cache(name, version) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/librarian/puppet/source/githubtarball.rb', line 97

def vendor_cache(name, version)
  clean_up_old_cached_versions(name)

  url = "https://api.github.com/repos/#{name}/tarball/#{version}"
  url << "?access_token=#{ENV['GITHUB_API_TOKEN']}" if ENV['GITHUB_API_TOKEN']
  `curl #{url} -o #{vendored_path(name, version).to_s} -L 2>&1`
end

#vendored?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/librarian/puppet/source/githubtarball.rb', line 88

def vendored?(name, version)
  vendored_path(name, version).exist?
end

#vendored_path(name, version) ⇒ Object



92
93
94
95
# File 'lib/librarian/puppet/source/githubtarball.rb', line 92

def vendored_path(name, version)
  environment.vendor_cache.mkpath
  environment.vendor_cache.join("#{name.sub("/", "-")}-#{version}.tar.gz")
end

#version_unpacked_cache_path(version) ⇒ Object



69
70
71
# File 'lib/librarian/puppet/source/githubtarball.rb', line 69

def version_unpacked_cache_path(version)
  cache_path.join('version').join(hexdigest(version.to_s))
end

#versionsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/librarian/puppet/source/githubtarball.rb', line 19

def versions
  data = api_call("/repos/#{source.uri}/tags")
  if data.nil?
    raise Error, "Unable to find module '#{source.uri}' on https://github.com"
  end

  all_versions = data.map { |r| r['name'] }.sort.reverse

  all_versions = all_versions.map do |version|
    version.gsub(/^v/, '')
  end

  all_versions.delete_if do |version|
    version !~ /\A\d\.\d(\.\d.*)?\z/
  end

  all_versions.compact
end