Class: Thrud

Inherits:
Object
  • Object
show all
Defined in:
lib/thrud.rb

Direct Known Subclasses

Zeus::CLI

Defined Under Namespace

Classes: Task

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.desc(name, a) ⇒ Object



9
10
11
# File 'lib/thrud.rb', line 9

def self.desc(name, a)
  @desc = a
end

.long_desc(a) ⇒ Object



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

def self.long_desc(a)
  @long_desc = a
end

.map(a) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/thrud.rb', line 28

def self.map(a)
  a.each do |aliases, target|
    aliases = [aliases] unless aliases.kind_of?(Array)
    aliases.each do |name|
      @tasks[name.to_s] = @tasks[target.to_s]
    end
  end
end

.method_added(m) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/thrud.rb', line 20

def self.method_added(m)
  desc, long_desc, method_options = @desc, @long_desc, (@method_options||[])
  @desc, @long_desc, @method_options = nil, nil, nil

  @tasks ||= {}
  @tasks[m.to_s] = Task.new(m, desc, long_desc, method_options)
end

.method_option(*a) ⇒ Object



15
16
17
18
# File 'lib/thrud.rb', line 15

def self.method_option(*a)
  @method_options ||= []
  @method_options << a
end

.startObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/thrud.rb', line 78

def self.start
  taskname = ARGV.shift
  arguments = ARGV

  taskname ||= "help"

  unless task = @tasks[taskname.to_s]
    Zeus.ui.error "Could not find task \"#{taskname}\""
    exit 1
  end

  instance = new
  if instance.method(task.method_name).arity == 0 && arguments.any?
    Zeus.ui.error "\"#{task.method_name}\" was called incorrectly. Call as \"zeus #{task.method_name}\"."
    exit 1
  end
  instance.send(task.method_name, *arguments)
end

.task_for_name(name) ⇒ Object



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

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

Instance Method Details

#help(taskname = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/thrud.rb', line 45

def help(taskname = nil)
  if taskname && task = task_for_name(taskname)
    arity = task.arity(self)
    Zeus.ui.info <<-BANNER
Usage:
zeus #{taskname} #{arity == -1 ? "[ARGS]" : ""}

Description:
#{task.long_desc || task.desc}
BANNER
  else
    # this is super non-generic. problem for future-burke.
    project_tasks = self.class.instance_variable_get("@tasks").
      reject{|k,v|['definition_file', 'initialize', 'version', 'init', 'start', 'help'].include?(v.method_name.to_s)}.values.uniq

    tasks = project_tasks.map { |task|
      "  zeus %-14s # %s" % [task.method_name, task.desc]
    }

    Zeus.ui.info <<-BANNER
Global Commands:
zeus help           # show this help menu
zeus help [COMMAND] # show help for a specific command
zeus init           # #{task_for_name(:init).desc}
zeus start          # #{task_for_name(:start).desc}
zeus version        # #{task_for_name(:version).desc}

Project-local Commands:
#{tasks.join("\n")}
BANNER
  end
end

#task_for_name(name) ⇒ Object



41
42
43
# File 'lib/thrud.rb', line 41

def task_for_name(name)
  self.class.task_for_name(name)
end