Class: PuppetfileResolver::ResolutionProvider

Inherits:
Object
  • Object
show all
Includes:
Molinillo::SpecificationProvider
Defined in:
lib/puppetfile-resolver/resolution_provider.rb

Instance Method Summary collapse

Constructor Details

#initialize(puppetfile_document, puppet_version, resolver_ui, options = {}) ⇒ ResolutionProvider

options

module_paths                : Array of paths (Deprecated)
strict_mode                 : [Boolean] Whether missing dependencies throw an error (default: false)
spec_searcher_configuration : PuppetfileResolver::SpecSearchers::Configuration


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 18

def initialize(puppetfile_document, puppet_version, resolver_ui, options = {})
  require 'semantic_puppet'

  @puppetfile_document = puppetfile_document
  raise 'The UI object must be of type Molinillo::UI' if resolver_ui.nil? || !resolver_ui.is_a?(Molinillo::UI)
  @resolver_ui = resolver_ui
  @spec_searcher_configuration = options[:spec_searcher_configuration] || PuppetfileResolver::SpecSearchers::Configuration.new
  @allow_missing_modules = options[:allow_missing_modules].nil? ? true : options[:allow_missing_modules] == true
  # There can be only one puppet specification in existance so we pre-load here.
  @puppet_specification = Models::PuppetSpecification.new(puppet_version)
  @module_info = {}
  @cache = options[:cache].nil? ? Cache::Base.new : options[:cache]

  # Check for deprecated options
  unless options[:module_paths].nil? # rubocop:disable Style/GuardClause
    Warning.warn 'The use of the module_paths option has been deprecated'
    @spec_searcher_configuration.local.puppet_module_paths = options[:module_paths]
  end
end

Instance Method Details

#allow_missing?(dependency) ⇒ Boolean

Returns whether this dependency, which has no possible matching specifications, can safely be ignored.

Parameters:

  • dependency (Object)

Returns:

  • (Boolean)

    whether this dependency can safely be skipped.



127
128
129
130
131
132
133
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 127

def allow_missing?(dependency)
  # Puppet dependencies must _always_ be resolvable
  return false if dependency.is_a?(Models::PuppetDependency)
  # Explicit Puppetfile dependencies must _always_ be resolvable
  return false if dependency.is_a?(Models::PuppetfileDependency)
  @allow_missing_modules
end

#dependencies_for(specification) ⇒ Array<Object>

Note:

This method should be ‘pure’, i.e. the return value should depend only on the ‘specification` parameter.

Returns the dependencies of ‘specification`.

Parameters:

  • specification (Object)

Returns:

  • (Array<Object>)

    the dependencies that are required by the given ‘specification`.



77
78
79
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 77

def dependencies_for(specification)
  specification.dependencies(@cache, @resolver_ui)
end

#find_puppet_specifications(dependency) ⇒ Object



65
66
67
68
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 65

def find_puppet_specifications(dependency)
  # Puppet specifications are a bit special as there can be only one (Highlander style)
  dependency.satisified_by?(@puppet_specification) ? [@puppet_specification] : []
end

#name_for(dependency) ⇒ String

Note:

This method should be ‘pure’, i.e. the return value should depend only on the ‘dependency` parameter.

Returns the name for the given ‘dependency`.

Parameters:

  • dependency (Object)

Returns:

  • (String)

    the name for the given ‘dependency`.



87
88
89
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 87

def name_for(dependency)
  dependency.name
end

#name_for_explicit_dependency_sourceObject



104
105
106
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 104

def name_for_explicit_dependency_source
  'Puppetfile'
end

#name_for_locking_dependency_sourceObject



108
109
110
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 108

def name_for_locking_dependency_source
  'Puppetfile'
end

#requirement_satisfied_by?(requirement, _activated, spec) ⇒ Boolean

Determines whether the given ‘requirement` is satisfied by the given `spec`, in the context of the current `activated` dependency graph.

Parameters:

  • requirement (Object)
  • activated (DependencyGraph)

    the current dependency graph in the resolution process.

  • spec (Object)

Returns:

  • (Boolean)

    whether ‘requirement` is satisfied by `spec` in the context of the current `activated` dependency graph.



100
101
102
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 100

def requirement_satisfied_by?(requirement, _activated, spec)
  requirement.satisified_by?(spec)
end

#search_for(dependency) ⇒ Array<Object>

Note:

This method should be ‘pure’, i.e. the return value should depend only on the ‘dependency` parameter.

Search for the specifications that match the given dependency. The specifications in the returned array will be considered in reverse order, so the latest version ought to be last.

Parameters:

  • dependency (Object)

Returns:

  • (Array<Object>)

    the specifications that satisfy the given ‘dependency`.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 47

def search_for(dependency)
  case dependency
  when Models::PuppetDependency
    result = find_puppet_specifications(dependency)
  when Models::ModuleDependency
    result = find_all_module_specifications(dependency).select do |spec|
      dependency.satisified_by?(spec)
    end
  else
    # No idea how we got here?!?!
    raise ArgumentError, "Unknown Dependency type #{dependency.class}"
  end

  return result if result.empty? || result.count == 1
  # Reverse sort by version
  result.sort! { |a, b| a.version > b.version ? 1 : -1 }
end

#sort_dependencies(dependencies, activated, conflicts) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument You’re drunk rubocop



112
113
114
115
116
117
118
119
120
# File 'lib/puppetfile-resolver/resolution_provider.rb', line 112

def sort_dependencies(dependencies, activated, conflicts) # rubocop:disable Lint/UnusedMethodArgument You're drunk rubocop
  dependencies.sort_by do |dependency|
    name = name_for(dependency)
    [
      activated.vertex_named(name).payload ? 0 : 1,
      conflicts[name] ? 0 : 1
    ]
  end
end