Module: DO::Tasks

Extended by:
Tasks
Included in:
Commands, Tasks
Defined in:
lib/do/tasks.rb

Defined Under Namespace

Classes: NotFound

Instance Method Summary collapse

Instance Method Details

#desc(*args) ⇒ Object



10
11
12
# File 'lib/do/tasks.rb', line 10

def desc(*args)
  @_desc = args.shift
end

#namespace(text, &block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/do/tasks.rb', line 14

def namespace(text, &block)
  namespace_was, @_namespace = @_namespace, text.to_s
  @_namespace = '%s:%s' % [namespace_was, @_namespace] if namespace_was && namespace_was != ''
  block.call
ensure
  @_namespace = namespace_was
end

#task(name, options = {}, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/do/tasks.rb', line 22

def task(name, options={},  &block)
  name, deps, options = *case name
    when Hash
      in_was = name.delete(:in)
      name_and_deps = name.shift
      name.merge!(:in => in_was) if in_was
      name_and_deps.push(name)
    else [name, [], options]
  end
  tasks.push(options.merge({
    :name      => name.to_s,
    :desc      => @_desc.to_s,
    :deps      => Array(deps),
    :namespace => @_namespace.to_s,
    :block     => block,
    :in        => Array(options[:in] ? options[:in] : options[:on])
  }))
  tasks[-1]
ensure
  @_desc = nil
end

#task_find(name) ⇒ Object



70
71
72
73
74
# File 'lib/do/tasks.rb', line 70

def task_find(name)
  spaces = name.to_s.split(":")
  task   = spaces.pop
  tasks.find { |t| t[:name] == task && t[:namespace] == spaces.join(":") } || raise(NotFound, 'Task with "%s" not found' % name)
end

#task_run(*args) ⇒ Object Also known as: run_task



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/do/tasks.rb', line 44

def task_run(*args)
  args_was = args.dup
  task = task_find(args.shift)
  opts = DO::Parser.new(*args)
  task[:deps].each do |dep|
    name = dep.is_a?(Symbol) && task[:namespace] != '' ? '%s:%s' % [task[:namespace], dep] : dep
    task_run(*args.unshift(name).push('--dependency'))
  end
  if task[:in].empty?
    task[:block].arity == 1 ? task[:block].call(opts) : task[:block].call if task[:block]
  else
    task[:in] = send(task[:in][0]) if task[:in].size == 1 && method_defined?(task[:in][0])
    task[:in].each do |d|
      parent = task_find(d)
      case parent[:block].arity
        when 1 then parent[:block].call(task[:block])
        when 2 then parent[:block].call(opts, task[:block])
        else parent[:block].call
      end
    end
  end
rescue NotFound => e
  method_defined?(args_was[0]) ? send(args_was.shift) : raise(e)
end

#tasksObject



6
7
8
# File 'lib/do/tasks.rb', line 6

def tasks
  @_tasks ||= []
end