Class: Faith::Task

Inherits:
Object
  • Object
show all
Includes:
Named
Defined in:
lib/faith/task.rb

Direct Known Subclasses

Namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Named

#full_name, #root?

Constructor Details

#initialize(name, parent, mixins: [], dependencies: [], &action) ⇒ Task

Returns a new instance of Task.



3
4
5
6
7
8
9
# File 'lib/faith/task.rb', line 3

def initialize(name, parent, mixins: [], dependencies: [], &action)
  @name = name
  @parent = parent
  @mixins = mixins
  @dependencies = dependencies
  @action = action
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



11
12
13
# File 'lib/faith/task.rb', line 11

def action
  @action
end

#dependenciesObject

Returns the value of attribute dependencies.



11
12
13
# File 'lib/faith/task.rb', line 11

def dependencies
  @dependencies
end

#mixinsObject

Returns the value of attribute mixins.



11
12
13
# File 'lib/faith/task.rb', line 11

def mixins
  @mixins
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/faith/task.rb', line 11

def name
  @name
end

#parentObject

Returns the value of attribute parent.



11
12
13
# File 'lib/faith/task.rb', line 11

def parent
  @parent
end

Instance Method Details

#child(name_to_resolve) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/faith/task.rb', line 97

def child(name_to_resolve)
  if is_a?(Namespace)
    children.find { |child| child.name == name_to_resolve }
  else
    nil
  end
end

#ensure_all_resolved(objs) ⇒ Object



59
60
61
# File 'lib/faith/task.rb', line 59

def ensure_all_resolved(objs)
  objs.map { |obj| ensure_resolved(obj) }
end

#ensure_resolved(obj) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/faith/task.rb', line 63

def ensure_resolved(obj)
  if obj.is_a?(String)
    resolve(obj)
  elsif obj.is_a?(Task) || obj.is_a?(Mixin)
    obj
  else
    raise "cannot resolve using #{obj} (of type #{obj.class})"
  end
end

#resolve(name_to_resolve) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/faith/task.rb', line 73

def resolve(name_to_resolve)
  parts = name_to_resolve.split(':')
  raise 'empty name' if parts.length.zero?

  current = self
  until parts.empty?
    current_child = current.child(parts.shift)
    
    # If nil, give up finding here
    if current_child.nil?
      if parent.nil?
        raise "could not resolve #{name_to_resolve}"
      else
        return parent.resolve(name_to_resolve)
      end
    end

    current = current_child
  end

  # If we actually managed to get parts to be empty, then we found what we were looking for
  current
end

#resolve_self!Object



54
55
56
57
# File 'lib/faith/task.rb', line 54

def resolve_self!
  @mixins = ensure_all_resolved(@mixins)
  @dependencies = ensure_all_resolved(@dependencies)
end

#run(context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
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/faith/task.rb', line 15

def run(context)
  context.tasks_executed << self
  
  # Run dependencies, if not run before
  any_dependencies = dependencies.reject { |dep| context.ran?(dep) }.any?
  if any_dependencies
    context.output.dependencies(self)
    context.output.indent
  end
  dependencies.each do |dep|
    unless context.ran?(dep)
      dep.run(context) 
    end
  end
  context.output.dedent if any_dependencies

  # Instantiate mixins
  new_mixin_instances = mixins.map { |m| m.instantiate(context) }
  new_mixin_instances.each do |m|
    context.output.mixin(m.mixin)
    context.output.indent
    context.output.mixin_action("before")
    m.instance_exec(context, &m.mixin.before) unless m.mixin.before.nil?
    context.mixin_instances << m
  end

  # Run this task
  context.instance_exec(context, &action)
  context.output.run(self) unless is_a?(Sequence)

  # Tear down mixins
  new_mixin_instances.each do |m|
    context.output.mixin_action("after")
    m.instance_exec(context, &m.mixin.after) unless m.mixin.after.nil?
    context.output.dedent
    context.mixin_instances.delete(m)
  end
end