Class: Rsm::Runner

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/rsm/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

If a task is not found on Thor::Runner, method missing is invoked and Thor::Runner is then responsable for finding the task in all classes.



22
23
24
25
26
27
28
# File 'lib/rsm/runner.rb', line 22

def method_missing(meth, *args)
  meth = meth.to_s
  initialize_thorfiles(meth)
  klass, task = Thor::Util.find_class_and_task_by_namespace(meth)
  args.unshift(task) if task
  klass.start(args, :shell => self.shell)
end

Class Method Details



60
61
62
# File 'lib/rsm/runner.rb', line 60

def self.banner(task, all = false, subcommand = false)
  "rsm " + task.formatted_usage(self, all, subcommand)
end

Instance Method Details

#display_klasses(with_modules = false, show_internal = false, klasses = Thor::Base.subclasses) ⇒ Object (private)

Display information about the given klasses. If with_module is given, it shows a table with information extracted from the yaml file.

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rsm/runner.rb', line 88

def display_klasses(with_modules=false, show_internal=false, klasses=Thor::Base.subclasses)
  klasses -= [Thor, Thor::Group, Rsm::Runner, Rsm::Base] unless show_internal

  raise Error, "No Thor tasks available" if klasses.empty?
  # show_modules if with_modules && !thor_yaml.empty?

  list = Hash.new { |h,k| h[k] = [] }
  groups = klasses.select { |k| k.ancestors.include?(Thor::Group) }

  # Get classes which inherit from Thor
  (klasses - groups).each { |k| list[k.namespace.split(":").first] += k.printable_tasks(false) }

  # Get classes which inherit from Thor::Base
  groups.map! { |k| k.printable_tasks(false).first }
  list["root"] = groups

  # Order namespaces with default coming first
  list = list.sort{ |a,b| a[0].sub(/^default/, '') <=> b[0].sub(/^default/, '') }
  list.each { |n, tasks| display_tasks(n, tasks) unless tasks.empty? }
end

#display_tasks(namespace, list) ⇒ Object (private)

:nodoc:



109
110
111
112
113
114
115
116
117
# File 'lib/rsm/runner.rb', line 109

def display_tasks(namespace, list) #:nodoc:
  list.sort!{ |a,b| a[0] <=> b[0] }

  say shell.set_color(namespace, :blue, true)
  say "-" * namespace.size

  print_table(list, :truncate => true)
  say
end

#help(meth = nil) ⇒ Object

Override Thor#help so it can give information about any class and any method.



48
49
50
51
52
53
54
55
56
# File 'lib/rsm/runner.rb', line 48

def help(meth = nil)
  if meth && !self.respond_to?(meth)
    initialize_thorfiles(meth)
    klass, task = Thor::Util.find_class_and_task_by_namespace(meth)
    klass.start(["-h", task].compact, :shell => self.shell)
  else
    super
  end
end

#initialize_thorfiles(relevant_to = nil, skip_lookup = false) ⇒ Object (private)

Load the thorfiles. If relevant_to is supplied, looks for specific files in the thor_root instead of loading them all.

By default, it also traverses the current path until find Thor files, as described in thorfiles. This look up can be skipped by suppliying skip_lookup true.



79
80
81
82
83
# File 'lib/rsm/runner.rb', line 79

def initialize_thorfiles(relevant_to=nil, skip_lookup=false)
  thorfiles(relevant_to, skip_lookup).each do |f|
    require f unless Thor::Base.subclass_files.keys.include?(File.expand_path(f))
  end
end

#list(search = "") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rsm/runner.rb', line 32

def list(search="")
  initialize_thorfiles
  
  search = ".*#{search}" if options["substring"]
  search = /^#{search}.*/i
  group  = options[:group] || "standard"
  
  klasses = Thor::Base.subclasses.select do |k|
    (options[:all] || k.group == group) && k.namespace =~ search
  end
  
  display_klasses(false, false, klasses)
end

#show_modulesObject (private)

:nodoc:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rsm/runner.rb', line 119

def show_modules #:nodoc:
  info  = []
  labels = ["Modules", "Namespaces"]

  info << labels
  info << [ "-" * labels[0].size, "-" * labels[1].size ]

  thor_yaml.each do |name, hash|
    info << [ name, hash[:namespaces].join(", ") ]
  end

  print_table info
  say ""
end

#thor_rootObject (private)



64
65
66
# File 'lib/rsm/runner.rb', line 64

def thor_root
  File.expand_path("../../tasks", __FILE__)
end

#thorfiles(relevant_to = nil, skip_lookup = false) ⇒ Object (private)



68
69
70
# File 'lib/rsm/runner.rb', line 68

def thorfiles(relevant_to=nil, skip_lookup=false)
  Dir["#{thor_root}/**/*.rb"]
end

#versionObject



14
15
16
17
# File 'lib/rsm/runner.rb', line 14

def version
  require 'rsm/version'
  say "RSM version #{Rsm::VERSION}"
end