Class: Librarian::Puppet::Source::Forge::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/librarian/puppet/source/forge.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/forge.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/forge.rb', line 11

def name
  @name
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#cache_pathObject



62
63
64
# File 'lib/librarian/puppet/source/forge.rb', line 62

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

#cache_version_unpacked!(version) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/librarian/puppet/source/forge.rb', line 74

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

  path.mkpath

  target = vendored?(name, version) ? vendored_path(name, version) : name

  `puppet module install --target-dir #{path} --modulepath #{path} --ignore-dependencies #{target}`
end

#dependencies(version) ⇒ Object



28
29
30
31
# File 'lib/librarian/puppet/source/forge.rb', line 28

def dependencies(version)
  data = api_call("api/v1/releases.json?module=#{name}&version=#{version}")
  data[name].first['dependencies']
end

#download(name, version, &block) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/librarian/puppet/source/forge.rb', line 101

def download(name, version, &block)
  data = api_call("api/v1/releases.json?module=#{name}&version=#{version}")

  info = data[name].detect {|h| h['version'] == version.to_s }

  stream(info['file'], &block)
end

#environmentObject



58
59
60
# File 'lib/librarian/puppet/source/forge.rb', line 58

def environment
  source.environment
end

#hexdigest(value) ⇒ Object



70
71
72
# File 'lib/librarian/puppet/source/forge.rb', line 70

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

#install_version!(version, install_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/librarian/puppet/source/forge.rb', line 39

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

  if environment.vendor?
    vendor_cache(name, version) unless vendored?(name, version)
  end

  cache_version_unpacked! version

  if install_path.exist?
    install_path.rmtree
  end

  unpacked_path = version_unpacked_cache_path(version).join(name.split('/').last)
  FileUtils.cp_r(unpacked_path, install_path)
end

#manifestsObject



33
34
35
36
37
# File 'lib/librarian/puppet/source/forge.rb', line 33

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

#stream(file, &block) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/librarian/puppet/source/forge.rb', line 109

def stream(file, &block)
  Net::HTTP.get_response(URI.parse("#{source}#{file}")) do |res|
    res.code

    res.read_body(&block)
  end
end

#vendor_cache(name, version) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/librarian/puppet/source/forge.rb', line 93

def vendor_cache(name, version)
  File.open(vendored_path(name, version).to_s, 'w') do |f|
    download(name, version) do |data|
      f << data
    end
  end
end

#vendored?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/librarian/puppet/source/forge.rb', line 85

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

#vendored_path(name, version) ⇒ Object



89
90
91
# File 'lib/librarian/puppet/source/forge.rb', line 89

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

#version_unpacked_cache_path(version) ⇒ Object



66
67
68
# File 'lib/librarian/puppet/source/forge.rb', line 66

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
# File 'lib/librarian/puppet/source/forge.rb', line 19

def versions
  data = api_call("#{name}.json")
  if data.nil?
    raise Error, "Unable to find module '#{name}' on #{source}"
  end

  data['releases'].map { |r| r['version'] }.sort.reverse
end