Class: Inspec::DependencySet

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

Overview

A DependencySet manages a list of dependencies for a profile.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cwd, cache, dep_list, backend) ⇒ dependencies

initialize

Parameters:

  • cwd (String)

    current working directory for relative path includes

  • vendor_path (String)

    path which contains vendored dependencies



59
60
61
62
63
64
# File 'lib/inspec/dependencies/dependency_set.rb', line 59

def initialize(cwd, cache, dep_list, backend)
  @cwd = cwd
  @cache = cache
  @dep_list = dep_list
  @backend = backend
end

Instance Attribute Details

#dep_listObject

Returns the value of attribute dep_list.



53
54
55
# File 'lib/inspec/dependencies/dependency_set.rb', line 53

def dep_list
  @dep_list
end

#vendor_pathObject (readonly)

Returns the value of attribute vendor_path.



52
53
54
# File 'lib/inspec/dependencies/dependency_set.rb', line 52

def vendor_path
  @vendor_path
end

Class Method Details

.flatten_dep_tree(dep_tree) ⇒ Object

This is experimental code to test the working of the dependency loader - perform a proper dependency related search in the future.

Flatten tree because that is all we know how to deal with for right now. Last dep seen for a given name wins right now.



41
42
43
44
45
46
47
48
49
50
# File 'lib/inspec/dependencies/dependency_set.rb', line 41

def self.flatten_dep_tree(dep_tree)
  dep_list = {}
  dep_tree.each do |d|
    # if depedent profile does not have a source version then only name is used in dependency hash
    key_name = ((d.source_version.nil? || d.source_version.empty?) ? "#{d.name}" : "#{d.name}-#{d.source_version}") rescue "#{d.name}"
    dep_list[key_name] = d
    dep_list.merge!(flatten_dep_tree(d.dependencies))
  end
  dep_list
end

.from_array(dependencies, cwd, cache, backend) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/inspec/dependencies/dependency_set.rb', line 25

def self.from_array(dependencies, cwd, cache, backend)
  dep_list = {}
  dependencies.each do |d|
    # if depedent profile does not have a source version then only name is used in dependency hash
    key_name = ((d.source_version.nil? || d.source_version.empty?) ? "#{d.name}" : "#{d.name}-#{d.source_version}") rescue "#{d.name}"
    dep_list[key_name] = d
  end
  new(cwd, cache, dep_list, backend)
end

.from_lockfile(lockfile, config, opts = {}) ⇒ Object

Return a dependency set given a lockfile.

Parameters:

  • lockfile (Inspec::Lockfile)

    A lockfile to generate the dependency set from

  • cwd (String)

    Current working directory for relative path includes

  • vendor_path (String)

    Path to the vendor directory



16
17
18
19
20
21
22
23
# File 'lib/inspec/dependencies/dependency_set.rb', line 16

def self.from_lockfile(lockfile, config, opts = {})
  dep_tree = lockfile.deps.map do |dep|
    Inspec::Requirement.from_lock_entry(dep, config, opts)
  end

  dep_list = flatten_dep_tree(dep_tree)
  new(config[:cwd], config[:cache], dep_list, config[:backend])
end

Instance Method Details

#eachObject



66
67
68
69
70
# File 'lib/inspec/dependencies/dependency_set.rb', line 66

def each
  @dep_list.each do |_k, v|
    yield v.profile
  end
end

#listObject



72
73
74
# File 'lib/inspec/dependencies/dependency_set.rb', line 72

def list
  @dep_list || {}
end

#to_arrayObject



76
77
78
79
80
81
82
# File 'lib/inspec/dependencies/dependency_set.rb', line 76

def to_array
  return [] if @dep_list.nil?

  @dep_list.map do |_k, v|
    v.to_hash
  end.compact
end

#vendor(dependencies) ⇒ nil

  1. Get dependencies, pull things to a local cache if necessary

  2. Resolve dependencies

Parameters:

  • dependencies (Gem::Dependency)

    list of dependencies

Returns:

  • (nil)


91
92
93
94
95
# File 'lib/inspec/dependencies/dependency_set.rb', line 91

def vendor(dependencies)
  return nil if dependencies.nil? || dependencies.empty?

  @dep_list = Resolver.resolve(dependencies, @cache, @cwd, @backend)
end