Class: Abid::Task

Inherits:
Rake::Task
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/abid/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_name, app) ⇒ Task

Returns a new instance of Task.



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

def initialize(task_name, app)
  super(task_name, app)
  @siblings = {}
end

Instance Attribute Details

#extendsObject

Returns the value of attribute extends.



5
6
7
# File 'lib/abid/task.rb', line 5

def extends
  @extends
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/abid/task.rb', line 6

def params
  @params
end

#playObject (readonly)

Returns the value of attribute play.



6
7
8
# File 'lib/abid/task.rb', line 6

def play
  @play
end

#play_class_definitionObject

Returns the value of attribute play_class_definition.



5
6
7
# File 'lib/abid/task.rb', line 5

def play_class_definition
  @play_class_definition
end

Class Method Details

.define_play(*args, &block) ⇒ Object

:nodoc:



130
131
132
# File 'lib/abid/task.rb', line 130

def define_play(*args, &block) # :nodoc:
  Rake.application.define_play(self, *args, &block)
end

Instance Method Details

#bind(**params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/abid/task.rb', line 28

def bind(**params)
  fail 'already bound' if bound?

  parsed_params = ParamsParser.parse(params, play_class.params_spec)
  return @siblings[parsed_params] if @siblings.include?(parsed_params)

  sorted_params = parsed_params.sort.to_h
  sorted_params.freeze

  @siblings[sorted_params] = dup.tap do |t|
    t.instance_eval do
      @prerequisites = []
      @params = sorted_params
      @play = play_class.new(t)
      call_play_hooks(:setup)
      bind_play_hooks(:before, :before_execute)
      bind_play_hooks(:after, :after_invoke)
    end
  end
end

#bind_play_hooks(tag, to = nil) ⇒ Object



119
120
121
122
# File 'lib/abid/task.rb', line 119

def bind_play_hooks(tag, to = nil)
  to ||= tag
  hooks[to] = [proc { |*args| call_play_hooks(tag, *args) }]
end

#bound?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/abid/task.rb', line 24

def bound?
  !@play.nil?
end

#call_play_hooks(tag, *args) ⇒ Object



124
125
126
127
# File 'lib/abid/task.rb', line 124

def call_play_hooks(tag, *args)
  return unless bound?
  play_class.hooks[tag].each { |blk| play.instance_exec(*args, &blk) }
end

#concerned?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
# File 'lib/abid/task.rb', line 105

def concerned?
  state.reload

  if !application.options.repair && state.failed? && !top_level?
    fail "#{name} -- task has been failed"
  end

  application.options.repair || !state.successed?
end

#execute(_args = nil) ⇒ Object

Execute the play associated with this task.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/abid/task.rb', line 91

def execute(_args = nil)
  fail 'no play is bound yet' unless bound?

  if application.options.dryrun
    application.trace "** Execute (dry run) #{name_with_params}"
    return
  end
  if application.options.trace
    application.trace "** Execute #{name_with_params}"
  end

  play.run
end

#name_with_argsObject

Name of task with argument list description.



58
59
60
61
62
63
64
# File 'lib/abid/task.rb', line 58

def name_with_args # :nodoc:
  if params_description
    "#{super} #{params_description}"
  else
    super
  end
end

#name_with_paramsObject

Name of task with params



67
68
69
70
71
72
73
# File 'lib/abid/task.rb', line 67

def name_with_params # :nodoc:
  if params_description
    "#{name} #{params_description}"
  else
    super
  end
end

#needed?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/abid/task.rb', line 115

def needed?
  !state.successed? || prerequisite_tasks.any? { |t| t.session.successed? }
end

#params_descriptionObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/abid/task.rb', line 75

def params_description
  sig_params = play_class.params_spec.select do |_, spec|
    spec[:significant]
  end
  return if sig_params.empty?

  if bound? # unbound
    p = sig_params.map { |name, _| "#{name}=#{params[name]}" }
  else
    p = sig_params.map { |name, spec| "#{name}:#{spec[:type]}" }
  end

  p.join(' ')
end

#play_classObject



15
16
17
18
19
20
21
22
# File 'lib/abid/task.rb', line 15

def play_class
  return @play_class if @play_class

  klass = application.lookup_play_class(extends, scope)
  @play_class = Class.new(klass, &play_class_definition).tap do |c|
    c.task = self
  end
end

#prerequisite_tasksObject



49
50
51
52
53
54
55
# File 'lib/abid/task.rb', line 49

def prerequisite_tasks
  fail 'no play is bound yet' unless bound?

  prerequisites.map do |pre, params|
    application[pre, @scope, **self.params.merge(params)]
  end
end