Module: Asgard::Base::DependencyGraph

Included in:
Asgard::Base
Defined in:
lib/asgard/base/dependency_graph.rb

Overview

Dependency declaration (depends_on) and full-graph validation (validate_deps!), backed by stdlib TSort for cycle detection.

Instance Method Summary collapse

Instance Method Details

#_depsObject



8
9
10
# File 'lib/asgard/base/dependency_graph.rb', line 8

def _deps
  @_deps ||= {}
end

#depends_on(*tasks, &block) ⇒ Object

Declare dependencies for the next task. Bare symbols run sequentially; arrays within the splat run in parallel.

depends_on :build                          # sequential
depends_on :build, :lint                   # both sequential
depends_on [:build, :lint]                 # build and lint in parallel
depends_on :setup, [:build, :lint], :test  # setup, then build+lint, then test

A sole Proc/lambda, or a block in place of the splat args, defers resolution to validate_deps! (after every .loki file has loaded), instead of now. It must return the same shape the splat form above would: an array of stages, each a Symbol (sequential) or Array (parallel group).

depends_on -> { [all_commands.keys.grep(/_check\z/).map(&:to_sym)] }
depends_on { [all_commands.keys.grep(/_check\z/).map(&:to_sym)] }


28
29
30
31
32
33
34
35
36
# File 'lib/asgard/base/dependency_graph.rb', line 28

def depends_on(*tasks, &block)
  if block
    raise Asgard::Error, "depends_on accepts either task arguments or a block, not both" if tasks.any?

    tasks = [block]
  end

  @_pending_deps = tasks
end

#validate_deps!Object

Validate the full dep graph for cycles using stdlib TSort.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/asgard/base/dependency_graph.rb', line 39

def validate_deps!
  _check_orphaned_deps!
  return if _deps.empty?

  _resolve_lazy_deps!
  all_task_names = all_commands.keys.map(&:to_sym)
  _check_undefined_deps!(all_task_names)
  _check_dep_arities!
  _build_and_sort_graph(all_task_names)
rescue TSort::Cyclic => e
  raise Asgard::CircularDependencyError, e.message
end