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
rubocop:disable Metrics/ClassLength.
-
#dep ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
- #dependencies ⇒ Object
-
#name ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
-
#opts ⇒ Object
readonly
rubocop:disable Metrics/ClassLength.
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
- #fetcher ⇒ Object
-
#initialize(name, version_constraints, vendor_index, cwd, opts) ⇒ Requirement
constructor
A new instance of Requirement.
- #is_vendored? ⇒ Boolean
- #local_path ⇒ Object
- #lock_deps(dep_array) ⇒ Object
- #path ⇒ Object
- #profile ⇒ Object
- #pull ⇒ Object
- #required_version ⇒ Object
- #source_satisfies_spec? ⇒ Boolean
- #source_url ⇒ Object
- #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.
38 39 40 41 42 43 44 45 46 |
# File 'lib/inspec/dependencies/requirement.rb', line 38 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
#cwd ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
12 13 14 |
# File 'lib/inspec/dependencies/requirement.rb', line 12 def cwd @cwd end |
#dep ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
12 13 14 |
# File 'lib/inspec/dependencies/requirement.rb', line 12 def dep @dep end |
#dependencies ⇒ Object
127 128 129 130 131 |
# File 'lib/inspec/dependencies/requirement.rb', line 127 def dependencies @dependencies ||= profile..dependencies.map do |r| Inspec::Requirement.(r, @vendor_index, cwd: @cwd, backend: @backend) end end |
#name ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
12 13 14 |
# File 'lib/inspec/dependencies/requirement.rb', line 12 def name @name end |
#opts ⇒ Object (readonly)
rubocop:disable Metrics/ClassLength
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 36 |
# 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, { url: entry['resolved_source'], 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_hash ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/inspec/dependencies/requirement.rb', line 87 def content_hash @content_hash ||= begin archive_path = @vendor_index.archive_entry_for(@name, source_url) fail "No vendored archive path for #{self}, cannot take content hash" if archive_path.nil? Digest::SHA256.hexdigest File.read(archive_path) end end |
#fetcher ⇒ Object
111 112 113 114 115 |
# File 'lib/inspec/dependencies/requirement.rb', line 111 def fetcher @fetcher ||= Inspec::Fetcher.resolve(source_url) fail "No fetcher for #{name} (options: #{opts})" if @fetcher.nil? @fetcher end |
#is_vendored? ⇒ Boolean
83 84 85 |
# File 'lib/inspec/dependencies/requirement.rb', line 83 def is_vendored? @vendor_index.exists?(@name, source_url) end |
#local_path ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/inspec/dependencies/requirement.rb', line 103 def local_path @local_path ||= if fetcher.class == Fetchers::Local File.(fetcher.target, @cwd) else @vendor_index.prefered_entry_for(@name, source_url) end end |
#lock_deps(dep_array) ⇒ Object
79 80 81 |
# File 'lib/inspec/dependencies/requirement.rb', line 79 def lock_deps(dep_array) @dependencies = dep_array end |
#path ⇒ Object
137 138 139 |
# File 'lib/inspec/dependencies/requirement.rb', line 137 def path @path ||= pull end |
#profile ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/inspec/dependencies/requirement.rb', line 141 def profile return nil if path.nil? opts = { backend: @backend } if !@dependencies.nil? opts[:dependencies] = Inspec::DependencySet.from_array(@dependencies, @cwd, @vendor_index, @backend) end @profile ||= Inspec::Profile.for_target(path, opts) end |
#pull ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/inspec/dependencies/requirement.rb', line 117 def pull # TODO(ssd): Dispatch on the class here is gross. Seems like # Fetcher is missing an API we want. if fetcher.class == Fetchers::Local || @vendor_index.exists?(@name, source_url) local_path else @vendor_index.add(@name, source_url, fetcher.archive_path) end end |
#required_version ⇒ Object
48 49 50 |
# File 'lib/inspec/dependencies/requirement.rb', line 48 def required_version @version_requirement end |
#source_satisfies_spec? ⇒ Boolean
56 57 58 59 60 |
# File 'lib/inspec/dependencies/requirement.rb', line 56 def source_satisfies_spec? name = profile..params[:name] version = profile..params[:version] @dep.match?(name, version) end |
#source_url ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/inspec/dependencies/requirement.rb', line 95 def source_url if opts[:path] "file://#{File.expand_path(opts[:path], @cwd)}" elsif opts[:url] opts[:url] end end |
#source_version ⇒ Object
52 53 54 |
# File 'lib/inspec/dependencies/requirement.rb', line 52 def source_version profile..params[:version] end |
#to_hash ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/inspec/dependencies/requirement.rb', line 62 def to_hash h = { 'name' => name, 'resolved_source' => source_url, 'version_constraints' => @version_requirement.to_s, } if !dependencies.empty? h['dependencies'] = dependencies.map(&:to_hash) end if is_vendored? h['content_hash'] = content_hash end h end |
#to_s ⇒ Object
133 134 135 |
# File 'lib/inspec/dependencies/requirement.rb', line 133 def to_s "#{dep} (#{source_url})" end |