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.



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

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#cache_pathObject



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

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

#cache_version_unpacked!(version) ⇒ Object



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

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



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

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



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

def environment
  source.environment
end

#hexdigest(value) ⇒ Object



75
76
77
# File 'lib/librarian/puppet/source/githubtarball.rb', line 75

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

#install_version!(version, install_path) ⇒ Object



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

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



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

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

#vendor_cache(name, version) ⇒ Object



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

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)


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

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

#vendored_path(name, version) ⇒ Object



94
95
96
97
# File 'lib/librarian/puppet/source/githubtarball.rb', line 94

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



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

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

#versionsObject



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

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