Module: Mybot::Tasks

Includes:
Fmt
Included in:
Base
Defined in:
lib/mybot/tasks.rb

Constant Summary

Constants included from Fmt

Fmt::WIDTH

Instance Method Summary collapse

Methods included from Fmt

#asterisks, #colored, #print_cmd, #print_cmd!, #print_progress, #print_stderr, #print_stdout, #spaces

Instance Method Details

#find_task(name) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/mybot/tasks.rb', line 49

def find_task(name)
  parts = name.split ":"
  task = parts.pop
  ns = parts.join ":"

  tasks.find do |t|
    t[:name] == task && (t[:namespace] == ns || t[:namespace].nil?)
  end
end

#namespace(name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/mybot/tasks.rb', line 9

def namespace(name)
  outer_ns, @namespace = @namespace, name.to_s
  if outer_ns && outer_ns != ""
    @namespace = "#{outer_ns}:#{@namespace}"
  end

  yield
ensure
  @namespace = outer_ns
end

#run_task(name, options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mybot/tasks.rb', line 59

def run_task(name, options)
  task = find_task(name)

  unless task
  	print_cmd! "error", "cannot find task '#{name}'", :red, :bold
  	abort
  end

  print_cmd! "task", name, :cyan, :bold

  task[:deps].each do |d|
    run_task(d, options)
  end

  task[:block].call(options)
end

#task(name, &block) ⇒ Object



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
# File 'lib/mybot/tasks.rb', line 20

def task(name, &block)
  name, deps = *case name
  when Hash
    name.shift
  else
    [name, []]
  end

  deps = Array(deps).map do |d|
    case d
    when Symbol
      "#{@namespace}:#{d}"
    when String
      d.include?(":") ? d : "#{@namespace}:#{d}"
    end
  end.uniq

  task = tasks.find do |t|
    t[:name] == name.to_s && t[:namespace] == @namespace
  end

  tasks.push({
    :name => name.to_s,
    :namespace => @namespace,
    :deps => deps,
    :block => block
  }) unless task
end

#tasksObject



5
6
7
# File 'lib/mybot/tasks.rb', line 5

def tasks
  @tasks ||= []
end