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

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

Constant Summary collapse

TOKEN_KEY =
'GITHUB_API_TOKEN'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, name) ⇒ Repo

Returns a new instance of Repo.



17
18
19
20
# File 'lib/librarian/puppet/source/githubtarball.rb', line 17

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#cache_pathObject



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

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

#cache_version_unpacked!(version) ⇒ Object



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

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



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

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



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

def environment
  source.environment
end

#hexdigest(value) ⇒ Object



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

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

#install_version!(version, install_path) ⇒ Object



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

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



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

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

#vendor_cache(name, version) ⇒ Object



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

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)


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

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

#vendored_path(name, version) ⇒ Object



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

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



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

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

#versionsObject



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

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'].gsub(/^v/, '') }.sort.reverse

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

  all_versions.compact
end