Class: Inspec::Requirement
- Inherits:
-
Object
- Object
- Inspec::Requirement
- 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
-
#cwd ⇒ Object
readonly
Returns the value of attribute cwd.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#required_version ⇒ Object
readonly
Returns the value of attribute required_version.
Class Method Summary collapse
- .from_lock_entry(entry, cwd, vendor_index, backend) ⇒ Object
- .from_metadata(dep, vendor_index, opts) ⇒ Object
Instance Method Summary collapse
- #content_hash ⇒ Object
- #dependencies ⇒ Object
- #fetcher ⇒ Object
- #gem_dep ⇒ Object
-
#initialize(name, version_constraints, vendor_index, cwd, opts) ⇒ Requirement
constructor
A new instance of Requirement.
- #lock_deps(dep_array) ⇒ Object
-
#name ⇒ Object
A dependency can be renamed in inspec.yml/inspec.lock.
- #profile ⇒ Object
- #resolved_source ⇒ Object
- #source_satisfies_spec? ⇒ Boolean
- #source_version ⇒ Object
- #to_hash ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, version_constraints, vendor_index, cwd, opts) ⇒ Requirement
Returns a new instance of Requirement.
33 34 35 36 37 38 39 40 |
# File 'lib/inspec/dependencies/requirement.rb', line 33 def initialize(name, version_constraints, vendor_index, cwd, opts) @name = name @required_version = Gem::Requirement.new(Array(version_constraints)) @vendor_index = vendor_index @backend = opts[:backend] @opts = opts @cwd = cwd end |
Instance Attribute Details
#cwd ⇒ Object (readonly)
Returns the value of attribute cwd.
32 33 34 |
# File 'lib/inspec/dependencies/requirement.rb', line 32 def cwd @cwd end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
32 33 34 |
# File 'lib/inspec/dependencies/requirement.rb', line 32 def opts @opts end |
#required_version ⇒ Object (readonly)
Returns the value of attribute required_version.
32 33 34 |
# File 'lib/inspec/dependencies/requirement.rb', line 32 def required_version @required_version end |
Class Method Details
.from_lock_entry(entry, cwd, vendor_index, backend) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/inspec/dependencies/requirement.rb', line 17 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
12 13 14 15 |
# File 'lib/inspec/dependencies/requirement.rb', line 12 def self.(dep, vendor_index, opts) fail 'Cannot load empty dependency.' if dep.nil? || dep.empty? new(dep[:name], dep[:version], vendor_index, opts[:cwd], opts.merge(dep)) end |
Instance Method Details
#content_hash ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/inspec/dependencies/requirement.rb', line 85 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 |
#dependencies ⇒ Object
100 101 102 103 104 |
# File 'lib/inspec/dependencies/requirement.rb', line 100 def dependencies @dependencies ||= profile..dependencies.map do |r| Inspec::Requirement.(r, @vendor_index, cwd: @cwd, backend: @backend) end end |
#fetcher ⇒ Object
94 95 96 97 98 |
# File 'lib/inspec/dependencies/requirement.rb', line 94 def fetcher @fetcher ||= Inspec::Fetcher.resolve(opts) fail "No fetcher for #{name} (options: #{opts})" if @fetcher.nil? @fetcher end |
#gem_dep ⇒ Object
58 59 60 |
# File 'lib/inspec/dependencies/requirement.rb', line 58 def gem_dep @gem_dep ||= Gem::Dependency.new(profile.name, required_version, :runtime) end |
#lock_deps(dep_array) ⇒ Object
81 82 83 |
# File 'lib/inspec/dependencies/requirement.rb', line 81 def lock_deps(dep_array) @dependencies = dep_array end |
#name ⇒ Object
A dependency can be renamed in inspec.yml/inspec.lock. Prefer the name the user gave this dependency over the profile name.
46 47 48 |
# File 'lib/inspec/dependencies/requirement.rb', line 46 def name @name || profile.name end |
#profile ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/inspec/dependencies/requirement.rb', line 110 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 |
#resolved_source ⇒ Object
62 63 64 |
# File 'lib/inspec/dependencies/requirement.rb', line 62 def resolved_source @resolved_source ||= fetcher.resolved_source end |
#source_satisfies_spec? ⇒ Boolean
54 55 56 |
# File 'lib/inspec/dependencies/requirement.rb', line 54 def source_satisfies_spec? gem_dep.match?(profile.name, profile.version) end |
#source_version ⇒ Object
50 51 52 |
# File 'lib/inspec/dependencies/requirement.rb', line 50 def source_version profile.version end |
#to_hash ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/inspec/dependencies/requirement.rb', line 66 def to_hash h = { 'name' => name, 'resolved_source' => resolved_source, 'version_constraints' => required_version.to_s, } if !dependencies.empty? h['dependencies'] = dependencies.map(&:to_hash) end h['content_hash'] = content_hash if content_hash h end |
#to_s ⇒ Object
106 107 108 |
# File 'lib/inspec/dependencies/requirement.rb', line 106 def to_s "#{name ? name : '<unfetched>'} (#{resolved_source})" end |