Class: ChefCLI::Policyfile::SolutionDependencies

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-cli/policyfile/solution_dependencies.rb

Defined Under Namespace

Classes: Cookbook

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSolutionDependencies

Returns a new instance of SolutionDependencies.



67
68
69
70
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 67

def initialize
  @policyfile_dependencies = []
  @cookbook_dependencies = {}
end

Instance Attribute Details

#cookbook_dependenciesObject (readonly)

Returns the value of attribute cookbook_dependencies.



65
66
67
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 65

def cookbook_dependencies
  @cookbook_dependencies
end

#policyfile_dependenciesObject (readonly)

Returns the value of attribute policyfile_dependencies.



63
64
65
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 63

def policyfile_dependencies
  @policyfile_dependencies
end

Class Method Details

.from_lock(lock_data) ⇒ Object



59
60
61
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 59

def self.from_lock(lock_data)
  new.tap { |e| e.consume_lock_data(lock_data) }
end

Instance Method Details

#add_cookbook_dep(cookbook_name, version, dependency_list) ⇒ Object



76
77
78
79
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 76

def add_cookbook_dep(cookbook_name, version, dependency_list)
  cookbook = Cookbook.new(cookbook_name, version)
  add_cookbook_obj_dep(cookbook, dependency_list)
end

#add_policyfile_dep(cookbook, constraint) ⇒ Object



72
73
74
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 72

def add_policyfile_dep(cookbook, constraint)
  @policyfile_dependencies << [ cookbook, Semverse::Constraint.new(constraint) ]
end

#consume_lock_data(lock_data) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 86

def consume_lock_data(lock_data)
  unless lock_data.key?("Policyfile") && lock_data.key?("dependencies")
    msg = %Q|lockfile solution_dependencies must be a Hash of the form `{"Policyfile": [], "dependencies": {} }' (got: #{lock_data.inspect})|
    raise InvalidLockfile, msg
  end

  set_policyfile_deps_from_lock_data(lock_data)
  set_cookbook_deps_from_lock_data(lock_data)
end

#cookbook_deps_for_lockObject



115
116
117
118
119
120
121
122
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 115

def cookbook_deps_for_lock
  cookbook_dependencies.inject({}) do |map, (cookbook, deps)|
    map[cookbook.to_s] = deps.map do |name, constraint|
      [ name, constraint.to_s ]
    end
    map
  end.sort.to_h
end

#policyfile_dependencies_for_lockObject



109
110
111
112
113
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 109

def policyfile_dependencies_for_lock
  policyfile_dependencies.map do |name, constraint|
    [ name, constraint.to_s ]
  end.sort
end

#test_conflict!(cookbook_name, version) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 96

def test_conflict!(cookbook_name, version)
  unless have_cookbook_dep?(cookbook_name, version)
    raise CookbookNotInWorkingSet, "Cookbook #{cookbook_name} (#{version}) not in the working set, cannot test for conflicts"
  end

  assert_cookbook_version_valid!(cookbook_name, version)
  assert_cookbook_deps_valid!(cookbook_name, version)
end

#to_lockObject



105
106
107
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 105

def to_lock
  { "Policyfile" => policyfile_dependencies_for_lock, "dependencies" => cookbook_deps_for_lock }
end

#transitive_deps(names) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 124

def transitive_deps(names)
  deps = Set.new
  to_explore = names.dup
  until to_explore.empty?
    ck_name = to_explore.shift
    next unless deps.add?(ck_name) # explore each ck only once

    my_deps = find_cookbook_dep_by_name(ck_name)
    dep_names = my_deps[1].map(&:first)
    to_explore += dep_names
  end
  deps.to_a.sort
end

#update_cookbook_dep(cookbook_name, new_version, new_dependency_list) ⇒ Object



81
82
83
84
# File 'lib/chef-cli/policyfile/solution_dependencies.rb', line 81

def update_cookbook_dep(cookbook_name, new_version, new_dependency_list)
  @cookbook_dependencies.delete_if { |cb, _deps| cb.name == cookbook_name }
  add_cookbook_dep(cookbook_name, new_version, new_dependency_list)
end