Class: Inspec::Requirement

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/dependencies/requirement.rb

Overview

Inspec::Requirement represents a given profile dependency, where appropriate we delegate to Inspec::Profile directly.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version_constraints, vendor_index, cwd, opts) ⇒ Requirement



37
38
39
40
41
42
43
44
45
# File 'lib/inspec/dependencies/requirement.rb', line 37

def initialize(name, version_constraints, vendor_index, cwd, opts)
  @name = name
  @version_requirement = Gem::Requirement.new(Array(version_constraints))
  @dep = Gem::Dependency.new(name, @version_requirement, :runtime)
  @vendor_index = vendor_index
  @backend = opts[:backend]
  @opts = opts
  @cwd = cwd
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



12
13
14
# File 'lib/inspec/dependencies/requirement.rb', line 12

def cwd
  @cwd
end

#depObject (readonly)

Returns the value of attribute dep.



12
13
14
# File 'lib/inspec/dependencies/requirement.rb', line 12

def dep
  @dep
end

#dependenciesObject



99
100
101
102
103
# File 'lib/inspec/dependencies/requirement.rb', line 99

def dependencies
  @dependencies ||= profile..dependencies.map do |r|
    Inspec::Requirement.(r, @vendor_index, cwd: @cwd, backend: @backend)
  end
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/inspec/dependencies/requirement.rb', line 12

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



12
13
14
# File 'lib/inspec/dependencies/requirement.rb', line 12

def opts
  @opts
end

Class Method Details

.from_lock_entry(entry, cwd, vendor_index, backend) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/inspec/dependencies/requirement.rb', line 22

def self.from_lock_entry(entry, cwd, vendor_index, backend)
  req = new(entry[:name],
            entry[:version_constraints],
            vendor_index,
            cwd,
            entry[:resolved_source].merge(backend: backend))

  locked_deps = []
  Array(entry['dependencies']).each do |dep_entry|
    locked_deps << Inspec::Requirement.from_lock_entry(dep_entry, cwd, vendor_index, backend)
  end
  req.lock_deps(locked_deps)
  req
end

.from_metadata(dep, vendor_index, opts) ⇒ Object



15
16
17
18
19
20
# File 'lib/inspec/dependencies/requirement.rb', line 15

def self.(dep, vendor_index, opts)
  fail 'Cannot load empty dependency.' if dep.nil? || dep.empty?
  name = dep[:name] || fail('You must provide a name for all dependencies')
  version = dep[:version]
  new(name, version, vendor_index, opts[:cwd], opts.merge(dep))
end

Instance Method Details

#content_hashObject



84
85
86
87
88
89
90
91
# File 'lib/inspec/dependencies/requirement.rb', line 84

def content_hash
  @content_hash ||= begin
                      archive_path = @vendor_index.archive_entry_for(fetcher.cache_key) || fetcher.archive_path
                      if archive_path && File.file?(archive_path)
                        Digest::SHA256.hexdigest File.read(archive_path)
                      end
                    end
end

#fetcherObject



93
94
95
96
97
# File 'lib/inspec/dependencies/requirement.rb', line 93

def fetcher
  @fetcher ||= Inspec::Fetcher.resolve(opts)
  fail "No fetcher for #{name} (options: #{opts})" if @fetcher.nil?
  @fetcher
end

#lock_deps(dep_array) ⇒ Object



80
81
82
# File 'lib/inspec/dependencies/requirement.rb', line 80

def lock_deps(dep_array)
  @dependencies = dep_array
end

#profileObject



109
110
111
112
113
114
115
116
117
# File 'lib/inspec/dependencies/requirement.rb', line 109

def profile
  opts = @opts.dup
  opts[:cache] = @vendor_index
  opts[:backend] = @backend
  if !@dependencies.nil?
    opts[:dependencies] = Inspec::DependencySet.from_array(@dependencies, @cwd, @vendor_index, @backend)
  end
  @profile ||= Inspec::Profile.for_target(opts, opts)
end

#required_versionObject



47
48
49
# File 'lib/inspec/dependencies/requirement.rb', line 47

def required_version
  @version_requirement
end

#resolved_sourceObject



61
62
63
# File 'lib/inspec/dependencies/requirement.rb', line 61

def resolved_source
  @resolved_source ||= fetcher.resolved_source
end

#source_satisfies_spec?Boolean



55
56
57
58
59
# File 'lib/inspec/dependencies/requirement.rb', line 55

def source_satisfies_spec?
  name = profile..params[:name]
  version = profile..params[:version]
  @dep.match?(name, version)
end

#source_versionObject



51
52
53
# File 'lib/inspec/dependencies/requirement.rb', line 51

def source_version
  profile..params[:version]
end

#to_hashObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/inspec/dependencies/requirement.rb', line 65

def to_hash
  h = {
    'name' => name,
    'resolved_source' => resolved_source,
    'version_constraints' => @version_requirement.to_s,
  }

  if !dependencies.empty?
    h['dependencies'] = dependencies.map(&:to_hash)
  end

  h['content_hash'] = content_hash if content_hash
  h
end

#to_sObject



105
106
107
# File 'lib/inspec/dependencies/requirement.rb', line 105

def to_s
  "#{dep} (#{resolved_source})"
end