Class: Nake::AbstractTask

Inherits:
Object show all
Defined in:
lib/nake/abstract_task.rb

Direct Known Subclasses

FileTask, Rule, Task

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *dependencies, &block) ⇒ AbstractTask

Returns a new instance of AbstractTask.



39
40
41
42
43
44
45
# File 'lib/nake/abstract_task.rb', line 39

def initialize(name, *dependencies, &block)
  @hidden = false
  @name, @blocks = name.to_sym, Array.new
  @dependencies  = Array.new
  self.register
  self.setup(*dependencies, &block)
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



38
39
40
# File 'lib/nake/abstract_task.rb', line 38

def blocks
  @blocks
end

#configObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nake/abstract_task.rb', line 61

def config
  @config ||= begin
    Hash.new do |hash, key|
      raise ConfigurationError, "Configuration key #{key} in task #{name} doesn't exist"
    end.tap do |hash|
      hash.define_singleton_method(:declare) do |*keys|
        keys.each { |key| self[key] = nil unless self.has_key?(key) }
      end
    end
  end
end

#dependenciesObject

Returns the value of attribute dependencies.



37
38
39
# File 'lib/nake/abstract_task.rb', line 37

def dependencies
  @dependencies
end

#descriptionObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nake/abstract_task.rb', line 48

def description
  if @description.respond_to?(:call)
    # task.description = lambda { "Edit #{self.config[:task_file]}" }
    @description.call
  elsif @description
    # task.description = "Edit %{task_file}"
    @description % self.config
  else
    nil
  end
end

#hiddenObject

Returns the value of attribute hidden.



37
38
39
# File 'lib/nake/abstract_task.rb', line 37

def hidden
  @hidden
end

#nameObject

Returns the value of attribute name.



37
38
39
# File 'lib/nake/abstract_task.rb', line 37

def name
  @name
end

#original_argsObject

Returns the value of attribute original_args.



37
38
39
# File 'lib/nake/abstract_task.rb', line 37

def original_args
  @original_args
end

Class Method Details

.[](name) ⇒ Object



12
13
14
# File 'lib/nake/abstract_task.rb', line 12

def self.[](name)
  self.tasks[name.to_s]
end

.[]=(name, task) ⇒ Object



16
17
18
# File 'lib/nake/abstract_task.rb', line 16

def self.[]=(name, task)
  self.tasks[name.to_s] = task
end

.bootObject



25
26
27
28
29
# File 'lib/nake/abstract_task.rb', line 25

def self.boot
  self.tasks.each do |name, task|
    task.boot!
  end
end

.new(name, *dependencies, &block) ⇒ Object

return existing task if task with given name already exist



32
33
34
35
# File 'lib/nake/abstract_task.rb', line 32

def self.new(name, *dependencies, &block)
  task = self[name]
  task && task.setup(*dependencies, &block) || super(name, *dependencies, &block)
end

.tasksObject



21
22
23
# File 'lib/nake/abstract_task.rb', line 21

def self.tasks
  @@tasks ||= Hash.new
end

Instance Method Details

#boot(*dependencies, &block) ⇒ Object



132
133
134
135
# File 'lib/nake/abstract_task.rb', line 132

def boot(*dependencies, &block)
  self.boot_dependencies.push(*dependencies)
  self.bootloaders.push(block)
end

#boot!Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/nake/abstract_task.rb', line 137

def boot!
  unless self.boot_dependencies.empty? && self.bootloaders.empty?
    note "Booting #{self.name}"
  end
  while dependency = self.boot_dependencies.shift
    self.class[dependency].tap do |task|
      if task.nil?
        raise TaskNotFound, "Task #{dependency} doesn't exist!"
      end
      task.boot!
    end
  end
  while block = self.bootloaders.shift
    block.call
  end
end

#boot_dependenciesObject



128
129
130
# File 'lib/nake/abstract_task.rb', line 128

def boot_dependencies
  @boot_dependencies ||= Array.new
end

#booted?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/nake/abstract_task.rb', line 154

def booted?
  self.bootloaders.empty? && self.boot_dependencies.empty?
end

#bootloadersObject



124
125
126
# File 'lib/nake/abstract_task.rb', line 124

def bootloaders
  @bootloaders ||= Array.new
end

#call(args = Array.new, options = Hash.new) ⇒ Object

NOTE: the reason why we don’t have splat for args is that when we have Task which doesn’t have any args and options, we wall just call it without any arguments, but when we use splat, then we will have to call it at least with Hash.new for options



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/nake/abstract_task.rb', line 96

def call(args = Array.new, options = Hash.new)
  unless self.dependencies.empty?
    info "Invoking task #{name}"
    self.invoke_dependencies(*args, options)
  end
  unless self.blocks.empty?
    note "Executing task #{name} with arguments #{args.inspect} and options #{options.inspect}"
    debug "Config #{self.config.inspect}"
    self.blocks.each do |block|
      block.call(*args, options)
    end
    ## we can't use arity, because it returns 0 for lambda { |options = Hash.new| }.arity
    ## if we define method with one optional argument, it will returns -1
    #
    ## better argv parsing maybe, task(:spec) do |*paths, options| is fine, but task(:spec) do |path, options| will fail if no path specified ... but actually task(:spec) do |path = "spec", options| might be fine, try it ... BTW task(["-i", "--interactive"]) do |task, *args, options| doesn't work ... putting options as a first argument should solve everything, but it's not very intuitive :/
    #if RUBY_VERSION >= "1.9.2"
    #  raise ArgumentError, "Task can't take block arguments" if args.length + 1 > block.parameters.select { |type, name| type.eql?(:req) || type.eql?(:opt) }.length
    #  raise ArgumentError, "Task can't take block arguments" if block.parameters.any? { |type, name| type.eql?(:block) }
    #end
    #
    #if RUBY_VERSION >= "1.9.2" && block.parameters.empty?
    #  block.call
    #else
    #  block.call(*args, options)
    #end
  end
end

#define(&block) ⇒ Object



79
80
81
# File 'lib/nake/abstract_task.rb', line 79

def define(&block)
  @blocks.push(block)
end

#hidden?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/nake/abstract_task.rb', line 83

def hidden?
  @hidden
end

#reset!Object



158
159
160
161
# File 'lib/nake/abstract_task.rb', line 158

def reset!
  self.dependencies.clear
  self.blocks.clear
end

#run(args) ⇒ Object



87
88
89
90
91
# File 'lib/nake/abstract_task.rb', line 87

def run(args)
  self.original_args = args
  options = ArgvParser.extract!(args)
  self.call(args, options)
end

#setup(*dependencies, &block) ⇒ Object



73
74
75
76
77
# File 'lib/nake/abstract_task.rb', line 73

def setup(*dependencies, &block)
  self.dependencies.push(*dependencies)
  self.instance_exec(self, &block) if block
  return self
end