Class: Vop::DependencyResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/vop/parts/dependency_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(op) ⇒ DependencyResolver

Returns a new instance of DependencyResolver.


5
6
7
# File 'lib/vop/parts/dependency_resolver.rb', line 5

def initialize(op)
  @op = op
end

Instance Method Details

#resolve(plugin, resolved, unresolved, level = 0) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vop/parts/dependency_resolver.rb', line 29

def resolve(plugin, resolved, unresolved, level = 0)
  $logger.debug "#{' ' * level}checking dependencies for #{plugin.name}"
  unresolved << plugin.name

  plugin.dependencies.each do |dep|
    $logger.debug "#{' ' * level}resolving #{dep}"
    already_loaded = @op.plugins.map(&:name).include? dep
    unless already_loaded
      unless resolved.include? dep
        if unresolved.include? dep
          raise ::Vop::Errors::RunningInCircles, "circular dependency #{plugin.name} -> #{dep}"
        else
          unless @plugins.has_key? dep
            raise ::Vop::Errors::MissingPlugin, "dependency not met: #{plugin.name} depends on #{dep}"
          end
          dependency = @plugins[dep]
          resolve(dependency, resolved, unresolved, level + 1)
        end
      end
    end
  end

  resolved << plugin.name
end

#sort(plugins) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vop/parts/dependency_resolver.rb', line 9

def sort(plugins)
  @plugins = {}
  plugins.each do |plugin|
    @plugins[plugin.name] = plugin
  end

  resolved = []
  unresolved = []

  root_plugin = Plugin.new(@op, "__root__", nil)
  root_plugin.dependencies = @plugins.keys

  $logger.debug "root dummy : #{root_plugin}"

  resolve(root_plugin, resolved, unresolved)
  resolved.delete_if { |x| x == root_plugin.name }

  resolved.map { |x| @plugins[x] }
end